Logo AND Algorithmique Numérique Distribuée

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