Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cosmetics
[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 static xbt_dict_t trace_list = NULL;
13 static void _tmgr_trace_free(void *trace)
14 {
15   tmgr_trace_free(trace);
16 }
17
18 tmgr_history_t tmgr_history_new(void)
19 {
20   tmgr_history_t history;
21
22   history = xbt_new0(s_tmgr_history_t, 1);
23
24   history->heap = xbt_heap_new(8, xbt_free);    /* Why 8 ? Well, why not... */
25
26   return history;
27 }
28
29 void tmgr_history_free(tmgr_history_t history)
30 {
31   xbt_heap_free(history->heap);
32   xbt_free(history);
33 }
34
35 tmgr_trace_t tmgr_trace_new(const char *filename)
36 {
37   tmgr_trace_t trace = NULL;
38   FILE *f = NULL;
39   int linecount = 0;
40   char line[256];
41   xbt_heap_float_t periodicity = -1.0;  /* No periodicity by default */
42   s_tmgr_event_t event;
43   tmgr_event_t last_event = NULL;
44
45   if (trace_list) {
46     xbt_dict_get(trace_list, filename, (void **) &trace);
47     if (trace)
48       return trace;
49   }
50
51   /* Parsing et création de la trace */
52
53   if ((f = fopen(filename, "r")) == NULL) {
54     fprintf(stderr, "Cannot open file '%s'\n", filename);
55     return NULL;
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         fprintf(stderr,
70                 "%s,%d: Syntax error. Periodicity has to be positive\n",
71                 filename, linecount);
72         abort();
73 /*      xbt_dynar_free(&(trace->event_list)); */
74 /*      xbt_free(trace);         */
75 /*      return NULL; */
76       }
77       continue;
78     }
79
80     if (sscanf
81         (line, XBT_HEAP_FLOAT_T " " XBT_MAXMIN_FLOAT_T "\n", &event.delta,
82          &event.value) != 2) {
83       fprintf(stderr, "%s,%d: Syntax error\n", filename, linecount);
84       abort();
85 /*       xbt_dynar_free(&(trace->event_list)); */
86 /*       xbt_free(trace);        */
87 /*       return NULL; */
88     }
89
90     if (last_event) {
91       if ((last_event->delta = event.delta - last_event->delta) <= 0) {
92         fprintf(stderr,
93                 "%s,%d: Invalid trace value, events have to be sorted\n",
94                 filename, linecount);
95         abort();
96       }
97     }
98     xbt_dynar_push(trace->event_list, &event);
99     last_event = xbt_dynar_get_ptr(trace->event_list,
100                                    xbt_dynar_length(trace->event_list) -
101                                    1);
102 /*     printf(XBT_HEAP_FLOAT_T " " XBT_MAXMIN_FLOAT_T "\n", event.delta, */
103 /*         event.value); */
104   }
105
106   if (periodicity > 0) {
107     if (last_event)
108       last_event->delta = periodicity;
109   }
110
111   if (!trace_list)
112     trace_list = xbt_dict_new();
113
114   xbt_dict_set(trace_list, filename, (void *) trace, _tmgr_trace_free);
115
116   fclose(f);
117
118   return trace;
119 }
120
121
122 void tmgr_trace_free(tmgr_trace_t trace)
123 {
124   if (!trace)
125     return;
126   xbt_dynar_free(&(trace->event_list));
127   xbt_free(trace);
128 }
129
130 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t history, tmgr_trace_t trace,
131                                           xbt_heap_float_t start_time, int offset,
132                                           void *resource)
133 {
134   tmgr_trace_event_t trace_event = NULL;
135
136
137   trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
138   trace_event->trace = trace;
139   trace_event->idx = offset;
140   trace_event->resource = resource;
141
142   if (trace_event->idx >= xbt_dynar_length(trace->event_list))
143     abort();
144
145   xbt_heap_push(history->heap, trace_event, start_time);
146
147   return trace_event;
148 }
149
150 xbt_heap_float_t tmgr_history_next_date(tmgr_history_t history)
151 {
152   if (xbt_heap_size(history->heap))
153     return (xbt_heap_maxkey(history->heap));
154   else
155     return -1.0;
156 }
157
158 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t history,
159                                                    xbt_heap_float_t date,
160                                                    xbt_maxmin_float_t * value,
161                                                    void **resource)
162 {
163   xbt_heap_float_t event_date = xbt_heap_maxkey(history->heap);
164   tmgr_trace_event_t trace_event = NULL;
165   tmgr_event_t event = NULL;
166   tmgr_trace_t trace = NULL;
167
168   if (event_date > date)
169     return NULL;
170
171   if (!(trace_event = xbt_heap_pop(history->heap)))
172     return NULL;
173
174   trace = trace_event->trace;
175   event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
176
177
178   *value = event->value;
179   *resource = trace_event->resource;
180
181   if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
182     xbt_heap_push(history->heap, trace_event, event_date + event->delta);
183     trace_event->idx++;
184   } else if (event->delta > 0) {        /* Last element, checking for periodicity */
185     xbt_heap_push(history->heap, trace_event, event_date + event->delta);
186     trace_event->idx = 0;
187   } else {                      /* We don't need this trace_event anymore */
188     xbt_free(trace_event);
189   }
190
191   return trace_event;
192 }
193
194 void tmgr_finalize(void)
195 {
196   xbt_dict_free(&trace_list);
197 }