Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e4a73c73d05982a089b4703d8e8b59721266bee9
[simgrid.git] / src / instr / instr_paje_trace.cpp
1 /* Copyright (c) 2010-2016. 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 "src/instr/instr_private.h"
8 #include "src/instr/instr_smpi.h"
9 #include "src/smpi/private.hpp"
10 #include "typeinfo"
11 #include "xbt/virtu.h" /* sg_cmdline */
12 #include "simgrid/sg_config.h"
13
14 #include <sstream>
15 #include <vector>
16 #include <iomanip> /** std::setprecision **/
17 #include <sys/stat.h>
18 #ifdef WIN32
19 #include <direct.h> // _mkdir
20 #endif
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_paje_trace, instr, "tracing event system");
23
24 extern s_instr_trace_writer_t active_writer;
25
26 static std::stringstream stream;
27 FILE *tracing_file = nullptr;
28
29 static xbt_dict_t tracing_files = nullptr; // TI specific
30 static double prefix=0.0; // TI specific
31
32
33 void print_NULL(PajeEvent* event){}
34
35 /* The active set of functions for the selected trace format
36  * By default, they all do nothing, hence the print_NULL to avoid segfaults */
37
38 s_instr_trace_writer_t active_writer = {&print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL,
39                                         &print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL,
40                                         &print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL};
41
42 std::vector<PajeEvent*> buffer;
43 void buffer_debug(std::vector<PajeEvent*> *buf);
44
45 void dump_comment (const char *comment)
46 {
47   if (!strlen(comment)) return;
48   fprintf (tracing_file, "# %s\n", comment);
49 }
50
51 void dump_comment_file (const char *filename)
52 {
53   if (!strlen(filename)) return;
54   FILE *file = fopen (filename, "r");
55   if (!file){
56     THROWF (system_error, 1, "Comment file %s could not be opened for reading.", filename);
57   }
58   while (!feof(file)){
59     char c;
60     c = fgetc(file);
61     if (feof(file)) break;
62     fprintf (tracing_file, "# ");
63     while (c != '\n'){
64       fprintf (tracing_file, "%c", c);
65       c = fgetc(file);
66       if (feof(file)) break;
67     }
68     fprintf (tracing_file, "\n");
69   }
70   fclose(file);
71 }
72
73 double TRACE_last_timestamp_to_dump = 0;
74 //dumps the trace file until the timestamp TRACE_last_timestamp_to_dump
75 void TRACE_paje_dump_buffer (int force)
76 {
77   if (!TRACE_is_enabled()) return;
78   XBT_DEBUG("%s: dump until %f. starts", __FUNCTION__, TRACE_last_timestamp_to_dump);
79   if (force){
80     for (auto event : buffer){
81       event->print();
82       delete event;
83     }
84     buffer.clear();
85   }else{
86     std::vector<PajeEvent*>::iterator i = buffer.begin();
87     for (auto event :buffer){
88       double head_timestamp = event->timestamp;
89       if (head_timestamp > TRACE_last_timestamp_to_dump)
90         break;
91       event->print();
92       delete event;
93       ++i;
94     }
95     buffer.erase(buffer.begin(), i);
96   }
97   XBT_DEBUG("%s: ends", __FUNCTION__);
98 }
99
100 void buffer_debug(std::vector<PajeEvent*> *buf);
101 void buffer_debug(std::vector<PajeEvent*> *buf) {
102   return;
103   XBT_DEBUG(">>>>>> Dump the state of the buffer. %zu events", buf->size());
104   for (auto event :*buf){
105     event->print();
106     XBT_DEBUG("%p %s", event, stream.str().c_str());
107     stream.str("");
108     stream.clear();
109   }
110   XBT_DEBUG("<<<<<<");
111 }
112
113 static void print_row() {
114   stream << std::endl;
115   fprintf(tracing_file, "%s", stream.str().c_str());
116   XBT_DEBUG("Dump %s", stream.str().c_str());
117   stream.str("");
118   stream.clear();
119 }
120
121 static void print_timestamp(PajeEvent* event) {
122   stream << " ";
123   /* prevent 0.0000 in the trace - this was the behavior before the transition to c++ */
124   if (event->timestamp < 1e-12)
125     stream << 0;
126   else 
127     stream << event->timestamp;
128 }  
129
130 /* internal do the instrumentation module */
131 static void insert_into_buffer (PajeEvent* tbi)
132 {
133   if (TRACE_buffer() == 0){
134     tbi->print ();
135     delete tbi;
136     return;
137   }
138   buffer_debug(&buffer);
139
140   XBT_DEBUG("%s: insert event_type=%d, timestamp=%f, buffersize=%zu)",
141       __FUNCTION__, (int)tbi->event_type, tbi->timestamp, buffer.size());
142   std::vector<PajeEvent*>::reverse_iterator i;
143   for (i = buffer.rbegin(); i != buffer.rend(); ++i) {
144     PajeEvent* e1 = *i;
145     XBT_DEBUG("compare to %p is of type %d; timestamp:%f", e1,
146         (int)e1->event_type, e1->timestamp);
147     if (e1->timestamp <= tbi->timestamp)
148       break;
149   }
150   buffer.insert(i.base(), tbi);
151   if (i == buffer.rend())
152     XBT_DEBUG("%s: inserted at beginning", __FUNCTION__);
153   else if (i == buffer.rbegin())
154     XBT_DEBUG("%s: inserted at end", __FUNCTION__);
155   else
156     XBT_DEBUG("%s: inserted at pos= %zd from its end", __FUNCTION__,
157         std::distance(buffer.rbegin(),i));
158
159   buffer_debug(&buffer);
160 }
161
162 PajeEvent:: ~PajeEvent()
163 {
164   XBT_DEBUG("%s not implemented for %p: event_type=%d, timestamp=%f", __FUNCTION__,
165       this, (int)event_type, timestamp);
166 //  xbt_backtrace_display_current();
167
168  /* switch (event->event_type){
169   case PAJE_StartLink:
170     xbt_free (((startLink_t)(event->data))->value);
171     xbt_free (((startLink_t)(event->data))->key);
172     break;
173   case PAJE_EndLink:
174     xbt_free (((endLink_t)(event->data))->value);
175     xbt_free (((endLink_t)(event->data))->key);
176     break;
177   default:
178     break;
179   }*/
180 }
181
182 void TRACE_paje_start() {
183   char *filename = TRACE_get_filename();
184   tracing_file = fopen(filename, "w");
185   if (tracing_file == nullptr){
186     THROWF (system_error, 1, "Tracefile %s could not be opened for writing.", filename);
187   }
188
189   XBT_DEBUG("Filename %s is open for writing", filename);
190
191   /* output generator version */
192   fprintf (tracing_file, "#This file was generated using SimGrid-%d.%d.%d\n",
193            SIMGRID_VERSION_MAJOR, SIMGRID_VERSION_MINOR, SIMGRID_VERSION_PATCH);
194   fprintf (tracing_file, "#[");
195   unsigned int cpt;
196   char *str;
197   xbt_dynar_foreach (xbt_cmdline, cpt, str){
198     fprintf(tracing_file, "%s ",str);
199   }
200   fprintf (tracing_file, "]\n");
201
202   /* output one line comment */
203   dump_comment (TRACE_get_comment());
204
205   /* output comment file */
206   dump_comment_file (TRACE_get_comment_file());
207
208   /* output header */
209   TRACE_header(TRACE_basic(),TRACE_display_sizes());
210 }
211
212 void TRACE_paje_end() {
213   fclose(tracing_file);
214   char *filename = TRACE_get_filename();
215   XBT_DEBUG("Filename %s is closed", filename);
216 }
217
218 DefineContainerEvent::DefineContainerEvent(type_t type)
219 {
220
221   event_type                            = PAJE_DefineContainerType;
222   timestamp                             = 0;
223   this->type = type;
224   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
225   //print it
226   print ();
227 }
228
229 void DefineContainerEvent::print() {
230   XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
231   stream << std::fixed << std::setprecision(TRACE_precision());
232   stream << (int)this->event_type;
233   stream << " " << type->id
234          << " " << type->father->id
235          << " " << type->name;
236   print_row();
237 }
238
239
240
241 DefineVariableTypeEvent::DefineVariableTypeEvent(type_t type)
242 {
243   this->event_type                           = PAJE_DefineVariableType;
244   this->timestamp                            = 0;
245   this->type = type;
246
247   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
248
249   //print it
250   print ();
251 }
252
253 void DefineVariableTypeEvent::print() {
254   XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
255   stream << std::fixed << std::setprecision(TRACE_precision());
256   stream << (int)this->event_type;
257   stream << " " << type->id
258          << " " << type->father->id
259          << " " << type->name;
260   if (type->color)
261     stream << " \"" << type->color << "\"";
262   print_row();
263 }
264
265 DefineStateTypeEvent::DefineStateTypeEvent(type_t type)
266 {
267   this->event_type                        = PAJE_DefineStateType;
268   this->timestamp                         = 0;
269   this->type = type;
270
271   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
272
273   //print it
274   print();
275 }
276
277
278 DefineEventTypeEvent::DefineEventTypeEvent(type_t type)
279 {
280   this->event_type                        = PAJE_DefineEventType;
281   this->timestamp                         = 0;
282   this->type = type;
283
284   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
285
286   //print it
287   print();
288 }
289
290
291 void DefineStateTypeEvent::print() {
292   XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
293   stream << std::fixed << std::setprecision(TRACE_precision());
294   stream << (int)this->event_type;
295   stream << " " << type->id
296          << " " << type->father->id
297          << " " << type->name;
298   print_row();
299 }
300
301 void DefineEventTypeEvent::print() {
302   XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
303   stream << std::fixed << std::setprecision(TRACE_precision());
304   stream << (int)this->event_type;
305   stream << " " << type->id
306          << " " << type->father->id
307          << " " << type->name;
308   print_row();
309 }
310
311 DefineLinkTypeEvent::DefineLinkTypeEvent(type_t type, type_t source, type_t dest)
312 {
313   this->event_type                         = PAJE_DefineLinkType;
314   this->timestamp                          = 0;
315   this->type   = type;
316   this->source = source;
317   this->dest   = dest;
318
319   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
320
321   //print it
322   print();
323 }
324
325 void DefineLinkTypeEvent::print() {
326   XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
327   stream << std::fixed << std::setprecision(TRACE_precision());
328   stream << (int)this->event_type;
329   stream << " " << type->id 
330          << " " << type->father->id 
331          << " " << source->id 
332          << " " << dest->id 
333          << " " << type->name;
334   print_row();
335 }
336
337 DefineEntityValueEvent::DefineEntityValueEvent (val_t value)
338 {
339   this->event_type                           = PAJE_DefineEntityValue;
340   this->timestamp                            = 0;
341   this->value = value;
342
343   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
344
345   //print it
346   print();
347 }
348
349
350 void DefineEntityValueEvent::print() {
351   XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
352   stream << std::fixed << std::setprecision(TRACE_precision());
353   stream << (int)this->event_type;
354   stream << " "   << value->id
355          << " "   << value->father->id
356          << " "   << value->name;
357   if(value->color)
358     stream << " \"" << value->color << "\"";
359   print_row();
360 }
361
362 CreateContainerEvent::CreateContainerEvent (container_t container)
363 {
364   this->event_type                             = PAJE_CreateContainer;
365   this->timestamp                              = SIMIX_get_clock();
366   this->container = container;
367
368   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
369
370   print();
371 }
372
373 void CreateContainerEvent::print() {
374         if (instr_fmt_type == instr_fmt_paje) {
375                 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
376                 stream << std::fixed << std::setprecision(TRACE_precision());
377                 stream << (int)this->event_type;
378                 print_timestamp(this);
379                 stream << " "   << container->id
380                                 << " "   << container->type->id
381                                 << " "   << container->father->id
382                                 << " \"" << container->name << "\"";
383
384                 print_row();
385         } else if (instr_fmt_type == instr_fmt_TI) {
386                 //if we are in the mode with only one file
387                 static FILE *temp = nullptr;
388
389                 if (tracing_files == nullptr) {
390                         tracing_files = xbt_dict_new_homogeneous(nullptr);
391                         //generate unique run id with time
392                         prefix = xbt_os_time();
393                 }
394
395                 if (!xbt_cfg_get_boolean("tracing/smpi/format/ti-one-file") || temp == nullptr) {
396                         char *folder_name = bprintf("%s_files", TRACE_get_filename());
397                         char *filename = bprintf("%s/%f_%s.txt", folder_name, prefix, container->name);
398 #ifdef WIN32
399                         _mkdir(folder_name);
400 #else
401                         mkdir(folder_name, S_IRWXU | S_IRWXG | S_IRWXO);
402 #endif
403                         temp = fopen(filename, "w");
404                         xbt_assert(temp, "Tracefile %s could not be opened for writing: %s", filename, strerror(errno));
405                         fprintf(tracing_file, "%s\n", filename);
406
407                         xbt_free(folder_name);
408                         xbt_free(filename);
409                 }
410
411                 xbt_dict_set(tracing_files, container->name, (void *) temp, nullptr);
412         } else {
413                 THROW_IMPOSSIBLE;
414         }
415 }
416
417 DestroyContainerEvent::DestroyContainerEvent (container_t container)
418 {
419   this->event_type                              = PAJE_DestroyContainer;
420   this->timestamp                               = SIMIX_get_clock();
421   this->container = container;
422
423   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
424
425   print();
426 }
427
428 void DestroyContainerEvent::print() {
429         if (instr_fmt_type == instr_fmt_paje) {
430                 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
431                 stream << std::fixed << std::setprecision(TRACE_precision());
432                 stream << (int)this->event_type;
433                 print_timestamp(this);
434                 stream << " "   << container->type->id
435                                 << " "   << container->id;
436
437                 print_row();
438         } else if (instr_fmt_type == instr_fmt_TI) {
439                 if (!xbt_cfg_get_boolean("tracing/smpi/format/ti-one-file")|| xbt_dict_length(tracing_files) == 1) {
440                         FILE* f = (FILE*)xbt_dict_get_or_null(tracing_files, container->name);
441                         fclose(f);
442                 }
443                 xbt_dict_remove(tracing_files, container->name);
444         } else {
445                 THROW_IMPOSSIBLE;
446         }
447 }
448
449 SetVariableEvent::SetVariableEvent (double timestamp, container_t container, type_t type, double value)
450 {
451   this->event_type                         = PAJE_SetVariable;
452   this->timestamp                          = timestamp;
453   this->type      = type;
454   this->container = container;
455   this->value     = value;
456
457   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
458
459   insert_into_buffer (this);
460 }
461
462 void SetVariableEvent::print() {
463   XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
464   stream << std::fixed << std::setprecision(TRACE_precision());
465   stream << (int)this->event_type;
466   print_timestamp(this);
467   stream << " " << type->id
468          << " " << container->id
469          << " " << value;
470   print_row();
471 }
472
473 AddVariableEvent::AddVariableEvent (double timestamp, container_t container, type_t type, double value)
474 {
475   this->event_type                         = PAJE_AddVariable;
476   this->timestamp                          = timestamp;
477   this->type      = type;
478   this->container = container;
479   this->value     = value;
480
481   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
482
483   insert_into_buffer (this);
484 }
485
486 void AddVariableEvent::print() {
487   XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
488   stream << std::fixed << std::setprecision(TRACE_precision());
489   stream << (int)this->event_type;
490   print_timestamp(this);
491   stream << " " << type->id
492          << " " << container->id
493          << " " << value;
494   print_row();
495 }
496
497 SubVariableEvent::SubVariableEvent (double timestamp, container_t container, type_t type, double value)
498 {
499   this->event_type                         = PAJE_SubVariable;
500   this->timestamp                          = timestamp;
501   this->type      = type;
502   this->container = container;
503   this->value     = value;
504
505   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
506
507   insert_into_buffer (this);
508 }
509
510 void SubVariableEvent::print() {
511   XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
512   stream << std::fixed << std::setprecision(TRACE_precision());
513   stream << (int)this->event_type;
514   print_timestamp(this);
515   stream << " " << type->id
516          << " " << container->id
517          << " " << value;
518   print_row();
519 }
520
521 SetStateEvent::SetStateEvent (double timestamp, container_t container, type_t type, val_t value)
522 {
523   this->event_type                      = PAJE_SetState;
524   this->timestamp                       = timestamp;
525   this->type      = type;
526   this->container = container;
527   this->value     = value;
528
529 #if HAVE_SMPI
530   if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
531     smpi_trace_call_location_t* loc = smpi_trace_get_call_location();
532     filename   = loc->filename;
533     linenumber = loc->linenumber;
534   }
535 #endif
536
537   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
538
539   insert_into_buffer (this);
540 }
541
542 void SetStateEvent::print() {
543   XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
544   stream << std::fixed << std::setprecision(TRACE_precision());
545   stream << (int)this->event_type;
546   print_timestamp(this);
547   stream << " " << type->id
548          << " " << container->id;
549   stream << " " <<value->id;
550 #if HAVE_SMPI
551   if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
552     stream << " \"" << filename
553            << "\" " << linenumber;
554   }
555 #endif
556   print_row();
557 }
558
559 PushStateEvent::PushStateEvent (double timestamp, container_t container, type_t type, val_t value, void* extra)
560 {
561   this->event_type                  = PAJE_PushState;
562   this->timestamp                   = timestamp;
563   this->type = type;
564   this->container = container;
565   this->value     = value;
566   this->extra_     = extra;
567
568 #if HAVE_SMPI
569   if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
570     smpi_trace_call_location_t* loc = smpi_trace_get_call_location();
571     filename   = loc->filename;
572     linenumber = loc->linenumber;
573   }
574 #endif
575
576   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
577
578   insert_into_buffer (this);
579 }
580
581 PushStateEvent::PushStateEvent (double timestamp, container_t container, type_t type, val_t value)
582  : PushStateEvent(timestamp, container, type, value, nullptr)
583 {}
584 void PushStateEvent::print() {
585         if (instr_fmt_type == instr_fmt_paje) {
586                 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
587                 stream << std::fixed << std::setprecision(TRACE_precision());
588                 stream << (int)this->event_type;
589                 print_timestamp(this);
590                 stream << " " << type->id
591                                 << " " << container->id;
592                 stream << " " <<value->id;
593
594                 if (TRACE_display_sizes()) {
595                         stream << " ";
596                         if (extra_ != nullptr) {
597                                 stream << static_cast<instr_extra_data>(extra_)->send_size;
598                         }
599                         else {
600                                 stream << 0;
601                         }
602                 }
603 #if HAVE_SMPI
604                 if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
605                         stream << " \"" << filename
606                                         << "\" " << linenumber;
607                 }
608 #endif
609                 print_row();
610
611                 if (extra_ != nullptr) {
612                         if (static_cast<instr_extra_data>(extra_)->sendcounts != nullptr)
613                                 xbt_free(static_cast<instr_extra_data>(extra_)->sendcounts);
614                         if (static_cast<instr_extra_data>(extra_)->recvcounts != nullptr)
615                                 xbt_free(static_cast<instr_extra_data>(extra_)->recvcounts);
616                         xbt_free(extra_);
617                 }
618         } else if (instr_fmt_type == instr_fmt_TI) {
619                   if (extra_ == nullptr)
620                     return;
621                   instr_extra_data extra = (instr_extra_data)extra_;
622
623                   char *process_id = nullptr;
624                   //FIXME: dirty extract "rank-" from the name, as we want the bare process id here
625                   if (strstr(container->name, "rank-") == nullptr)
626                     process_id = xbt_strdup(container->name);
627                   else
628                     process_id = xbt_strdup(container->name + 5);
629
630                   FILE* trace_file =  (FILE* )xbt_dict_get(tracing_files, container->name);
631
632                   switch (extra->type) {
633                   case TRACING_INIT:
634                     fprintf(trace_file, "%s init\n", process_id);
635                     break;
636                   case TRACING_FINALIZE:
637                     fprintf(trace_file, "%s finalize\n", process_id);
638                     break;
639                   case TRACING_SEND:
640                     fprintf(trace_file, "%s send %d %d %s\n", process_id, extra->dst, extra->send_size, extra->datatype1);
641                     break;
642                   case TRACING_ISEND:
643                     fprintf(trace_file, "%s Isend %d %d %s\n", process_id, extra->dst, extra->send_size, extra->datatype1);
644                     break;
645                   case TRACING_RECV:
646                     fprintf(trace_file, "%s recv %d %d %s\n", process_id, extra->src, extra->send_size, extra->datatype1);
647                     break;
648                   case TRACING_IRECV:
649                     fprintf(trace_file, "%s Irecv %d %d %s\n", process_id, extra->src, extra->send_size, extra->datatype1);
650                     break;
651                   case TRACING_TEST:
652                     fprintf(trace_file, "%s test\n", process_id);
653                     break;
654                   case TRACING_WAIT:
655                     fprintf(trace_file, "%s wait\n", process_id);
656                     break;
657                   case TRACING_WAITALL:
658                     fprintf(trace_file, "%s waitAll\n", process_id);
659                     break;
660                   case TRACING_BARRIER:
661                     fprintf(trace_file, "%s barrier\n", process_id);
662                     break;
663                   case TRACING_BCAST:          // rank bcast size (root) (datatype)
664                     fprintf(trace_file, "%s bcast %d ", process_id, extra->send_size);
665                     if (extra->root != 0 || (extra->datatype1 && strcmp(extra->datatype1, "")))
666                       fprintf(trace_file, "%d %s", extra->root, extra->datatype1);
667                     fprintf(trace_file, "\n");
668                     break;
669                   case TRACING_REDUCE:         // rank reduce comm_size comp_size (root) (datatype)
670                     fprintf(trace_file, "%s reduce %d %f ", process_id, extra->send_size, extra->comp_size);
671                     if (extra->root != 0 || (extra->datatype1 && strcmp(extra->datatype1, "")))
672                       fprintf(trace_file, "%d %s", extra->root, extra->datatype1);
673                     fprintf(trace_file, "\n");
674                     break;
675                   case TRACING_ALLREDUCE:      // rank allreduce comm_size comp_size (datatype)
676                     fprintf(trace_file, "%s allReduce %d %f %s\n", process_id, extra->send_size, extra->comp_size, extra->datatype1);
677                     break;
678                   case TRACING_ALLTOALL:       // rank alltoall send_size recv_size (sendtype) (recvtype)
679                     fprintf(trace_file, "%s allToAll %d %d %s %s\n", process_id, extra->send_size, extra->recv_size, extra->datatype1,
680                             extra->datatype2);
681                     break;
682                   case TRACING_ALLTOALLV:      // rank alltoallv send_size [sendcounts] recv_size [recvcounts] (sendtype) (recvtype)
683                     fprintf(trace_file, "%s allToAllV %d ", process_id, extra->send_size);
684                     for (int i = 0; i < extra->num_processes; i++)
685                       fprintf(trace_file, "%d ", extra->sendcounts[i]);
686                     fprintf(trace_file, "%d ", extra->recv_size);
687                     for (int i = 0; i < extra->num_processes; i++)
688                       fprintf(trace_file, "%d ", extra->recvcounts[i]);
689                     fprintf(trace_file, "%s %s \n", extra->datatype1, extra->datatype2);
690                     break;
691                   case TRACING_GATHER:         // rank gather send_size recv_size root (sendtype) (recvtype)
692                     fprintf(trace_file, "%s gather %d %d %d %s %s\n", process_id, extra->send_size, extra->recv_size, extra->root,
693                             extra->datatype1, extra->datatype2);
694                     break;
695                   case TRACING_ALLGATHERV:     // rank allgatherv send_size [recvcounts] (sendtype) (recvtype)
696                     fprintf(trace_file, "%s allGatherV %d ", process_id, extra->send_size);
697                     for (int i = 0; i < extra->num_processes; i++)
698                       fprintf(trace_file, "%d ", extra->recvcounts[i]);
699                     fprintf(trace_file, "%s %s \n", extra->datatype1, extra->datatype2);
700                     break;
701                   case TRACING_REDUCE_SCATTER: // rank reducescatter [recvcounts] comp_size (sendtype)
702                     fprintf(trace_file, "%s reduceScatter ", process_id);
703                     for (int i = 0; i < extra->num_processes; i++)
704                       fprintf(trace_file, "%d ", extra->recvcounts[i]);
705                     fprintf(trace_file, "%f %s\n", extra->comp_size, extra->datatype1);
706                     break;
707                   case TRACING_COMPUTING:
708                     fprintf(trace_file, "%s compute %f\n", process_id, extra->comp_size);
709                     break;
710                   case TRACING_SLEEPING:
711                     fprintf(trace_file, "%s sleep %f\n", process_id, extra->sleep_duration);
712                     break;
713                   case TRACING_GATHERV: // rank gatherv send_size [recvcounts] root (sendtype) (recvtype)
714                     fprintf(trace_file, "%s gatherV %d ", process_id, extra->send_size);
715                     for (int i = 0; i < extra->num_processes; i++)
716                       fprintf(trace_file, "%d ", extra->recvcounts[i]);
717                     fprintf(trace_file, "%d %s %s\n", extra->root, extra->datatype1, extra->datatype2);
718                     break;
719                   case TRACING_WAITANY:
720                   case TRACING_SENDRECV:
721                   case TRACING_SCATTER:
722                   case TRACING_SCATTERV:
723                   case TRACING_ALLGATHER:
724                   case TRACING_SCAN:
725                   case TRACING_EXSCAN:
726                   case TRACING_COMM_SIZE:
727                   case TRACING_COMM_SPLIT:
728                   case TRACING_COMM_DUP:
729                   case TRACING_SSEND:
730                   case TRACING_ISSEND:
731                   default:
732                     XBT_WARN ("Call from %s impossible to translate into replay command : Not implemented (yet)",
733                          value->name);
734                     break;
735                   }
736
737                   if (extra->recvcounts != nullptr)
738                     xbt_free(extra->recvcounts);
739                   if (extra->sendcounts != nullptr)
740                     xbt_free(extra->sendcounts);
741                   xbt_free(process_id);
742                   xbt_free(extra);
743
744         } else {
745                 THROW_IMPOSSIBLE;
746         }
747 }
748
749
750 PopStateEvent::PopStateEvent (double timestamp, container_t container, type_t type)
751 {
752   this->event_type                      = PAJE_PopState;
753   this->timestamp                       = timestamp;
754   this->type      = type;
755   this->container = container;
756
757   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
758
759   insert_into_buffer (this);
760 }
761
762 void PopStateEvent::print() {
763   XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
764   stream << std::fixed << std::setprecision(TRACE_precision());
765   stream << (int)this->event_type;
766   print_timestamp(this);
767   stream << " " << type->id
768          << " " << container->id;
769   print_row();
770 }
771
772 ResetStateEvent::ResetStateEvent (double timestamp, container_t container, type_t type)
773 {
774   this->event_type                        = PAJE_ResetState;
775   this->timestamp                         = timestamp;
776   this->type      = type;
777   this->container = container;
778
779   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
780
781   insert_into_buffer (this);
782 }
783
784 void ResetStateEvent::print() {
785   XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
786   stream << std::fixed << std::setprecision(TRACE_precision());
787   stream << (int)this->event_type;
788   print_timestamp(this);
789   stream << " " << type->id
790          << " " << container->id;
791   print_row();
792 }
793
794 StartLinkEvent::StartLinkEvent (double timestamp, container_t container,
795     type_t type, container_t sourceContainer, const char *value, const char *key)
796   : StartLinkEvent(timestamp, container, type, sourceContainer, value, key, -1)
797 {}
798
799 StartLinkEvent::StartLinkEvent (double timestamp, container_t container, type_t type, container_t sourceContainer,
800                                 const char *value, const char *key, int size)
801 {
802   event_type                             = PAJE_StartLink;
803   this->timestamp       = timestamp;
804   this->type            = type;
805   this->container       = container;
806   this->sourceContainer = sourceContainer;
807   this->value           = xbt_strdup(value);
808   this->key             = xbt_strdup(key);
809   this->size            = size;
810
811   XBT_DEBUG("%s: event_type=%d, timestamp=%f, value:%s", __FUNCTION__,
812       (int)event_type, this->timestamp, this->value);
813
814   insert_into_buffer (this);
815 }
816
817 void StartLinkEvent::print() {
818   XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
819   stream << std::fixed << std::setprecision(TRACE_precision());
820   stream << (int)this->event_type;
821   print_timestamp(this);
822   stream << " " <<type->id
823          << " " <<container->id
824          << " " <<value;
825   stream << " " << sourceContainer->id
826          << " " << key;
827
828   if (TRACE_display_sizes()) {
829     stream << " " << size;
830   }
831   print_row();
832 }
833
834 EndLinkEvent::EndLinkEvent (double timestamp, container_t container, type_t type, container_t destContainer,
835                       const char *value, const char *key)
836 {
837   this->event_type                         = PAJE_EndLink;
838   this->timestamp                          = timestamp;
839   this->type          = type;
840   this->container     = container;
841   this->destContainer = destContainer;
842   this->value         = xbt_strdup(value);
843   this->key           = xbt_strdup(key);
844
845   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
846
847   insert_into_buffer (this);
848 }
849
850
851 void EndLinkEvent::print() {
852   XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
853   stream << std::fixed << std::setprecision(TRACE_precision());
854   stream << (int)this->event_type;
855   print_timestamp(this);
856   stream << " " <<type->id
857          << " " <<container->id
858          << " " <<value;
859   stream << " " << destContainer->id
860          << " " << key;
861   print_row();
862 }
863
864 NewEvent::NewEvent (double timestamp, container_t container, type_t type, val_t value)
865 {
866   this->event_type                      = PAJE_NewEvent;
867   this->timestamp                       = timestamp;
868   this->type      = type;
869   this->container = container;
870   this->value     = value;
871
872   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
873
874   insert_into_buffer (this);
875 }
876
877 void NewEvent::print () {
878   XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
879   stream << std::fixed << std::setprecision(TRACE_precision());
880   stream << (int)this->event_type;
881   print_timestamp(this);
882   stream << " " << type->id
883          << " " << container->id
884          << " " << value->id;
885   print_row();
886 }
887
888
889 void TRACE_TI_start()
890 {
891   char *filename = TRACE_get_filename();
892   tracing_file = fopen(filename, "w");
893   if (tracing_file == nullptr)
894     THROWF(system_error, 1, "Tracefile %s could not be opened for writing.", filename);
895
896   XBT_DEBUG("Filename %s is open for writing", filename);
897
898   /* output one line comment */
899   dump_comment(TRACE_get_comment());
900
901   /* output comment file */
902   dump_comment_file(TRACE_get_comment_file());
903 }
904
905 void TRACE_TI_end()
906 {
907   xbt_dict_free(&tracing_files);
908   fclose(tracing_file);
909   char *filename = TRACE_get_filename();
910   XBT_DEBUG("Filename %s is closed", filename);
911 }
912