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 static xbt_dict_t trace_list = NULL;
16 static void _tmgr_trace_free(void *trace)
18 tmgr_trace_free(trace);
21 tmgr_history_t tmgr_history_new(void)
25 h = xbt_new0(s_tmgr_history_t, 1);
27 h->heap = xbt_heap_new(8, xbt_free_f); /* Why 8 ? Well, why not... */
32 void tmgr_history_free(tmgr_history_t h)
34 xbt_heap_free(h->heap);
38 tmgr_trace_t tmgr_trace_new_from_string(const char *id, const char *input,
41 tmgr_trace_t trace = NULL;
44 tmgr_event_t last_event = NULL;
50 trace = xbt_dict_get_or_null(trace_list, id);
55 if (periodicity <= 0) {
56 xbt_assert1(0, "Periodicity has to be positive. Your value %lg",
60 trace = xbt_new0(s_tmgr_trace_t, 1);
61 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
63 list = xbt_str_split(input, "\n\r");
66 xbt_dynar_foreach(list, cpt, val) {
68 xbt_str_trim(val, " \t\n\r\x0B");
69 if (strlen(val) > 0) {
70 if (sscanf(val, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) {
71 xbt_assert2(0, "%s\n%d: Syntax error", input, linecount);
74 if ((last_event->delta = event.delta - last_event->delta) <= 0) {
76 "%s\n%d: Invalid trace value, events have to be sorted",
80 xbt_dynar_push(trace->event_list, &event);
82 xbt_dynar_get_ptr(trace->event_list,
83 xbt_dynar_length(trace->event_list) - 1);
84 if (periodicity > 0) {
86 last_event->delta = periodicity;
92 trace_list = xbt_dict_new();
94 xbt_dict_set(trace_list, id, (void *) trace, _tmgr_trace_free);
96 xbt_dynar_free(&list);
100 tmgr_trace_t tmgr_trace_new(const char *filename)
102 tmgr_trace_t trace = NULL;
106 double periodicity = -1.0; /* No periodicity by default */
107 s_tmgr_event_t event;
108 tmgr_event_t last_event = NULL;
111 trace = xbt_dict_get_or_null(trace_list, filename);
116 if ((f = surf_fopen(filename, "r")) == NULL) {
117 xbt_assert1(0, "Cannot open file '%s'", filename);
120 trace = xbt_new0(s_tmgr_trace_t, 1);
121 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
123 while (fgets(line, 256, f)) {
125 if ((line[0] == '#') || (line[0] == '\n') || (line[0] == '%'))
128 if (sscanf(line, "PERIODICITY " "%lg" "\n", &(periodicity))
130 if (periodicity <= 0) {
132 "%s,%d: Syntax error. Periodicity has to be positive",
133 filename, linecount);
138 if (sscanf(line, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) {
139 xbt_assert2(0, "%s,%d: Syntax error", filename, linecount);
143 if ((last_event->delta = event.delta - last_event->delta) <= 0) {
145 "%s,%d: Invalid trace value, events have to be sorted",
146 filename, linecount);
149 xbt_dynar_push(trace->event_list, &event);
150 last_event = xbt_dynar_get_ptr(trace->event_list,
151 xbt_dynar_length(trace->event_list) - 1);
154 if (periodicity > 0) {
156 last_event->delta = periodicity;
160 trace_list = xbt_dict_new();
162 xbt_dict_set(trace_list, filename, (void *) trace, _tmgr_trace_free);
169 tmgr_trace_t tmgr_empty_trace_new(void)
171 tmgr_trace_t trace = NULL;
172 /*double periodicity = -1.0; No periodicity by default; unused variables
173 tmgr_event_t last_event = NULL; */
174 s_tmgr_event_t event;
176 trace = xbt_new0(s_tmgr_trace_t, 1);
177 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
181 xbt_dynar_push(trace->event_list, &event);
186 void tmgr_trace_free(tmgr_trace_t trace)
190 xbt_dynar_free(&(trace->event_list));
194 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t h,
197 unsigned int offset, void *model)
199 tmgr_trace_event_t trace_event = NULL;
201 trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
202 trace_event->trace = trace;
203 trace_event->idx = offset;
204 trace_event->model = model;
206 xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)),
207 "You're referring to an event that does not exist!");
209 xbt_heap_push(h->heap, trace_event, start_time);
214 double tmgr_history_next_date(tmgr_history_t h)
216 if (xbt_heap_size(h->heap))
217 return (xbt_heap_maxkey(h->heap));
222 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t h,
227 double event_date = tmgr_history_next_date(h);
228 tmgr_trace_event_t trace_event = NULL;
229 tmgr_event_t event = NULL;
230 tmgr_trace_t trace = NULL;
232 if (event_date > date)
235 if (!(trace_event = xbt_heap_pop(h->heap)))
238 trace = trace_event->trace;
239 event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
241 *value = event->value;
242 *model = trace_event->model;
244 if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
245 xbt_heap_push(h->heap, trace_event, event_date + event->delta);
247 } else if (event->delta > 0) { /* Last element, checking for periodicity */
248 xbt_heap_push(h->heap, trace_event, event_date + event->delta);
249 trace_event->idx = 0;
250 } else { /* We don't need this trace_event anymore */
258 void tmgr_finalize(void)
260 xbt_dict_free(&trace_list);