Logo AND Algorithmique Numérique Distribuée

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