Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move function to class
[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 extern std::ofstream tracing_file;
17
18 std::vector<simgrid::instr::PajeEvent*> buffer;
19
20 void dump_comment(std::string comment)
21 {
22   if (not comment.empty())
23     tracing_file << "# " << comment << std::endl;
24 }
25
26 void dump_comment_file(std::string filename)
27 {
28   if (filename.empty())
29     return;
30   std::ifstream* fs = new std::ifstream();
31   fs->open(filename.c_str(), std::ifstream::in);
32
33   if (fs->fail()) {
34     THROWF(system_error, 1, "Comment file %s could not be opened for reading.", filename.c_str());
35   }
36   while (not fs->eof()) {
37     std::string line;
38     tracing_file << "# ";
39     std::getline(*fs, line);
40     tracing_file << line;
41   }
42   fs->close();
43 }
44
45 double TRACE_last_timestamp_to_dump = 0;
46 //dumps the trace file until the timestamp TRACE_last_timestamp_to_dump
47 void TRACE_paje_dump_buffer(bool force)
48 {
49   if (not TRACE_is_enabled())
50     return;
51   XBT_DEBUG("%s: dump until %f. starts", __func__, TRACE_last_timestamp_to_dump);
52   if (force){
53     for (auto const& event : buffer) {
54       event->print();
55       delete event;
56     }
57     buffer.clear();
58   }else{
59     std::vector<simgrid::instr::PajeEvent*>::iterator i = buffer.begin();
60     for (auto const& event : buffer) {
61       double head_timestamp = event->timestamp_;
62       if (head_timestamp > TRACE_last_timestamp_to_dump)
63         break;
64       event->print();
65       delete event;
66       ++i;
67     }
68     buffer.erase(buffer.begin(), i);
69   }
70   XBT_DEBUG("%s: ends", __func__);
71 }
72
73 static void buffer_debug(std::vector<simgrid::instr::PajeEvent*>* buf)
74 {
75   if (not XBT_LOG_ISENABLED(instr_paje_trace, xbt_log_priority_debug))
76     return;
77   XBT_DEBUG(">>>>>> Dump the state of the buffer. %zu events", buf->size());
78   for (auto const& event : *buf) {
79     event->print();
80     XBT_DEBUG("%p %s", event, event->stream_.str().c_str());
81     event->stream_.str("");
82     event->stream_.clear();
83   }
84   XBT_DEBUG("<<<<<<");
85 }
86
87 /* internal do the instrumentation module */
88 void simgrid::instr::PajeEvent::insertIntoBuffer()
89 {
90   if (not TRACE_buffer()) {
91     print();
92     delete this;
93     return;
94   }
95   buffer_debug(&buffer);
96
97   XBT_DEBUG("%s: insert event_type=%u, timestamp=%f, buffersize=%zu)", __func__, eventType_, timestamp_, buffer.size());
98   std::vector<simgrid::instr::PajeEvent*>::reverse_iterator i;
99   for (i = buffer.rbegin(); i != buffer.rend(); ++i) {
100     simgrid::instr::PajeEvent* e1 = *i;
101     XBT_DEBUG("compare to %p is of type %u; timestamp:%f", e1, e1->eventType_, e1->timestamp_);
102     if (e1->timestamp_ <= timestamp_)
103       break;
104   }
105   if (i == buffer.rend())
106     XBT_DEBUG("%s: inserted at beginning", __func__);
107   else if (i == buffer.rbegin())
108     XBT_DEBUG("%s: inserted at end", __func__);
109   else
110     XBT_DEBUG("%s: inserted at pos= %zd from its end", __func__, std::distance(buffer.rbegin(), i));
111   buffer.insert(i.base(), this);
112
113   buffer_debug(&buffer);
114 }