Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Continue to reorganize instr
[simgrid.git] / src / instr / instr_paje_trace.cpp
1 /* Copyright (c) 2010-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
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"
11 #include "typeinfo"
12 #include "xbt/virtu.h" /* sg_cmdline */
13 #include <fstream>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_paje_trace, instr, "tracing event system");
16
17 static std::stringstream stream;
18 FILE *tracing_file = nullptr;
19
20 std::vector<simgrid::instr::PajeEvent*> buffer;
21 void buffer_debug(std::vector<simgrid::instr::PajeEvent*>* buf);
22
23 void dump_comment(std::string comment)
24 {
25   if (comment.empty())
26     return;
27   fprintf(tracing_file, "# %s\n", comment.c_str());
28 }
29
30 void dump_comment_file(std::string filename)
31 {
32   if (filename.empty())
33     return;
34   std::ifstream* fs = new std::ifstream();
35   fs->open(filename.c_str(), std::ifstream::in);
36
37   if (fs->fail()) {
38     THROWF(system_error, 1, "Comment file %s could not be opened for reading.", filename.c_str());
39   }
40   while (not fs->eof()) {
41     std::string line;
42     fprintf (tracing_file, "# ");
43     std::getline(*fs, line);
44     fprintf(tracing_file, "%s", line.c_str());
45   }
46   fs->close();
47 }
48
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)
52 {
53   if (not TRACE_is_enabled())
54     return;
55   XBT_DEBUG("%s: dump until %f. starts", __FUNCTION__, TRACE_last_timestamp_to_dump);
56   if (force){
57     for (auto const& event : buffer) {
58       event->print();
59       delete event;
60     }
61     buffer.clear();
62   }else{
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)
67         break;
68       event->print();
69       delete event;
70       ++i;
71     }
72     buffer.erase(buffer.begin(), i);
73   }
74   XBT_DEBUG("%s: ends", __FUNCTION__);
75 }
76
77 void buffer_debug(std::vector<simgrid::instr::PajeEvent*>* buf)
78 {
79   return;
80   XBT_DEBUG(">>>>>> Dump the state of the buffer. %zu events", buf->size());
81   for (auto const& event : *buf) {
82     event->print();
83     XBT_DEBUG("%p %s", event, stream.str().c_str());
84     stream.str("");
85     stream.clear();
86   }
87   XBT_DEBUG("<<<<<<");
88 }
89
90 static void print_row() {
91   stream << std::endl;
92   fprintf(tracing_file, "%s", stream.str().c_str());
93   XBT_DEBUG("Dump %s", stream.str().c_str());
94   stream.str("");
95   stream.clear();
96 }
97
98 static void print_timestamp(simgrid::instr::PajeEvent* event)
99 {
100   stream << " ";
101   /* prevent 0.0000 in the trace - this was the behavior before the transition to c++ */
102   if (event->timestamp_ < 1e-12)
103     stream << 0;
104   else
105     stream << event->timestamp_;
106 }
107
108 /* internal do the instrumentation module */
109 void simgrid::instr::PajeEvent::insertIntoBuffer()
110 {
111   if (not TRACE_buffer()) {
112     print();
113     delete this;
114     return;
115   }
116   buffer_debug(&buffer);
117
118   XBT_DEBUG("%s: insert event_type=%u, timestamp=%f, buffersize=%zu)", __FUNCTION__, eventType_, timestamp_,
119             buffer.size());
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_)
125       break;
126   }
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__);
131   else
132     XBT_DEBUG("%s: inserted at pos= %zd from its end", __FUNCTION__, std::distance(buffer.rbegin(), i));
133   buffer.insert(i.base(), this);
134
135   buffer_debug(&buffer);
136 }
137
138 void TRACE_paje_start() {
139   char *filename = TRACE_get_filename();
140   tracing_file = fopen(filename, "w");
141   if (tracing_file == nullptr){
142     THROWF (system_error, 1, "Tracefile %s could not be opened for writing.", filename);
143   }
144
145   XBT_DEBUG("Filename %s is open for writing", filename);
146
147   /* output generator version */
148   fprintf (tracing_file, "#This file was generated using SimGrid-%d.%d.%d\n",
149            SIMGRID_VERSION_MAJOR, SIMGRID_VERSION_MINOR, SIMGRID_VERSION_PATCH);
150   fprintf (tracing_file, "#[");
151   unsigned int cpt;
152   char *str;
153   xbt_dynar_foreach (xbt_cmdline, cpt, str){
154     fprintf(tracing_file, "%s ",str);
155   }
156   fprintf (tracing_file, "]\n");
157
158   /* output one line comment */
159   dump_comment (TRACE_get_comment());
160
161   /* output comment file */
162   dump_comment_file (TRACE_get_comment_file());
163
164   /* output header */
165   TRACE_header(TRACE_basic(),TRACE_display_sizes());
166 }
167
168 void TRACE_paje_end() {
169   fclose(tracing_file);
170   char *filename = TRACE_get_filename();
171   XBT_DEBUG("Filename %s is closed", filename);
172 }
173
174 simgrid::instr::NewEvent::NewEvent(double timestamp, container_t container, Type* type, EntityValue* val)
175     : simgrid::instr::PajeEvent::PajeEvent(container, type, timestamp, PAJE_NewEvent), val(val)
176 {
177   XBT_DEBUG("%s: event_type=%u, timestamp=%f", __FUNCTION__, eventType_, this->timestamp_);
178
179   insertIntoBuffer();
180 }
181
182 void simgrid::instr::NewEvent::print()
183 {
184   if (instr_fmt_type == instr_fmt_paje) {
185     XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
186     stream << std::fixed << std::setprecision(TRACE_precision());
187     stream << eventType_;
188     print_timestamp(this);
189     stream << " " << type->getId() << " " << container->getId() << " " << val->getId();
190     print_row();
191   } else if (instr_fmt_type == instr_fmt_TI) {
192     /* Nothing to do */
193   } else {
194     THROW_IMPOSSIBLE;
195   }
196 }
197
198 void TRACE_TI_start()
199 {
200   char *filename = TRACE_get_filename();
201   tracing_file = fopen(filename, "w");
202   if (tracing_file == nullptr)
203     THROWF(system_error, 1, "Tracefile %s could not be opened for writing.", filename);
204
205   XBT_DEBUG("Filename %s is open for writing", filename);
206
207   /* output one line comment */
208   dump_comment(TRACE_get_comment());
209
210   /* output comment file */
211   dump_comment_file(TRACE_get_comment_file());
212 }
213
214 void TRACE_TI_end()
215 {
216   fclose(tracing_file);
217   XBT_DEBUG("Filename %s is closed", TRACE_get_filename());
218 }