Logo AND Algorithmique Numérique Distribuée

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