Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
use signal to decouple instr from surf code
[simgrid.git] / src / instr / instr_paje_trace.cpp
1 /* Copyright (c) 2010-2020. 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/Exception.hpp"
8 #include "simgrid/sg_config.hpp"
9 #include "src/instr/instr_private.hpp"
10 #include "src/instr/instr_smpi.hpp"
11 #include "src/smpi/include/private.hpp"
12 #include "typeinfo"
13 #include <fstream>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_paje_trace, instr, "tracing event system");
16
17 extern std::ofstream tracing_file;
18
19 static std::vector<simgrid::instr::PajeEvent*> buffer;
20
21 void dump_comment_file(const std::string& filename)
22 {
23   if (filename.empty())
24     return;
25   std::ifstream fs(filename.c_str(), std::ifstream::in);
26
27   if (fs.fail())
28     throw simgrid::TracingError(
29         XBT_THROW_POINT,
30         simgrid::xbt::string_printf("Comment file %s could not be opened for reading.", filename.c_str()));
31
32   while (not fs.eof()) {
33     std::string line;
34     std::getline(fs, line);
35     tracing_file << "# " << line;
36   }
37   fs.close();
38 }
39
40 double TRACE_last_timestamp_to_dump = 0;
41 //dumps the trace file until the timestamp TRACE_last_timestamp_to_dump
42 void TRACE_paje_dump_buffer(bool force)
43 {
44   if (not TRACE_is_enabled())
45     return;
46   XBT_DEBUG("%s: dump until %f. starts", __func__, TRACE_last_timestamp_to_dump);
47   if (force){
48     for (auto const& event : buffer) {
49       event->print();
50       delete event;
51     }
52     buffer.clear();
53   } else {
54     std::vector<simgrid::instr::PajeEvent*>::iterator i = buffer.begin();
55     for (auto const& event : buffer) {
56       double head_timestamp = event->timestamp_;
57       if (head_timestamp > TRACE_last_timestamp_to_dump)
58         break;
59       event->print();
60       delete event;
61       ++i;
62     }
63     buffer.erase(buffer.begin(), i);
64   }
65   XBT_DEBUG("%s: ends", __func__);
66 }
67
68 /* internal do the instrumentation module */
69 void simgrid::instr::PajeEvent::insert_into_buffer()
70 {
71   XBT_DEBUG("%s: insert event_type=%u, timestamp=%f, buffersize=%zu)", __func__, eventType_, timestamp_, buffer.size());
72   std::vector<simgrid::instr::PajeEvent*>::reverse_iterator i;
73   for (i = buffer.rbegin(); i != buffer.rend(); ++i) {
74     simgrid::instr::PajeEvent* e1 = *i;
75     XBT_DEBUG("compare to %p is of type %u; timestamp:%f", e1, e1->eventType_, e1->timestamp_);
76     if (e1->timestamp_ <= timestamp_)
77       break;
78   }
79   if (i == buffer.rend())
80     XBT_DEBUG("%s: inserted at beginning", __func__);
81   else if (i == buffer.rbegin())
82     XBT_DEBUG("%s: inserted at end", __func__);
83   else
84     XBT_DEBUG("%s: inserted at pos= %zd from its end", __func__, std::distance(buffer.rbegin(), i));
85   buffer.insert(i.base(), this);
86 }