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)
80 XBT_DEBUG(">>>>>> Dump the state of the buffer. %zu events", buf->size());
81 for (auto const& event : *buf) {
83 XBT_DEBUG("%p %s", event, stream.str().c_str());
90 static void print_row() {
92 fprintf(tracing_file, "%s", stream.str().c_str());
93 XBT_DEBUG("Dump %s", stream.str().c_str());
98 static void print_timestamp(simgrid::instr::PajeEvent* event)
101 /* prevent 0.0000 in the trace - this was the behavior before the transition to c++ */
102 if (event->timestamp_ < 1e-12)
105 stream << event->timestamp_;
108 /* internal do the instrumentation module */
109 void simgrid::instr::PajeEvent::insertIntoBuffer()
111 if (not TRACE_buffer()) {
116 buffer_debug(&buffer);
118 XBT_DEBUG("%s: insert event_type=%u, timestamp=%f, buffersize=%zu)", __FUNCTION__, eventType_, timestamp_,
120 std::vector<simgrid::instr::PajeEvent*>::reverse_iterator i;
121 for (i = buffer.rbegin(); i != buffer.rend(); ++i) {
122 simgrid::instr::PajeEvent* e1 = *i;
123 XBT_DEBUG("compare to %p is of type %u; timestamp:%f", e1, e1->eventType_, e1->timestamp_);
124 if (e1->timestamp_ <= timestamp_)
127 if (i == buffer.rend())
128 XBT_DEBUG("%s: inserted at beginning", __FUNCTION__);
129 else if (i == buffer.rbegin())
130 XBT_DEBUG("%s: inserted at end", __FUNCTION__);
132 XBT_DEBUG("%s: inserted at pos= %zd from its end", __FUNCTION__, std::distance(buffer.rbegin(), i));
133 buffer.insert(i.base(), this);
135 buffer_debug(&buffer);
138 simgrid::instr::PajeEvent::~PajeEvent()
140 XBT_DEBUG("%s not implemented for %p: event_type=%u, timestamp=%f", __FUNCTION__, this, eventType_, timestamp_);
143 void TRACE_paje_start() {
144 char *filename = TRACE_get_filename();
145 tracing_file = fopen(filename, "w");
146 if (tracing_file == nullptr){
147 THROWF (system_error, 1, "Tracefile %s could not be opened for writing.", filename);
150 XBT_DEBUG("Filename %s is open for writing", filename);
152 /* output generator version */
153 fprintf (tracing_file, "#This file was generated using SimGrid-%d.%d.%d\n",
154 SIMGRID_VERSION_MAJOR, SIMGRID_VERSION_MINOR, SIMGRID_VERSION_PATCH);
155 fprintf (tracing_file, "#[");
158 xbt_dynar_foreach (xbt_cmdline, cpt, str){
159 fprintf(tracing_file, "%s ",str);
161 fprintf (tracing_file, "]\n");
163 /* output one line comment */
164 dump_comment (TRACE_get_comment());
166 /* output comment file */
167 dump_comment_file (TRACE_get_comment_file());
170 TRACE_header(TRACE_basic(),TRACE_display_sizes());
173 void TRACE_paje_end() {
174 fclose(tracing_file);
175 char *filename = TRACE_get_filename();
176 XBT_DEBUG("Filename %s is closed", filename);
179 simgrid::instr::SetVariableEvent::SetVariableEvent(double timestamp, container_t container, Type* type, double value)
180 : simgrid::instr::PajeEvent::PajeEvent(container, type, timestamp, PAJE_SetVariable), value(value)
182 XBT_DEBUG("%s: event_type=%u, timestamp=%f", __FUNCTION__, eventType_, this->timestamp_);
186 void simgrid::instr::SetVariableEvent::print()
188 if (instr_fmt_type == instr_fmt_paje) {
189 XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
190 stream << std::fixed << std::setprecision(TRACE_precision()) << this->eventType_;
191 print_timestamp(this);
192 stream << " " << type->getId() << " " << container->getId() << " " << value;
194 } else if (instr_fmt_type == instr_fmt_TI) {
201 simgrid::instr::AddVariableEvent::AddVariableEvent(double timestamp, container_t container, simgrid::instr::Type* type,
203 : simgrid::instr::PajeEvent::PajeEvent(container, type, timestamp, PAJE_AddVariable), value(value)
205 XBT_DEBUG("%s: event_type=%u, timestamp=%f", __FUNCTION__, eventType_, this->timestamp_);
209 void simgrid::instr::AddVariableEvent::print()
211 if (instr_fmt_type == instr_fmt_paje) {
212 XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
213 stream << std::fixed << std::setprecision(TRACE_precision());
214 stream << eventType_;
215 print_timestamp(this);
216 stream << " " << type->getId() << " " << container->getId() << " " << value;
218 } else if (instr_fmt_type == instr_fmt_TI) {
225 simgrid::instr::SubVariableEvent::SubVariableEvent(double timestamp, container_t container, Type* type, double value)
226 : simgrid::instr::PajeEvent::PajeEvent(container, type, timestamp, PAJE_SubVariable), value(value)
228 XBT_DEBUG("%s: event_type=%u, timestamp=%f", __FUNCTION__, eventType_, this->timestamp_);
232 void simgrid::instr::SubVariableEvent::print()
234 if (instr_fmt_type == instr_fmt_paje) {
235 XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
236 stream << std::fixed << std::setprecision(TRACE_precision());
237 stream << eventType_;
238 print_timestamp(this);
239 stream << " " << type->getId() << " " << container->getId() << " " << value;
241 } else if (instr_fmt_type == instr_fmt_TI) {
248 simgrid::instr::StartLinkEvent::StartLinkEvent(double timestamp, container_t container, Type* type,
249 container_t sourceContainer, std::string value, std::string key)
250 : StartLinkEvent(timestamp, container, type, sourceContainer, value, key, -1)
253 simgrid::instr::StartLinkEvent::StartLinkEvent(double timestamp, container_t container, Type* type,
254 container_t sourceContainer, std::string value, std::string key,
256 : simgrid::instr::PajeEvent::PajeEvent(container, type, timestamp, PAJE_StartLink)
257 , sourceContainer_(sourceContainer)
262 XBT_DEBUG("%s: event_type=%u, timestamp=%f, value:%s", __FUNCTION__, eventType_, this->timestamp_,
263 this->value_.c_str());
267 void simgrid::instr::StartLinkEvent::print()
269 if (instr_fmt_type == instr_fmt_paje) {
270 XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
271 stream << std::fixed << std::setprecision(TRACE_precision());
272 stream << eventType_;
273 print_timestamp(this);
274 stream << " " << type->getId() << " " << container->getId() << " " << value_;
275 stream << " " << sourceContainer_->getId() << " " << key_;
277 if (TRACE_display_sizes()) {
278 stream << " " << size_;
281 } else if (instr_fmt_type == instr_fmt_TI) {
288 simgrid::instr::EndLinkEvent::EndLinkEvent(double timestamp, container_t container, Type* type,
289 container_t destContainer, std::string value, std::string key)
290 : simgrid::instr::PajeEvent::PajeEvent(container, type, timestamp, PAJE_EndLink)
291 , destContainer(destContainer)
295 XBT_DEBUG("%s: event_type=%u, timestamp=%f", __FUNCTION__, eventType_, this->timestamp_);
299 void simgrid::instr::EndLinkEvent::print()
301 if (instr_fmt_type == instr_fmt_paje) {
302 XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
303 stream << std::fixed << std::setprecision(TRACE_precision());
304 stream << eventType_;
305 print_timestamp(this);
306 stream << " " << type->getId() << " " << container->getId() << " " << value;
307 stream << " " << destContainer->getId() << " " << key;
309 } else if (instr_fmt_type == instr_fmt_TI) {
316 simgrid::instr::NewEvent::NewEvent(double timestamp, container_t container, Type* type, EntityValue* val)
317 : simgrid::instr::PajeEvent::PajeEvent(container, type, timestamp, PAJE_NewEvent), val(val)
319 XBT_DEBUG("%s: event_type=%u, timestamp=%f", __FUNCTION__, eventType_, this->timestamp_);
324 void simgrid::instr::NewEvent::print()
326 if (instr_fmt_type == instr_fmt_paje) {
327 XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
328 stream << std::fixed << std::setprecision(TRACE_precision());
329 stream << eventType_;
330 print_timestamp(this);
331 stream << " " << type->getId() << " " << container->getId() << " " << val->getId();
333 } else if (instr_fmt_type == instr_fmt_TI) {
340 void TRACE_TI_start()
342 char *filename = TRACE_get_filename();
343 tracing_file = fopen(filename, "w");
344 if (tracing_file == nullptr)
345 THROWF(system_error, 1, "Tracefile %s could not be opened for writing.", filename);
347 XBT_DEBUG("Filename %s is open for writing", filename);
349 /* output one line comment */
350 dump_comment(TRACE_get_comment());
352 /* output comment file */
353 dump_comment_file(TRACE_get_comment_file());
358 fclose(tracing_file);
359 char *filename = TRACE_get_filename();
360 XBT_DEBUG("Filename %s is closed", filename);