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;
48 trace = xbt_dict_get_or_null(trace_list, id);
53 if (periodicity <= 0) {
54 xbt_assert1(0, "Periodicity has to be positive. Your value %lg", periodicity);
57 trace = xbt_new0(s_tmgr_trace_t, 1);
58 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
60 list = xbt_str_split(input,"\n\r");
63 xbt_dynar_foreach(list, cpt, val) {
65 xbt_str_trim(val, " \t\n\r\x0B");
66 if (strlen(val) > 0) {
67 if (sscanf(val, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) {
68 xbt_assert2(0, "%s\n%d: Syntax error", input, linecount);
71 if ((last_event->delta = event.delta - last_event->delta) <= 0) {
72 xbt_assert2(0, "%s\n%d: Invalid trace value, events have to be sorted", input, linecount);
75 xbt_dynar_push(trace->event_list, &event);
76 last_event = xbt_dynar_get_ptr(trace->event_list, xbt_dynar_length(trace->event_list) - 1);
77 if (periodicity > 0) {
79 last_event->delta = periodicity;
85 trace_list = xbt_dict_new();
87 xbt_dict_set(trace_list, id, (void *) trace, _tmgr_trace_free);
89 xbt_dynar_free(&list);
93 tmgr_trace_t tmgr_trace_new(const char *filename)
95 tmgr_trace_t trace = NULL;
99 double periodicity = -1.0; /* No periodicity by default */
100 s_tmgr_event_t event;
101 tmgr_event_t last_event = NULL;
104 trace = xbt_dict_get_or_null(trace_list, filename);
109 if ((f = surf_fopen(filename, "r")) == NULL) {
110 xbt_assert1(0, "Cannot open file '%s'", filename);
113 trace = xbt_new0(s_tmgr_trace_t, 1);
114 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
116 while (fgets(line, 256, f)) {
118 if ((line[0] == '#') || (line[0] == '\n') || (line[0] == '%'))
121 if (sscanf(line, "PERIODICITY " "%lg" "\n", &(periodicity))
123 if (periodicity <= 0) {
125 "%s,%d: Syntax error. Periodicity has to be positive",
126 filename, linecount);
132 (line, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) {
133 xbt_assert2(0, "%s,%d: Syntax error", filename, linecount);
137 if ((last_event->delta = event.delta - last_event->delta) <= 0) {
139 "%s,%d: Invalid trace value, events have to be sorted",
140 filename, linecount);
143 xbt_dynar_push(trace->event_list, &event);
144 last_event = xbt_dynar_get_ptr(trace->event_list,
145 xbt_dynar_length(trace->event_list) -
149 if (periodicity > 0) {
151 last_event->delta = periodicity;
155 trace_list = xbt_dict_new();
157 xbt_dict_set(trace_list, filename, (void *) trace, _tmgr_trace_free);
164 tmgr_trace_t tmgr_empty_trace_new(void)
166 tmgr_trace_t trace = NULL;
167 /*double periodicity = -1.0; No periodicity by default; unused variables
168 tmgr_event_t last_event = NULL; */
169 s_tmgr_event_t event;
171 trace = xbt_new0(s_tmgr_trace_t, 1);
172 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
176 xbt_dynar_push(trace->event_list, &event);
181 void tmgr_trace_free(tmgr_trace_t trace)
185 xbt_dynar_free(&(trace->event_list));
189 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t h,
191 double start_time, unsigned int offset,
194 tmgr_trace_event_t trace_event = NULL;
196 trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
197 trace_event->trace = trace;
198 trace_event->idx = offset;
199 trace_event->model = model;
201 xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)),
202 "You're refering to an event that does not exist!");
204 xbt_heap_push(h->heap, trace_event, start_time);
209 double tmgr_history_next_date(tmgr_history_t h)
211 if (xbt_heap_size(h->heap))
212 return (xbt_heap_maxkey(h->heap));
217 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t h,
222 double event_date = tmgr_history_next_date(h);
223 tmgr_trace_event_t trace_event = NULL;
224 tmgr_event_t event = NULL;
225 tmgr_trace_t trace = NULL;
227 if (event_date > date)
230 if (!(trace_event = xbt_heap_pop(h->heap)))
233 trace = trace_event->trace;
234 event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
236 *value = event->value;
237 *model = trace_event->model;
239 if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
240 xbt_heap_push(h->heap, trace_event, event_date + event->delta);
242 } else if (event->delta > 0) { /* Last element, checking for periodicity */
243 xbt_heap_push(h->heap, trace_event, event_date + event->delta);
244 trace_event->idx = 0;
245 } else { /* We don't need this trace_event anymore */
253 void tmgr_finalize(void)
255 xbt_dict_free(&trace_list);