Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SURF: Unify the types of models in a uniq s_surf_model_t (using an union) +reindent...
[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/log.h"
10 #include "xbt/str.h"
11 #include "xbt/dict.h"
12 #include "trace_mgr_private.h"
13 #include "surf_private.h"
14
15 static xbt_dict_t trace_list = NULL;
16 static void _tmgr_trace_free(void *trace)
17 {
18   tmgr_trace_free(trace);
19 }
20
21 tmgr_history_t tmgr_history_new(void)
22 {
23   tmgr_history_t h;
24
25   h = xbt_new0(s_tmgr_history_t, 1);
26
27   h->heap = xbt_heap_new(8, xbt_free_f);        /* Why 8 ? Well, why not... */
28
29   return h;
30 }
31
32 void tmgr_history_free(tmgr_history_t h)
33 {
34   xbt_heap_free(h->heap);
35   free(h);
36 }
37
38 tmgr_trace_t tmgr_trace_new_from_string(const char *id, const char *input,
39                                         double periodicity)
40 {
41   tmgr_trace_t trace = NULL;
42   int linecount = 0;
43   s_tmgr_event_t event;
44   tmgr_event_t last_event = NULL;
45   xbt_dynar_t list;
46   unsigned int cpt;
47   char *val;
48
49   if (trace_list) {
50     trace = xbt_dict_get_or_null(trace_list, id);
51     if (trace)
52       return trace;
53   }
54
55   if (periodicity <= 0) {
56     xbt_assert1(0, "Periodicity has to be positive. Your value %lg",
57                 periodicity);
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   list = xbt_str_split(input, "\n\r");
64
65
66   xbt_dynar_foreach(list, cpt, val) {
67     linecount++;
68     xbt_str_trim(val, " \t\n\r\x0B");
69     if (strlen(val) > 0) {
70       if (sscanf(val, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) {
71         xbt_assert2(0, "%s\n%d: Syntax error", input, linecount);
72       }
73       if (last_event) {
74         if ((last_event->delta = event.delta - last_event->delta) <= 0) {
75           xbt_assert2(0,
76                       "%s\n%d: Invalid trace value, events have to be sorted",
77                       input, linecount);
78         }
79       }
80       xbt_dynar_push(trace->event_list, &event);
81       last_event =
82         xbt_dynar_get_ptr(trace->event_list,
83                           xbt_dynar_length(trace->event_list) - 1);
84       if (periodicity > 0) {
85         if (last_event)
86           last_event->delta = periodicity;
87       }
88     }
89   }
90
91   if (!trace_list)
92     trace_list = xbt_dict_new();
93
94   xbt_dict_set(trace_list, id, (void *) trace, _tmgr_trace_free);
95
96   xbt_dynar_free(&list);
97   return trace;
98 }
99
100 tmgr_trace_t tmgr_trace_new(const char *filename)
101 {
102   tmgr_trace_t trace = NULL;
103   FILE *f = NULL;
104   int linecount = 0;
105   char line[256];
106   double periodicity = -1.0;    /* No periodicity by default */
107   s_tmgr_event_t event;
108   tmgr_event_t last_event = NULL;
109
110   if (trace_list) {
111     trace = xbt_dict_get_or_null(trace_list, filename);
112     if (trace)
113       return trace;
114   }
115
116   if ((f = surf_fopen(filename, "r")) == NULL) {
117     xbt_assert1(0, "Cannot open file '%s'", filename);
118   }
119
120   trace = xbt_new0(s_tmgr_trace_t, 1);
121   trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
122
123   while (fgets(line, 256, f)) {
124     linecount++;
125     if ((line[0] == '#') || (line[0] == '\n') || (line[0] == '%'))
126       continue;
127
128     if (sscanf(line, "PERIODICITY " "%lg" "\n", &(periodicity))
129         == 1) {
130       if (periodicity <= 0) {
131         xbt_assert2(0,
132                     "%s,%d: Syntax error. Periodicity has to be positive",
133                     filename, linecount);
134       }
135       continue;
136     }
137
138     if (sscanf(line, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) {
139       xbt_assert2(0, "%s,%d: Syntax error", filename, linecount);
140     }
141
142     if (last_event) {
143       if ((last_event->delta = event.delta - last_event->delta) <= 0) {
144         xbt_assert2(0,
145                     "%s,%d: Invalid trace value, events have to be sorted",
146                     filename, linecount);
147       }
148     }
149     xbt_dynar_push(trace->event_list, &event);
150     last_event = xbt_dynar_get_ptr(trace->event_list,
151                                    xbt_dynar_length(trace->event_list) - 1);
152   }
153
154   if (periodicity > 0) {
155     if (last_event)
156       last_event->delta = periodicity;
157   }
158
159   if (!trace_list)
160     trace_list = xbt_dict_new();
161
162   xbt_dict_set(trace_list, filename, (void *) trace, _tmgr_trace_free);
163
164   fclose(f);
165
166   return trace;
167 }
168
169 tmgr_trace_t tmgr_empty_trace_new(void)
170 {
171   tmgr_trace_t trace = NULL;
172   /*double periodicity = -1.0;   No periodicity by default; unused variables
173      tmgr_event_t last_event = NULL; */
174   s_tmgr_event_t event;
175
176   trace = xbt_new0(s_tmgr_trace_t, 1);
177   trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
178
179   event.delta = 0.0;
180   event.value = 0.0;
181   xbt_dynar_push(trace->event_list, &event);
182
183   return trace;
184 }
185
186 void tmgr_trace_free(tmgr_trace_t trace)
187 {
188   if (!trace)
189     return;
190   xbt_dynar_free(&(trace->event_list));
191   free(trace);
192 }
193
194 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t h,
195                                           tmgr_trace_t trace,
196                                           double start_time,
197                                           unsigned int offset, void *model)
198 {
199   tmgr_trace_event_t trace_event = NULL;
200
201   trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
202   trace_event->trace = trace;
203   trace_event->idx = offset;
204   trace_event->model = model;
205
206   xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)),
207               "You're referring to an event that does not exist!");
208
209   xbt_heap_push(h->heap, trace_event, start_time);
210
211   return trace_event;
212 }
213
214 double tmgr_history_next_date(tmgr_history_t h)
215 {
216   if (xbt_heap_size(h->heap))
217     return (xbt_heap_maxkey(h->heap));
218   else
219     return -1.0;
220 }
221
222 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t h,
223                                                    double date,
224                                                    double *value,
225                                                    void **model)
226 {
227   double event_date = tmgr_history_next_date(h);
228   tmgr_trace_event_t trace_event = NULL;
229   tmgr_event_t event = NULL;
230   tmgr_trace_t trace = NULL;
231
232   if (event_date > date)
233     return NULL;
234
235   if (!(trace_event = xbt_heap_pop(h->heap)))
236     return NULL;
237
238   trace = trace_event->trace;
239   event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
240
241   *value = event->value;
242   *model = trace_event->model;
243
244   if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
245     xbt_heap_push(h->heap, trace_event, event_date + event->delta);
246     trace_event->idx++;
247   } else if (event->delta > 0) {        /* Last element, checking for periodicity */
248     xbt_heap_push(h->heap, trace_event, event_date + event->delta);
249     trace_event->idx = 0;
250   } else {                      /* We don't need this trace_event anymore */
251     free(trace_event);
252     return NULL;
253   }
254
255   return trace_event;
256 }
257
258 void tmgr_finalize(void)
259 {
260   xbt_dict_free(&trace_list);
261 }