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"
11 #include "trace_mgr_private.h"
12 #include "surf_private.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trace, surf,
16 "Logging specific to the SURF trace module");
18 static xbt_dict_t trace_list = NULL;
19 static void _tmgr_trace_free(void *trace)
21 tmgr_trace_free(trace);
24 tmgr_history_t tmgr_history_new(void)
28 h = xbt_new0(s_tmgr_history_t, 1);
30 h->heap = xbt_heap_new(8, free); /* Why 8 ? Well, why not... */
35 void tmgr_history_free(tmgr_history_t h)
37 xbt_heap_free(h->heap);
41 tmgr_trace_t tmgr_trace_new(const char *filename)
43 tmgr_trace_t trace = NULL;
47 double periodicity = -1.0; /* No periodicity by default */
49 tmgr_event_t last_event = NULL;
52 xbt_dict_get(trace_list, filename, (void **) &trace);
57 if ((f = surf_fopen(filename, "r")) == NULL) {
58 xbt_assert1(0, "Cannot open file '%s'", filename);
61 trace = xbt_new0(s_tmgr_trace_t, 1);
62 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
64 while (fgets(line, 256, f)) {
66 if ((line[0] == '#') || (line[0] == '\n') || (line[0] == '%'))
69 if (sscanf(line, "PERIODICITY " "%lg" "\n", &(periodicity))
71 if (periodicity <= 0) {
73 "%s,%d: Syntax error. Periodicity has to be positive",
80 (line, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) {
81 xbt_assert2(0, "%s,%d: Syntax error", filename, linecount);
85 if ((last_event->delta = event.delta - last_event->delta) <= 0) {
87 "%s,%d: Invalid trace value, events have to be sorted",
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) -
97 if (periodicity > 0) {
99 last_event->delta = periodicity;
103 trace_list = xbt_dict_new();
105 xbt_dict_set(trace_list, filename, (void *) trace, _tmgr_trace_free);
112 tmgr_trace_t tmgr_empty_trace_new(void)
114 tmgr_trace_t trace = NULL;
115 double periodicity = -1.0; /* No periodicity by default */
116 s_tmgr_event_t event;
117 tmgr_event_t last_event = NULL;
119 trace = xbt_new0(s_tmgr_trace_t, 1);
120 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
124 xbt_dynar_push(trace->event_list, &event);
129 void tmgr_trace_free(tmgr_trace_t trace)
133 xbt_dynar_free(&(trace->event_list));
137 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t h,
139 double start_time, int offset,
142 tmgr_trace_event_t trace_event = NULL;
144 trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
145 trace_event->trace = trace;
146 trace_event->idx = offset;
147 trace_event->resource = resource;
149 xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)),
150 "You're refering to an event that does not exist!");
152 xbt_heap_push(h->heap, trace_event, start_time);
157 double tmgr_history_next_date(tmgr_history_t h)
159 if (xbt_heap_size(h->heap))
160 return (xbt_heap_maxkey(h->heap));
165 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t h,
170 double event_date = tmgr_history_next_date(h);
171 tmgr_trace_event_t trace_event = NULL;
172 tmgr_event_t event = NULL;
173 tmgr_trace_t trace = NULL;
175 if (event_date > date)
178 if (!(trace_event = xbt_heap_pop(h->heap)))
181 trace = trace_event->trace;
182 event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
184 *value = event->value;
185 *resource = trace_event->resource;
187 if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
188 xbt_heap_push(h->heap, trace_event, event_date + event->delta);
190 } else if (event->delta > 0) { /* Last element, checking for periodicity */
191 xbt_heap_push(h->heap, trace_event, event_date + event->delta);
192 trace_event->idx = 0;
193 } else { /* We don't need this trace_event anymore */
200 void tmgr_finalize(void)
202 xbt_dict_free(&trace_list);