Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8843d0989bb4d9d7144a37ed559100d160268ca8
[simgrid.git] / src / instr / instr_paje_trace.cpp
1 /* Copyright (c) 2010-2016. 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 "src/instr/instr_private.h"
8 #include "xbt/virtu.h" /* sg_cmdline */
9 #include <sstream>
10 #include <vector>
11 #include <iomanip> /** std::setprecision **/
12 #include "simgrid/sg_config.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_paje_trace, instr_trace, "tracing event system");
15
16 extern FILE * tracing_file;
17 extern s_instr_trace_writer_t active_writer;
18
19 static std::stringstream stream;
20
21 void buffer_debug(std::vector<PajeEvent*> *buf);
22 void buffer_debug(std::vector<PajeEvent*> *buf) {
23   return;
24   XBT_DEBUG(">>>>>> Dump the state of the buffer. %zu events", buf->size());
25   for (auto event :*buf){
26     event->print();
27     XBT_DEBUG("%p %s", event, stream.str().c_str());
28     stream.str("");
29     stream.clear();
30   }
31   XBT_DEBUG("<<<<<<");
32 }
33
34 static void print_paje_debug(std::string functionName, PajeEvent* event) {
35   XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event->event_type, TRACE_precision(),
36             event->timestamp);
37 }
38
39 static void init_stream(PajeEvent* event) {
40   stream << std::fixed << std::setprecision(TRACE_precision());
41   stream << (int) event->event_type;
42 }
43
44 static void print_row() {
45   stream << std::endl;
46   fprintf(tracing_file, "%s", stream.str().c_str());
47   XBT_DEBUG("Dump %s", stream.str().c_str());
48   stream.str("");
49   stream.clear();
50 }
51
52 static void print_timestamp(PajeEvent* event) {
53   stream << " ";
54   /* prevent 0.0000 in the trace - this was the behavior before the transition to c++ */
55   if (event->timestamp < 1e-12)
56     stream << 0;
57   else 
58     stream << event->timestamp;
59 }  
60
61 void TRACE_paje_start() {
62   char *filename = TRACE_get_filename();
63   tracing_file = fopen(filename, "w");
64   if (tracing_file == nullptr){
65     THROWF (system_error, 1, "Tracefile %s could not be opened for writing.", filename);
66   }
67
68   XBT_DEBUG("Filename %s is open for writing", filename);
69
70   /* output generator version */
71   fprintf (tracing_file, "#This file was generated using SimGrid-%d.%d.%d\n",
72            SIMGRID_VERSION_MAJOR, SIMGRID_VERSION_MINOR, SIMGRID_VERSION_PATCH);
73   fprintf (tracing_file, "#[");
74   unsigned int cpt;
75   char *str;
76   xbt_dynar_foreach (xbt_cmdline, cpt, str){
77     fprintf(tracing_file, "%s ",str);
78   }
79   fprintf (tracing_file, "]\n");
80
81   /* output one line comment */
82   dump_comment (TRACE_get_comment());
83
84   /* output comment file */
85   dump_comment_file (TRACE_get_comment_file());
86
87   /* output header */
88   TRACE_header(TRACE_basic(),TRACE_display_sizes());
89 }
90
91 void TRACE_paje_end() {
92   fclose(tracing_file);
93   char *filename = TRACE_get_filename();
94   XBT_DEBUG("Filename %s is closed", filename);
95 }
96
97 void DefineContainerEvent::print() {
98   print_paje_debug(__FUNCTION__, this);
99   init_stream(this);
100   stream << " " << type->id
101          << " " << type->father->id
102          << " " << type->name;
103   print_row();
104 }
105
106 void DefineVariableTypeEvent::print() {
107   print_paje_debug(__FUNCTION__, this);
108  init_stream(this);
109   stream << " " << type->id
110          << " " << type->father->id
111          << " " << type->name;
112   if (type->color)
113     stream << " \"" << type->color << "\"";
114   print_row();
115 }
116
117 void DefineStateTypeEvent::print() {
118   print_paje_debug(__FUNCTION__, this);
119   init_stream(this);
120   stream << " " << type->id
121          << " " << type->father->id
122          << " " << type->name;
123   print_row();
124 }
125
126 void DefineEventTypeEvent::print() {
127   print_paje_debug(__FUNCTION__, this);
128   init_stream(this);
129   stream << " " << type->id
130          << " " << type->father->id
131          << " " << type->name;
132   print_row();
133 }
134
135 void DefineLinkTypeEvent::print() {
136   print_paje_debug(__FUNCTION__, this);
137   init_stream (this);
138   stream << " " << type->id 
139          << " " << type->father->id 
140          << " " << source->id 
141          << " " << dest->id 
142          << " " << type->name;
143   print_row();
144 }
145
146 void DefineEntityValueEvent::print() {
147   print_paje_debug(__FUNCTION__, this);
148   init_stream(this);
149   stream << " "   << value->id
150          << " "   << value->father->id
151          << " "   << value->name;
152   if(value->color)
153     stream << " \"" << value->color << "\"";
154   print_row();
155 }
156
157 void CreateContainerEvent::print() {
158   print_paje_debug(__FUNCTION__, this);
159   init_stream(this);
160   print_timestamp(this);
161   stream << " "   << container->id
162          << " "   << container->type->id
163          << " "   << container->father->id
164          << " \"" << container->name << "\"";
165
166   print_row();
167 }
168
169 void DestroyContainerEvent::print() {
170   print_paje_debug(__FUNCTION__, this);
171   init_stream(this);
172   print_timestamp(this);
173   stream << " "   << container->type->id
174          << " "   << container->id;
175
176   print_row();
177 }
178
179 void SetVariableEvent::print() {
180   print_paje_debug(__FUNCTION__, this);
181   init_stream(this);
182   print_timestamp(this);
183   stream << " " << type->id
184          << " " << container->id
185          << " " << value;
186   print_row();
187 }
188
189 void AddVariableEvent::print() {
190   print_paje_debug(__FUNCTION__, this);
191   init_stream(this);
192   print_timestamp(this);
193   stream << " " << type->id
194          << " " << container->id
195          << " " << value;
196   print_row();
197 }
198
199 void SubVariableEvent::print() {
200   print_paje_debug(__FUNCTION__, this);
201   init_stream(this);
202   print_timestamp(this);
203   stream << " " << type->id
204          << " " << container->id
205          << " " << value;
206   print_row();
207 }
208
209 void SetStateEvent::print() {
210   print_paje_debug(__FUNCTION__, this);
211   init_stream(this);
212   print_timestamp(this);
213   stream << " " << type->id
214          << " " << container->id;
215   stream << " " <<value->id;
216 #if HAVE_SMPI
217   if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
218     stream << " \"" << filename
219            << "\" " << linenumber;
220   }
221 #endif
222   print_row();
223 }
224
225 void PushStateEvent::print() {
226   print_paje_debug(__FUNCTION__, this);
227     init_stream(this);
228   print_timestamp(this);
229   stream << " " << type->id
230          << " " << container->id;
231   stream << " " <<value->id;
232
233   if (TRACE_display_sizes()) {
234     stream << " ";
235     if (extra != nullptr) {
236       stream << static_cast<instr_extra_data>(extra)->send_size;
237     }
238     else {
239       stream << 0;
240     }
241   }
242 #if HAVE_SMPI
243   if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
244     stream << " \"" << filename
245            << "\" " << linenumber;
246   }
247 #endif
248   print_row();
249
250   if (extra != nullptr) {
251     if (static_cast<instr_extra_data>(extra)->sendcounts != nullptr)
252       xbt_free(static_cast<instr_extra_data>(extra)->sendcounts);
253     if (static_cast<instr_extra_data>(extra)->recvcounts != nullptr)
254       xbt_free(static_cast<instr_extra_data>(extra)->recvcounts);
255     xbt_free(extra);
256   }
257 }
258
259 void PopStateEvent::print() {
260   print_paje_debug(__FUNCTION__, this);
261     init_stream(this);
262   print_timestamp(this);
263   stream << " " << type->id
264          << " " << container->id;
265   print_row();
266 }
267
268 void ResetStateEvent::print() {
269   print_paje_debug(__FUNCTION__, this);
270     init_stream(this);
271   print_timestamp(this);
272   stream << " " << type->id
273          << " " << container->id;
274   print_row();
275 }
276
277 void StartLinkEvent::print() {
278   print_paje_debug(__FUNCTION__, this);
279     init_stream(this);
280   print_timestamp(this);
281   stream << " " <<type->id
282          << " " <<container->id
283          << " " <<value;
284   stream << " " << sourceContainer->id
285          << " " << key;
286
287   if (TRACE_display_sizes()) {
288     stream << " " << size;
289   }
290   print_row();
291 }
292
293 void EndLinkEvent::print() {
294   print_paje_debug(__FUNCTION__, this);
295     init_stream(this);
296   print_timestamp(this);
297   stream << " " <<type->id
298          << " " <<container->id
299          << " " <<value;
300   stream << " " << destContainer->id
301          << " " << key;
302   print_row();
303 }
304
305 void NewEvent::print () {
306   print_paje_debug(__FUNCTION__, this);
307   init_stream (this);
308   print_timestamp(this);
309   stream << " " << type->id
310          << " " << container->id
311          << " " << value->id;
312   print_row();
313 }