1 /* Copyright (c) 2010-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
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 */
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_trace, instr, "tracing event system");
15 FILE *tracing_file = nullptr;
17 void print_NULL(paje_event_t event){}
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 */
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};
26 std::vector<paje_event_t> buffer;
28 void dump_comment (const char *comment)
30 if (!strlen(comment)) return;
31 fprintf (tracing_file, "# %s\n", comment);
34 void dump_comment_file (const char *filename)
36 if (!strlen(filename)) return;
37 FILE *file = fopen (filename, "r");
39 THROWF (system_error, 1, "Comment file %s could not be opened for reading.", filename);
44 if (feof(file)) break;
45 fprintf (tracing_file, "# ");
47 fprintf (tracing_file, "%c", c);
49 if (feof(file)) break;
51 fprintf (tracing_file, "\n");
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)
60 if (!TRACE_is_enabled()) return;
61 XBT_DEBUG("%s: dump until %f. starts", __FUNCTION__, TRACE_last_timestamp_to_dump);
63 for (auto event :buffer){
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)
78 buffer.erase(buffer.begin(), i);
80 XBT_DEBUG("%s: ends", __FUNCTION__);
83 /* internal do the instrumentation module */
84 static void insert_into_buffer (paje_event_t tbi)
86 if (TRACE_buffer() == 0){
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) {
97 if (e1->timestamp <= tbi->timestamp)
100 buffer.insert(i.base(), tbi);
101 if (i == buffer.rend())
102 XBT_DEBUG("%s: inserted at beginning", __FUNCTION__);
104 XBT_DEBUG("%s: inserted at%s %zd", __FUNCTION__, (i == buffer.rbegin()) ? " end" :"pos =",
105 std::distance(buffer.rend(),i));
108 paje_event:: ~paje_event()
110 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, timestamp);
112 /* switch (event->event_type){
114 xbt_free (((startLink_t)(event->data))->value);
115 xbt_free (((startLink_t)(event->data))->key);
118 xbt_free (((endLink_t)(event->data))->value);
119 xbt_free (((endLink_t)(event->data))->key);
127 //--------------------------------------------------------
129 DefineContainerEvent::DefineContainerEvent(type_t type)
132 event_type = PAJE_DefineContainerType;
135 XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
141 DefineVariableTypeEvent::DefineVariableTypeEvent(type_t type)
143 this->event_type = PAJE_DefineVariableType;
147 XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
152 // TODO convertir tt les constructeurs proprement.
153 DefineStateTypeEvent::DefineStateTypeEvent(type_t type)
155 this->event_type = PAJE_DefineStateType;
159 XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
165 DefineEventTypeEvent::DefineEventTypeEvent(type_t type)
167 this->event_type = PAJE_DefineEventType;
171 XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
177 DefineLinkTypeEvent::DefineLinkTypeEvent(type_t type, type_t source, type_t dest)
179 this->event_type = PAJE_DefineLinkType;
182 this->source = source;
185 XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
191 DefineEntityValueEvent::DefineEntityValueEvent (val_t value)
193 this->event_type = PAJE_DefineEntityValue;
197 XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
203 CreateContainerEvent::CreateContainerEvent (container_t container)
205 this->event_type = PAJE_CreateContainer;
206 this->timestamp = SIMIX_get_clock();
207 this->container = container;
209 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
215 DestroyContainerEvent::DestroyContainerEvent (container_t container)
217 this->event_type = PAJE_DestroyContainer;
218 this->timestamp = SIMIX_get_clock();
219 this->container = container;
221 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
227 SetVariableEvent::SetVariableEvent (double timestamp, container_t container, type_t type, double value)
229 this->event_type = PAJE_SetVariable;
230 this->timestamp = timestamp;
232 this->container = container;
235 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
237 insert_into_buffer (this);
241 AddVariableEvent::AddVariableEvent (double timestamp, container_t container, type_t type, double value)
243 this->event_type = PAJE_AddVariable;
244 this->timestamp = timestamp;
246 this->container = container;
249 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
251 insert_into_buffer (this);
254 SubVariableEvent::SubVariableEvent (double timestamp, container_t container, type_t type, double value)
256 this->event_type = PAJE_SubVariable;
257 this->timestamp = timestamp;
259 this->container = container;
262 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
264 insert_into_buffer (this);
267 SetStateEvent::SetStateEvent (double timestamp, container_t container, type_t type, val_t value)
269 this->event_type = PAJE_SetState;
270 this->timestamp = timestamp;
272 this->container = container;
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;
283 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
285 insert_into_buffer (this);
289 PushStateEvent::PushStateEvent (double timestamp, container_t container, type_t type, val_t value, void* extra)
291 this->event_type = PAJE_PushState;
292 this->timestamp = timestamp;
294 this->container = container;
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;
306 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
308 insert_into_buffer (this);
312 PushStateEvent::PushStateEvent (double timestamp, container_t container, type_t type, val_t value)
314 new PushStateEvent(timestamp, container, type, value, nullptr);
317 PopStateEvent::PopStateEvent (double timestamp, container_t container, type_t type)
319 this->event_type = PAJE_PopState;
320 this->timestamp = timestamp;
322 this->container = container;
324 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
326 insert_into_buffer (this);
330 ResetStateEvent::ResetStateEvent (double timestamp, container_t container, type_t type)
332 this->event_type = PAJE_ResetState;
333 this->timestamp = timestamp;
335 this->container = container;
337 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
339 insert_into_buffer (this);
342 StartLinkEvent::StartLinkEvent (double timestamp, container_t container, type_t type, container_t sourceContainer,
343 const char *value, const char *key)
345 StartLinkEvent(timestamp, container, type, sourceContainer, value, key, -1);
348 StartLinkEvent::StartLinkEvent (double timestamp, container_t container, type_t type, container_t sourceContainer,
349 const char *value, const char *key, int size)
351 event_type = PAJE_StartLink;
352 timestamp = timestamp;
354 this->container = container;
355 this->sourceContainer = sourceContainer;
356 value = xbt_strdup(value);
357 key = xbt_strdup(key);
360 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
362 insert_into_buffer (this);
365 EndLinkEvent::EndLinkEvent (double timestamp, container_t container, type_t type, container_t destContainer,
366 const char *value, const char *key)
368 this->event_type = PAJE_EndLink;
369 this->timestamp = timestamp;
371 this->container = container;
372 this->destContainer = destContainer;
373 this->value = xbt_strdup(value);
374 this->key = xbt_strdup(key);
376 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
378 insert_into_buffer (this);
381 NewEvent::NewEvent (double timestamp, container_t container, type_t type, val_t value)
383 this->event_type = PAJE_NewEvent;
384 this->timestamp = timestamp;
386 this->container = container;
389 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
391 insert_into_buffer (this);