Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d4984054a8b79235b92c8c99a6c9148484136ec4
[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   /* Parsing et création de la trace */
55
56   if ((f = fopen(filename, "r")) == NULL) {
57     CRITICAL1("Cannot open file '%s'\n", filename);
58     xbt_abort();
59   }
60
61   trace = xbt_new0(s_tmgr_trace_t, 1);
62   trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
63
64   while (fgets(line, 256, f)) {
65     linecount++;
66     if ((line[0] == '#') || (line[0] == '\n') || (line[0] == '%'))
67       continue;
68
69     if (sscanf(line, "PERIODICITY " XBT_HEAP_FLOAT_T "\n", &(periodicity))
70         == 1) {
71       if (periodicity <= 0) {
72         CRITICAL2("%s,%d: Syntax error. Periodicity has to be positive\n",
73                   filename, linecount);
74         xbt_abort();
75       }
76       continue;
77     }
78
79     if (sscanf
80         (line, XBT_HEAP_FLOAT_T " " XBT_MAXMIN_FLOAT_T "\n", &event.delta,
81          &event.value) != 2) {
82       CRITICAL2("%s,%d: Syntax error\n", filename, linecount);
83       xbt_abort();
84 /*       xbt_dynar_free(&(trace->event_list)); */
85 /*       xbt_free(trace);        */
86 /*       return NULL; */
87     }
88
89     if (last_event) {
90       if ((last_event->delta = event.delta - last_event->delta) <= 0) {
91         CRITICAL2("%s,%d: Invalid trace value, events have to be sorted\n",
92                   filename, linecount);
93         xbt_abort();
94       }
95     }
96     xbt_dynar_push(trace->event_list, &event);
97     last_event = xbt_dynar_get_ptr(trace->event_list,
98                                    xbt_dynar_length(trace->event_list) -
99                                    1);
100 /*     printf(XBT_HEAP_FLOAT_T " " XBT_MAXMIN_FLOAT_T "\n", event.delta, */
101 /*         event.value); */
102   }
103
104   if (periodicity > 0) {
105     if (last_event)
106       last_event->delta = periodicity;
107   }
108
109   if (!trace_list)
110     trace_list = xbt_dict_new();
111
112   xbt_dict_set(trace_list, filename, (void *) trace, _tmgr_trace_free);
113
114   fclose(f);
115
116   return trace;
117 }
118
119
120 void tmgr_trace_free(tmgr_trace_t trace)
121 {
122   if (!trace)
123     return;
124   xbt_dynar_free(&(trace->event_list));
125   xbt_free(trace);
126 }
127
128 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t history, tmgr_trace_t trace,
129                                           xbt_heap_float_t start_time, int offset,
130                                           void *resource)
131 {
132   tmgr_trace_event_t trace_event = NULL;
133
134
135   trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
136   trace_event->trace = trace;
137   trace_event->idx = offset;
138   trace_event->resource = resource;
139
140   if (trace_event->idx >= xbt_dynar_length(trace->event_list))
141     xbt_abort();
142
143   xbt_heap_push(history->heap, trace_event, start_time);
144
145   return trace_event;
146 }
147
148 xbt_heap_float_t tmgr_history_next_date(tmgr_history_t history)
149 {
150   if (xbt_heap_size(history->heap))
151     return (xbt_heap_maxkey(history->heap));
152   else
153     return -1.0;
154 }
155
156 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t history,
157                                                    xbt_heap_float_t date,
158                                                    xbt_maxmin_float_t * value,
159                                                    void **resource)
160 {
161   xbt_heap_float_t event_date = xbt_heap_maxkey(history->heap);
162   tmgr_trace_event_t trace_event = NULL;
163   tmgr_event_t event = NULL;
164   tmgr_trace_t trace = NULL;
165
166   if (event_date > date)
167     return NULL;
168
169   if (!(trace_event = xbt_heap_pop(history->heap)))
170     return NULL;
171
172   trace = trace_event->trace;
173   event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
174
175
176   *value = event->value;
177   *resource = trace_event->resource;
178
179   if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
180     xbt_heap_push(history->heap, trace_event, event_date + event->delta);
181     trace_event->idx++;
182   } else if (event->delta > 0) {        /* Last element, checking for periodicity */
183     xbt_heap_push(history->heap, trace_event, event_date + event->delta);
184     trace_event->idx = 0;
185   } else {                      /* We don't need this trace_event anymore */
186     xbt_free(trace_event);
187   }
188
189   return trace_event;
190 }
191
192 void tmgr_finalize(void)
193 {
194   xbt_dict_free(&trace_list);
195 }