Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
try to minimize the addition of new tech debt
[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 template<typename T> 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 == 0) 
41     stream << 0;
42   else 
43     stream << event->timestamp;
44 }
45
46
47 template<typename T> static void print_default_pajeLink_row(paje_event_t& event) {
48   init_stream<T>(event);
49   print_timestamp(event);
50   stream << " " << static_cast<T>(event->data)->type->id
51          << " " << static_cast<T>(event->data)->container->id
52          << " " << static_cast<T>(event->data)->value;
53 }
54
55 template<typename T> static void print_default_pajeState_row(paje_event_t& event) {
56   init_stream<T>(event);
57   print_timestamp(event);
58   stream << " " << static_cast<T>(event->data)->type->id
59          << " " << static_cast<T>(event->data)->container->id;
60 }
61
62 template<typename T> static void print_default_pajeType_row(paje_event_t& event) {
63   init_stream<T>(event);
64   stream << " " << static_cast<T>(event->data)->type->id
65          << " " << static_cast<T>(event->data)->type->father->id
66          << " " << static_cast<T>(event->data)->type->name;
67 }
68
69 template<typename T> static void print_default_pajeVariable_row(paje_event_t& event) {
70   init_stream<T>(event);
71   print_timestamp(event);
72   stream << " " << static_cast<T>(event->data)->type->id
73          << " " << static_cast<T>(event->data)->container->id
74          << " " << static_cast<T>(event->data)->value;
75          
76   print_row();
77 }
78
79 void TRACE_paje_init(void) {
80   active_writer.print_DefineContainerType = print_pajeDefineContainerType;
81   active_writer.print_DefineVariableType  = print_pajeDefineVariableType;
82   active_writer.print_DefineStateType     = print_pajeDefineStateType;
83   active_writer.print_DefineEventType     = print_pajeDefineEventType;
84   active_writer.print_DefineLinkType      = print_pajeDefineLinkType;
85   active_writer.print_DefineEntityValue   = print_pajeDefineEntityValue;
86   active_writer.print_CreateContainer     = print_pajeCreateContainer;
87   active_writer.print_DestroyContainer    = print_pajeDestroyContainer;
88   active_writer.print_SetVariable         = print_pajeSetVariable;
89   active_writer.print_AddVariable         = print_pajeAddVariable;
90   active_writer.print_SubVariable         = print_pajeSubVariable;
91   active_writer.print_SetState            = print_pajeSetState;
92   active_writer.print_PushState           = print_pajePushState;
93   active_writer.print_PopState            = print_pajePopState;
94   active_writer.print_ResetState          = print_pajeResetState;
95   active_writer.print_StartLink           = print_pajeStartLink;
96   active_writer.print_EndLink             = print_pajeEndLink;
97   active_writer.print_NewEvent            = print_pajeNewEvent;
98 }
99
100 void TRACE_paje_start(void) {
101   char *filename = TRACE_get_filename();
102   tracing_file = fopen(filename, "w");
103   if (tracing_file == nullptr){
104     THROWF (system_error, 1, "Tracefile %s could not be opened for writing.", filename);
105   }
106
107   XBT_DEBUG("Filename %s is open for writing", filename);
108
109   /* output generator version */
110   fprintf (tracing_file, "#This file was generated using SimGrid-%d.%d.%d\n",
111            SIMGRID_VERSION_MAJOR, SIMGRID_VERSION_MINOR, SIMGRID_VERSION_PATCH);
112   fprintf (tracing_file, "#[");
113   unsigned int cpt;
114   char *str;
115   xbt_dynar_foreach (xbt_cmdline, cpt, str){
116     fprintf(tracing_file, "%s ",str);
117   }
118   fprintf (tracing_file, "]\n");
119
120   /* output one line comment */
121   dump_comment (TRACE_get_comment());
122
123   /* output comment file */
124   dump_comment_file (TRACE_get_comment_file());
125
126   /* output header */
127   TRACE_header(TRACE_basic(),TRACE_display_sizes());
128 }
129
130 void TRACE_paje_end(void) {
131   fclose(tracing_file);
132   char *filename = TRACE_get_filename();
133   XBT_DEBUG("Filename %s is closed", filename);
134 }
135
136 void print_pajeDefineContainerType(paje_event_t event) {
137   print_paje_debug(__FUNCTION__, event);
138   print_default_pajeType_row<defineContainerType_t>(event);
139   print_row();
140 }
141
142 void print_pajeDefineVariableType(paje_event_t event) {
143   print_paje_debug(__FUNCTION__, event);
144   print_default_pajeType_row<defineVariableType_t>(event);
145   if(static_cast<defineVariableType_t>(event->data)->type->color)
146     stream << " \"" << static_cast<defineVariableType_t>(event->data)->type->color << "\"";
147   print_row();
148 }
149
150 void print_pajeDefineStateType(paje_event_t event) {
151   print_paje_debug(__FUNCTION__, event);
152   print_default_pajeType_row<defineStateType_t>(event);
153   print_row();
154 }
155
156 void print_pajeDefineEventType(paje_event_t event) {
157   print_paje_debug(__FUNCTION__, event);
158   print_default_pajeType_row<defineEventType_t>(event);
159   print_row();
160 }
161
162 void print_pajeDefineLinkType(paje_event_t event) {
163   print_paje_debug(__FUNCTION__, event);
164   init_stream<defineLinkType_t>(event);
165   stream << " " << static_cast<defineLinkType_t>(event->data)->type->id 
166          << " " << static_cast<defineLinkType_t>(event->data)->type->father->id 
167          << " " << static_cast<defineLinkType_t>(event->data)->source->id 
168          << " " << static_cast<defineLinkType_t>(event->data)->dest->id 
169          << " " << static_cast<defineLinkType_t>(event->data)->type->name;
170   print_row();
171 }
172
173 void print_pajeDefineEntityValue (paje_event_t event) {
174   print_paje_debug(__FUNCTION__, event);
175   init_stream<defineEntityValue_t>(event);
176   stream << " "   << static_cast<defineEntityValue_t>(event->data)->value->id
177          << " "   << static_cast<defineEntityValue_t>(event->data)->value->father->id
178          << " "   << static_cast<defineEntityValue_t>(event->data)->value->name;
179   if(static_cast<defineEntityValue_t>(event->data)->value->color)
180     stream << " \"" << static_cast<defineEntityValue_t>(event->data)->value->color << "\"";
181   print_row();
182 }
183
184 void print_pajeCreateContainer(paje_event_t event) {
185   print_paje_debug(__FUNCTION__, event);
186   init_stream<createContainer_t>(event);
187   print_timestamp(event);
188   stream << " "   << static_cast<createContainer_t>(event->data)->container->id
189          << " "   << static_cast<createContainer_t>(event->data)->container->type->id
190          << " "   << static_cast<createContainer_t>(event->data)->container->father->id
191          << " \"" << static_cast<createContainer_t>(event->data)->container->name << "\"";
192
193   print_row();
194 }
195
196 void print_pajeDestroyContainer(paje_event_t event) {
197   print_paje_debug(__FUNCTION__, event);
198   init_stream<createContainer_t>(event);
199   print_timestamp(event);
200   stream << " "   << static_cast<createContainer_t>(event->data)->container->type->id
201          << " "   << static_cast<createContainer_t>(event->data)->container->id;
202
203   print_row();
204 }
205
206 void print_pajeSetVariable(paje_event_t event) {
207   print_paje_debug(__FUNCTION__, event);
208   print_default_pajeVariable_row<setVariable_t>(event);
209 }
210
211 void print_pajeAddVariable(paje_event_t event) {
212   print_paje_debug(__FUNCTION__, event);
213   print_default_pajeVariable_row<addVariable_t>(event);
214 }
215
216 void print_pajeSubVariable(paje_event_t event) {
217   print_paje_debug(__FUNCTION__, event);
218   print_default_pajeVariable_row<subVariable_t>(event);
219 }
220
221 void print_pajeSetState(paje_event_t event) {
222   print_paje_debug(__FUNCTION__, event);
223
224   print_default_pajeState_row<setState_t>(event);
225   stream << " " << static_cast<setState_t>(event->data)->value->id;
226 #if HAVE_SMPI
227   if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
228     stream << " \"" << static_cast<setState_t>(event->data)->filename
229            << "\" " << static_cast<setState_t>(event->data)->linenumber;
230   }
231 #endif
232   print_row();
233 }
234
235 void print_pajePushState(paje_event_t event) {
236   print_paje_debug(__FUNCTION__, event);
237   print_default_pajeState_row<pushState_t>(event);
238   stream << " " << static_cast<pushState_t>(event->data)->value->id;
239
240   if (TRACE_display_sizes()) {
241     stream << " ";
242     if (static_cast<pushState_t>(event->data)->extra != nullptr) {
243       stream << static_cast<instr_extra_data>(static_cast<pushState_t>(event->data)->extra)->send_size;
244     }
245     else {
246       stream << 0;
247     }
248   }
249 #if HAVE_SMPI
250   if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
251     stream << " \"" << static_cast<pushState_t>(event->data)->filename
252            << "\" " << static_cast<pushState_t>(event->data)->linenumber;
253   }
254 #endif
255   print_row();
256
257   if (static_cast<pushState_t>(event->data)->extra != nullptr) {
258     if (static_cast<instr_extra_data>(static_cast<pushState_t>(event->data)->extra)->sendcounts != nullptr)
259       xbt_free(static_cast<instr_extra_data>(static_cast<pushState_t>(event->data)->extra)->sendcounts);
260     if (static_cast<instr_extra_data>(static_cast<pushState_t>(event->data)->extra)->recvcounts != nullptr)
261       xbt_free(static_cast<instr_extra_data>(static_cast<pushState_t>(event->data)->extra)->recvcounts);
262     xbt_free(static_cast<pushState_t>(event->data)->extra);
263   }
264 }
265
266 void print_pajePopState(paje_event_t event) {
267   print_paje_debug(__FUNCTION__, event);
268   print_default_pajeState_row<popState_t>(event);
269   print_row();
270 }
271
272 void print_pajeResetState(paje_event_t event) {
273   print_paje_debug(__FUNCTION__, event);
274   print_default_pajeState_row<resetState_t>(event);
275   print_row();
276 }
277
278 void print_pajeStartLink(paje_event_t event) {
279   print_paje_debug(__FUNCTION__, event);
280   print_default_pajeLink_row<startLink_t>(event);
281   stream << " " << static_cast<startLink_t>(event->data)->sourceContainer->id
282          << " " << static_cast<startLink_t>(event->data)->key;
283
284   if (TRACE_display_sizes()) {
285     stream << " " << static_cast<startLink_t>(event->data)->size;
286   }
287   print_row();
288 }
289
290 void print_pajeEndLink(paje_event_t event) {
291   print_paje_debug(__FUNCTION__, event);
292   print_default_pajeLink_row<startLink_t>(event);
293   stream << " " << static_cast<endLink_t>(event->data)->destContainer->id
294          << " " << static_cast<endLink_t>(event->data)->key;
295   print_row();
296 }
297
298 void print_pajeNewEvent (paje_event_t event) {
299   print_paje_debug(__FUNCTION__, event);
300   init_stream<newEvent_t>(event);
301   print_timestamp(event);
302   stream << " " << static_cast<newEvent_t>(event->data)->type->id
303          << " " << static_cast<newEvent_t>(event->data)->container->id
304          << " " << static_cast<newEvent_t>(event->data)->value->id;
305   print_row();
306 }