Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2947389d06060cc64f7c55ccc08305a52fa7cf74
[simgrid.git] / src / surf / trace_mgr.c
1 /* Copyright (c) 2004, 2005, 2007, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "xbt/sysdep.h"
8 #include "xbt/log.h"
9 #include "xbt/str.h"
10 #include "xbt/dict.h"
11 #include "trace_mgr_private.h"
12 #include "surf_private.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_trace, surf, "Surf trace management");
15
16 static xbt_dict_t trace_list = NULL;
17
18 XBT_INLINE tmgr_history_t tmgr_history_new(void)
19 {
20   tmgr_history_t h;
21
22   h = xbt_new0(s_tmgr_history_t, 1);
23
24   h->heap = xbt_heap_new(8, xbt_free_f);        /* Why 8 ? Well, why not... */
25
26   return h;
27 }
28
29 XBT_INLINE void tmgr_history_free(tmgr_history_t h)
30 {
31   xbt_heap_free(h->heap);
32   free(h);
33 }
34
35 tmgr_trace_t tmgr_trace_new_uniform(double alpha, double beta)
36 {  
37   tmgr_trace_t trace = NULL;
38   
39   trace = xbt_new0(s_tmgr_trace_t, 1);
40   trace->type = e_trace_uniform;
41   trace->s_uniform.alpha = alpha;
42   trace->s_uniform.beta = beta;
43   
44   //FIXME Generate a new event date
45   
46   return trace;
47 }
48
49
50 tmgr_trace_t tmgr_trace_new_exponential(double lambda)
51 {  
52   tmgr_trace_t trace = NULL;
53   
54   trace = xbt_new0(s_tmgr_trace_t, 1);
55   trace->type = e_trace_exponential;
56   trace->s_exponential.lambda = lambda;
57   
58   // FIXME Generate a new event date
59   
60   return trace;
61 }
62
63 tmgr_trace_t tmgr_trace_new_weibull(double lambda, double k)
64 {  
65   tmgr_trace_t trace = NULL;
66   
67   trace = xbt_new0(s_tmgr_trace_t, 1);
68   trace->type = e_trace_weibull;
69   trace->s_weibull.lambda = lambda;
70   trace->s_weibull.k = k;
71   
72   // FIXME Generate a new event date
73   
74   return trace;
75 }
76
77 tmgr_trace_t tmgr_trace_new_from_string(const char *id, const char *input,
78                                         double periodicity)
79 {
80   tmgr_trace_t trace = NULL;
81   int linecount = 0;
82   s_tmgr_event_t event;
83   tmgr_event_t last_event = NULL;
84   xbt_dynar_t list;
85   unsigned int cpt;
86   char *val;
87
88   if (trace_list) {
89     trace = xbt_dict_get_or_null(trace_list, id);
90     if (trace) {
91       XBT_WARN("Ignoring redefinition of trace %s", id);
92       return trace;
93     }
94   }
95
96   xbt_assert(periodicity >= 0,
97               "Invalid periodicity %lg (must be positive)", periodicity);
98
99   trace = xbt_new0(s_tmgr_trace_t, 1);
100   trace->type = e_trace_list;
101   trace->s_list.event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
102
103   list = xbt_str_split(input, "\n\r");
104
105   xbt_dynar_foreach(list, cpt, val) {
106     linecount++;
107     xbt_str_trim(val, " \t\n\r\x0B");
108     if (val[0] == '#' || val[0] == '\0' || val[0] == '%')
109       continue;
110
111     if (sscanf(val, "PERIODICITY " "%lg" "\n", &periodicity) == 1)
112       continue;
113
114     if (sscanf(val, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2)
115       xbt_die("%s:%d: Syntax error in trace\n%s", id, linecount, input);
116
117     if (last_event) {
118       if (last_event->delta > event.delta) {
119         xbt_die("%s:%d: Invalid trace: Events must be sorted, "
120                 "but time %lg > time %lg.\n%s",
121                 id, linecount, last_event->delta, event.delta, input);
122       }
123       last_event->delta = event.delta - last_event->delta;
124     }
125     xbt_dynar_push(trace->s_list.event_list, &event);
126     last_event =
127         xbt_dynar_get_ptr(trace->s_list.event_list,
128                           xbt_dynar_length(trace->s_list.event_list) - 1);
129   }
130   if (last_event)
131     last_event->delta = periodicity;
132
133   if (!trace_list)
134     trace_list = xbt_dict_new_homogeneous((void (*)(void *)) tmgr_trace_free);
135
136   xbt_dict_set(trace_list, id, (void *) trace, NULL);
137
138   xbt_dynar_free(&list);
139   return trace;
140 }
141
142 tmgr_trace_t tmgr_trace_new_from_file(const char *filename)
143 {
144   char *tstr = NULL;
145   FILE *f = NULL;
146   tmgr_trace_t trace = NULL;
147
148   if ((!filename) || (strcmp(filename, "") == 0))
149     return NULL;
150
151   if (trace_list) {
152     trace = xbt_dict_get_or_null(trace_list, filename);
153     if (trace) {
154       XBT_WARN("Ignoring redefinition of trace %s", filename);
155       return trace;
156     }
157   }
158
159   f = surf_fopen(filename, "r");
160   xbt_assert(f != NULL, "Cannot open file '%s' (path=%s)", filename,
161               xbt_str_join(surf_path, ":"));
162
163   tstr = xbt_str_from_file(f);
164   fclose(f);
165   trace = tmgr_trace_new_from_string(filename, tstr, 0.);
166   xbt_free(tstr);
167
168   return trace;
169 }
170
171 tmgr_trace_t tmgr_empty_trace_new(void)
172 {
173   tmgr_trace_t trace = NULL;
174   s_tmgr_event_t event;
175
176   trace = xbt_new0(s_tmgr_trace_t, 1);
177   trace->s_list.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->s_list.event_list, &event);
182
183   return trace;
184 }
185
186 XBT_INLINE void tmgr_trace_free(tmgr_trace_t trace)
187 {
188   if (!trace)
189     return;
190   xbt_dynar_free(&(trace->s_list.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_assert((trace_event->idx < xbt_dynar_length(trace->s_list.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 XBT_INLINE 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->s_list.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->s_list.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     trace_event->free_me = 1;
252   }
253
254   return trace_event;
255 }
256
257 XBT_INLINE void tmgr_finalize(void)
258 {
259   xbt_dict_free(&trace_list);
260 }
261
262 int tmgr_trace_event_free(tmgr_trace_event_t trace_event)
263 {
264   if (trace_event->free_me) {
265     xbt_free(trace_event);
266     return 1;
267   }
268   return 0;
269 }