1 /* Copyright (c) 2010-2016. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "src/instr/instr_private.h"
8 #include "src/instr/instr_smpi.h"
9 #include "src/smpi/private.hpp"
11 #include "xbt/virtu.h" /* sg_cmdline */
14 #include <iomanip> /** std::setprecision **/
15 #include "simgrid/sg_config.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_paje_trace, instr, "tracing event system");
19 extern FILE * tracing_file;
20 extern s_instr_trace_writer_t active_writer;
22 static std::stringstream stream;
23 FILE *tracing_file = nullptr;
25 void print_NULL(PajeEvent* event){}
27 /* The active set of functions for the selected trace format
28 * By default, they all do nothing, hence the print_NULL to avoid segfaults */
30 s_instr_trace_writer_t active_writer = {&print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL,
31 &print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL,
32 &print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL};
34 std::vector<PajeEvent*> buffer;
35 void buffer_debug(std::vector<PajeEvent*> *buf);
37 void dump_comment (const char *comment)
39 if (!strlen(comment)) return;
40 fprintf (tracing_file, "# %s\n", comment);
43 void dump_comment_file (const char *filename)
45 if (!strlen(filename)) return;
46 FILE *file = fopen (filename, "r");
48 THROWF (system_error, 1, "Comment file %s could not be opened for reading.", filename);
53 if (feof(file)) break;
54 fprintf (tracing_file, "# ");
56 fprintf (tracing_file, "%c", c);
58 if (feof(file)) break;
60 fprintf (tracing_file, "\n");
65 double TRACE_last_timestamp_to_dump = 0;
66 //dumps the trace file until the timestamp TRACE_last_timestamp_to_dump
67 void TRACE_paje_dump_buffer (int force)
69 if (!TRACE_is_enabled()) return;
70 XBT_DEBUG("%s: dump until %f. starts", __FUNCTION__, TRACE_last_timestamp_to_dump);
72 for (auto event : buffer){
78 std::vector<PajeEvent*>::iterator i = buffer.begin();
79 for (auto event :buffer){
80 double head_timestamp = event->timestamp;
81 if (head_timestamp > TRACE_last_timestamp_to_dump)
87 buffer.erase(buffer.begin(), i);
89 XBT_DEBUG("%s: ends", __FUNCTION__);
92 void buffer_debug(std::vector<PajeEvent*> *buf);
93 void buffer_debug(std::vector<PajeEvent*> *buf) {
95 XBT_DEBUG(">>>>>> Dump the state of the buffer. %zu events", buf->size());
96 for (auto event :*buf){
98 XBT_DEBUG("%p %s", event, stream.str().c_str());
105 static void print_row() {
107 fprintf(tracing_file, "%s", stream.str().c_str());
108 XBT_DEBUG("Dump %s", stream.str().c_str());
113 static void print_timestamp(PajeEvent* event) {
115 /* prevent 0.0000 in the trace - this was the behavior before the transition to c++ */
116 if (event->timestamp < 1e-12)
119 stream << event->timestamp;
122 /* internal do the instrumentation module */
123 static void insert_into_buffer (PajeEvent* tbi)
125 if (TRACE_buffer() == 0){
130 buffer_debug(&buffer);
132 XBT_DEBUG("%s: insert event_type=%d, timestamp=%f, buffersize=%zu)",
133 __FUNCTION__, (int)tbi->event_type, tbi->timestamp, buffer.size());
134 std::vector<PajeEvent*>::reverse_iterator i;
135 for (i = buffer.rbegin(); i != buffer.rend(); ++i) {
137 XBT_DEBUG("compare to %p is of type %d; timestamp:%f", e1,
138 (int)e1->event_type, e1->timestamp);
139 if (e1->timestamp <= tbi->timestamp)
142 buffer.insert(i.base(), tbi);
143 if (i == buffer.rend())
144 XBT_DEBUG("%s: inserted at beginning", __FUNCTION__);
145 else if (i == buffer.rbegin())
146 XBT_DEBUG("%s: inserted at end", __FUNCTION__);
148 XBT_DEBUG("%s: inserted at pos= %zd from its end", __FUNCTION__,
149 std::distance(buffer.rbegin(),i));
151 buffer_debug(&buffer);
154 PajeEvent:: ~PajeEvent()
156 XBT_DEBUG("%s not implemented for %p: event_type=%d, timestamp=%f", __FUNCTION__,
157 this, (int)event_type, timestamp);
158 // xbt_backtrace_display_current();
160 /* switch (event->event_type){
162 xbt_free (((startLink_t)(event->data))->value);
163 xbt_free (((startLink_t)(event->data))->key);
166 xbt_free (((endLink_t)(event->data))->value);
167 xbt_free (((endLink_t)(event->data))->key);
174 void TRACE_paje_start() {
175 char *filename = TRACE_get_filename();
176 tracing_file = fopen(filename, "w");
177 if (tracing_file == nullptr){
178 THROWF (system_error, 1, "Tracefile %s could not be opened for writing.", filename);
181 XBT_DEBUG("Filename %s is open for writing", filename);
183 /* output generator version */
184 fprintf (tracing_file, "#This file was generated using SimGrid-%d.%d.%d\n",
185 SIMGRID_VERSION_MAJOR, SIMGRID_VERSION_MINOR, SIMGRID_VERSION_PATCH);
186 fprintf (tracing_file, "#[");
189 xbt_dynar_foreach (xbt_cmdline, cpt, str){
190 fprintf(tracing_file, "%s ",str);
192 fprintf (tracing_file, "]\n");
194 /* output one line comment */
195 dump_comment (TRACE_get_comment());
197 /* output comment file */
198 dump_comment_file (TRACE_get_comment_file());
201 TRACE_header(TRACE_basic(),TRACE_display_sizes());
204 void TRACE_paje_end() {
205 fclose(tracing_file);
206 char *filename = TRACE_get_filename();
207 XBT_DEBUG("Filename %s is closed", filename);
210 DefineContainerEvent::DefineContainerEvent(type_t type)
213 event_type = PAJE_DefineContainerType;
216 XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
221 void DefineContainerEvent::print() {
222 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
223 stream << std::fixed << std::setprecision(TRACE_precision());
224 stream << (int)this->event_type;
225 stream << " " << type->id
226 << " " << type->father->id
227 << " " << type->name;
233 DefineVariableTypeEvent::DefineVariableTypeEvent(type_t type)
235 this->event_type = PAJE_DefineVariableType;
239 XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
245 void DefineVariableTypeEvent::print() {
246 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
247 stream << std::fixed << std::setprecision(TRACE_precision());
248 stream << (int)this->event_type;
249 stream << " " << type->id
250 << " " << type->father->id
251 << " " << type->name;
253 stream << " \"" << type->color << "\"";
257 DefineStateTypeEvent::DefineStateTypeEvent(type_t type)
259 this->event_type = PAJE_DefineStateType;
263 XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
270 DefineEventTypeEvent::DefineEventTypeEvent(type_t type)
272 this->event_type = PAJE_DefineEventType;
276 XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
283 void DefineStateTypeEvent::print() {
284 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
285 stream << std::fixed << std::setprecision(TRACE_precision());
286 stream << (int)this->event_type;
287 stream << " " << type->id
288 << " " << type->father->id
289 << " " << type->name;
293 void DefineEventTypeEvent::print() {
294 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
295 stream << std::fixed << std::setprecision(TRACE_precision());
296 stream << (int)this->event_type;
297 stream << " " << type->id
298 << " " << type->father->id
299 << " " << type->name;
303 DefineLinkTypeEvent::DefineLinkTypeEvent(type_t type, type_t source, type_t dest)
305 this->event_type = PAJE_DefineLinkType;
308 this->source = source;
311 XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
317 void DefineLinkTypeEvent::print() {
318 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
319 stream << std::fixed << std::setprecision(TRACE_precision());
320 stream << (int)this->event_type;
321 stream << " " << type->id
322 << " " << type->father->id
325 << " " << type->name;
329 DefineEntityValueEvent::DefineEntityValueEvent (val_t value)
331 this->event_type = PAJE_DefineEntityValue;
335 XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
342 void DefineEntityValueEvent::print() {
343 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
344 stream << std::fixed << std::setprecision(TRACE_precision());
345 stream << (int)this->event_type;
346 stream << " " << value->id
347 << " " << value->father->id
348 << " " << value->name;
350 stream << " \"" << value->color << "\"";
354 CreateContainerEvent::CreateContainerEvent (container_t container)
356 this->event_type = PAJE_CreateContainer;
357 this->timestamp = SIMIX_get_clock();
358 this->container = container;
360 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
366 void CreateContainerEvent::print() {
367 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
368 stream << std::fixed << std::setprecision(TRACE_precision());
369 stream << (int)this->event_type;
370 print_timestamp(this);
371 stream << " " << container->id
372 << " " << container->type->id
373 << " " << container->father->id
374 << " \"" << container->name << "\"";
379 DestroyContainerEvent::DestroyContainerEvent (container_t container)
381 this->event_type = PAJE_DestroyContainer;
382 this->timestamp = SIMIX_get_clock();
383 this->container = container;
385 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
390 void DestroyContainerEvent::print() {
391 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
392 stream << std::fixed << std::setprecision(TRACE_precision());
393 stream << (int)this->event_type;
394 print_timestamp(this);
395 stream << " " << container->type->id
396 << " " << container->id;
401 SetVariableEvent::SetVariableEvent (double timestamp, container_t container, type_t type, double value)
403 this->event_type = PAJE_SetVariable;
404 this->timestamp = timestamp;
406 this->container = container;
409 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
411 insert_into_buffer (this);
414 void SetVariableEvent::print() {
415 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
416 stream << std::fixed << std::setprecision(TRACE_precision());
417 stream << (int)this->event_type;
418 print_timestamp(this);
419 stream << " " << type->id
420 << " " << container->id
425 AddVariableEvent::AddVariableEvent (double timestamp, container_t container, type_t type, double value)
427 this->event_type = PAJE_AddVariable;
428 this->timestamp = timestamp;
430 this->container = container;
433 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
435 insert_into_buffer (this);
438 void AddVariableEvent::print() {
439 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
440 stream << std::fixed << std::setprecision(TRACE_precision());
441 stream << (int)this->event_type;
442 print_timestamp(this);
443 stream << " " << type->id
444 << " " << container->id
449 SubVariableEvent::SubVariableEvent (double timestamp, container_t container, type_t type, double value)
451 this->event_type = PAJE_SubVariable;
452 this->timestamp = timestamp;
454 this->container = container;
457 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
459 insert_into_buffer (this);
462 void SubVariableEvent::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
473 SetStateEvent::SetStateEvent (double timestamp, container_t container, type_t type, val_t value)
475 this->event_type = PAJE_SetState;
476 this->timestamp = timestamp;
478 this->container = container;
482 if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
483 smpi_trace_call_location_t* loc = smpi_trace_get_call_location();
484 filename = loc->filename;
485 linenumber = loc->linenumber;
489 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
491 insert_into_buffer (this);
494 void SetStateEvent::print() {
495 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
496 stream << std::fixed << std::setprecision(TRACE_precision());
497 stream << (int)this->event_type;
498 print_timestamp(this);
499 stream << " " << type->id
500 << " " << container->id;
501 stream << " " <<value->id;
503 if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
504 stream << " \"" << filename
505 << "\" " << linenumber;
511 PushStateEvent::PushStateEvent (double timestamp, container_t container, type_t type, val_t value, void* extra)
513 this->event_type = PAJE_PushState;
514 this->timestamp = timestamp;
516 this->container = container;
521 if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
522 smpi_trace_call_location_t* loc = smpi_trace_get_call_location();
523 filename = loc->filename;
524 linenumber = loc->linenumber;
528 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
530 insert_into_buffer (this);
533 PushStateEvent::PushStateEvent (double timestamp, container_t container, type_t type, val_t value)
534 : PushStateEvent(timestamp, container, type, value, nullptr)
536 void PushStateEvent::print() {
537 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
538 stream << std::fixed << std::setprecision(TRACE_precision());
539 stream << (int)this->event_type;
540 print_timestamp(this);
541 stream << " " << type->id
542 << " " << container->id;
543 stream << " " <<value->id;
545 if (TRACE_display_sizes()) {
547 if (extra != nullptr) {
548 stream << static_cast<instr_extra_data>(extra)->send_size;
555 if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
556 stream << " \"" << filename
557 << "\" " << linenumber;
562 if (extra != nullptr) {
563 if (static_cast<instr_extra_data>(extra)->sendcounts != nullptr)
564 xbt_free(static_cast<instr_extra_data>(extra)->sendcounts);
565 if (static_cast<instr_extra_data>(extra)->recvcounts != nullptr)
566 xbt_free(static_cast<instr_extra_data>(extra)->recvcounts);
572 PopStateEvent::PopStateEvent (double timestamp, container_t container, type_t type)
574 this->event_type = PAJE_PopState;
575 this->timestamp = timestamp;
577 this->container = container;
579 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
581 insert_into_buffer (this);
584 void PopStateEvent::print() {
585 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
586 stream << std::fixed << std::setprecision(TRACE_precision());
587 stream << (int)this->event_type;
588 print_timestamp(this);
589 stream << " " << type->id
590 << " " << container->id;
594 ResetStateEvent::ResetStateEvent (double timestamp, container_t container, type_t type)
596 this->event_type = PAJE_ResetState;
597 this->timestamp = timestamp;
599 this->container = container;
601 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
603 insert_into_buffer (this);
606 void ResetStateEvent::print() {
607 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
608 stream << std::fixed << std::setprecision(TRACE_precision());
609 stream << (int)this->event_type;
610 print_timestamp(this);
611 stream << " " << type->id
612 << " " << container->id;
616 StartLinkEvent::StartLinkEvent (double timestamp, container_t container,
617 type_t type, container_t sourceContainer, const char *value, const char *key)
618 : StartLinkEvent(timestamp, container, type, sourceContainer, value, key, -1)
621 StartLinkEvent::StartLinkEvent (double timestamp, container_t container, type_t type, container_t sourceContainer,
622 const char *value, const char *key, int size)
624 event_type = PAJE_StartLink;
625 this->timestamp = timestamp;
627 this->container = container;
628 this->sourceContainer = sourceContainer;
629 this->value = xbt_strdup(value);
630 this->key = xbt_strdup(key);
633 XBT_DEBUG("%s: event_type=%d, timestamp=%f, value:%s", __FUNCTION__,
634 (int)event_type, this->timestamp, this->value);
636 insert_into_buffer (this);
639 void StartLinkEvent::print() {
640 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
641 stream << std::fixed << std::setprecision(TRACE_precision());
642 stream << (int)this->event_type;
643 print_timestamp(this);
644 stream << " " <<type->id
645 << " " <<container->id
647 stream << " " << sourceContainer->id
650 if (TRACE_display_sizes()) {
651 stream << " " << size;
656 EndLinkEvent::EndLinkEvent (double timestamp, container_t container, type_t type, container_t destContainer,
657 const char *value, const char *key)
659 this->event_type = PAJE_EndLink;
660 this->timestamp = timestamp;
662 this->container = container;
663 this->destContainer = destContainer;
664 this->value = xbt_strdup(value);
665 this->key = xbt_strdup(key);
667 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
669 insert_into_buffer (this);
673 void EndLinkEvent::print() {
674 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
675 stream << std::fixed << std::setprecision(TRACE_precision());
676 stream << (int)this->event_type;
677 print_timestamp(this);
678 stream << " " <<type->id
679 << " " <<container->id
681 stream << " " << destContainer->id
686 NewEvent::NewEvent (double timestamp, container_t container, type_t type, val_t value)
688 this->event_type = PAJE_NewEvent;
689 this->timestamp = timestamp;
691 this->container = container;
694 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
696 insert_into_buffer (this);
699 void NewEvent::print () {
700 XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event_type, TRACE_precision(), timestamp);
701 stream << std::fixed << std::setprecision(TRACE_precision());
702 stream << (int)this->event_type;
703 print_timestamp(this);
704 stream << " " << type->id
705 << " " << container->id