Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Throw std::out_of_range.
[simgrid.git] / src / instr / instr_paje_trace.cpp
1 /* Copyright (c) 2010-2019. 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 static std::vector<simgrid::instr::PajeEvent*> buffer;
19
20 void dump_comment(const std::string& comment)
21 {
22   if (not comment.empty())
23     tracing_file << "# " << comment << std::endl;
24 }
25
26 void dump_comment_file(const 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 /* internal do the instrumentation module */
72 void simgrid::instr::PajeEvent::insert_into_buffer()
73 {
74   XBT_DEBUG("%s: insert event_type=%u, timestamp=%f, buffersize=%zu)", __func__, eventType_, timestamp_, buffer.size());
75   std::vector<simgrid::instr::PajeEvent*>::reverse_iterator i;
76   for (i = buffer.rbegin(); i != buffer.rend(); ++i) {
77     simgrid::instr::PajeEvent* e1 = *i;
78     XBT_DEBUG("compare to %p is of type %u; timestamp:%f", e1, e1->eventType_, e1->timestamp_);
79     if (e1->timestamp_ <= timestamp_)
80       break;
81   }
82   if (i == buffer.rend())
83     XBT_DEBUG("%s: inserted at beginning", __func__);
84   else if (i == buffer.rbegin())
85     XBT_DEBUG("%s: inserted at end", __func__);
86   else
87     XBT_DEBUG("%s: inserted at pos= %zd from its end", __func__, std::distance(buffer.rbegin(), i));
88   buffer.insert(i.base(), this);
89 }