1 /* Copyright (c) 2004, 2005, 2007, 2009, 2010. 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 "xbt/sysdep.h"
11 #include "trace_mgr_private.h"
12 #include "surf_private.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_trace, surf, "Surf trace management");
16 static xbt_dict_t trace_list = NULL;
18 XBT_INLINE tmgr_history_t tmgr_history_new(void)
22 h = xbt_new0(s_tmgr_history_t, 1);
24 h->heap = xbt_heap_new(8, xbt_free_f); /* Why 8 ? Well, why not... */
29 XBT_INLINE void tmgr_history_free(tmgr_history_t h)
31 xbt_heap_free(h->heap);
35 tmgr_trace_t tmgr_trace_new_from_string(const char *id, const char *input,
38 tmgr_trace_t trace = NULL;
41 tmgr_event_t last_event = NULL;
47 trace = xbt_dict_get_or_null(trace_list, id);
49 XBT_WARN("Ignoring redefinition of trace %s", id);
54 xbt_assert1(periodicity >= 0,
55 "Invalid periodicity %lg (must be positive)", periodicity);
57 trace = xbt_new0(s_tmgr_trace_t, 1);
58 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
60 list = xbt_str_split(input, "\n\r");
62 xbt_dynar_foreach(list, cpt, val) {
64 xbt_str_trim(val, " \t\n\r\x0B");
65 if (val[0] == '#' || val[0] == '\0' || val[0] == '%')
68 if (sscanf(val, "PERIODICITY " "%lg" "\n", &periodicity) == 1)
71 if (sscanf(val, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2)
73 ("%s:%d: Syntax error in trace\n%s", id, linecount, input));
76 if (last_event->delta > event.delta) {
78 ("%s:%d: Invalid trace: Events must be sorted, but time %lg > time %lg.\n%s",
79 id, linecount, last_event->delta, event.delta, input));
81 last_event->delta = event.delta - last_event->delta;
83 xbt_dynar_push(trace->event_list, &event);
85 xbt_dynar_get_ptr(trace->event_list,
86 xbt_dynar_length(trace->event_list) - 1);
89 last_event->delta = periodicity;
92 trace_list = xbt_dict_new();
94 xbt_dict_set(trace_list, id, (void *) trace,
95 (void (*)(void *)) tmgr_trace_free);
97 xbt_dynar_free(&list);
101 tmgr_trace_t tmgr_trace_new(const char *filename)
105 tmgr_trace_t trace = NULL;
107 if ((!filename) || (strcmp(filename, "") == 0))
111 trace = xbt_dict_get_or_null(trace_list, filename);
113 XBT_WARN("Ignoring redefinition of trace %s", filename);
118 f = surf_fopen(filename, "r");
119 xbt_assert2(f != NULL, "Cannot open file '%s' (path=%s)", filename,
120 xbt_str_join(surf_path, ":"));
122 tstr = xbt_str_from_file(f);
124 trace = tmgr_trace_new_from_string(filename, tstr, 0.);
130 tmgr_trace_t tmgr_empty_trace_new(void)
132 tmgr_trace_t trace = NULL;
133 s_tmgr_event_t event;
135 trace = xbt_new0(s_tmgr_trace_t, 1);
136 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
140 xbt_dynar_push(trace->event_list, &event);
145 XBT_INLINE void tmgr_trace_free(tmgr_trace_t trace)
149 xbt_dynar_free(&(trace->event_list));
153 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t h,
156 unsigned int offset, void *model)
158 tmgr_trace_event_t trace_event = NULL;
160 trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
161 trace_event->trace = trace;
162 trace_event->idx = offset;
163 trace_event->model = model;
165 xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)),
166 "You're referring to an event that does not exist!");
168 xbt_heap_push(h->heap, trace_event, start_time);
173 XBT_INLINE double tmgr_history_next_date(tmgr_history_t h)
175 if (xbt_heap_size(h->heap))
176 return (xbt_heap_maxkey(h->heap));
181 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t h,
186 double event_date = tmgr_history_next_date(h);
187 tmgr_trace_event_t trace_event = NULL;
188 tmgr_event_t event = NULL;
189 tmgr_trace_t trace = NULL;
191 if (event_date > date)
194 if (!(trace_event = xbt_heap_pop(h->heap)))
197 trace = trace_event->trace;
198 event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
200 *value = event->value;
201 *model = trace_event->model;
203 if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
204 xbt_heap_push(h->heap, trace_event, event_date + event->delta);
206 } else if (event->delta > 0) { /* Last element, checking for periodicity */
207 xbt_heap_push(h->heap, trace_event, event_date + event->delta);
208 trace_event->idx = 0;
209 } else { /* We don't need this trace_event anymore */
210 trace_event->free_me = 1;
216 XBT_INLINE void tmgr_finalize(void)
218 xbt_dict_free(&trace_list);
221 int tmgr_trace_event_free(tmgr_trace_event_t trace_event)
223 if (trace_event->free_me) {
224 xbt_free(trace_event);