Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4d55e557b09a276f88dab6c1d45a4f13ad14f002
[simgrid.git] / src / instr / instr_paje_trace.cpp
1 /* Copyright (c) 2010-2018. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/sg_config.hpp"
8 #include "src/instr/instr_private.hpp"
9 #include "src/instr/instr_smpi.hpp"
10 #include "src/smpi/include/private.hpp"
11 #include "typeinfo"
12 #include <fstream>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_paje_trace, instr, "tracing event system");
15
16 static std::stringstream stream;
17 FILE *tracing_file = nullptr;
18
19 std::vector<simgrid::instr::PajeEvent*> buffer;
20 void buffer_debug(std::vector<simgrid::instr::PajeEvent*>* buf);
21
22 void dump_comment(std::string comment)
23 {
24   if (comment.empty())
25     return;
26   fprintf(tracing_file, "# %s\n", comment.c_str());
27 }
28
29 void dump_comment_file(std::string filename)
30 {
31   if (filename.empty())
32     return;
33   std::ifstream* fs = new std::ifstream();
34   fs->open(filename.c_str(), std::ifstream::in);
35
36   if (fs->fail()) {
37     THROWF(system_error, 1, "Comment file %s could not be opened for reading.", filename.c_str());
38   }
39   while (not fs->eof()) {
40     std::string line;
41     fprintf (tracing_file, "# ");
42     std::getline(*fs, line);
43     fprintf(tracing_file, "%s", line.c_str());
44   }
45   fs->close();
46 }
47
48 double TRACE_last_timestamp_to_dump = 0;
49 //dumps the trace file until the timestamp TRACE_last_timestamp_to_dump
50 void TRACE_paje_dump_buffer(bool force)
51 {
52   if (not TRACE_is_enabled())
53     return;
54   XBT_DEBUG("%s: dump until %f. starts", __func__, TRACE_last_timestamp_to_dump);
55   if (force){
56     for (auto const& event : buffer) {
57       event->print();
58       delete event;
59     }
60     buffer.clear();
61   }else{
62     std::vector<simgrid::instr::PajeEvent*>::iterator i = buffer.begin();
63     for (auto const& event : buffer) {
64       double head_timestamp = event->timestamp_;
65       if (head_timestamp > TRACE_last_timestamp_to_dump)
66         break;
67       event->print();
68       delete event;
69       ++i;
70     }
71     buffer.erase(buffer.begin(), i);
72   }
73   XBT_DEBUG("%s: ends", __func__);
74 }
75
76 void buffer_debug(std::vector<simgrid::instr::PajeEvent*>* buf)
77 {
78   if (not XBT_LOG_ISENABLED(instr_paje_trace, xbt_log_priority_debug))
79     return;
80   XBT_DEBUG(">>>>>> Dump the state of the buffer. %zu events", buf->size());
81   for (auto const& event : *buf) {
82     event->print();
83     XBT_DEBUG("%p %s", event, stream.str().c_str());
84     stream.str("");
85     stream.clear();
86   }
87   XBT_DEBUG("<<<<<<");
88 }
89
90 /* internal do the instrumentation module */
91 void simgrid::instr::PajeEvent::insertIntoBuffer()
92 {
93   if (not TRACE_buffer()) {
94     print();
95     delete this;
96     return;
97   }
98   buffer_debug(&buffer);
99
100   XBT_DEBUG("%s: insert event_type=%u, timestamp=%f, buffersize=%zu)", __func__, eventType_, timestamp_, buffer.size());
101   std::vector<simgrid::instr::PajeEvent*>::reverse_iterator i;
102   for (i = buffer.rbegin(); i != buffer.rend(); ++i) {
103     simgrid::instr::PajeEvent* e1 = *i;
104     XBT_DEBUG("compare to %p is of type %u; timestamp:%f", e1, e1->eventType_, e1->timestamp_);
105     if (e1->timestamp_ <= timestamp_)
106       break;
107   }
108   if (i == buffer.rend())
109     XBT_DEBUG("%s: inserted at beginning", __func__);
110   else if (i == buffer.rbegin())
111     XBT_DEBUG("%s: inserted at end", __func__);
112   else
113     XBT_DEBUG("%s: inserted at pos= %zd from its end", __func__, std::distance(buffer.rbegin(), i));
114   buffer.insert(i.base(), this);
115
116   buffer_debug(&buffer);
117 }