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 static xbt_dict_t trace_list = NULL;
13 static void _tmgr_trace_free(void *trace)
15 tmgr_trace_free(trace);
18 tmgr_history_t tmgr_history_new(void)
20 tmgr_history_t history;
22 history = xbt_new0(s_tmgr_history_t, 1);
24 history->heap = xbt_heap_new(8, xbt_free); /* Why 8 ? Well, why not... */
29 void tmgr_history_free(tmgr_history_t history)
31 xbt_heap_free(history->heap);
35 tmgr_trace_t tmgr_trace_new(const char *filename)
37 tmgr_trace_t trace = NULL;
41 xbt_heap_float_t current_time = 0.0, previous_time = 0.0;
42 xbt_maxmin_float_t value = -1.0;
43 xbt_heap_float_t periodicity = -1.0; /* No periodicity by default */
45 tmgr_event_t last_event = NULL;
48 xbt_dict_get(trace_list, filename, (void **) &trace);
53 /* Parsing et création de la trace */
55 if ((f = fopen(filename, "r")) == NULL) {
56 fprintf(stderr, "Cannot open file '%s'\n", filename);
60 trace = xbt_new0(s_tmgr_trace_t, 1);
61 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
63 while (fgets(line, 256, f)) {
65 if ((line[0] == '#') || (line[0] == '\n') || (line[0] == '%'))
68 if (sscanf(line, "PERIODICITY " XBT_HEAP_FLOAT_T "\n", &(periodicity))
70 if (periodicity <= 0) {
72 "%s,%d: Syntax error. Periodicity has to be positive\n",
75 /* xbt_dynar_free(&(trace->event_list)); */
76 /* xbt_free(trace); */
83 (line, XBT_HEAP_FLOAT_T " " XBT_MAXMIN_FLOAT_T "\n", &event.delta,
85 fprintf(stderr, "%s,%d: Syntax error\n", filename, linecount);
87 /* xbt_dynar_free(&(trace->event_list)); */
88 /* xbt_free(trace); */
93 if ((last_event->delta = event.delta - last_event->delta) <= 0) {
95 "%s,%d: Invalid trace value, events have to be sorted\n",
100 xbt_dynar_push(trace->event_list, &event);
101 last_event = xbt_dynar_get_ptr(trace->event_list,
102 xbt_dynar_length(trace->event_list) -
104 /* printf(XBT_HEAP_FLOAT_T " " XBT_MAXMIN_FLOAT_T "\n", event.delta, */
108 if (periodicity > 0) {
110 last_event->delta = periodicity;
114 trace_list = xbt_dict_new();
116 xbt_dict_set(trace_list, filename, (void *) trace, _tmgr_trace_free);
124 void tmgr_trace_free(tmgr_trace_t trace)
128 xbt_dynar_free(&(trace->event_list));
132 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t history, tmgr_trace_t trace,
133 xbt_heap_float_t start_time, int offset,
136 tmgr_trace_event_t trace_event = NULL;
139 trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
140 trace_event->trace = trace;
141 trace_event->idx = offset;
142 trace_event->resource = resource;
144 if (trace_event->idx >= xbt_dynar_length(trace->event_list))
147 xbt_heap_push(history->heap, trace_event, start_time);
152 xbt_heap_float_t tmgr_history_next_date(tmgr_history_t history)
154 if (xbt_heap_size(history->heap))
155 return (xbt_heap_maxkey(history->heap));
160 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t history,
161 xbt_heap_float_t date,
162 xbt_maxmin_float_t * value,
165 xbt_heap_float_t event_date = xbt_heap_maxkey(history->heap);
166 tmgr_trace_event_t trace_event = NULL;
167 tmgr_event_t event = NULL;
168 tmgr_trace_t trace = NULL;
170 if (event_date > date)
173 if (!(trace_event = xbt_heap_pop(history->heap)))
176 trace = trace_event->trace;
177 event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
180 *value = event->value;
181 *resource = trace_event->resource;
183 if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
184 xbt_heap_push(history->heap, trace_event, event_date + event->delta);
186 } else if (event->delta > 0) { /* Last element, checking for periodicity */
187 xbt_heap_push(history->heap, trace_event, event_date + event->delta);
188 trace_event->idx = 0;
189 } else { /* We don't need this trace_event anymore */
190 xbt_free(trace_event);