Logo AND Algorithmique Numérique Distribuée

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