Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
HUSH !
[simgrid.git] / src / surf / trace_mgr.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
4
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. */
7
8 #include "xbt/sysdep.h"
9 #include "xbt/error.h"
10 #include "xbt/dict.h"
11 #include "trace_mgr_private.h"
12 #include <stdlib.h>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trace, surf,
15                                 "Logging specific to the SURF trace module");
16
17 static xbt_dict_t trace_list = NULL;
18 static void _tmgr_trace_free(void *trace)
19 {
20   tmgr_trace_free(trace);
21 }
22
23 tmgr_history_t tmgr_history_new(void)
24 {
25   tmgr_history_t history;
26
27   history = xbt_new0(s_tmgr_history_t, 1);
28
29   history->heap = xbt_heap_new(8, xbt_free);    /* Why 8 ? Well, why not... */
30
31   return history;
32 }
33
34 void tmgr_history_free(tmgr_history_t history)
35 {
36   xbt_heap_free(history->heap);
37   xbt_free(history);
38 }
39
40 tmgr_trace_t tmgr_trace_new(const char *filename)
41 {
42   tmgr_trace_t trace = NULL;
43   FILE *f = NULL;
44   int linecount = 0;
45   char line[256];
46   xbt_heap_float_t periodicity = -1.0;  /* No periodicity by default */
47   s_tmgr_event_t event;
48   tmgr_event_t last_event = NULL;
49
50   if (trace_list) {
51     xbt_dict_get(trace_list, filename, (void **) &trace);
52     if (trace)
53       return trace;
54   }
55
56   if ((f = fopen(filename, "r")) == NULL) {
57     xbt_assert1(0,"Cannot open file '%s'", filename);
58   }
59
60   trace = xbt_new0(s_tmgr_trace_t, 1);
61   trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
62
63   while (fgets(line, 256, f)) {
64     linecount++;
65     if ((line[0] == '#') || (line[0] == '\n') || (line[0] == '%'))
66       continue;
67
68     if (sscanf(line, "PERIODICITY " XBT_HEAP_FLOAT_T "\n", &(periodicity))
69         == 1) {
70       if (periodicity <= 0) {
71         xbt_assert2(0,"%s,%d: Syntax error. Periodicity has to be positive",
72                     filename, linecount);
73       }
74       continue;
75     }
76
77     if (sscanf
78         (line, XBT_HEAP_FLOAT_T " " XBT_MAXMIN_FLOAT_T "\n", &event.delta,
79          &event.value) != 2) {
80       xbt_assert2(0,"%s,%d: Syntax error", filename, linecount);
81     }
82
83     if (last_event) {
84       if ((last_event->delta = event.delta - last_event->delta) <= 0) {
85         xbt_assert2(0,"%s,%d: Invalid trace value, events have to be sorted",
86                     filename, linecount);
87       }
88     }
89     xbt_dynar_push(trace->event_list, &event);
90     last_event = xbt_dynar_get_ptr(trace->event_list,
91                                    xbt_dynar_length(trace->event_list) -
92                                    1);
93   }
94
95   if (periodicity > 0) {
96     if (last_event)
97       last_event->delta = periodicity;
98   }
99
100   if (!trace_list)
101     trace_list = xbt_dict_new();
102
103   xbt_dict_set(trace_list, filename, (void *) trace, _tmgr_trace_free);
104
105   fclose(f);
106
107   return trace;
108 }
109
110
111 void tmgr_trace_free(tmgr_trace_t trace)
112 {
113   if (!trace)
114     return;
115   xbt_dynar_free(&(trace->event_list));
116   xbt_free(trace);
117 }
118
119 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t history, tmgr_trace_t trace,
120                                           xbt_heap_float_t start_time, int offset,
121                                           void *resource)
122 {
123   tmgr_trace_event_t trace_event = NULL;
124
125
126   trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
127   trace_event->trace = trace;
128   trace_event->idx = offset;
129   trace_event->resource = resource;
130
131   xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)),
132               "You're refering to an event that does not exist!");
133
134   xbt_heap_push(history->heap, trace_event, start_time);
135
136   return trace_event;
137 }
138
139 xbt_heap_float_t tmgr_history_next_date(tmgr_history_t history)
140 {
141   if (xbt_heap_size(history->heap))
142     return (xbt_heap_maxkey(history->heap));
143   else
144     return -1.0;
145 }
146
147 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t history,
148                                                    xbt_heap_float_t date,
149                                                    xbt_maxmin_float_t * value,
150                                                    void **resource)
151 {
152   xbt_heap_float_t event_date = xbt_heap_maxkey(history->heap);
153   tmgr_trace_event_t trace_event = NULL;
154   tmgr_event_t event = NULL;
155   tmgr_trace_t trace = NULL;
156
157   if (event_date > date)
158     return NULL;
159
160   if (!(trace_event = xbt_heap_pop(history->heap)))
161     return NULL;
162
163   trace = trace_event->trace;
164   event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
165
166
167   *value = event->value;
168   *resource = trace_event->resource;
169
170   if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
171     xbt_heap_push(history->heap, trace_event, event_date + event->delta);
172     trace_event->idx++;
173   } else if (event->delta > 0) {        /* Last element, checking for periodicity */
174     xbt_heap_push(history->heap, trace_event, event_date + event->delta);
175     trace_event->idx = 0;
176   } else {                      /* We don't need this trace_event anymore */
177     xbt_free(trace_event);
178   }
179
180   return trace_event;
181 }
182
183 void tmgr_finalize(void)
184 {
185   xbt_dict_free(&trace_list);
186 }