1 /* Copyright (c) 2010-2017. 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 "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"
12 #include "xbt/virtu.h" /* sg_cmdline */
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_paje_trace, instr, "tracing event system");
17 static std::stringstream stream;
18 FILE *tracing_file = nullptr;
20 std::vector<simgrid::instr::PajeEvent*> buffer;
21 void buffer_debug(std::vector<simgrid::instr::PajeEvent*>* buf);
23 void dump_comment(std::string comment)
27 fprintf(tracing_file, "# %s\n", comment.c_str());
30 void dump_comment_file(std::string filename)
34 std::ifstream* fs = new std::ifstream();
35 fs->open(filename.c_str(), std::ifstream::in);
38 THROWF(system_error, 1, "Comment file %s could not be opened for reading.", filename.c_str());
40 while (not fs->eof()) {
42 fprintf (tracing_file, "# ");
43 std::getline(*fs, line);
44 fprintf(tracing_file, "%s", line.c_str());
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)
53 if (not TRACE_is_enabled())
55 XBT_DEBUG("%s: dump until %f. starts", __FUNCTION__, TRACE_last_timestamp_to_dump);
57 for (auto const& event : buffer) {
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)
72 buffer.erase(buffer.begin(), i);
74 XBT_DEBUG("%s: ends", __FUNCTION__);
77 void buffer_debug(std::vector<simgrid::instr::PajeEvent*>* buf)
79 if (not XBT_LOG_ISENABLED(instr_paje_trace, xbt_log_priority_debug))
81 XBT_DEBUG(">>>>>> Dump the state of the buffer. %zu events", buf->size());
82 for (auto const& event : *buf) {
84 XBT_DEBUG("%p %s", event, stream.str().c_str());
91 /* internal do the instrumentation module */
92 void simgrid::instr::PajeEvent::insertIntoBuffer()
94 if (not TRACE_buffer()) {
99 buffer_debug(&buffer);
101 XBT_DEBUG("%s: insert event_type=%u, timestamp=%f, buffersize=%zu)", __FUNCTION__, eventType_, timestamp_,
103 std::vector<simgrid::instr::PajeEvent*>::reverse_iterator i;
104 for (i = buffer.rbegin(); i != buffer.rend(); ++i) {
105 simgrid::instr::PajeEvent* e1 = *i;
106 XBT_DEBUG("compare to %p is of type %u; timestamp:%f", e1, e1->eventType_, e1->timestamp_);
107 if (e1->timestamp_ <= timestamp_)
110 if (i == buffer.rend())
111 XBT_DEBUG("%s: inserted at beginning", __FUNCTION__);
112 else if (i == buffer.rbegin())
113 XBT_DEBUG("%s: inserted at end", __FUNCTION__);
115 XBT_DEBUG("%s: inserted at pos= %zd from its end", __FUNCTION__, std::distance(buffer.rbegin(), i));
116 buffer.insert(i.base(), this);
118 buffer_debug(&buffer);
121 void TRACE_paje_start() {
122 std::string filename = TRACE_get_filename();
123 tracing_file = fopen(filename.c_str(), "w");
124 if (tracing_file == nullptr){
125 THROWF(system_error, 1, "Tracefile %s could not be opened for writing.", filename.c_str());
128 XBT_DEBUG("Filename %s is open for writing", filename.c_str());
130 /* output generator version */
131 fprintf (tracing_file, "#This file was generated using SimGrid-%d.%d.%d\n",
132 SIMGRID_VERSION_MAJOR, SIMGRID_VERSION_MINOR, SIMGRID_VERSION_PATCH);
133 fprintf (tracing_file, "#[");
136 xbt_dynar_foreach (xbt_cmdline, cpt, str){
137 fprintf(tracing_file, "%s ",str);
139 fprintf (tracing_file, "]\n");
141 /* output one line comment */
142 dump_comment (TRACE_get_comment());
144 /* output comment file */
145 dump_comment_file (TRACE_get_comment_file());
148 TRACE_header(TRACE_basic(),TRACE_display_sizes());
151 void TRACE_paje_end() {
152 fclose(tracing_file);
153 XBT_DEBUG("Filename %s is closed", TRACE_get_filename().c_str());
157 void TRACE_TI_start()
159 std::string filename = TRACE_get_filename();
160 tracing_file = fopen(filename.c_str(), "w");
161 if (tracing_file == nullptr) {
162 THROWF(system_error, 1, "Tracefile %s could not be opened for writing.", filename.c_str());
165 XBT_DEBUG("Filename %s is open for writing", filename.c_str());
167 /* output one line comment */
168 dump_comment(TRACE_get_comment());
170 /* output comment file */
171 dump_comment_file(TRACE_get_comment_file());
176 fclose(tracing_file);
177 XBT_DEBUG("Filename %s is closed", TRACE_get_filename().c_str());