1 /* Copyright (c) 2010-2018. 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.hpp"
8 #include "src/instr/instr_private.hpp"
9 #include "src/instr/instr_smpi.hpp"
10 #include "src/smpi/include/private.hpp"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_paje_trace, instr, "tracing event system");
16 static std::stringstream stream;
17 FILE *tracing_file = nullptr;
19 std::vector<simgrid::instr::PajeEvent*> buffer;
20 void buffer_debug(std::vector<simgrid::instr::PajeEvent*>* buf);
22 void dump_comment(std::string comment)
26 fprintf(tracing_file, "# %s\n", comment.c_str());
29 void dump_comment_file(std::string filename)
33 std::ifstream* fs = new std::ifstream();
34 fs->open(filename.c_str(), std::ifstream::in);
37 THROWF(system_error, 1, "Comment file %s could not be opened for reading.", filename.c_str());
39 while (not fs->eof()) {
41 fprintf (tracing_file, "# ");
42 std::getline(*fs, line);
43 fprintf(tracing_file, "%s", line.c_str());
48 double TRACE_last_timestamp_to_dump = 0;
49 //dumps the trace file until the timestamp TRACE_last_timestamp_to_dump
50 void TRACE_paje_dump_buffer(bool force)
52 if (not TRACE_is_enabled())
54 XBT_DEBUG("%s: dump until %f. starts", __func__, TRACE_last_timestamp_to_dump);
56 for (auto const& event : buffer) {
62 std::vector<simgrid::instr::PajeEvent*>::iterator i = buffer.begin();
63 for (auto const& event : buffer) {
64 double head_timestamp = event->timestamp_;
65 if (head_timestamp > TRACE_last_timestamp_to_dump)
71 buffer.erase(buffer.begin(), i);
73 XBT_DEBUG("%s: ends", __func__);
76 void buffer_debug(std::vector<simgrid::instr::PajeEvent*>* buf)
78 if (not XBT_LOG_ISENABLED(instr_paje_trace, xbt_log_priority_debug))
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 /* internal do the instrumentation module */
91 void simgrid::instr::PajeEvent::insertIntoBuffer()
93 if (not TRACE_buffer()) {
98 buffer_debug(&buffer);
100 XBT_DEBUG("%s: insert event_type=%u, timestamp=%f, buffersize=%zu)", __func__, eventType_, timestamp_, buffer.size());
101 std::vector<simgrid::instr::PajeEvent*>::reverse_iterator i;
102 for (i = buffer.rbegin(); i != buffer.rend(); ++i) {
103 simgrid::instr::PajeEvent* e1 = *i;
104 XBT_DEBUG("compare to %p is of type %u; timestamp:%f", e1, e1->eventType_, e1->timestamp_);
105 if (e1->timestamp_ <= timestamp_)
108 if (i == buffer.rend())
109 XBT_DEBUG("%s: inserted at beginning", __func__);
110 else if (i == buffer.rbegin())
111 XBT_DEBUG("%s: inserted at end", __func__);
113 XBT_DEBUG("%s: inserted at pos= %zd from its end", __func__, std::distance(buffer.rbegin(), i));
114 buffer.insert(i.base(), this);
116 buffer_debug(&buffer);