Logo AND Algorithmique Numérique Distribuée

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