Logo AND Algorithmique Numérique Distribuée

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