1 /* Authors: Arnaud Legrand */
3 /* This program is free software; you can redistribute it and/or modify it
4 under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "xbt/sysdep.h"
9 #include "trace_mgr_private.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trace, surf,
13 "Logging specific to the SURF trace module");
15 static xbt_dict_t trace_list = NULL;
16 static void _tmgr_trace_free(void *trace)
18 tmgr_trace_free(trace);
21 tmgr_history_t tmgr_history_new(void)
23 tmgr_history_t history;
25 history = xbt_new0(s_tmgr_history_t, 1);
27 history->heap = xbt_heap_new(8, xbt_free); /* Why 8 ? Well, why not... */
32 void tmgr_history_free(tmgr_history_t history)
34 xbt_heap_free(history->heap);
38 tmgr_trace_t tmgr_trace_new(const char *filename)
40 tmgr_trace_t trace = NULL;
44 xbt_heap_float_t periodicity = -1.0; /* No periodicity by default */
46 tmgr_event_t last_event = NULL;
49 xbt_dict_get(trace_list, filename, (void **) &trace);
54 if ((f = fopen(filename, "r")) == NULL) {
55 CRITICAL1("Cannot open file '%s'\n", filename);
59 trace = xbt_new0(s_tmgr_trace_t, 1);
60 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
62 while (fgets(line, 256, f)) {
64 if ((line[0] == '#') || (line[0] == '\n') || (line[0] == '%'))
67 if (sscanf(line, "PERIODICITY " XBT_HEAP_FLOAT_T "\n", &(periodicity))
69 if (periodicity <= 0) {
70 CRITICAL2("%s,%d: Syntax error. Periodicity has to be positive\n",
78 (line, XBT_HEAP_FLOAT_T " " XBT_MAXMIN_FLOAT_T "\n", &event.delta,
80 CRITICAL2("%s,%d: Syntax error\n", filename, linecount);
85 if ((last_event->delta = event.delta - last_event->delta) <= 0) {
86 CRITICAL2("%s,%d: Invalid trace value, events have to be sorted\n",
91 xbt_dynar_push(trace->event_list, &event);
92 last_event = xbt_dynar_get_ptr(trace->event_list,
93 xbt_dynar_length(trace->event_list) -
95 /* printf(XBT_HEAP_FLOAT_T " " XBT_MAXMIN_FLOAT_T "\n", event.delta, */
99 if (periodicity > 0) {
101 last_event->delta = periodicity;
105 trace_list = xbt_dict_new();
107 xbt_dict_set(trace_list, filename, (void *) trace, _tmgr_trace_free);
115 void tmgr_trace_free(tmgr_trace_t trace)
119 xbt_dynar_free(&(trace->event_list));
123 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t history, tmgr_trace_t trace,
124 xbt_heap_float_t start_time, int offset,
127 tmgr_trace_event_t trace_event = NULL;
130 trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
131 trace_event->trace = trace;
132 trace_event->idx = offset;
133 trace_event->resource = resource;
135 if (trace_event->idx >= xbt_dynar_length(trace->event_list))
138 xbt_heap_push(history->heap, trace_event, start_time);
143 xbt_heap_float_t tmgr_history_next_date(tmgr_history_t history)
145 if (xbt_heap_size(history->heap))
146 return (xbt_heap_maxkey(history->heap));
151 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t history,
152 xbt_heap_float_t date,
153 xbt_maxmin_float_t * value,
156 xbt_heap_float_t event_date = xbt_heap_maxkey(history->heap);
157 tmgr_trace_event_t trace_event = NULL;
158 tmgr_event_t event = NULL;
159 tmgr_trace_t trace = NULL;
161 if (event_date > date)
164 if (!(trace_event = xbt_heap_pop(history->heap)))
167 trace = trace_event->trace;
168 event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
171 *value = event->value;
172 *resource = trace_event->resource;
174 if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
175 xbt_heap_push(history->heap, trace_event, event_date + event->delta);
177 } else if (event->delta > 0) { /* Last element, checking for periodicity */
178 xbt_heap_push(history->heap, trace_event, event_date + event->delta);
179 trace_event->idx = 0;
180 } else { /* We don't need this trace_event anymore */
181 xbt_free(trace_event);
187 void tmgr_finalize(void)
189 xbt_dict_free(&trace_list);