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"
14 static xbt_dict_t trace_list = NULL;
15 static void _tmgr_trace_free(void *trace)
17 tmgr_trace_free(trace);
20 tmgr_history_t tmgr_history_new(void)
24 h = xbt_new0(s_tmgr_history_t, 1);
26 h->heap = xbt_heap_new(8, xbt_free_f); /* Why 8 ? Well, why not... */
31 void tmgr_history_free(tmgr_history_t h)
33 xbt_heap_free(h->heap);
37 tmgr_trace_t tmgr_trace_new(const char *filename)
39 tmgr_trace_t trace = NULL;
43 double periodicity = -1.0; /* No periodicity by default */
45 tmgr_event_t last_event = NULL;
48 trace = xbt_dict_get_or_null(trace_list, filename);
53 if ((f = surf_fopen(filename, "r")) == NULL) {
54 xbt_assert1(0, "Cannot open file '%s'", filename);
57 trace = xbt_new0(s_tmgr_trace_t, 1);
58 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
60 while (fgets(line, 256, f)) {
62 if ((line[0] == '#') || (line[0] == '\n') || (line[0] == '%'))
65 if (sscanf(line, "PERIODICITY " "%lg" "\n", &(periodicity))
67 if (periodicity <= 0) {
69 "%s,%d: Syntax error. Periodicity has to be positive",
76 (line, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) {
77 xbt_assert2(0, "%s,%d: Syntax error", filename, linecount);
81 if ((last_event->delta = event.delta - last_event->delta) <= 0) {
83 "%s,%d: Invalid trace value, events have to be sorted",
87 xbt_dynar_push(trace->event_list, &event);
88 last_event = xbt_dynar_get_ptr(trace->event_list,
89 xbt_dynar_length(trace->event_list) -
93 if (periodicity > 0) {
95 last_event->delta = periodicity;
99 trace_list = xbt_dict_new();
101 xbt_dict_set(trace_list, filename, (void *) trace, _tmgr_trace_free);
108 tmgr_trace_t tmgr_empty_trace_new(void)
110 tmgr_trace_t trace = NULL;
111 /*double periodicity = -1.0; No periodicity by default; unused variables
112 tmgr_event_t last_event = NULL;*/
113 s_tmgr_event_t event;
115 trace = xbt_new0(s_tmgr_trace_t, 1);
116 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
120 xbt_dynar_push(trace->event_list, &event);
125 void tmgr_trace_free(tmgr_trace_t trace)
129 xbt_dynar_free(&(trace->event_list));
133 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t h,
135 double start_time, int offset,
138 tmgr_trace_event_t trace_event = NULL;
140 trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
141 trace_event->trace = trace;
142 trace_event->idx = offset;
143 trace_event->resource = resource;
145 xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)),
146 "You're refering to an event that does not exist!");
148 xbt_heap_push(h->heap, trace_event, start_time);
153 double tmgr_history_next_date(tmgr_history_t h)
155 if (xbt_heap_size(h->heap))
156 return (xbt_heap_maxkey(h->heap));
161 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t h,
166 double event_date = tmgr_history_next_date(h);
167 tmgr_trace_event_t trace_event = NULL;
168 tmgr_event_t event = NULL;
169 tmgr_trace_t trace = NULL;
171 if (event_date > date)
174 if (!(trace_event = xbt_heap_pop(h->heap)))
177 trace = trace_event->trace;
178 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(h->heap, trace_event, event_date + event->delta);
186 } else if (event->delta > 0) { /* Last element, checking for periodicity */
187 xbt_heap_push(h->heap, trace_event, event_date + event->delta);
188 trace_event->idx = 0;
189 } else { /* We don't need this trace_event anymore */
196 void tmgr_finalize(void)
198 xbt_dict_free(&trace_list);