Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Attempt to correct error tests
[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 #include "typeinfo"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_trace, instr, "tracing event system");
14
15 FILE *tracing_file = nullptr;
16
17 void print_NULL(paje_event_t event){}
18
19 /* The active set of functions for the selected trace format
20  * By default, they all do nothing, hence the print_NULL to avoid segfaults */
21
22 s_instr_trace_writer_t active_writer = {&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                                         &print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL, &print_NULL};
25
26 std::vector<paje_event_t> buffer;
27
28 void dump_comment (const char *comment)
29 {
30   if (!strlen(comment)) return;
31   fprintf (tracing_file, "# %s\n", comment);
32 }
33
34 void dump_comment_file (const char *filename)
35 {
36   if (!strlen(filename)) return;
37   FILE *file = fopen (filename, "r");
38   if (!file){
39     THROWF (system_error, 1, "Comment file %s could not be opened for reading.", filename);
40   }
41   while (!feof(file)){
42     char c;
43     c = fgetc(file);
44     if (feof(file)) break;
45     fprintf (tracing_file, "# ");
46     while (c != '\n'){
47       fprintf (tracing_file, "%c", c);
48       c = fgetc(file);
49       if (feof(file)) break;
50     }
51     fprintf (tracing_file, "\n");
52   }
53   fclose(file);
54 }
55
56 double TRACE_last_timestamp_to_dump = 0;
57 //dumps the trace file until the timestamp TRACE_last_timestamp_to_dump
58 void TRACE_paje_dump_buffer (int force)
59 {
60   if (!TRACE_is_enabled()) return;
61   XBT_DEBUG("%s: dump until %f. starts", __FUNCTION__, TRACE_last_timestamp_to_dump);
62   if (force){
63     for (auto event :buffer){
64       event->print();
65       delete event;
66     }
67     buffer.clear();
68   }else{
69     std::vector<paje_event_t>::iterator i = buffer.begin();
70     for (auto event :buffer){
71       double head_timestamp = event->timestamp;
72       if (head_timestamp > TRACE_last_timestamp_to_dump)
73         break;
74       event->print();
75       delete event;
76       ++i;
77     }
78     buffer.erase(buffer.begin(), i);
79   }
80   XBT_DEBUG("%s: ends", __FUNCTION__);
81 }
82
83 /* internal do the instrumentation module */
84 static void insert_into_buffer (paje_event_t tbi)
85 {
86   if (TRACE_buffer() == 0){
87     tbi->print ();
88     delete tbi;
89     return;
90   }
91
92   XBT_DEBUG("%s: insert event_type=%d, timestamp=%f, buffersize=%zu)",
93       __FUNCTION__, (int)tbi->event_type, tbi->timestamp, buffer.size());
94   std::vector<paje_event_t>::reverse_iterator i;
95   for (i = buffer.rbegin(); i != buffer.rend(); ++i) {
96     paje_event_t e1 = *i;
97     if (e1->timestamp <= tbi->timestamp)
98       break;
99   }
100   buffer.insert(i.base(), tbi);
101   if (i == buffer.rend())
102     XBT_DEBUG("%s: inserted at beginning", __FUNCTION__);
103   else
104     XBT_DEBUG("%s: inserted at%s %zd", __FUNCTION__, (i == buffer.rbegin()) ? " end" :"pos =",
105         std::distance(buffer.rend(),i));
106 }
107
108 paje_event:: ~paje_event()
109 {
110   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, timestamp);
111  
112  /* switch (event->event_type){
113   case PAJE_StartLink:
114     xbt_free (((startLink_t)(event->data))->value);
115     xbt_free (((startLink_t)(event->data))->key);
116     break;
117   case PAJE_EndLink:
118     xbt_free (((endLink_t)(event->data))->value);
119     xbt_free (((endLink_t)(event->data))->key);
120     break;
121   default:
122     break;
123   }*/
124 }
125
126
127 //--------------------------------------------------------
128
129 DefineContainerEvent::DefineContainerEvent(type_t type)
130 {
131
132   event_type                            = PAJE_DefineContainerType;
133   timestamp                             = 0;
134   this->type = type;
135   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
136   //print it
137   print ();
138 }
139
140
141 DefineVariableTypeEvent::DefineVariableTypeEvent(type_t type)
142 {
143   this->event_type                           = PAJE_DefineVariableType;
144   this->timestamp                            = 0;
145   this->type = type;
146
147   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
148
149   //print it
150   print ();
151 }
152 // TODO convertir tt les constructeurs proprement.
153 DefineStateTypeEvent::DefineStateTypeEvent(type_t type)
154 {
155   this->event_type                        = PAJE_DefineStateType;
156   this->timestamp                         = 0;
157   this->type = type;
158
159   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
160
161   //print it
162   print();
163 }
164
165 DefineEventTypeEvent::DefineEventTypeEvent(type_t type)
166 {
167   this->event_type                        = PAJE_DefineEventType;
168   this->timestamp                         = 0;
169   this->type = type;
170
171   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
172
173   //print it
174   print();
175 }
176
177 DefineLinkTypeEvent::DefineLinkTypeEvent(type_t type, type_t source, type_t dest)
178 {
179   this->event_type                         = PAJE_DefineLinkType;
180   this->timestamp                          = 0;
181   this->type   = type;
182   this->source = source;
183   this->dest   = dest;
184
185   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
186
187   //print it
188   print();
189 }
190
191 DefineEntityValueEvent::DefineEntityValueEvent (val_t value)
192 {
193   this->event_type                           = PAJE_DefineEntityValue;
194   this->timestamp                            = 0;
195   this->value = value;
196
197   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
198
199   //print it
200   print();
201 }
202
203 CreateContainerEvent::CreateContainerEvent (container_t container)
204 {
205   this->event_type                             = PAJE_CreateContainer;
206   this->timestamp                              = SIMIX_get_clock();
207   this->container = container;
208
209   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
210
211   //print it
212   print();
213 }
214
215 DestroyContainerEvent::DestroyContainerEvent (container_t container)
216 {
217   this->event_type                              = PAJE_DestroyContainer;
218   this->timestamp                               = SIMIX_get_clock();
219   this->container = container;
220
221   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
222
223   //print it
224   print();
225 }
226
227 SetVariableEvent::SetVariableEvent (double timestamp, container_t container, type_t type, double value)
228 {
229   this->event_type                         = PAJE_SetVariable;
230   this->timestamp                          = timestamp;
231   this->type      = type;
232   this->container = container;
233   this->value     = value;
234
235   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
236
237   insert_into_buffer (this);
238 }
239
240
241 AddVariableEvent::AddVariableEvent (double timestamp, container_t container, type_t type, double value)
242 {
243   this->event_type                         = PAJE_AddVariable;
244   this->timestamp                          = timestamp;
245   this->type      = type;
246   this->container = container;
247   this->value     = value;
248
249   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
250
251   insert_into_buffer (this);
252 }
253
254 SubVariableEvent::SubVariableEvent (double timestamp, container_t container, type_t type, double value)
255 {
256   this->event_type                         = PAJE_SubVariable;
257   this->timestamp                          = timestamp;
258   this->type      = type;
259   this->container = container;
260   this->value     = value;
261
262   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
263
264   insert_into_buffer (this);
265 }
266
267 SetStateEvent::SetStateEvent (double timestamp, container_t container, type_t type, val_t value)
268 {
269   this->event_type                      = PAJE_SetState;
270   this->timestamp                       = timestamp;
271   this->type      = type;
272   this->container = container;
273   this->value     = value;
274
275 #if HAVE_SMPI
276   if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
277     smpi_trace_call_location_t* loc = smpi_trace_get_call_location();
278     filename   = loc->filename;
279     linenumber = loc->linenumber;
280   }
281 #endif
282
283   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
284
285   insert_into_buffer (this);
286 }
287
288
289 PushStateEvent::PushStateEvent (double timestamp, container_t container, type_t type, val_t value, void* extra)
290 {
291   this->event_type                  = PAJE_PushState;
292   this->timestamp                   = timestamp;
293   this->type = type;
294   this->container = container;
295   this->value     = value;
296   this->extra     = extra;
297
298 #if HAVE_SMPI
299   if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
300     smpi_trace_call_location_t* loc = smpi_trace_get_call_location();
301     filename   = loc->filename;
302     linenumber = loc->linenumber;
303   }
304 #endif
305
306   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
307
308   insert_into_buffer (this);
309 }
310
311
312 PushStateEvent::PushStateEvent (double timestamp, container_t container, type_t type, val_t value)
313 {
314   new PushStateEvent(timestamp, container, type, value, nullptr);
315 }
316
317 PopStateEvent::PopStateEvent (double timestamp, container_t container, type_t type)
318 {
319   this->event_type                      = PAJE_PopState;
320   this->timestamp                       = timestamp;
321   this->type      = type;
322   this->container = container;
323
324   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
325
326   insert_into_buffer (this);
327 }
328
329
330 ResetStateEvent::ResetStateEvent (double timestamp, container_t container, type_t type)
331 {
332   this->event_type                        = PAJE_ResetState;
333   this->timestamp                         = timestamp;
334   this->type      = type;
335   this->container = container;
336
337   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
338
339   insert_into_buffer (this);
340 }
341
342 StartLinkEvent::StartLinkEvent (double timestamp, container_t container, type_t type, container_t sourceContainer,
343                         const char *value, const char *key)
344 {
345   StartLinkEvent(timestamp, container, type, sourceContainer, value, key, -1);
346 }
347
348 StartLinkEvent::StartLinkEvent (double timestamp, container_t container, type_t type, container_t sourceContainer,
349                                 const char *value, const char *key, int size)
350 {
351   event_type                             = PAJE_StartLink;
352   timestamp                              = timestamp;
353   this->type            = type;
354   this->container       = container;
355   this->sourceContainer = sourceContainer;
356   value           = xbt_strdup(value);
357   key             = xbt_strdup(key);
358   this->size            = size;
359
360   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
361
362   insert_into_buffer (this);
363 }
364
365 EndLinkEvent::EndLinkEvent (double timestamp, container_t container, type_t type, container_t destContainer,
366                       const char *value, const char *key)
367 {
368   this->event_type                         = PAJE_EndLink;
369   this->timestamp                          = timestamp;
370   this->type          = type;
371   this->container     = container;
372   this->destContainer = destContainer;
373   this->value         = xbt_strdup(value);
374   this->key           = xbt_strdup(key);
375
376   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
377
378   insert_into_buffer (this);
379 }
380
381 NewEvent::NewEvent (double timestamp, container_t container, type_t type, val_t value)
382 {
383   this->event_type                      = PAJE_NewEvent;
384   this->timestamp                       = timestamp;
385   this->type      = type;
386   this->container = container;
387   this->value     = value;
388
389   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
390
391   insert_into_buffer (this);
392 }