Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix some doxygen warnings.
[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 "xbt/virtu.h" /* sg_cmdline */
13 #include <fstream>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_paje_trace, instr, "tracing event system");
16
17 static std::stringstream stream;
18 FILE *tracing_file = nullptr;
19
20 std::vector<simgrid::instr::PajeEvent*> buffer;
21 void buffer_debug(std::vector<simgrid::instr::PajeEvent*>* buf);
22
23 void dump_comment(std::string comment)
24 {
25   if (comment.empty())
26     return;
27   fprintf(tracing_file, "# %s\n", comment.c_str());
28 }
29
30 void dump_comment_file(std::string filename)
31 {
32   if (filename.empty())
33     return;
34   std::ifstream* fs = new std::ifstream();
35   fs->open(filename.c_str(), std::ifstream::in);
36
37   if (fs->fail()) {
38     THROWF(system_error, 1, "Comment file %s could not be opened for reading.", filename.c_str());
39   }
40   while (not fs->eof()) {
41     std::string line;
42     fprintf (tracing_file, "# ");
43     std::getline(*fs, line);
44     fprintf(tracing_file, "%s", line.c_str());
45   }
46   fs->close();
47 }
48
49 double TRACE_last_timestamp_to_dump = 0;
50 //dumps the trace file until the timestamp TRACE_last_timestamp_to_dump
51 void TRACE_paje_dump_buffer(bool force)
52 {
53   if (not TRACE_is_enabled())
54     return;
55   XBT_DEBUG("%s: dump until %f. starts", __func__, TRACE_last_timestamp_to_dump);
56   if (force){
57     for (auto const& event : buffer) {
58       event->print();
59       delete event;
60     }
61     buffer.clear();
62   }else{
63     std::vector<simgrid::instr::PajeEvent*>::iterator i = buffer.begin();
64     for (auto const& event : buffer) {
65       double head_timestamp = event->timestamp_;
66       if (head_timestamp > TRACE_last_timestamp_to_dump)
67         break;
68       event->print();
69       delete event;
70       ++i;
71     }
72     buffer.erase(buffer.begin(), i);
73   }
74   XBT_DEBUG("%s: ends", __func__);
75 }
76
77 void buffer_debug(std::vector<simgrid::instr::PajeEvent*>* buf)
78 {
79   if (not XBT_LOG_ISENABLED(instr_paje_trace, xbt_log_priority_debug))
80     return;
81   XBT_DEBUG(">>>>>> Dump the state of the buffer. %zu events", buf->size());
82   for (auto const& event : *buf) {
83     event->print();
84     XBT_DEBUG("%p %s", event, stream.str().c_str());
85     stream.str("");
86     stream.clear();
87   }
88   XBT_DEBUG("<<<<<<");
89 }
90
91 /* internal do the instrumentation module */
92 void simgrid::instr::PajeEvent::insertIntoBuffer()
93 {
94   if (not TRACE_buffer()) {
95     print();
96     delete this;
97     return;
98   }
99   buffer_debug(&buffer);
100
101   XBT_DEBUG("%s: insert event_type=%u, timestamp=%f, buffersize=%zu)", __func__, eventType_, timestamp_, buffer.size());
102   std::vector<simgrid::instr::PajeEvent*>::reverse_iterator i;
103   for (i = buffer.rbegin(); i != buffer.rend(); ++i) {
104     simgrid::instr::PajeEvent* e1 = *i;
105     XBT_DEBUG("compare to %p is of type %u; timestamp:%f", e1, e1->eventType_, e1->timestamp_);
106     if (e1->timestamp_ <= timestamp_)
107       break;
108   }
109   if (i == buffer.rend())
110     XBT_DEBUG("%s: inserted at beginning", __func__);
111   else if (i == buffer.rbegin())
112     XBT_DEBUG("%s: inserted at end", __func__);
113   else
114     XBT_DEBUG("%s: inserted at pos= %zd from its end", __func__, std::distance(buffer.rbegin(), i));
115   buffer.insert(i.base(), this);
116
117   buffer_debug(&buffer);
118 }
119
120 void TRACE_paje_start() {
121   std::string filename = TRACE_get_filename();
122   tracing_file         = fopen(filename.c_str(), "w");
123   if (tracing_file == nullptr){
124     THROWF(system_error, 1, "Tracefile %s could not be opened for writing.", filename.c_str());
125   }
126
127   XBT_DEBUG("Filename %s is open for writing", filename.c_str());
128
129   /* output generator version */
130   fprintf (tracing_file, "#This file was generated using SimGrid-%d.%d.%d\n",
131            SIMGRID_VERSION_MAJOR, SIMGRID_VERSION_MINOR, SIMGRID_VERSION_PATCH);
132   fprintf (tracing_file, "#[");
133   unsigned int cpt;
134   char *str;
135   xbt_dynar_foreach (xbt_cmdline, cpt, str){
136     fprintf(tracing_file, "%s ",str);
137   }
138   fprintf (tracing_file, "]\n");
139
140   /* output one line comment */
141   dump_comment (TRACE_get_comment());
142
143   /* output comment file */
144   dump_comment_file (TRACE_get_comment_file());
145
146   /* output header */
147   TRACE_header(TRACE_basic(),TRACE_display_sizes());
148 }
149
150 void TRACE_paje_end() {
151   fclose(tracing_file);
152   XBT_DEBUG("Filename %s is closed", TRACE_get_filename().c_str());
153 }
154
155
156 void TRACE_TI_start()
157 {
158   std::string filename = TRACE_get_filename();
159   tracing_file         = fopen(filename.c_str(), "w");
160   if (tracing_file == nullptr) {
161     THROWF(system_error, 1, "Tracefile %s could not be opened for writing.", filename.c_str());
162   }
163
164   XBT_DEBUG("Filename %s is open for writing", filename.c_str());
165
166   /* output one line comment */
167   dump_comment(TRACE_get_comment());
168
169   /* output comment file */
170   dump_comment_file(TRACE_get_comment_file());
171 }
172
173 void TRACE_TI_end()
174 {
175   fclose(tracing_file);
176   XBT_DEBUG("Filename %s is closed", TRACE_get_filename().c_str());
177 }