Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
one more comment
[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 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 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("%s:%d: Syntax error in trace\n%s", id, linecount, input));
74
75     if (last_event) {
76       if (last_event->delta > event.delta) {
77         xbt_die(bprintf("%s:%d: Invalid trace: Events must be sorted, 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 (periodicity > 0)
88     if (last_event)
89       last_event->delta = periodicity;
90
91   if (!trace_list)
92     trace_list = xbt_dict_new();
93
94   xbt_dict_set(trace_list, id, (void *) trace, (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   FILE *f = NULL;
103   tmgr_trace_t trace = NULL;
104
105   if ((!filename) || (strcmp(filename, "") == 0))
106     return NULL;
107
108   if (trace_list) {
109     trace = xbt_dict_get_or_null(trace_list, filename);
110     if (trace) {
111       WARN1("Ignoring redefinition of trace %s",filename);
112       return trace;
113     }
114   }
115
116   f = surf_fopen(filename, "r");
117   xbt_assert1(f!=NULL, "Cannot open file '%s'", filename);
118
119   char *tstr = xbt_str_from_file(f);
120   fclose(f);
121   trace = tmgr_trace_new_from_string(filename, tstr, 0.);
122   xbt_free(tstr);
123
124   return trace;
125 }
126
127 tmgr_trace_t tmgr_empty_trace_new(void)
128 {
129   tmgr_trace_t trace = NULL;
130   s_tmgr_event_t event;
131
132   trace = xbt_new0(s_tmgr_trace_t, 1);
133   trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
134
135   event.delta = 0.0;
136   event.value = 0.0;
137   xbt_dynar_push(trace->event_list, &event);
138
139   return trace;
140 }
141
142 void tmgr_trace_free(tmgr_trace_t trace)
143 {
144   if (!trace)
145     return;
146   xbt_dynar_free(&(trace->event_list));
147   free(trace);
148 }
149
150 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t h,
151                                           tmgr_trace_t trace,
152                                           double start_time,
153                                           unsigned int offset, void *model)
154 {
155   tmgr_trace_event_t trace_event = NULL;
156
157   trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
158   trace_event->trace = trace;
159   trace_event->idx = offset;
160   trace_event->model = model;
161
162   xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)),
163               "You're referring to an event that does not exist!");
164
165   xbt_heap_push(h->heap, trace_event, start_time);
166
167   return trace_event;
168 }
169
170 double tmgr_history_next_date(tmgr_history_t h)
171 {
172   if (xbt_heap_size(h->heap))
173     return (xbt_heap_maxkey(h->heap));
174   else
175     return -1.0;
176 }
177
178 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t h,
179                                                    double date,
180                                                    double *value,
181                                                    void **model)
182 {
183   double event_date = tmgr_history_next_date(h);
184   tmgr_trace_event_t trace_event = NULL;
185   tmgr_event_t event = NULL;
186   tmgr_trace_t trace = NULL;
187
188   if (event_date > date)
189     return NULL;
190
191   if (!(trace_event = xbt_heap_pop(h->heap)))
192     return NULL;
193
194   trace = trace_event->trace;
195   event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
196
197   *value = event->value;
198   *model = trace_event->model;
199
200   if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
201     xbt_heap_push(h->heap, trace_event, event_date + event->delta);
202     trace_event->idx++;
203   } else if (event->delta > 0) {        /* Last element, checking for periodicity */
204     xbt_heap_push(h->heap, trace_event, event_date + event->delta);
205     trace_event->idx = 0;
206   } else {                      /* We don't need this trace_event anymore */
207     free(trace_event);
208     return NULL;
209   }
210
211   return trace_event;
212 }
213
214 void tmgr_finalize(void)
215 {
216   xbt_dict_free(&trace_list);
217 }