Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Sonar: use lowercase for field names.
[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 simgrid::instr::PajeEvent::~PajeEvent()
139 {
140   XBT_DEBUG("%s not implemented for %p: event_type=%u, timestamp=%f", __FUNCTION__, this, eventType_, timestamp_);
141 }
142
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);
148   }
149
150   XBT_DEBUG("Filename %s is open for writing", filename);
151
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, "#[");
156   unsigned int cpt;
157   char *str;
158   xbt_dynar_foreach (xbt_cmdline, cpt, str){
159     fprintf(tracing_file, "%s ",str);
160   }
161   fprintf (tracing_file, "]\n");
162
163   /* output one line comment */
164   dump_comment (TRACE_get_comment());
165
166   /* output comment file */
167   dump_comment_file (TRACE_get_comment_file());
168
169   /* output header */
170   TRACE_header(TRACE_basic(),TRACE_display_sizes());
171 }
172
173 void TRACE_paje_end() {
174   fclose(tracing_file);
175   char *filename = TRACE_get_filename();
176   XBT_DEBUG("Filename %s is closed", filename);
177 }
178
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)
182 {}
183
184 simgrid::instr::StartLinkEvent::StartLinkEvent(double timestamp, container_t container, Type* type,
185                                                container_t sourceContainer, std::string value, std::string key,
186                                                int size)
187     : simgrid::instr::PajeEvent::PajeEvent(container, type, timestamp, PAJE_StartLink)
188     , sourceContainer_(sourceContainer)
189     , value_(value)
190     , key_(key)
191     , size_(size)
192 {
193   XBT_DEBUG("%s: event_type=%u, timestamp=%f, value:%s", __FUNCTION__, eventType_, this->timestamp_,
194             this->value_.c_str());
195   insertIntoBuffer();
196 }
197
198 void simgrid::instr::StartLinkEvent::print()
199 {
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_;
207
208     if (TRACE_display_sizes()) {
209       stream << " " << size_;
210     }
211     print_row();
212   } else if (instr_fmt_type == instr_fmt_TI) {
213     /* Nothing to do */
214   } else {
215     THROW_IMPOSSIBLE;
216   }
217 }
218
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)
223     , value(value)
224     , key(key)
225 {
226   XBT_DEBUG("%s: event_type=%u, timestamp=%f", __FUNCTION__, eventType_, this->timestamp_);
227   insertIntoBuffer();
228 }
229
230 void simgrid::instr::EndLinkEvent::print()
231 {
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;
239     print_row();
240   } else if (instr_fmt_type == instr_fmt_TI) {
241     /* Nothing to do */
242   } else {
243     THROW_IMPOSSIBLE;
244   }
245 }
246
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)
249 {
250   XBT_DEBUG("%s: event_type=%u, timestamp=%f", __FUNCTION__, eventType_, this->timestamp_);
251
252   insertIntoBuffer();
253 }
254
255 void simgrid::instr::NewEvent::print()
256 {
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();
263     print_row();
264   } else if (instr_fmt_type == instr_fmt_TI) {
265     /* Nothing to do */
266   } else {
267     THROW_IMPOSSIBLE;
268   }
269 }
270
271 void TRACE_TI_start()
272 {
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);
277
278   XBT_DEBUG("Filename %s is open for writing", filename);
279
280   /* output one line comment */
281   dump_comment(TRACE_get_comment());
282
283   /* output comment file */
284   dump_comment_file(TRACE_get_comment_file());
285 }
286
287 void TRACE_TI_end()
288 {
289   fclose(tracing_file);
290   char *filename = TRACE_get_filename();
291   XBT_DEBUG("Filename %s is closed", filename);
292 }