Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Last bits of convertion from xbt_error_t to exceptions. Some more cleanups (mainly...
[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/log.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     trace = xbt_dict_get_or_null(trace_list, filename);
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; unused variables
116   tmgr_event_t last_event = NULL;*/
117   s_tmgr_event_t event;
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   trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
145   trace_event->trace = trace;
146   trace_event->idx = offset;
147   trace_event->resource = resource;
148
149   xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)),
150               "You're refering to an event that does not exist!");
151
152   xbt_heap_push(h->heap, trace_event, start_time);
153
154   return trace_event;
155 }
156
157 double tmgr_history_next_date(tmgr_history_t h)
158 {
159   if (xbt_heap_size(h->heap))
160     return (xbt_heap_maxkey(h->heap));
161   else
162     return -1.0;
163 }
164
165 tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t h,
166                                                    double date,
167                                                    double *value,
168                                                    void **resource)
169 {
170   double event_date = tmgr_history_next_date(h);
171   tmgr_trace_event_t trace_event = NULL;
172   tmgr_event_t event = NULL;
173   tmgr_trace_t trace = NULL;
174
175   if (event_date > date)
176     return NULL;
177
178   if (!(trace_event = xbt_heap_pop(h->heap)))
179     return NULL;
180
181   trace = trace_event->trace;
182   event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
183
184   *value = event->value;
185   *resource = trace_event->resource;
186
187   if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
188     xbt_heap_push(h->heap, trace_event, event_date + event->delta);
189     trace_event->idx++;
190   } else if (event->delta > 0) {        /* Last element, checking for periodicity */
191     xbt_heap_push(h->heap, trace_event, event_date + event->delta);
192     trace_event->idx = 0;
193   } else {                      /* We don't need this trace_event anymore */
194     free(trace_event);
195   }
196
197   return trace_event;
198 }
199
200 void tmgr_finalize(void)
201 {
202   xbt_dict_free(&trace_list);
203 }