Logo AND Algorithmique Numérique Distribuée

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