Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b61bac05bec0454be276f580f2148f339bd7e200
[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 tmgr_trace_t tmgr_empty_trace_new(void)
113 {
114   tmgr_trace_t trace = NULL;
115   double periodicity = -1.0;    /* No periodicity by default */
116   s_tmgr_event_t event;
117   tmgr_event_t last_event = NULL;
118
119   trace = xbt_new0(s_tmgr_trace_t, 1);
120   trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
121
122   event.delta = 0.0;
123   event.value = 0.0;
124   xbt_dynar_push(trace->event_list, &event);
125
126   return trace;
127 }
128
129 void tmgr_trace_free(tmgr_trace_t trace)
130 {
131   if (!trace)
132     return;
133   xbt_dynar_free(&(trace->event_list));
134   free(trace);
135 }
136
137 tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t h,
138                                           tmgr_trace_t trace,
139                                           double start_time, int offset,
140                                           void *resource)
141 {
142   tmgr_trace_event_t trace_event = NULL;
143
144
145   trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
146   trace_event->trace = trace;
147   trace_event->idx = offset;
148   trace_event->resource = resource;
149
150   xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)),
151               "You're refering to an event that does not exist!");
152
153   xbt_heap_push(h->heap, trace_event, start_time);
154
155   return trace_event;
156 }
157
158 double tmgr_history_next_date(tmgr_history_t h)
159 {
160   if (xbt_heap_size(h->heap))
161     return (xbt_heap_maxkey(h->heap));
162   else
163     return -1.0;
164 }
165
166 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t h,
167                                                    double date,
168                                                    double *value,
169                                                    void **resource)
170 {
171   double event_date = xbt_heap_maxkey(h->heap);
172   tmgr_trace_event_t trace_event = NULL;
173   tmgr_event_t event = NULL;
174   tmgr_trace_t trace = NULL;
175
176   if (event_date > date)
177     return NULL;
178
179   if (!(trace_event = xbt_heap_pop(h->heap)))
180     return NULL;
181
182   trace = trace_event->trace;
183   event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
184
185
186   *value = event->value;
187   *resource = trace_event->resource;
188
189   if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
190     xbt_heap_push(h->heap, trace_event, event_date + event->delta);
191     trace_event->idx++;
192   } else if (event->delta > 0) {        /* Last element, checking for periodicity */
193     xbt_heap_push(h->heap, trace_event, event_date + event->delta);
194     trace_event->idx = 0;
195   } else {                      /* We don't need this trace_event anymore */
196     free(trace_event);
197   }
198
199   return trace_event;
200 }
201
202 void tmgr_finalize(void)
203 {
204   xbt_dict_free(&trace_list);
205 }