Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
try to get rid of smpi tracing segmentation faults on some platforms.
[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
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_paje_trace, instr_trace, "tracing event system");
13
14 extern FILE * tracing_file;
15 extern s_instr_trace_writer_t active_writer;
16
17 static std::stringstream stream;
18
19 static void print_paje_debug(std::string functionName, paje_event_t event) {
20   XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event->event_type, TRACE_precision(),
21             event->timestamp);
22 }
23
24 template<typename T> static void init_stream(paje_event_t event) {
25   stream << std::fixed << std::setprecision(TRACE_precision());
26   stream << (int) event->event_type;
27 }
28
29 static void print_row() {
30   stream << std::endl;
31   fprintf(tracing_file, "%s", stream.str().c_str());
32   stream.str("");
33   stream.clear();
34 }
35
36 static void print_timestamp(paje_event_t event) {
37   stream << " ";
38   /* prevent 0.0000 in the trace - this was the behavior before the transition to c++ */
39   if (event->timestamp == 0) 
40     stream << 0;
41   else 
42     stream << event->timestamp;
43 }
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(void) {
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(void) {
100   char *filename = TRACE_get_filename();
101   tracing_file = fopen(filename, "w");
102   if (tracing_file == NULL){
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(void) {
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   print_row();
226 }
227
228 void print_pajePushState(paje_event_t event) {
229   print_paje_debug(__FUNCTION__, event);
230   print_default_pajeState_row<pushState_t>(event);
231   stream << " " << static_cast<pushState_t>(event->data)->value->id;
232
233   if (TRACE_display_sizes()) {
234     stream << " ";
235     if (static_cast<pushState_t>(event->data)->extra != NULL) {
236       stream << static_cast<instr_extra_data>(static_cast<pushState_t>(event->data)->extra)->send_size;
237     }
238     else {
239       stream << 0;
240     }
241   }
242   print_row();
243
244   if (static_cast<pushState_t>(event->data)->extra != NULL) {
245     if (static_cast<instr_extra_data>(static_cast<pushState_t>(event->data)->extra)->sendcounts != NULL)
246       xbt_free(static_cast<instr_extra_data>(static_cast<pushState_t>(event->data)->extra)->sendcounts);
247     if (static_cast<instr_extra_data>(static_cast<pushState_t>(event->data)->extra)->recvcounts != NULL)
248       xbt_free(static_cast<instr_extra_data>(static_cast<pushState_t>(event->data)->extra)->recvcounts);
249     xbt_free(static_cast<pushState_t>(event->data)->extra);
250   }
251 }
252
253 void print_pajePopState(paje_event_t event) {
254   print_paje_debug(__FUNCTION__, event);
255   print_default_pajeState_row<popState_t>(event);
256   print_row();
257 }
258
259 void print_pajeResetState(paje_event_t event) {
260   print_paje_debug(__FUNCTION__, event);
261   print_default_pajeState_row<resetState_t>(event);
262   print_row();
263 }
264
265 void print_pajeStartLink(paje_event_t event) {
266   print_paje_debug(__FUNCTION__, event);
267   print_default_pajeLink_row<startLink_t>(event);
268   stream << " " << static_cast<startLink_t>(event->data)->sourceContainer->id
269          << " " << static_cast<startLink_t>(event->data)->key;
270
271   if (TRACE_display_sizes()) {
272     stream << " " << static_cast<startLink_t>(event->data)->size;
273   }
274   print_row();
275 }
276
277 void print_pajeEndLink(paje_event_t event) {
278   print_paje_debug(__FUNCTION__, event);
279   print_default_pajeLink_row<startLink_t>(event);
280   stream << " " << static_cast<endLink_t>(event->data)->destContainer->id
281          << " " << static_cast<endLink_t>(event->data)->key;
282   print_row();
283 }
284
285 void print_pajeNewEvent (paje_event_t event) {
286   print_paje_debug(__FUNCTION__, event);
287   init_stream<newEvent_t>(event);
288   print_timestamp(event);
289   stream << " " << static_cast<newEvent_t>(event->data)->type->id
290          << " " << static_cast<newEvent_t>(event->data)->container->id
291          << " " << static_cast<newEvent_t>(event->data)->value->id;
292   print_row();
293 }