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, double periodicity)
40 tmgr_trace_t trace = NULL;
43 tmgr_event_t last_event = NULL;
49 trace = xbt_dict_get_or_null(trace_list, id);
54 if (periodicity <= 0) {
55 xbt_assert1(0, "Periodicity has to be positive. Your value %lg", periodicity);
58 trace = xbt_new0(s_tmgr_trace_t, 1);
59 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
61 list = xbt_str_split(input,"\n\r");
64 xbt_dynar_foreach(list, cpt, val) {
66 xbt_str_trim(val, " \t\n\r\x0B");
67 if (strlen(val) > 0) {
68 if (sscanf(val, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) {
69 xbt_assert2(0, "%s\n%d: Syntax error", input, linecount);
72 if ((last_event->delta = event.delta - last_event->delta) <= 0) {
73 xbt_assert2(0, "%s\n%d: Invalid trace value, events have to be sorted", input, linecount);
76 xbt_dynar_push(trace->event_list, &event);
77 last_event = xbt_dynar_get_ptr(trace->event_list, xbt_dynar_length(trace->event_list) - 1);
78 if (periodicity > 0) {
80 last_event->delta = periodicity;
86 trace_list = xbt_dict_new();
88 xbt_dict_set(trace_list, id, (void *) trace, _tmgr_trace_free);
90 xbt_dynar_free(&list);
94 tmgr_trace_t tmgr_trace_new(const char *filename)
96 tmgr_trace_t trace = NULL;
100 double periodicity = -1.0; /* No periodicity by default */
101 s_tmgr_event_t event;
102 tmgr_event_t last_event = NULL;
105 trace = xbt_dict_get_or_null(trace_list, filename);
110 if ((f = surf_fopen(filename, "r")) == NULL) {
111 xbt_assert1(0, "Cannot open file '%s'", filename);
114 trace = xbt_new0(s_tmgr_trace_t, 1);
115 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
117 while (fgets(line, 256, f)) {
119 if ((line[0] == '#') || (line[0] == '\n') || (line[0] == '%'))
122 if (sscanf(line, "PERIODICITY " "%lg" "\n", &(periodicity))
124 if (periodicity <= 0) {
126 "%s,%d: Syntax error. Periodicity has to be positive",
127 filename, linecount);
133 (line, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) {
134 xbt_assert2(0, "%s,%d: Syntax error", filename, linecount);
138 if ((last_event->delta = event.delta - last_event->delta) <= 0) {
140 "%s,%d: Invalid trace value, events have to be sorted",
141 filename, linecount);
144 xbt_dynar_push(trace->event_list, &event);
145 last_event = xbt_dynar_get_ptr(trace->event_list,
146 xbt_dynar_length(trace->event_list) -
150 if (periodicity > 0) {
152 last_event->delta = periodicity;
156 trace_list = xbt_dict_new();
158 xbt_dict_set(trace_list, filename, (void *) trace, _tmgr_trace_free);
165 tmgr_trace_t tmgr_empty_trace_new(void)
167 tmgr_trace_t trace = NULL;
168 /*double periodicity = -1.0; No periodicity by default; unused variables
169 tmgr_event_t last_event = NULL; */
170 s_tmgr_event_t event;
172 trace = xbt_new0(s_tmgr_trace_t, 1);
173 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
177 xbt_dynar_push(trace->event_list, &event);
182 void tmgr_trace_free(tmgr_trace_t trace)
186 xbt_dynar_free(&(trace->event_list));
190 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t h,
192 double start_time, unsigned int offset,
195 tmgr_trace_event_t trace_event = NULL;
197 trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
198 trace_event->trace = trace;
199 trace_event->idx = offset;
200 trace_event->model = model;
202 xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)),
203 "You're refering to an event that does not exist!");
205 xbt_heap_push(h->heap, trace_event, start_time);
210 double tmgr_history_next_date(tmgr_history_t h)
212 if (xbt_heap_size(h->heap))
213 return (xbt_heap_maxkey(h->heap));
218 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t h,
223 double event_date = tmgr_history_next_date(h);
224 tmgr_trace_event_t trace_event = NULL;
225 tmgr_event_t event = NULL;
226 tmgr_trace_t trace = NULL;
228 if (event_date > date)
231 if (!(trace_event = xbt_heap_pop(h->heap)))
234 trace = trace_event->trace;
235 event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
237 *value = event->value;
238 *model = trace_event->model;
240 if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
241 xbt_heap_push(h->heap, trace_event, event_date + event->delta);
243 } else if (event->delta > 0) { /* Last element, checking for periodicity */
244 xbt_heap_push(h->heap, trace_event, event_date + event->delta);
245 trace_event->idx = 0;
246 } else { /* We don't need this trace_event anymore */
254 void tmgr_finalize(void)
256 xbt_dict_free(&trace_list);