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::StartLinkEvent::StartLinkEvent(double timestamp, container_t container, Type* type,
180 container_t sourceContainer, std::string value, std::string key)
181 : StartLinkEvent(timestamp, container, type, sourceContainer, value, key, -1)
184 simgrid::instr::StartLinkEvent::StartLinkEvent(double timestamp, container_t container, Type* type,
185 container_t sourceContainer, std::string value, std::string key,
187 : simgrid::instr::PajeEvent::PajeEvent(container, type, timestamp, PAJE_StartLink)
188 , sourceContainer_(sourceContainer)
193 XBT_DEBUG("%s: event_type=%u, timestamp=%f, value:%s", __FUNCTION__, eventType_, this->timestamp_,
194 this->value_.c_str());
198 void simgrid::instr::StartLinkEvent::print()
200 if (instr_fmt_type == instr_fmt_paje) {
201 XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
202 stream << std::fixed << std::setprecision(TRACE_precision());
203 stream << eventType_;
204 print_timestamp(this);
205 stream << " " << type->getId() << " " << container->getId() << " " << value_;
206 stream << " " << sourceContainer_->getId() << " " << key_;
208 if (TRACE_display_sizes()) {
209 stream << " " << size_;
212 } else if (instr_fmt_type == instr_fmt_TI) {
219 simgrid::instr::EndLinkEvent::EndLinkEvent(double timestamp, container_t container, Type* type,
220 container_t destContainer, std::string value, std::string key)
221 : simgrid::instr::PajeEvent::PajeEvent(container, type, timestamp, PAJE_EndLink)
222 , destContainer(destContainer)
226 XBT_DEBUG("%s: event_type=%u, timestamp=%f", __FUNCTION__, eventType_, this->timestamp_);
230 void simgrid::instr::EndLinkEvent::print()
232 if (instr_fmt_type == instr_fmt_paje) {
233 XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
234 stream << std::fixed << std::setprecision(TRACE_precision());
235 stream << eventType_;
236 print_timestamp(this);
237 stream << " " << type->getId() << " " << container->getId() << " " << value;
238 stream << " " << destContainer->getId() << " " << key;
240 } else if (instr_fmt_type == instr_fmt_TI) {
247 simgrid::instr::NewEvent::NewEvent(double timestamp, container_t container, Type* type, EntityValue* val)
248 : simgrid::instr::PajeEvent::PajeEvent(container, type, timestamp, PAJE_NewEvent), val(val)
250 XBT_DEBUG("%s: event_type=%u, timestamp=%f", __FUNCTION__, eventType_, this->timestamp_);
255 void simgrid::instr::NewEvent::print()
257 if (instr_fmt_type == instr_fmt_paje) {
258 XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
259 stream << std::fixed << std::setprecision(TRACE_precision());
260 stream << eventType_;
261 print_timestamp(this);
262 stream << " " << type->getId() << " " << container->getId() << " " << val->getId();
264 } else if (instr_fmt_type == instr_fmt_TI) {
271 void TRACE_TI_start()
273 char *filename = TRACE_get_filename();
274 tracing_file = fopen(filename, "w");
275 if (tracing_file == nullptr)
276 THROWF(system_error, 1, "Tracefile %s could not be opened for writing.", filename);
278 XBT_DEBUG("Filename %s is open for writing", filename);
280 /* output one line comment */
281 dump_comment(TRACE_get_comment());
283 /* output comment file */
284 dump_comment_file(TRACE_get_comment_file());
289 fclose(tracing_file);
290 char *filename = TRACE_get_filename();
291 XBT_DEBUG("Filename %s is closed", filename);