3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "xbt/sysdep.h"
12 #include "trace_mgr_private.h"
13 #include "surf_private.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_trace, surf, "Surf trace management");
17 static xbt_dict_t trace_list = NULL;
19 tmgr_history_t tmgr_history_new(void)
23 h = xbt_new0(s_tmgr_history_t, 1);
25 h->heap = xbt_heap_new(8, xbt_free_f); /* Why 8 ? Well, why not... */
30 void tmgr_history_free(tmgr_history_t h)
32 xbt_heap_free(h->heap);
36 tmgr_trace_t tmgr_trace_new_from_string(const char *id, const char *input,
37 double periodicity, double timestep)
39 tmgr_trace_t trace = NULL;
42 tmgr_event_t last_event = NULL;
48 trace = xbt_dict_get_or_null(trace_list, id);
50 WARN1("Ignoring redefinition of trace %s", id);
55 xbt_assert1(periodicity >= 0,
56 "Invalid periodicity %lg (must be positive)", periodicity);
58 trace = xbt_new0(s_tmgr_trace_t, 1);
59 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
61 list = xbt_str_split(input, "\n\r");
63 xbt_dynar_foreach(list, cpt, val) {
65 xbt_str_trim(val, " \t\n\r\x0B");
66 if (val[0] == '#' || val[0] == '\0' || val[0] == '%')
69 if (sscanf(val, "PERIODICITY " "%lg" "\n", &periodicity) == 1)
72 if (sscanf(val, "TIMESTEP " "%lg" "\n", ×tep) == 1)
75 if (sscanf(val, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2)
77 ("%s:%d: Syntax error in trace\n%s", id, linecount, input));
80 if (last_event->delta > event.delta) {
82 ("%s:%d: Invalid trace: Events must be sorted, but time %lg > time %lg.\n%s",
83 id, linecount, last_event->delta, event.delta, input));
85 last_event->delta = event.delta - last_event->delta;
87 xbt_dynar_push(trace->event_list, &event);
89 xbt_dynar_get_ptr(trace->event_list,
90 xbt_dynar_length(trace->event_list) - 1);
93 last_event->delta = periodicity;
95 trace->timestep = timestep;
98 trace_list = xbt_dict_new();
100 xbt_dict_set(trace_list, id, (void *) trace,
101 (void (*)(void *)) tmgr_trace_free);
103 xbt_dynar_free(&list);
107 tmgr_trace_t tmgr_trace_new(const char *filename)
110 tmgr_trace_t trace = NULL;
112 if ((!filename) || (strcmp(filename, "") == 0))
116 trace = xbt_dict_get_or_null(trace_list, filename);
118 WARN1("Ignoring redefinition of trace %s", filename);
123 f = surf_fopen(filename, "r");
124 xbt_assert2(f!=NULL, "Cannot open file '%s' (path=%s)", filename,
125 xbt_str_join(surf_path,":"));
127 char *tstr = xbt_str_from_file(f);
129 trace = tmgr_trace_new_from_string(filename, tstr, 0., 10.);
135 tmgr_trace_t tmgr_empty_trace_new(void)
137 tmgr_trace_t trace = NULL;
138 s_tmgr_event_t event;
140 trace = xbt_new0(s_tmgr_trace_t, 1);
141 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
145 xbt_dynar_push(trace->event_list, &event);
150 void tmgr_trace_free(tmgr_trace_t trace)
154 xbt_dynar_free(&(trace->event_list));
158 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t h,
161 unsigned int offset, void *model)
163 tmgr_trace_event_t trace_event = NULL;
165 trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
166 trace_event->trace = trace;
167 trace_event->idx = offset;
168 trace_event->model = model;
170 xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)),
171 "You're referring to an event that does not exist!");
173 xbt_heap_push(h->heap, trace_event, start_time);
178 double tmgr_history_next_date(tmgr_history_t h)
180 if (xbt_heap_size(h->heap))
181 return (xbt_heap_maxkey(h->heap));
186 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t h,
191 double event_date = tmgr_history_next_date(h);
192 tmgr_trace_event_t trace_event = NULL;
193 tmgr_event_t event = NULL;
194 tmgr_trace_t trace = NULL;
196 if (event_date > date)
199 if (!(trace_event = xbt_heap_pop(h->heap)))
202 trace = trace_event->trace;
203 event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
205 *value = event->value;
206 *model = trace_event->model;
208 if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
209 xbt_heap_push(h->heap, trace_event, event_date + event->delta);
211 } else if (event->delta > 0) { /* Last element, checking for periodicity */
212 xbt_heap_push(h->heap, trace_event, event_date + event->delta);
213 trace_event->idx = 0;
214 } else { /* We don't need this trace_event anymore */
215 trace_event->free_me = 1;
221 void tmgr_finalize(void)
223 xbt_dict_free(&trace_list);
226 int tmgr_trace_event_free(tmgr_trace_event_t trace_event)
228 if (trace_event->free_me) {
229 xbt_free(trace_event);