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_from_string(const char* id, const char *input, double periodicity)
39 tmgr_trace_t trace = NULL;
42 tmgr_event_t last_event = NULL;
45 trace = xbt_dict_get_or_null(trace_list, id);
50 if (periodicity <= 0) {
51 xbt_assert1(0, "Periodicity has to be positive. Your value %lg", periodicity);
54 trace = xbt_new0(s_tmgr_trace_t, 1);
55 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
57 xbt_dynar_t list = xbt_str_split(input,"\n\r");
61 xbt_dynar_foreach(list, cpt, val) {
63 xbt_str_trim(val, " \t\n\r\x0B");
64 if (strlen(val) > 0) {
65 if (sscanf(val, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) {
66 xbt_assert2(0, "%s\n%d: Syntax error", input, linecount);
69 if ((last_event->delta = event.delta - last_event->delta) <= 0) {
70 xbt_assert2(0, "%s\n%d: Invalid trace value, events have to be sorted", input, linecount);
73 xbt_dynar_push(trace->event_list, &event);
74 last_event = xbt_dynar_get_ptr(trace->event_list, xbt_dynar_length(trace->event_list) - 1);
75 if (periodicity > 0) {
77 last_event->delta = periodicity;
83 trace_list = xbt_dict_new();
85 xbt_dict_set(trace_list, id, (void *) trace, _tmgr_trace_free);
87 xbt_dynar_free(&list);
91 tmgr_trace_t tmgr_trace_new(const char *filename)
93 tmgr_trace_t trace = NULL;
97 double periodicity = -1.0; /* No periodicity by default */
99 tmgr_event_t last_event = NULL;
102 trace = xbt_dict_get_or_null(trace_list, filename);
107 if ((f = surf_fopen(filename, "r")) == NULL) {
108 xbt_assert1(0, "Cannot open file '%s'", filename);
111 trace = xbt_new0(s_tmgr_trace_t, 1);
112 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
114 while (fgets(line, 256, f)) {
116 if ((line[0] == '#') || (line[0] == '\n') || (line[0] == '%'))
119 if (sscanf(line, "PERIODICITY " "%lg" "\n", &(periodicity))
121 if (periodicity <= 0) {
123 "%s,%d: Syntax error. Periodicity has to be positive",
124 filename, linecount);
130 (line, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) {
131 xbt_assert2(0, "%s,%d: Syntax error", filename, linecount);
135 if ((last_event->delta = event.delta - last_event->delta) <= 0) {
137 "%s,%d: Invalid trace value, events have to be sorted",
138 filename, linecount);
141 xbt_dynar_push(trace->event_list, &event);
142 last_event = xbt_dynar_get_ptr(trace->event_list,
143 xbt_dynar_length(trace->event_list) -
147 if (periodicity > 0) {
149 last_event->delta = periodicity;
153 trace_list = xbt_dict_new();
155 xbt_dict_set(trace_list, filename, (void *) trace, _tmgr_trace_free);
162 tmgr_trace_t tmgr_empty_trace_new(void)
164 tmgr_trace_t trace = NULL;
165 /*double periodicity = -1.0; No periodicity by default; unused variables
166 tmgr_event_t last_event = NULL; */
167 s_tmgr_event_t event;
169 trace = xbt_new0(s_tmgr_trace_t, 1);
170 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
174 xbt_dynar_push(trace->event_list, &event);
179 void tmgr_trace_free(tmgr_trace_t trace)
183 xbt_dynar_free(&(trace->event_list));
187 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t h,
189 double start_time, unsigned int offset,
192 tmgr_trace_event_t trace_event = NULL;
194 trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
195 trace_event->trace = trace;
196 trace_event->idx = offset;
197 trace_event->model = model;
199 xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)),
200 "You're refering to an event that does not exist!");
202 xbt_heap_push(h->heap, trace_event, start_time);
207 double tmgr_history_next_date(tmgr_history_t h)
209 if (xbt_heap_size(h->heap))
210 return (xbt_heap_maxkey(h->heap));
215 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t h,
220 double event_date = tmgr_history_next_date(h);
221 tmgr_trace_event_t trace_event = NULL;
222 tmgr_event_t event = NULL;
223 tmgr_trace_t trace = NULL;
225 if (event_date > date)
228 if (!(trace_event = xbt_heap_pop(h->heap)))
231 trace = trace_event->trace;
232 event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
234 *value = event->value;
235 *model = trace_event->model;
237 if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
238 xbt_heap_push(h->heap, trace_event, event_date + event->delta);
240 } else if (event->delta > 0) { /* Last element, checking for periodicity */
241 xbt_heap_push(h->heap, trace_event, event_date + event->delta);
242 trace_event->idx = 0;
243 } else { /* We don't need this trace_event anymore */
251 void tmgr_finalize(void)
253 xbt_dict_free(&trace_list);