Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr/gitroot/simgrid/simgrid
[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   stream << " \"" << static_cast<defineVariableType_t>(event->data)->type->color << "\"";
145   print_row();
146 }
147
148 void print_pajeDefineStateType(paje_event_t event) {
149   print_paje_debug(__FUNCTION__, event);
150   print_default_pajeType_row<defineStateType_t>(event);
151   print_row();
152 }
153
154 void print_pajeDefineEventType(paje_event_t event) {
155   print_paje_debug(__FUNCTION__, event);
156   print_default_pajeType_row<defineEventType_t>(event);
157   print_row();
158 }
159
160 void print_pajeDefineLinkType(paje_event_t event) {
161   print_paje_debug(__FUNCTION__, event);
162   init_stream<defineLinkType_t>(event);
163   stream << " " << static_cast<defineLinkType_t>(event->data)->type->id 
164          << " " << static_cast<defineLinkType_t>(event->data)->type->father->id 
165          << " " << static_cast<defineLinkType_t>(event->data)->source->id 
166          << " " << static_cast<defineLinkType_t>(event->data)->dest->id 
167          << " " << static_cast<defineLinkType_t>(event->data)->type->name;
168   print_row();
169 }
170
171 void print_pajeDefineEntityValue (paje_event_t event) {
172   print_paje_debug(__FUNCTION__, event);
173   init_stream<defineEntityValue_t>(event);
174   stream << " "   << static_cast<defineEntityValue_t>(event->data)->value->id
175          << " "   << static_cast<defineEntityValue_t>(event->data)->value->father->id
176          << " "   << static_cast<defineEntityValue_t>(event->data)->value->name
177          << " \"" << static_cast<defineEntityValue_t>(event->data)->value->color << "\"";
178   print_row();
179 }
180
181 void print_pajeCreateContainer(paje_event_t event) {
182   print_paje_debug(__FUNCTION__, event);
183   init_stream<createContainer_t>(event);
184   print_timestamp(event);
185   stream << " "   << static_cast<createContainer_t>(event->data)->container->id
186          << " "   << static_cast<createContainer_t>(event->data)->container->type->id
187          << " "   << static_cast<createContainer_t>(event->data)->container->father->id
188          << " \"" << static_cast<createContainer_t>(event->data)->container->name << "\"";
189
190   print_row();
191 }
192
193 void print_pajeDestroyContainer(paje_event_t event) {
194   print_paje_debug(__FUNCTION__, event);
195   init_stream<createContainer_t>(event);
196   print_timestamp(event);
197   stream << " "   << static_cast<createContainer_t>(event->data)->container->type->id
198          << " "   << static_cast<createContainer_t>(event->data)->container->id;
199
200   print_row();
201 }
202
203 void print_pajeSetVariable(paje_event_t event) {
204   print_paje_debug(__FUNCTION__, event);
205   print_default_pajeVariable_row<setVariable_t>(event);
206 }
207
208 void print_pajeAddVariable(paje_event_t event) {
209   print_paje_debug(__FUNCTION__, event);
210   print_default_pajeVariable_row<addVariable_t>(event);
211 }
212
213 void print_pajeSubVariable(paje_event_t event) {
214   print_paje_debug(__FUNCTION__, event);
215   print_default_pajeVariable_row<subVariable_t>(event);
216 }
217
218 void print_pajeSetState(paje_event_t event) {
219   print_paje_debug(__FUNCTION__, event);
220
221   print_default_pajeState_row<setState_t>(event);
222   stream << " " << static_cast<setState_t>(event->data)->value->id;
223   print_row();
224 }
225
226 void print_pajePushState(paje_event_t event) {
227   print_paje_debug(__FUNCTION__, event);
228   print_default_pajeState_row<pushState_t>(event);
229   stream << " " << static_cast<pushState_t>(event->data)->value->id;
230
231   if (TRACE_display_sizes()) {
232     stream << " ";
233     if (static_cast<pushState_t>(event->data)->extra != NULL) {
234       stream << static_cast<instr_extra_data>(static_cast<pushState_t>(event->data)->extra)->send_size;
235     }
236     else {
237       stream << 0;
238     }
239   }
240   print_row();
241
242   if (static_cast<pushState_t>(event->data)->extra != NULL) {
243     if (static_cast<instr_extra_data>(static_cast<pushState_t>(event->data)->extra)->sendcounts != NULL)
244       xbt_free(static_cast<instr_extra_data>(static_cast<pushState_t>(event->data)->extra)->sendcounts);
245     if (static_cast<instr_extra_data>(static_cast<pushState_t>(event->data)->extra)->recvcounts != NULL)
246       xbt_free(static_cast<instr_extra_data>(static_cast<pushState_t>(event->data)->extra)->recvcounts);
247     xbt_free(static_cast<pushState_t>(event->data)->extra);
248   }
249 }
250
251 void print_pajePopState(paje_event_t event) {
252   print_paje_debug(__FUNCTION__, event);
253   print_default_pajeState_row<popState_t>(event);
254   print_row();
255 }
256
257 void print_pajeResetState(paje_event_t event) {
258   print_paje_debug(__FUNCTION__, event);
259   print_default_pajeState_row<resetState_t>(event);
260   print_row();
261 }
262
263 void print_pajeStartLink(paje_event_t event) {
264   print_paje_debug(__FUNCTION__, event);
265   print_default_pajeLink_row<startLink_t>(event);
266   stream << " " << static_cast<startLink_t>(event->data)->sourceContainer->id
267          << " " << static_cast<startLink_t>(event->data)->key;
268
269   if (TRACE_display_sizes()) {
270     stream << " " << static_cast<startLink_t>(event->data)->size;
271   }
272   print_row();
273 }
274
275 void print_pajeEndLink(paje_event_t event) {
276   print_paje_debug(__FUNCTION__, event);
277   print_default_pajeLink_row<startLink_t>(event);
278   stream << " " << static_cast<endLink_t>(event->data)->destContainer->id
279          << " " << static_cast<endLink_t>(event->data)->key;
280   print_row();
281 }
282
283 void print_pajeNewEvent (paje_event_t event) {
284   print_paje_debug(__FUNCTION__, event);
285   init_stream<newEvent_t>(event);
286   print_timestamp(event);
287   stream << " " << static_cast<newEvent_t>(event->data)->type->id
288          << " " << static_cast<newEvent_t>(event->data)->container->id
289          << " " << static_cast<newEvent_t>(event->data)->value->id;
290   print_row();
291 }