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"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trace, surf,
15 "Logging specific to the SURF trace module");
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)
25 tmgr_history_t history;
27 history = xbt_new0(s_tmgr_history_t, 1);
29 history->heap = xbt_heap_new(8, xbt_free); /* Why 8 ? Well, why not... */
34 void tmgr_history_free(tmgr_history_t history)
36 xbt_heap_free(history->heap);
40 tmgr_trace_t tmgr_trace_new(const char *filename)
42 tmgr_trace_t trace = NULL;
46 double periodicity = -1.0; /* No periodicity by default */
48 tmgr_event_t last_event = NULL;
51 xbt_dict_get(trace_list, filename, (void **) &trace);
56 if ((f = fopen(filename, "r")) == NULL) {
57 xbt_assert1(0, "Cannot open file '%s'", filename);
60 trace = xbt_new0(s_tmgr_trace_t, 1);
61 trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
63 while (fgets(line, 256, f)) {
65 if ((line[0] == '#') || (line[0] == '\n') || (line[0] == '%'))
68 if (sscanf(line, "PERIODICITY " "%lg" "\n", &(periodicity))
70 if (periodicity <= 0) {
72 "%s,%d: Syntax error. Periodicity has to be positive",
79 (line, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) {
80 xbt_assert2(0, "%s,%d: Syntax error", filename, linecount);
84 if ((last_event->delta = event.delta - last_event->delta) <= 0) {
86 "%s,%d: Invalid trace value, events have to be sorted",
90 xbt_dynar_push(trace->event_list, &event);
91 last_event = xbt_dynar_get_ptr(trace->event_list,
92 xbt_dynar_length(trace->event_list) -
96 if (periodicity > 0) {
98 last_event->delta = periodicity;
102 trace_list = xbt_dict_new();
104 xbt_dict_set(trace_list, filename, (void *) trace, _tmgr_trace_free);
112 void tmgr_trace_free(tmgr_trace_t trace)
116 xbt_dynar_free(&(trace->event_list));
120 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t history,
122 double start_time, int offset,
125 tmgr_trace_event_t trace_event = NULL;
128 trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
129 trace_event->trace = trace;
130 trace_event->idx = offset;
131 trace_event->resource = resource;
133 xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)),
134 "You're refering to an event that does not exist!");
136 xbt_heap_push(history->heap, trace_event, start_time);
141 double tmgr_history_next_date(tmgr_history_t history)
143 if (xbt_heap_size(history->heap))
144 return (xbt_heap_maxkey(history->heap));
149 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t history,
154 double event_date = xbt_heap_maxkey(history->heap);
155 tmgr_trace_event_t trace_event = NULL;
156 tmgr_event_t event = NULL;
157 tmgr_trace_t trace = NULL;
159 if (event_date > date)
162 if (!(trace_event = xbt_heap_pop(history->heap)))
165 trace = trace_event->trace;
166 event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
169 *value = event->value;
170 *resource = trace_event->resource;
172 if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
173 xbt_heap_push(history->heap, trace_event, event_date + event->delta);
175 } else if (event->delta > 0) { /* Last element, checking for periodicity */
176 xbt_heap_push(history->heap, trace_event, event_date + event->delta);
177 trace_event->idx = 0;
178 } else { /* We don't need this trace_event anymore */
179 xbt_free(trace_event);
185 void tmgr_finalize(void)
187 xbt_dict_free(&trace_list);