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 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)
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__);
108 XBT_DEBUG("%s: inserted at pos= %zd from its end", __FUNCTION__,
109 std::distance(buffer.rbegin(),i));
112 PajeEvent:: ~PajeEvent()
114 XBT_DEBUG("%s not implemented: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, timestamp);
116 /* switch (event->event_type){
118 xbt_free (((startLink_t)(event->data))->value);
119 xbt_free (((startLink_t)(event->data))->key);
122 xbt_free (((endLink_t)(event->data))->value);
123 xbt_free (((endLink_t)(event->data))->key);
131 //--------------------------------------------------------
133 DefineContainerEvent::DefineContainerEvent(type_t type)
136 event_type = PAJE_DefineContainerType;
139 XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
145 DefineVariableTypeEvent::DefineVariableTypeEvent(type_t type)
147 this->event_type = PAJE_DefineVariableType;
151 XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
156 // TODO convertir tt les constructeurs proprement.
157 DefineStateTypeEvent::DefineStateTypeEvent(type_t type)
159 this->event_type = PAJE_DefineStateType;
163 XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
169 DefineEventTypeEvent::DefineEventTypeEvent(type_t type)
171 this->event_type = PAJE_DefineEventType;
175 XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
181 DefineLinkTypeEvent::DefineLinkTypeEvent(type_t type, type_t source, type_t dest)
183 this->event_type = PAJE_DefineLinkType;
186 this->source = source;
189 XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
195 DefineEntityValueEvent::DefineEntityValueEvent (val_t value)
197 this->event_type = PAJE_DefineEntityValue;
201 XBT_DEBUG("%s: event_type=%d", __FUNCTION__, (int)event_type);
207 CreateContainerEvent::CreateContainerEvent (container_t container)
209 this->event_type = PAJE_CreateContainer;
210 this->timestamp = SIMIX_get_clock();
211 this->container = container;
213 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
219 DestroyContainerEvent::DestroyContainerEvent (container_t container)
221 this->event_type = PAJE_DestroyContainer;
222 this->timestamp = SIMIX_get_clock();
223 this->container = container;
225 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
231 SetVariableEvent::SetVariableEvent (double timestamp, container_t container, type_t type, double value)
233 this->event_type = PAJE_SetVariable;
234 this->timestamp = timestamp;
236 this->container = container;
239 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
241 insert_into_buffer (this);
245 AddVariableEvent::AddVariableEvent (double timestamp, container_t container, type_t type, double value)
247 this->event_type = PAJE_AddVariable;
248 this->timestamp = timestamp;
250 this->container = container;
253 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
255 insert_into_buffer (this);
258 SubVariableEvent::SubVariableEvent (double timestamp, container_t container, type_t type, double value)
260 this->event_type = PAJE_SubVariable;
261 this->timestamp = timestamp;
263 this->container = container;
266 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
268 insert_into_buffer (this);
271 SetStateEvent::SetStateEvent (double timestamp, container_t container, type_t type, val_t value)
273 this->event_type = PAJE_SetState;
274 this->timestamp = timestamp;
276 this->container = container;
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;
287 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
289 insert_into_buffer (this);
293 PushStateEvent::PushStateEvent (double timestamp, container_t container, type_t type, val_t value, void* extra)
295 this->event_type = PAJE_PushState;
296 this->timestamp = timestamp;
298 this->container = container;
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;
310 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
312 insert_into_buffer (this);
316 PushStateEvent::PushStateEvent (double timestamp, container_t container, type_t type, val_t value)
318 new PushStateEvent(timestamp, container, type, value, nullptr);
321 PopStateEvent::PopStateEvent (double timestamp, container_t container, type_t type)
323 this->event_type = PAJE_PopState;
324 this->timestamp = timestamp;
326 this->container = container;
328 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
330 insert_into_buffer (this);
334 ResetStateEvent::ResetStateEvent (double timestamp, container_t container, type_t type)
336 this->event_type = PAJE_ResetState;
337 this->timestamp = timestamp;
339 this->container = container;
341 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
343 insert_into_buffer (this);
346 StartLinkEvent::StartLinkEvent (double timestamp, container_t container, type_t type, container_t sourceContainer,
347 const char *value, const char *key)
349 StartLinkEvent(timestamp, container, type, sourceContainer, value, key, -1);
352 StartLinkEvent::StartLinkEvent (double timestamp, container_t container, type_t type, container_t sourceContainer,
353 const char *value, const char *key, int size)
355 event_type = PAJE_StartLink;
356 timestamp = timestamp;
358 this->container = container;
359 this->sourceContainer = sourceContainer;
360 value = xbt_strdup(value);
361 key = xbt_strdup(key);
364 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
366 insert_into_buffer (this);
369 EndLinkEvent::EndLinkEvent (double timestamp, container_t container, type_t type, container_t destContainer,
370 const char *value, const char *key)
372 this->event_type = PAJE_EndLink;
373 this->timestamp = timestamp;
375 this->container = container;
376 this->destContainer = destContainer;
377 this->value = xbt_strdup(value);
378 this->key = xbt_strdup(key);
380 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
382 insert_into_buffer (this);
385 NewEvent::NewEvent (double timestamp, container_t container, type_t type, val_t value)
387 this->event_type = PAJE_NewEvent;
388 this->timestamp = timestamp;
390 this->container = container;
393 XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, (int)event_type, this->timestamp);
395 insert_into_buffer (this);