Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Getting rid of useless field
[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, free);      /* 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(const char *filename)
38 {
39   tmgr_trace_t trace = NULL;
40   FILE *f = NULL;
41   int linecount = 0;
42   char line[256];
43   double periodicity = -1.0;    /* No periodicity by default */
44   s_tmgr_event_t event;
45   tmgr_event_t last_event = NULL;
46
47   if (trace_list) {
48     trace = xbt_dict_get_or_null(trace_list, filename);
49     if (trace)
50       return trace;
51   }
52
53   if ((f = surf_fopen(filename, "r")) == NULL) {
54     xbt_assert1(0, "Cannot open file '%s'", filename);
55   }
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   while (fgets(line, 256, f)) {
61     linecount++;
62     if ((line[0] == '#') || (line[0] == '\n') || (line[0] == '%'))
63       continue;
64
65     if (sscanf(line, "PERIODICITY " "%lg" "\n", &(periodicity))
66         == 1) {
67       if (periodicity <= 0) {
68         xbt_assert2(0,
69                     "%s,%d: Syntax error. Periodicity has to be positive",
70                     filename, linecount);
71       }
72       continue;
73     }
74
75     if (sscanf
76         (line, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) {
77       xbt_assert2(0, "%s,%d: Syntax error", filename, linecount);
78     }
79
80     if (last_event) {
81       if ((last_event->delta = event.delta - last_event->delta) <= 0) {
82         xbt_assert2(0,
83                     "%s,%d: Invalid trace value, events have to be sorted",
84                     filename, linecount);
85       }
86     }
87     xbt_dynar_push(trace->event_list, &event);
88     last_event = xbt_dynar_get_ptr(trace->event_list,
89                                    xbt_dynar_length(trace->event_list) -
90                                    1);
91   }
92
93   if (periodicity > 0) {
94     if (last_event)
95       last_event->delta = periodicity;
96   }
97
98   if (!trace_list)
99     trace_list = xbt_dict_new();
100
101   xbt_dict_set(trace_list, filename, (void *) trace, _tmgr_trace_free);
102
103   fclose(f);
104
105   return trace;
106 }
107
108 tmgr_trace_t tmgr_empty_trace_new(void)
109 {
110   tmgr_trace_t trace = NULL;
111   /*double periodicity = -1.0;   No periodicity by default; unused variables
112   tmgr_event_t last_event = NULL;*/
113   s_tmgr_event_t event;
114
115   trace = xbt_new0(s_tmgr_trace_t, 1);
116   trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
117
118   event.delta = 0.0;
119   event.value = 0.0;
120   xbt_dynar_push(trace->event_list, &event);
121
122   return trace;
123 }
124
125 void tmgr_trace_free(tmgr_trace_t trace)
126 {
127   if (!trace)
128     return;
129   xbt_dynar_free(&(trace->event_list));
130   free(trace);
131 }
132
133 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t h,
134                                           tmgr_trace_t trace,
135                                           double start_time, int offset,
136                                           void *resource)
137 {
138   tmgr_trace_event_t trace_event = NULL;
139
140   trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
141   trace_event->trace = trace;
142   trace_event->idx = offset;
143   trace_event->resource = resource;
144
145   xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)),
146               "You're refering to an event that does not exist!");
147
148   xbt_heap_push(h->heap, trace_event, start_time);
149
150   return trace_event;
151 }
152
153 double tmgr_history_next_date(tmgr_history_t h)
154 {
155   if (xbt_heap_size(h->heap))
156     return (xbt_heap_maxkey(h->heap));
157   else
158     return -1.0;
159 }
160
161 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t h,
162                                                    double date,
163                                                    double *value,
164                                                    void **resource)
165 {
166   double event_date = tmgr_history_next_date(h);
167   tmgr_trace_event_t trace_event = NULL;
168   tmgr_event_t event = NULL;
169   tmgr_trace_t trace = NULL;
170
171   if (event_date > date)
172     return NULL;
173
174   if (!(trace_event = xbt_heap_pop(h->heap)))
175     return NULL;
176
177   trace = trace_event->trace;
178   event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
179
180   *value = event->value;
181   *resource = trace_event->resource;
182
183   if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
184     xbt_heap_push(h->heap, trace_event, event_date + event->delta);
185     trace_event->idx++;
186   } else if (event->delta > 0) {        /* Last element, checking for periodicity */
187     xbt_heap_push(h->heap, trace_event, event_date + event->delta);
188     trace_event->idx = 0;
189   } else {                      /* We don't need this trace_event anymore */
190     free(trace_event);
191   }
192
193   return trace_event;
194 }
195
196 void tmgr_finalize(void)
197 {
198   xbt_dict_free(&trace_list);
199 }