Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[INSTR] Purely cosmetics: Aligned '='
[simgrid.git] / src / instr / instr_trace.cpp
1 /* Copyright (c) 2010-2015. 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 "src/instr/instr_smpi.h"
9 #include "src/smpi/private.hpp"
10 #include "xbt/virtu.h" /* sg_cmdline */
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_trace, instr, "tracing event system");
13
14 FILE *tracing_file = nullptr;
15
16 void print_NULL(paje_event_t event){}
17
18 /* The active set of functions for the selected trace format
19  * By default, they all do nothing, hence the print_NULL to avoid segfaults */
20
21 s_instr_trace_writer_t active_writer = {&print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL,
22                                         &print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL,
23                                         &print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL};
24
25 std::vector<paje_event_t> buffer;
26
27 void dump_comment (const char *comment)
28 {
29   if (!strlen(comment)) return;
30   fprintf (tracing_file, "# %s\n", comment);
31 }
32
33 void dump_comment_file (const char *filename)
34 {
35   if (!strlen(filename)) return;
36   FILE *file = fopen (filename, "r");
37   if (!file){
38     THROWF (system_error, 1, "Comment file %s could not be opened for reading.", filename);
39   }
40   while (!feof(file)){
41     char c;
42     c = fgetc(file);
43     if (feof(file)) break;
44     fprintf (tracing_file, "# ");
45     while (c != '\n'){
46       fprintf (tracing_file, "%c", c);
47       c = fgetc(file);
48       if (feof(file)) break;
49     }
50     fprintf (tracing_file, "\n");
51   }
52   fclose(file);
53 }
54
55 double TRACE_last_timestamp_to_dump = 0;
56 //dumps the trace file until the timestamp TRACE_last_timestamp_to_dump
57 void TRACE_paje_dump_buffer (int force)
58 {
59   if (!TRACE_is_enabled()) return;
60   XBT_DEBUG("%s: dump until %f. starts", __FUNCTION__, TRACE_last_timestamp_to_dump);
61   if (force){
62     for (auto event :buffer){
63       event->print (event);
64       event->free (event);
65     }
66     buffer.clear();
67   }else{
68     std::vector<paje_event_t>::iterator i = buffer.begin();
69     for (auto event :buffer){
70       double head_timestamp = event->timestamp;
71       if (head_timestamp > TRACE_last_timestamp_to_dump)
72         break;
73       event->print (event);
74       event->free (event);
75       ++i;
76     }
77     buffer.erase(buffer.begin(), i);
78   }
79   XBT_DEBUG("%s: ends", __FUNCTION__);
80 }
81
82 /* internal do the instrumentation module */
83 static void insert_into_buffer (paje_event_t tbi)
84 {
85   if (TRACE_buffer() == 0){
86     tbi->print (tbi);
87     tbi->free (tbi);
88     return;
89   }
90
91   XBT_DEBUG("%s: insert event_type=%d, timestamp=%f, buffersize=%zu)",
92       __FUNCTION__, (int)tbi->event_type, tbi->timestamp, buffer.size());
93   std::vector<paje_event_t>::reverse_iterator i;
94   for (i = buffer.rbegin(); i != buffer.rend(); ++i) {
95     paje_event_t e1 = *i;
96     if (e1->timestamp <= tbi->timestamp)
97       break;
98   }
99   buffer.insert(i.base(), tbi);
100   if (i == buffer.rend())
101     XBT_DEBUG("%s: inserted at beginning", __FUNCTION__);
102   else
103     XBT_DEBUG("%s: inserted at%s %zd", __FUNCTION__, (i == buffer.rbegin()) ? " end" :"pos =",
104         std::distance(buffer.rend(),i));
105 }
106
107 static void free_paje_event (paje_event_t event)
108 {
109   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event->event_type, event->timestamp);
110   switch (event->event_type){
111   case PAJE_StartLink:
112     xbt_free (((startLink_t)(event->data))->value);
113     xbt_free (((startLink_t)(event->data))->key);
114     break;
115   case PAJE_EndLink:
116     xbt_free (((endLink_t)(event->data))->value);
117     xbt_free (((endLink_t)(event->data))->key);
118     break;
119   default:
120     break;
121   }
122   xbt_free (event->data);
123   xbt_free (event);
124 }
125
126 void new_pajeDefineContainerType(type_t type)
127 {
128   paje_event_t event                           = xbt_new0(s_paje_event_t, 1);
129   event->event_type                            = PAJE_DefineContainerType;
130   event->timestamp                             = 0;
131   event->print                                 = active_writer.print_DefineContainerType;
132   event->free                                  = &free_paje_event;
133   event->data                                  = xbt_new0(s_defineContainerType_t, 1);
134   ((defineContainerType_t)(event->data))->type = type;
135
136   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event->event_type);
137
138   //print it
139   event->print (event);
140   event->free (event);
141 }
142
143 void new_pajeDefineVariableType(type_t type)
144 {
145   paje_event_t event                          = xbt_new0(s_paje_event_t, 1);
146   event->event_type                           = PAJE_DefineVariableType;
147   event->timestamp                            = 0;
148   event->print                                = active_writer.print_DefineVariableType;
149   event->free                                 = &free_paje_event;
150   event->data                                 = xbt_new0(s_defineVariableType_t, 1);
151   ((defineVariableType_t)(event->data))->type = type;
152
153   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event->event_type);
154
155   //print it
156   event->print (event);
157   event->free (event);
158 }
159
160 void new_pajeDefineStateType(type_t type)
161 {
162   paje_event_t event                       = xbt_new0(s_paje_event_t, 1);
163   event->event_type                        = PAJE_DefineStateType;
164   event->timestamp                         = 0;
165   event->print                             = active_writer.print_DefineStateType;
166   event->free                              = &free_paje_event;
167   event->data                              = xbt_new0(s_defineStateType_t, 1);
168   ((defineStateType_t)(event->data))->type = type;
169
170   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event->event_type);
171
172   //print it
173   event->print (event);
174   event->free (event);
175 }
176
177 void new_pajeDefineEventType(type_t type)
178 {
179   paje_event_t event                       = xbt_new0(s_paje_event_t, 1);
180   event->event_type                        = PAJE_DefineEventType;
181   event->timestamp                         = 0;
182   event->print                             = active_writer.print_DefineEventType;
183   event->free                              = &free_paje_event;
184   event->data                              = xbt_new0(s_defineEventType_t, 1);
185   ((defineEventType_t)(event->data))->type = type;
186
187   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event->event_type);
188
189   //print it
190   event->print (event);
191   event->free (event);
192 }
193
194 void new_pajeDefineLinkType(type_t type, type_t source, type_t dest)
195 {
196   paje_event_t event                        = xbt_new0(s_paje_event_t, 1);
197   event->event_type                         = PAJE_DefineLinkType;
198   event->timestamp                          = 0;
199   event->print                              = active_writer.print_DefineLinkType;
200   event->free                               = &free_paje_event;
201   event->data                               = xbt_new0(s_defineLinkType_t, 1);
202   ((defineLinkType_t)(event->data))->type   = type;
203   ((defineLinkType_t)(event->data))->source = source;
204   ((defineLinkType_t)(event->data))->dest   = dest;
205
206   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event->event_type);
207
208   //print it
209   event->print (event);
210   event->free (event);
211 }
212
213 void new_pajeDefineEntityValue (val_t value)
214 {
215   paje_event_t event                          = xbt_new0(s_paje_event_t, 1);
216   event->event_type                           = PAJE_DefineEntityValue;
217   event->timestamp                            = 0;
218   event->print                                = active_writer.print_DefineEntityValue;
219   event->free                                 = &free_paje_event;
220   event->data                                 = xbt_new0(s_defineEntityValue_t, 1);
221   ((defineEntityValue_t)(event->data))->value = value;
222
223   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event->event_type);
224
225   //print it
226   event->print (event);
227   event->free (event);
228 }
229
230 void new_pajeCreateContainer (container_t container)
231 {
232   paje_event_t event                            = xbt_new0(s_paje_event_t, 1);
233   event->event_type                             = PAJE_CreateContainer;
234   event->timestamp                              = SIMIX_get_clock();
235   event->print                                  = active_writer.print_CreateContainer;
236   event->free                                   = &free_paje_event;
237   event->data                                   = xbt_new0(s_createContainer_t, 1);
238   ((createContainer_t)(event->data))->container = container;
239
240   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event->event_type, event->timestamp);
241
242   //print it
243   event->print (event);
244   event->free (event);
245 }
246
247 void new_pajeDestroyContainer (container_t container)
248 {
249   paje_event_t event                             = xbt_new0(s_paje_event_t, 1);
250   event->event_type                              = PAJE_DestroyContainer;
251   event->timestamp                               = SIMIX_get_clock();
252   event->print                                   = active_writer.print_DestroyContainer;
253   event->free                                    = &free_paje_event;
254   event->data                                    = xbt_new0(s_destroyContainer_t, 1);
255   ((destroyContainer_t)(event->data))->container = container;
256
257   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event->event_type, event->timestamp);
258
259   //print it
260   event->print (event);
261   event->free (event);
262 }
263
264 void new_pajeSetVariable (double timestamp, container_t container, type_t type, double value)
265 {
266   paje_event_t event                        = xbt_new0(s_paje_event_t, 1);
267   event->event_type                         = PAJE_SetVariable;
268   event->timestamp                          = timestamp;
269   event->print                              = active_writer.print_SetVariable;
270   event->free                               = &free_paje_event;
271   event->data                               = xbt_new0(s_setVariable_t, 1);
272   ((setVariable_t)(event->data))->type      = type;
273   ((setVariable_t)(event->data))->container = container;
274   ((setVariable_t)(event->data))->value     = value;
275
276   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event->event_type, event->timestamp);
277
278   insert_into_buffer (event);
279 }
280
281
282 void new_pajeAddVariable (double timestamp, container_t container, type_t type, double value)
283 {
284   paje_event_t event                        = xbt_new0(s_paje_event_t, 1);
285   event->event_type                         = PAJE_AddVariable;
286   event->timestamp                          = timestamp;
287   event->print                              = active_writer.print_AddVariable;
288   event->free                               = &free_paje_event;
289   event->data                               = xbt_new0(s_addVariable_t, 1);
290   ((addVariable_t)(event->data))->type      = type;
291   ((addVariable_t)(event->data))->container = container;
292   ((addVariable_t)(event->data))->value     = value;
293
294   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event->event_type, event->timestamp);
295
296   insert_into_buffer (event);
297 }
298
299 void new_pajeSubVariable (double timestamp, container_t container, type_t type, double value)
300 {
301   paje_event_t event                        = xbt_new0(s_paje_event_t, 1);
302   event->event_type                         = PAJE_SubVariable;
303   event->timestamp                          = timestamp;
304   event->print                              = active_writer.print_SubVariable;
305   event->free                               = &free_paje_event;
306   event->data                               = xbt_new0(s_subVariable_t, 1);
307   ((subVariable_t)(event->data))->type      = type;
308   ((subVariable_t)(event->data))->container = container;
309   ((subVariable_t)(event->data))->value     = value;
310
311   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event->event_type, event->timestamp);
312
313   insert_into_buffer (event);
314 }
315
316 void new_pajeSetState (double timestamp, container_t container, type_t type, val_t value)
317 {
318   paje_event_t event                     = xbt_new0(s_paje_event_t, 1);
319   event->event_type                      = PAJE_SetState;
320   event->timestamp                       = timestamp;
321   event->print                           = active_writer.print_SetState;
322   event->free                            = &free_paje_event;
323   event->data                            = xbt_new0(s_setState_t, 1);
324   ((setState_t)(event->data))->type      = type;
325   ((setState_t)(event->data))->container = container;
326   ((setState_t)(event->data))->value     = value;
327
328 #if HAVE_SMPI
329   if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
330     smpi_trace_call_location_t* loc = smpi_trace_get_call_location();
331     ((setState_t)(event->data))->filename   = loc->filename;
332     ((setState_t)(event->data))->linenumber = loc->linenumber;
333   }
334 #endif
335
336   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event->event_type, event->timestamp);
337
338   insert_into_buffer (event);
339 }
340
341
342 void new_pajePushStateWithExtra (double timestamp, container_t container, type_t type, val_t value, void* extra)
343 {
344   paje_event_t event                 = xbt_new0(s_paje_event_t, 1);
345   event->event_type                  = PAJE_PushState;
346   event->timestamp                   = timestamp;
347   event->print                       = active_writer.print_PushState;
348   event->free                        = &free_paje_event;
349   event->data                        = xbt_new0(s_pushState_t, 1);
350   ((pushState_t)(event->data))->type = type;
351   ((pushState_t)(event->data))->container = container;
352   ((pushState_t)(event->data))->value     = value;
353   ((pushState_t)(event->data))->extra     = extra;
354
355 #if HAVE_SMPI
356   if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
357     smpi_trace_call_location_t* loc = smpi_trace_get_call_location();
358     ((pushState_t)(event->data))->filename   = loc->filename;
359     ((pushState_t)(event->data))->linenumber = loc->linenumber;
360   }
361 #endif
362
363   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event->event_type, event->timestamp);
364
365   insert_into_buffer (event);
366 }
367
368
369 void new_pajePushState (double timestamp, container_t container, type_t type, val_t value)
370 {
371   new_pajePushStateWithExtra(timestamp, container, type, value, nullptr);
372 }
373
374 void new_pajePopState (double timestamp, container_t container, type_t type)
375 {
376   paje_event_t event                     = xbt_new0(s_paje_event_t, 1);
377   event->event_type                      = PAJE_PopState;
378   event->timestamp                       = timestamp;
379   event->print                           = active_writer.print_PopState;
380   event->free                            = &free_paje_event;
381   event->data                            = xbt_new0(s_popState_t, 1);
382   ((popState_t)(event->data))->type      = type;
383   ((popState_t)(event->data))->container = container;
384
385   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event->event_type, event->timestamp);
386
387   insert_into_buffer (event);
388 }
389
390
391 void new_pajeResetState (double timestamp, container_t container, type_t type)
392 {
393   paje_event_t event                       = xbt_new0(s_paje_event_t, 1);
394   event->event_type                        = PAJE_ResetState;
395   event->timestamp                         = timestamp;
396   event->print                             = active_writer.print_ResetState;
397   event->free                              = &free_paje_event;
398   event->data                              = xbt_new0(s_resetState_t, 1);
399   ((resetState_t)(event->data))->type      = type;
400   ((resetState_t)(event->data))->container = container;
401
402   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event->event_type, event->timestamp);
403
404   insert_into_buffer (event);
405 }
406
407 void new_pajeStartLink (double timestamp, container_t container, type_t type, container_t sourceContainer,
408                         const char *value, const char *key)
409 {
410   new_pajeStartLinkWithSize(timestamp, container, type, sourceContainer, value, key, -1);
411 }
412
413 void new_pajeStartLinkWithSize (double timestamp, container_t container, type_t type, container_t sourceContainer,
414                                 const char *value, const char *key, int size)
415 {
416   paje_event_t event                            = xbt_new0(s_paje_event_t, 1);
417   event->event_type                             = PAJE_StartLink;
418   event->timestamp                              = timestamp;
419   event->print                                  = active_writer.print_StartLink;
420   event->free                                   = &free_paje_event;
421   event->data                                   = xbt_new0(s_startLink_t, 1);
422   ((startLink_t)(event->data))->type            = type;
423   ((startLink_t)(event->data))->container       = container;
424   ((startLink_t)(event->data))->sourceContainer = sourceContainer;
425   ((startLink_t)(event->data))->value           = xbt_strdup(value);
426   ((startLink_t)(event->data))->key             = xbt_strdup(key);
427   ((startLink_t)(event->data))->size            = size;
428
429   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event->event_type, event->timestamp);
430
431   insert_into_buffer (event);
432 }
433
434 void new_pajeEndLink (double timestamp, container_t container, type_t type, container_t destContainer,
435                       const char *value, const char *key)
436 {
437   paje_event_t event                        = xbt_new0(s_paje_event_t, 1);
438   event->event_type                         = PAJE_EndLink;
439   event->timestamp                          = timestamp;
440   event->print                              = active_writer.print_EndLink;
441   event->free                               = &free_paje_event;
442   event->data                               = xbt_new0(s_endLink_t, 1);
443   ((endLink_t)(event->data))->type          = type;
444   ((endLink_t)(event->data))->container     = container;
445   ((endLink_t)(event->data))->destContainer = destContainer;
446   ((endLink_t)(event->data))->value         = xbt_strdup(value);
447   ((endLink_t)(event->data))->key           = xbt_strdup(key);
448
449   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event->event_type, event->timestamp);
450
451   insert_into_buffer (event);
452 }
453
454 void new_pajeNewEvent (double timestamp, container_t container, type_t type, val_t value)
455 {
456   paje_event_t event                     = xbt_new0(s_paje_event_t, 1);
457   event->event_type                      = PAJE_NewEvent;
458   event->timestamp                       = timestamp;
459   event->print                           = active_writer.print_NewEvent;
460   event->free                            = &free_paje_event;
461   event->data                            = xbt_new0(s_newEvent_t, 1);
462   ((newEvent_t)(event->data))->type      = type;
463   ((newEvent_t)(event->data))->container = container;
464   ((newEvent_t)(event->data))->value     = value;
465
466   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event->event_type, event->timestamp);
467
468   insert_into_buffer (event);
469 }