Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rework State related events
[simgrid.git] / src / instr / instr_paje_trace.cpp
1 /* Copyright (c) 2010-2017. 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.h"
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", __FUNCTION__, 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", __FUNCTION__);
75 }
76
77 void buffer_debug(std::vector<simgrid::instr::PajeEvent*>* buf)
78 {
79   return;
80   XBT_DEBUG(">>>>>> Dump the state of the buffer. %zu events", buf->size());
81   for (auto const& event : *buf) {
82     event->print();
83     XBT_DEBUG("%p %s", event, stream.str().c_str());
84     stream.str("");
85     stream.clear();
86   }
87   XBT_DEBUG("<<<<<<");
88 }
89
90 static void print_row() {
91   stream << std::endl;
92   fprintf(tracing_file, "%s", stream.str().c_str());
93   XBT_DEBUG("Dump %s", stream.str().c_str());
94   stream.str("");
95   stream.clear();
96 }
97
98 static void print_timestamp(simgrid::instr::PajeEvent* event)
99 {
100   stream << " ";
101   /* prevent 0.0000 in the trace - this was the behavior before the transition to c++ */
102   if (event->timestamp_ < 1e-12)
103     stream << 0;
104   else
105     stream << event->timestamp_;
106 }
107
108 /* internal do the instrumentation module */
109 void simgrid::instr::PajeEvent::insertIntoBuffer()
110 {
111   if (not TRACE_buffer()) {
112     print();
113     delete this;
114     return;
115   }
116   buffer_debug(&buffer);
117
118   XBT_DEBUG("%s: insert event_type=%u, timestamp=%f, buffersize=%zu)", __FUNCTION__, eventType_, timestamp_,
119             buffer.size());
120   std::vector<simgrid::instr::PajeEvent*>::reverse_iterator i;
121   for (i = buffer.rbegin(); i != buffer.rend(); ++i) {
122     simgrid::instr::PajeEvent* e1 = *i;
123     XBT_DEBUG("compare to %p is of type %u; timestamp:%f", e1, e1->eventType_, e1->timestamp_);
124     if (e1->timestamp_ <= timestamp_)
125       break;
126   }
127   if (i == buffer.rend())
128     XBT_DEBUG("%s: inserted at beginning", __FUNCTION__);
129   else if (i == buffer.rbegin())
130     XBT_DEBUG("%s: inserted at end", __FUNCTION__);
131   else
132     XBT_DEBUG("%s: inserted at pos= %zd from its end", __FUNCTION__, std::distance(buffer.rbegin(), i));
133   buffer.insert(i.base(), this);
134
135   buffer_debug(&buffer);
136 }
137
138 simgrid::instr::PajeEvent::~PajeEvent()
139 {
140   XBT_DEBUG("%s not implemented for %p: event_type=%u, timestamp=%f", __FUNCTION__, this, eventType_, timestamp_);
141 }
142
143 void TRACE_paje_start() {
144   char *filename = TRACE_get_filename();
145   tracing_file = fopen(filename, "w");
146   if (tracing_file == nullptr){
147     THROWF (system_error, 1, "Tracefile %s could not be opened for writing.", filename);
148   }
149
150   XBT_DEBUG("Filename %s is open for writing", filename);
151
152   /* output generator version */
153   fprintf (tracing_file, "#This file was generated using SimGrid-%d.%d.%d\n",
154            SIMGRID_VERSION_MAJOR, SIMGRID_VERSION_MINOR, SIMGRID_VERSION_PATCH);
155   fprintf (tracing_file, "#[");
156   unsigned int cpt;
157   char *str;
158   xbt_dynar_foreach (xbt_cmdline, cpt, str){
159     fprintf(tracing_file, "%s ",str);
160   }
161   fprintf (tracing_file, "]\n");
162
163   /* output one line comment */
164   dump_comment (TRACE_get_comment());
165
166   /* output comment file */
167   dump_comment_file (TRACE_get_comment_file());
168
169   /* output header */
170   TRACE_header(TRACE_basic(),TRACE_display_sizes());
171 }
172
173 void TRACE_paje_end() {
174   fclose(tracing_file);
175   char *filename = TRACE_get_filename();
176   XBT_DEBUG("Filename %s is closed", filename);
177 }
178
179 simgrid::instr::SetVariableEvent::SetVariableEvent(double timestamp, container_t container, Type* type, double value)
180     : simgrid::instr::PajeEvent::PajeEvent(container, type, timestamp, PAJE_SetVariable), value(value)
181 {
182   XBT_DEBUG("%s: event_type=%u, timestamp=%f", __FUNCTION__, eventType_, this->timestamp_);
183   insertIntoBuffer();
184 }
185
186 void simgrid::instr::SetVariableEvent::print()
187 {
188   if (instr_fmt_type == instr_fmt_paje) {
189     XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
190     stream << std::fixed << std::setprecision(TRACE_precision()) << this->eventType_;
191     print_timestamp(this);
192     stream << " " << type->getId() << " " << container->getId() << " " << value;
193     print_row();
194   } else if (instr_fmt_type == instr_fmt_TI) {
195     /* Nothing to do */
196   } else {
197     THROW_IMPOSSIBLE;
198   }
199 }
200
201 simgrid::instr::AddVariableEvent::AddVariableEvent(double timestamp, container_t container, simgrid::instr::Type* type,
202                                                    double value)
203     : simgrid::instr::PajeEvent::PajeEvent(container, type, timestamp, PAJE_AddVariable), value(value)
204 {
205   XBT_DEBUG("%s: event_type=%u, timestamp=%f", __FUNCTION__, eventType_, this->timestamp_);
206   insertIntoBuffer();
207 }
208
209 void simgrid::instr::AddVariableEvent::print()
210 {
211   if (instr_fmt_type == instr_fmt_paje) {
212     XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
213     stream << std::fixed << std::setprecision(TRACE_precision());
214     stream << eventType_;
215     print_timestamp(this);
216     stream << " " << type->getId() << " " << container->getId() << " " << value;
217     print_row();
218   } else if (instr_fmt_type == instr_fmt_TI) {
219     /* Nothing to do */
220   } else {
221     THROW_IMPOSSIBLE;
222   }
223 }
224
225 simgrid::instr::SubVariableEvent::SubVariableEvent(double timestamp, container_t container, Type* type, double value)
226     : simgrid::instr::PajeEvent::PajeEvent(container, type, timestamp, PAJE_SubVariable), value(value)
227 {
228   XBT_DEBUG("%s: event_type=%u, timestamp=%f", __FUNCTION__, eventType_, this->timestamp_);
229   insertIntoBuffer();
230 }
231
232 void simgrid::instr::SubVariableEvent::print()
233 {
234   if (instr_fmt_type == instr_fmt_paje) {
235     XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
236     stream << std::fixed << std::setprecision(TRACE_precision());
237     stream << eventType_;
238     print_timestamp(this);
239     stream << " " << type->getId() << " " << container->getId() << " " << value;
240     print_row();
241   } else if (instr_fmt_type == instr_fmt_TI) {
242     /* Nothing to do */
243   } else {
244     THROW_IMPOSSIBLE;
245   }
246 }
247
248 simgrid::instr::StartLinkEvent::StartLinkEvent(double timestamp, container_t container, Type* type,
249                                                container_t sourceContainer, std::string value, std::string key)
250     : StartLinkEvent(timestamp, container, type, sourceContainer, value, key, -1)
251 {}
252
253 simgrid::instr::StartLinkEvent::StartLinkEvent(double timestamp, container_t container, Type* type,
254                                                container_t sourceContainer, std::string value, std::string key,
255                                                int size)
256     : simgrid::instr::PajeEvent::PajeEvent(container, type, timestamp, PAJE_StartLink)
257     , sourceContainer_(sourceContainer)
258     , value_(value)
259     , key_(key)
260     , size_(size)
261 {
262   XBT_DEBUG("%s: event_type=%u, timestamp=%f, value:%s", __FUNCTION__, eventType_, this->timestamp_,
263             this->value_.c_str());
264   insertIntoBuffer();
265 }
266
267 void simgrid::instr::StartLinkEvent::print()
268 {
269   if (instr_fmt_type == instr_fmt_paje) {
270     XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
271     stream << std::fixed << std::setprecision(TRACE_precision());
272     stream << eventType_;
273     print_timestamp(this);
274     stream << " " << type->getId() << " " << container->getId() << " " << value_;
275     stream << " " << sourceContainer_->getId() << " " << key_;
276
277     if (TRACE_display_sizes()) {
278       stream << " " << size_;
279     }
280     print_row();
281   } else if (instr_fmt_type == instr_fmt_TI) {
282     /* Nothing to do */
283   } else {
284     THROW_IMPOSSIBLE;
285   }
286 }
287
288 simgrid::instr::EndLinkEvent::EndLinkEvent(double timestamp, container_t container, Type* type,
289                                            container_t destContainer, std::string value, std::string key)
290     : simgrid::instr::PajeEvent::PajeEvent(container, type, timestamp, PAJE_EndLink)
291     , destContainer(destContainer)
292     , value(value)
293     , key(key)
294 {
295   XBT_DEBUG("%s: event_type=%u, timestamp=%f", __FUNCTION__, eventType_, this->timestamp_);
296   insertIntoBuffer();
297 }
298
299 void simgrid::instr::EndLinkEvent::print()
300 {
301   if (instr_fmt_type == instr_fmt_paje) {
302     XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
303     stream << std::fixed << std::setprecision(TRACE_precision());
304     stream << eventType_;
305     print_timestamp(this);
306     stream << " " << type->getId() << " " << container->getId() << " " << value;
307     stream << " " << destContainer->getId() << " " << key;
308     print_row();
309   } else if (instr_fmt_type == instr_fmt_TI) {
310     /* Nothing to do */
311   } else {
312     THROW_IMPOSSIBLE;
313   }
314 }
315
316 simgrid::instr::NewEvent::NewEvent(double timestamp, container_t container, Type* type, EntityValue* val)
317     : simgrid::instr::PajeEvent::PajeEvent(container, type, timestamp, PAJE_NewEvent), val(val)
318 {
319   XBT_DEBUG("%s: event_type=%u, timestamp=%f", __FUNCTION__, eventType_, this->timestamp_);
320
321   insertIntoBuffer();
322 }
323
324 void simgrid::instr::NewEvent::print()
325 {
326   if (instr_fmt_type == instr_fmt_paje) {
327     XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
328     stream << std::fixed << std::setprecision(TRACE_precision());
329     stream << eventType_;
330     print_timestamp(this);
331     stream << " " << type->getId() << " " << container->getId() << " " << val->getId();
332     print_row();
333   } else if (instr_fmt_type == instr_fmt_TI) {
334     /* Nothing to do */
335   } else {
336     THROW_IMPOSSIBLE;
337   }
338 }
339
340 void TRACE_TI_start()
341 {
342   char *filename = TRACE_get_filename();
343   tracing_file = fopen(filename, "w");
344   if (tracing_file == nullptr)
345     THROWF(system_error, 1, "Tracefile %s could not be opened for writing.", filename);
346
347   XBT_DEBUG("Filename %s is open for writing", filename);
348
349   /* output one line comment */
350   dump_comment(TRACE_get_comment());
351
352   /* output comment file */
353   dump_comment_file(TRACE_get_comment_file());
354 }
355
356 void TRACE_TI_end()
357 {
358   fclose(tracing_file);
359   char *filename = TRACE_get_filename();
360   XBT_DEBUG("Filename %s is closed", filename);
361 }