Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Replace xbt_die(bprintf(...)) with xbt_die(...).
[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_from_string(const char *id, const char *input,
36                                         double periodicity)
37 {
38   tmgr_trace_t trace = NULL;
39   int linecount = 0;
40   s_tmgr_event_t event;
41   tmgr_event_t last_event = NULL;
42   xbt_dynar_t list;
43   unsigned int cpt;
44   char *val;
45
46   if (trace_list) {
47     trace = xbt_dict_get_or_null(trace_list, id);
48     if (trace) {
49       XBT_WARN("Ignoring redefinition of trace %s", id);
50       return trace;
51     }
52   }
53
54   xbt_assert1(periodicity >= 0,
55               "Invalid periodicity %lg (must be positive)", periodicity);
56
57   trace = xbt_new0(s_tmgr_trace_t, 1);
58   trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
59
60   list = xbt_str_split(input, "\n\r");
61
62   xbt_dynar_foreach(list, cpt, val) {
63     linecount++;
64     xbt_str_trim(val, " \t\n\r\x0B");
65     if (val[0] == '#' || val[0] == '\0' || val[0] == '%')
66       continue;
67
68     if (sscanf(val, "PERIODICITY " "%lg" "\n", &periodicity) == 1)
69       continue;
70
71     if (sscanf(val, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2)
72       xbt_die("%s:%d: Syntax error in trace\n%s", id, linecount, input);
73
74     if (last_event) {
75       if (last_event->delta > event.delta) {
76         xbt_die("%s:%d: Invalid trace: Events must be sorted, "
77                 "but time %lg > time %lg.\n%s",
78                 id, linecount, last_event->delta, event.delta, input);
79       }
80       last_event->delta = event.delta - last_event->delta;
81     }
82     xbt_dynar_push(trace->event_list, &event);
83     last_event =
84         xbt_dynar_get_ptr(trace->event_list,
85                           xbt_dynar_length(trace->event_list) - 1);
86   }
87   if (last_event)
88     last_event->delta = periodicity;
89
90   if (!trace_list)
91     trace_list = xbt_dict_new();
92
93   xbt_dict_set(trace_list, id, (void *) trace,
94                (void (*)(void *)) 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   char *tstr = NULL;
103   FILE *f = NULL;
104   tmgr_trace_t trace = NULL;
105
106   if ((!filename) || (strcmp(filename, "") == 0))
107     return NULL;
108
109   if (trace_list) {
110     trace = xbt_dict_get_or_null(trace_list, filename);
111     if (trace) {
112       XBT_WARN("Ignoring redefinition of trace %s", filename);
113       return trace;
114     }
115   }
116
117   f = surf_fopen(filename, "r");
118   xbt_assert2(f != NULL, "Cannot open file '%s' (path=%s)", filename,
119               xbt_str_join(surf_path, ":"));
120
121   tstr = xbt_str_from_file(f);
122   fclose(f);
123   trace = tmgr_trace_new_from_string(filename, tstr, 0.);
124   xbt_free(tstr);
125
126   return trace;
127 }
128
129 tmgr_trace_t tmgr_empty_trace_new(void)
130 {
131   tmgr_trace_t trace = NULL;
132   s_tmgr_event_t event;
133
134   trace = xbt_new0(s_tmgr_trace_t, 1);
135   trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
136
137   event.delta = 0.0;
138   event.value = 0.0;
139   xbt_dynar_push(trace->event_list, &event);
140
141   return trace;
142 }
143
144 XBT_INLINE void tmgr_trace_free(tmgr_trace_t trace)
145 {
146   if (!trace)
147     return;
148   xbt_dynar_free(&(trace->event_list));
149   free(trace);
150 }
151
152 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t h,
153                                           tmgr_trace_t trace,
154                                           double start_time,
155                                           unsigned int offset, void *model)
156 {
157   tmgr_trace_event_t trace_event = NULL;
158
159   trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
160   trace_event->trace = trace;
161   trace_event->idx = offset;
162   trace_event->model = model;
163
164   xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)),
165               "You're referring to an event that does not exist!");
166
167   xbt_heap_push(h->heap, trace_event, start_time);
168
169   return trace_event;
170 }
171
172 XBT_INLINE double tmgr_history_next_date(tmgr_history_t h)
173 {
174   if (xbt_heap_size(h->heap))
175     return (xbt_heap_maxkey(h->heap));
176   else
177     return -1.0;
178 }
179
180 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t h,
181                                                    double date,
182                                                    double *value,
183                                                    void **model)
184 {
185   double event_date = tmgr_history_next_date(h);
186   tmgr_trace_event_t trace_event = NULL;
187   tmgr_event_t event = NULL;
188   tmgr_trace_t trace = NULL;
189
190   if (event_date > date)
191     return NULL;
192
193   if (!(trace_event = xbt_heap_pop(h->heap)))
194     return NULL;
195
196   trace = trace_event->trace;
197   event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
198
199   *value = event->value;
200   *model = trace_event->model;
201
202   if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
203     xbt_heap_push(h->heap, trace_event, event_date + event->delta);
204     trace_event->idx++;
205   } else if (event->delta > 0) {        /* Last element, checking for periodicity */
206     xbt_heap_push(h->heap, trace_event, event_date + event->delta);
207     trace_event->idx = 0;
208   } else {                      /* We don't need this trace_event anymore */
209     trace_event->free_me = 1;
210   }
211
212   return trace_event;
213 }
214
215 XBT_INLINE void tmgr_finalize(void)
216 {
217   xbt_dict_free(&trace_list);
218 }
219
220 int tmgr_trace_event_free(tmgr_trace_event_t trace_event)
221 {
222   if (trace_event->free_me) {
223     xbt_free(trace_event);
224     return 1;
225   }
226   return 0;
227 }