Logo AND Algorithmique Numérique Distribuée

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