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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_trace, surf,"Surf trace management");
17 static xbt_dict_t trace_list = NULL;
18 static void _tmgr_trace_free(void *trace)
20 tmgr_trace_free(trace);
23 tmgr_history_t tmgr_history_new(void)
27 h = xbt_new0(s_tmgr_history_t, 1);
29 h->heap = xbt_heap_new(8, xbt_free_f); /* Why 8 ? Well, why not... */
34 void tmgr_history_free(tmgr_history_t h)
36 xbt_heap_free(h->heap);
40 tmgr_trace_t tmgr_trace_new_from_string(const char *id, const char *input,
43 tmgr_trace_t trace = NULL;
46 tmgr_event_t last_event = NULL;
52 trace = xbt_dict_get_or_null(trace_list, id);
57 if (periodicity <= 0) {
58 xbt_assert1(0, "Periodicity has to be positive. Your value %lg",
62 trace = xbt_new0(s_tmgr_trace_t, 1);
63 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
65 list = xbt_str_split(input, "\n\r");
68 xbt_dynar_foreach(list, cpt, val) {
70 xbt_str_trim(val, " \t\n\r\x0B");
71 if (strlen(val) > 0) {
72 if (sscanf(val, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) {
73 xbt_assert2(0, "%s\n%d: Syntax error", input, linecount);
76 if ((last_event->delta = event.delta - last_event->delta) <= 0) {
78 "%s\n%d: Invalid trace value, events have to be sorted",
82 xbt_dynar_push(trace->event_list, &event);
84 xbt_dynar_get_ptr(trace->event_list,
85 xbt_dynar_length(trace->event_list) - 1);
86 if (periodicity > 0) {
88 last_event->delta = periodicity;
94 trace_list = xbt_dict_new();
96 xbt_dict_set(trace_list, id, (void *) trace, _tmgr_trace_free);
98 xbt_dynar_free(&list);
102 tmgr_trace_t tmgr_trace_new(const char *filename)
104 tmgr_trace_t trace = NULL;
108 double periodicity = -1.0; /* No periodicity by default */
109 s_tmgr_event_t event;
110 tmgr_event_t last_event = NULL;
112 if ((!filename) || (strcmp(filename, "") == 0))
116 trace = xbt_dict_get_or_null(trace_list, filename);
118 WARN1("Ignoring redefinition of trace %s",filename);
123 f = surf_fopen(filename, "r");
124 xbt_assert1(f!=NULL, "Cannot open file '%s'", filename);
126 trace = xbt_new0(s_tmgr_trace_t, 1);
127 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
129 while (fgets(line, 256, f)) {
131 if ((line[0] == '#') || (line[0] == '\n') || (line[0] == '%'))
134 if (sscanf(line, "PERIODICITY " "%lg" "\n", &(periodicity))
136 if (periodicity <= 0) {
138 "%s,%d: Syntax error. Periodicity has to be positive",
139 filename, linecount);
144 if (sscanf(line, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) {
145 xbt_assert2(0, "%s,%d: Syntax error", filename, linecount);
149 if ((last_event->delta = event.delta - last_event->delta) <= 0) {
151 "%s,%d: Invalid trace value, events have to be sorted",
152 filename, linecount);
155 xbt_dynar_push(trace->event_list, &event);
156 last_event = xbt_dynar_get_ptr(trace->event_list,
157 xbt_dynar_length(trace->event_list) - 1);
160 if (periodicity > 0) {
162 last_event->delta = periodicity;
166 trace_list = xbt_dict_new();
168 xbt_dict_set(trace_list, filename, (void *) trace, _tmgr_trace_free);
175 tmgr_trace_t tmgr_empty_trace_new(void)
177 tmgr_trace_t trace = NULL;
178 /*double periodicity = -1.0; No periodicity by default; unused variables
179 tmgr_event_t last_event = NULL; */
180 s_tmgr_event_t event;
182 trace = xbt_new0(s_tmgr_trace_t, 1);
183 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
187 xbt_dynar_push(trace->event_list, &event);
192 void tmgr_trace_free(tmgr_trace_t trace)
196 xbt_dynar_free(&(trace->event_list));
200 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t h,
203 unsigned int offset, void *model)
205 tmgr_trace_event_t trace_event = NULL;
207 trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
208 trace_event->trace = trace;
209 trace_event->idx = offset;
210 trace_event->model = model;
212 xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)),
213 "You're referring to an event that does not exist!");
215 xbt_heap_push(h->heap, trace_event, start_time);
220 double tmgr_history_next_date(tmgr_history_t h)
222 if (xbt_heap_size(h->heap))
223 return (xbt_heap_maxkey(h->heap));
228 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t h,
233 double event_date = tmgr_history_next_date(h);
234 tmgr_trace_event_t trace_event = NULL;
235 tmgr_event_t event = NULL;
236 tmgr_trace_t trace = NULL;
238 if (event_date > date)
241 if (!(trace_event = xbt_heap_pop(h->heap)))
244 trace = trace_event->trace;
245 event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
247 *value = event->value;
248 *model = trace_event->model;
250 if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
251 xbt_heap_push(h->heap, trace_event, event_date + event->delta);
253 } else if (event->delta > 0) { /* Last element, checking for periodicity */
254 xbt_heap_push(h->heap, trace_event, event_date + event->delta);
255 trace_event->idx = 0;
256 } else { /* We don't need this trace_event anymore */
264 void tmgr_finalize(void)
266 xbt_dict_free(&trace_list);