Logo AND Algorithmique Numérique Distribuée

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