Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Martin wrote :
[simgrid.git] / src / surf / trace_mgr.c
1 /* Authors: Arnaud Legrand                                                  */
2
3 /* This program is free software; you can redistribute it and/or modify it
4    under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "xbt/sysdep.h"
7 #include "xbt/error.h"
8 #include "xbt/dict.h"
9 #include "trace_mgr_private.h"
10 #include <stdlib.h>
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trace, surf,
13                                 "Logging specific to the SURF trace module");
14
15 static xbt_dict_t trace_list = NULL;
16 static void _tmgr_trace_free(void *trace)
17 {
18   tmgr_trace_free(trace);
19 }
20
21 tmgr_history_t tmgr_history_new(void)
22 {
23   tmgr_history_t history;
24
25   history = xbt_new0(s_tmgr_history_t, 1);
26
27   history->heap = xbt_heap_new(8, xbt_free);    /* Why 8 ? Well, why not... */
28
29   return history;
30 }
31
32 void tmgr_history_free(tmgr_history_t history)
33 {
34   xbt_heap_free(history->heap);
35   xbt_free(history);
36 }
37
38 tmgr_trace_t tmgr_trace_new(const char *filename)
39 {
40   tmgr_trace_t trace = NULL;
41   FILE *f = NULL;
42   int linecount = 0;
43   char line[256];
44   xbt_heap_float_t periodicity = -1.0;  /* No periodicity by default */
45   s_tmgr_event_t event;
46   tmgr_event_t last_event = NULL;
47
48   if (trace_list) {
49     xbt_dict_get(trace_list, filename, (void **) &trace);
50     if (trace)
51       return trace;
52   }
53
54   if ((f = fopen(filename, "r")) == NULL) {
55     xbt_assert1(0,"Cannot open file '%s'", filename);
56   }
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   while (fgets(line, 256, f)) {
62     linecount++;
63     if ((line[0] == '#') || (line[0] == '\n') || (line[0] == '%'))
64       continue;
65
66     if (sscanf(line, "PERIODICITY " XBT_HEAP_FLOAT_T "\n", &(periodicity))
67         == 1) {
68       if (periodicity <= 0) {
69         xbt_assert2(0,"%s,%d: Syntax error. Periodicity has to be positive",
70                     filename, linecount);
71       }
72       continue;
73     }
74
75     if (sscanf
76         (line, XBT_HEAP_FLOAT_T " " XBT_MAXMIN_FLOAT_T "\n", &event.delta,
77          &event.value) != 2) {
78       xbt_assert2(0,"%s,%d: Syntax error", filename, linecount);
79     }
80
81     if (last_event) {
82       if ((last_event->delta = event.delta - last_event->delta) <= 0) {
83         xbt_assert2(0,"%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
109 void tmgr_trace_free(tmgr_trace_t trace)
110 {
111   if (!trace)
112     return;
113   xbt_dynar_free(&(trace->event_list));
114   xbt_free(trace);
115 }
116
117 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t history, tmgr_trace_t trace,
118                                           xbt_heap_float_t start_time, int offset,
119                                           void *resource)
120 {
121   tmgr_trace_event_t trace_event = NULL;
122
123
124   trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
125   trace_event->trace = trace;
126   trace_event->idx = offset;
127   trace_event->resource = resource;
128
129   xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)),
130               "You're refering an event that does not exist!");
131
132   xbt_heap_push(history->heap, trace_event, start_time);
133
134   return trace_event;
135 }
136
137 xbt_heap_float_t tmgr_history_next_date(tmgr_history_t history)
138 {
139   if (xbt_heap_size(history->heap))
140     return (xbt_heap_maxkey(history->heap));
141   else
142     return -1.0;
143 }
144
145 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t history,
146                                                    xbt_heap_float_t date,
147                                                    xbt_maxmin_float_t * value,
148                                                    void **resource)
149 {
150   xbt_heap_float_t event_date = xbt_heap_maxkey(history->heap);
151   tmgr_trace_event_t trace_event = NULL;
152   tmgr_event_t event = NULL;
153   tmgr_trace_t trace = NULL;
154
155   if (event_date > date)
156     return NULL;
157
158   if (!(trace_event = xbt_heap_pop(history->heap)))
159     return NULL;
160
161   trace = trace_event->trace;
162   event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
163
164
165   *value = event->value;
166   *resource = trace_event->resource;
167
168   if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
169     xbt_heap_push(history->heap, trace_event, event_date + event->delta);
170     trace_event->idx++;
171   } else if (event->delta > 0) {        /* Last element, checking for periodicity */
172     xbt_heap_push(history->heap, trace_event, event_date + event->delta);
173     trace_event->idx = 0;
174   } else {                      /* We don't need this trace_event anymore */
175     xbt_free(trace_event);
176   }
177
178   return trace_event;
179 }
180
181 void tmgr_finalize(void)
182 {
183   xbt_dict_free(&trace_list);
184 }