Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename paje_event into PajeEvent (+cosmetics)
[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     XBT_DEBUG("compare to %p is of type %d; timestamp:%f", e1,
98         (int)e1->event_type, e1->timestamp);
99     if (e1->timestamp <= tbi->timestamp)
100       break;
101   }
102   buffer.insert(i.base(), tbi);
103   if (i == buffer.rend())
104     XBT_DEBUG("%s: inserted at beginning", __FUNCTION__);
105   else if (i == buffer.rbegin())
106     XBT_DEBUG("%s: inserted at end", __FUNCTION__);
107   else
108     XBT_DEBUG("%s: inserted at pos= %zd from its end", __FUNCTION__,
109         std::distance(buffer.rbegin(),i));
110 }
111
112 PajeEvent:: ~PajeEvent()
113 {
114   XBT_DEBUG("%s not implemented: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, timestamp);
115  
116  /* switch (event->event_type){
117   case PAJE_StartLink:
118     xbt_free (((startLink_t)(event->data))->value);
119     xbt_free (((startLink_t)(event->data))->key);
120     break;
121   case PAJE_EndLink:
122     xbt_free (((endLink_t)(event->data))->value);
123     xbt_free (((endLink_t)(event->data))->key);
124     break;
125   default:
126     break;
127   }*/
128 }
129
130
131 //--------------------------------------------------------
132
133 DefineContainerEvent::DefineContainerEvent(type_t type)
134 {
135
136   event_type                            = PAJE_DefineContainerType;
137   timestamp                             = 0;
138   this->type = type;
139   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
140   //print it
141   print ();
142 }
143
144
145 DefineVariableTypeEvent::DefineVariableTypeEvent(type_t type)
146 {
147   this->event_type                           = PAJE_DefineVariableType;
148   this->timestamp                            = 0;
149   this->type = type;
150
151   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
152
153   //print it
154   print ();
155 }
156 // TODO convertir tt les constructeurs proprement.
157 DefineStateTypeEvent::DefineStateTypeEvent(type_t type)
158 {
159   this->event_type                        = PAJE_DefineStateType;
160   this->timestamp                         = 0;
161   this->type = type;
162
163   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
164
165   //print it
166   print();
167 }
168
169 DefineEventTypeEvent::DefineEventTypeEvent(type_t type)
170 {
171   this->event_type                        = PAJE_DefineEventType;
172   this->timestamp                         = 0;
173   this->type = type;
174
175   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
176
177   //print it
178   print();
179 }
180
181 DefineLinkTypeEvent::DefineLinkTypeEvent(type_t type, type_t source, type_t dest)
182 {
183   this->event_type                         = PAJE_DefineLinkType;
184   this->timestamp                          = 0;
185   this->type   = type;
186   this->source = source;
187   this->dest   = dest;
188
189   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
190
191   //print it
192   print();
193 }
194
195 DefineEntityValueEvent::DefineEntityValueEvent (val_t value)
196 {
197   this->event_type                           = PAJE_DefineEntityValue;
198   this->timestamp                            = 0;
199   this->value = value;
200
201   XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
202
203   //print it
204   print();
205 }
206
207 CreateContainerEvent::CreateContainerEvent (container_t container)
208 {
209   this->event_type                             = PAJE_CreateContainer;
210   this->timestamp                              = SIMIX_get_clock();
211   this->container = container;
212
213   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
214
215   //print it
216   print();
217 }
218
219 DestroyContainerEvent::DestroyContainerEvent (container_t container)
220 {
221   this->event_type                              = PAJE_DestroyContainer;
222   this->timestamp                               = SIMIX_get_clock();
223   this->container = container;
224
225   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
226
227   //print it
228   print();
229 }
230
231 SetVariableEvent::SetVariableEvent (double timestamp, container_t container, type_t type, double value)
232 {
233   this->event_type                         = PAJE_SetVariable;
234   this->timestamp                          = timestamp;
235   this->type      = type;
236   this->container = container;
237   this->value     = value;
238
239   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
240
241   insert_into_buffer (this);
242 }
243
244
245 AddVariableEvent::AddVariableEvent (double timestamp, container_t container, type_t type, double value)
246 {
247   this->event_type                         = PAJE_AddVariable;
248   this->timestamp                          = timestamp;
249   this->type      = type;
250   this->container = container;
251   this->value     = value;
252
253   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
254
255   insert_into_buffer (this);
256 }
257
258 SubVariableEvent::SubVariableEvent (double timestamp, container_t container, type_t type, double value)
259 {
260   this->event_type                         = PAJE_SubVariable;
261   this->timestamp                          = timestamp;
262   this->type      = type;
263   this->container = container;
264   this->value     = value;
265
266   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
267
268   insert_into_buffer (this);
269 }
270
271 SetStateEvent::SetStateEvent (double timestamp, container_t container, type_t type, val_t value)
272 {
273   this->event_type                      = PAJE_SetState;
274   this->timestamp                       = timestamp;
275   this->type      = type;
276   this->container = container;
277   this->value     = value;
278
279 #if HAVE_SMPI
280   if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
281     smpi_trace_call_location_t* loc = smpi_trace_get_call_location();
282     filename   = loc->filename;
283     linenumber = loc->linenumber;
284   }
285 #endif
286
287   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
288
289   insert_into_buffer (this);
290 }
291
292
293 PushStateEvent::PushStateEvent (double timestamp, container_t container, type_t type, val_t value, void* extra)
294 {
295   this->event_type                  = PAJE_PushState;
296   this->timestamp                   = timestamp;
297   this->type = type;
298   this->container = container;
299   this->value     = value;
300   this->extra     = extra;
301
302 #if HAVE_SMPI
303   if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
304     smpi_trace_call_location_t* loc = smpi_trace_get_call_location();
305     filename   = loc->filename;
306     linenumber = loc->linenumber;
307   }
308 #endif
309
310   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
311
312   insert_into_buffer (this);
313 }
314
315
316 PushStateEvent::PushStateEvent (double timestamp, container_t container, type_t type, val_t value)
317 {
318   new PushStateEvent(timestamp, container, type, value, nullptr);
319 }
320
321 PopStateEvent::PopStateEvent (double timestamp, container_t container, type_t type)
322 {
323   this->event_type                      = PAJE_PopState;
324   this->timestamp                       = timestamp;
325   this->type      = type;
326   this->container = container;
327
328   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
329
330   insert_into_buffer (this);
331 }
332
333
334 ResetStateEvent::ResetStateEvent (double timestamp, container_t container, type_t type)
335 {
336   this->event_type                        = PAJE_ResetState;
337   this->timestamp                         = timestamp;
338   this->type      = type;
339   this->container = container;
340
341   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
342
343   insert_into_buffer (this);
344 }
345
346 StartLinkEvent::StartLinkEvent (double timestamp, container_t container, type_t type, container_t sourceContainer,
347                         const char *value, const char *key)
348 {
349   StartLinkEvent(timestamp, container, type, sourceContainer, value, key, -1);
350 }
351
352 StartLinkEvent::StartLinkEvent (double timestamp, container_t container, type_t type, container_t sourceContainer,
353                                 const char *value, const char *key, int size)
354 {
355   event_type                             = PAJE_StartLink;
356   timestamp                              = timestamp;
357   this->type            = type;
358   this->container       = container;
359   this->sourceContainer = sourceContainer;
360   value           = xbt_strdup(value);
361   key             = xbt_strdup(key);
362   this->size            = size;
363
364   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
365
366   insert_into_buffer (this);
367 }
368
369 EndLinkEvent::EndLinkEvent (double timestamp, container_t container, type_t type, container_t destContainer,
370                       const char *value, const char *key)
371 {
372   this->event_type                         = PAJE_EndLink;
373   this->timestamp                          = timestamp;
374   this->type          = type;
375   this->container     = container;
376   this->destContainer = destContainer;
377   this->value         = xbt_strdup(value);
378   this->key           = xbt_strdup(key);
379
380   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
381
382   insert_into_buffer (this);
383 }
384
385 NewEvent::NewEvent (double timestamp, container_t container, type_t type, val_t value)
386 {
387   this->event_type                      = PAJE_NewEvent;
388   this->timestamp                       = timestamp;
389   this->type      = type;
390   this->container = container;
391   this->value     = value;
392
393   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
394
395   insert_into_buffer (this);
396 }