Logo AND Algorithmique Numérique Distribuée

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