Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b0803d57a99fb1260d0456185d021472b7aab058
[simgrid.git] / src / surf / surfxml_parse.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/misc.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
11 #include "surf/surfxml_parse_private.h"
12 #include "surf/surf_private.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
14                                 "Logging specific to the SURF parsing module");
15
16 #undef CLEANUP
17 #include "surfxml.c"
18
19 /* make sure these symbols are defined as strong ones in this file so that the linked can resolve them */
20 xbt_dynar_t STag_surfxml_platform_cb_list = NULL;
21 xbt_dynar_t ETag_surfxml_platform_cb_list = NULL;
22 xbt_dynar_t STag_surfxml_host_cb_list = NULL;
23 xbt_dynar_t ETag_surfxml_host_cb_list = NULL;
24 xbt_dynar_t STag_surfxml_router_cb_list = NULL;
25 xbt_dynar_t ETag_surfxml_router_cb_list = NULL;
26 xbt_dynar_t STag_surfxml_link_cb_list = NULL;
27 xbt_dynar_t ETag_surfxml_link_cb_list = NULL;
28 xbt_dynar_t STag_surfxml_route_cb_list = NULL;
29 xbt_dynar_t ETag_surfxml_route_cb_list = NULL;
30 xbt_dynar_t STag_surfxml_link_c_ctn_cb_list = NULL;
31 xbt_dynar_t ETag_surfxml_link_c_ctn_cb_list = NULL;
32 xbt_dynar_t STag_surfxml_process_cb_list = NULL;
33 xbt_dynar_t ETag_surfxml_process_cb_list = NULL;
34 xbt_dynar_t STag_surfxml_argument_cb_list = NULL;
35 xbt_dynar_t ETag_surfxml_argument_cb_list = NULL;
36 xbt_dynar_t STag_surfxml_prop_cb_list = NULL;
37 xbt_dynar_t ETag_surfxml_prop_cb_list = NULL;
38
39 xbt_dict_t current_property_set = NULL;
40
41 xbt_dict_t route_table = NULL;
42 xbt_dynar_t route_link_list = NULL;
43 xbt_dynar_t links = NULL;
44 xbt_dynar_t keys = NULL;
45
46 static xbt_dynar_t surf_input_buffer_stack = NULL;
47 static xbt_dynar_t surf_file_to_parse_stack = NULL;
48
49 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t);
50
51 YY_BUFFER_STATE surf_input_buffer;
52 FILE *surf_file_to_parse = NULL;
53
54 void make_route_table(void)
55 {
56   xbt_dict_cursor_t cursor = NULL;
57   char *key,*data;
58   const char *sep = "##";
59
60    xbt_dict_foreach(route_table, cursor, key, data) {
61      links = (xbt_dynar_t)data;
62      keys = xbt_str_split_str(key, sep);
63      surfxml_call_cb_functions(ETag_surfxml_platform_cb_list);
64    }
65
66    xbt_dict_free(&route_table);
67    xbt_dynar_free(&links);
68
69 }
70
71 void surf_parse_free_callbacks(void)
72 {
73   xbt_dynar_free(&STag_surfxml_platform_cb_list);
74   xbt_dynar_free(&ETag_surfxml_platform_cb_list);
75   xbt_dynar_free(&STag_surfxml_host_cb_list);
76   xbt_dynar_free(&ETag_surfxml_host_cb_list);
77   xbt_dynar_free(&STag_surfxml_router_cb_list);
78   xbt_dynar_free(&ETag_surfxml_router_cb_list);
79   xbt_dynar_free(&STag_surfxml_link_cb_list);
80   xbt_dynar_free(&ETag_surfxml_link_cb_list);
81   xbt_dynar_free(&STag_surfxml_route_cb_list);
82   xbt_dynar_free(&ETag_surfxml_route_cb_list);
83   xbt_dynar_free(&STag_surfxml_link_c_ctn_cb_list);
84   xbt_dynar_free(&ETag_surfxml_link_c_ctn_cb_list);
85   xbt_dynar_free(&STag_surfxml_process_cb_list);
86   xbt_dynar_free(&ETag_surfxml_process_cb_list);
87   xbt_dynar_free(&STag_surfxml_argument_cb_list);
88   xbt_dynar_free(&ETag_surfxml_argument_cb_list);
89   xbt_dynar_free(&STag_surfxml_prop_cb_list);
90   xbt_dynar_free(&ETag_surfxml_prop_cb_list);
91 }
92
93 void surf_parse_reset_parser(void)
94 {
95   surf_parse_free_callbacks();
96   STag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
97   ETag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
98   STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
99   ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
100   STag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
101   ETag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
102   STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
103   ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
104   STag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
105   ETag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
106   STag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
107   ETag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
108   STag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
109   ETag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
110   STag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
111   ETag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
112   STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
113   ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
114
115 }
116
117 void STag_surfxml_include(void)
118 {
119   xbt_dynar_push(surf_input_buffer_stack, &surf_input_buffer);
120   xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse);
121
122   surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r");
123   xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n",
124               A_surfxml_include_file);
125   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
126   surf_parse__switch_to_buffer(surf_input_buffer);
127   printf("STAG\n");
128   fflush(NULL);
129 }
130
131 void ETag_surfxml_include(void)
132 {
133   printf("ETAG\n");
134   fflush(NULL);
135   surf_parse__delete_buffer(surf_input_buffer);
136   fclose(surf_file_to_parse);
137   xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
138   xbt_dynar_pop(surf_input_buffer_stack, &surf_input_buffer);
139 }
140
141 void STag_surfxml_platform(void)
142 {
143   double version = 0.0;
144
145   sscanf(A_surfxml_platform_version, "%lg", &version);
146
147   xbt_assert0((version >= 1.0), "******* BIG FAT WARNING *********\n "
148               "You're using an ancient XML file. "
149               "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
150               "instead of MBytes, MFlops and seconds. "
151               "A script (surfxml_update.pl) to help you convert your old "
152               "platform files "
153               "is available in the contrib/platform_generation directory "
154               "of the simgrid repository. Please check also out the "
155               "SURF section of the ChangeLog for the 3.1 version. "
156               "Last, do not forget to also update your values for "
157               "the calls to MSG_task_create (if any).");
158   xbt_assert0((version >= 2.0), "******* BIG FAT WARNING *********\n "
159               "You're using an old XML file. "
160               "A script (surfxml_update.pl) to help you convert your old "
161               "platform files "
162               "is available in the contrib/platform_generation directory "
163               "of the simgrid repository.");
164
165   surfxml_call_cb_functions(STag_surfxml_platform_cb_list);
166 }
167
168 void ETag_surfxml_platform(void)
169 {
170   if (route_table != NULL) make_route_table();
171 }
172
173 void STag_surfxml_host(void)
174 {
175   surfxml_call_cb_functions(STag_surfxml_host_cb_list);
176 }
177
178 void ETag_surfxml_host(void)
179 {
180   surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
181 }
182
183 void STag_surfxml_router(void)
184 {
185   surfxml_call_cb_functions(STag_surfxml_router_cb_list);
186 }
187
188 void ETag_surfxml_router(void)
189 {
190   surfxml_call_cb_functions(ETag_surfxml_router_cb_list);
191 }
192
193 void STag_surfxml_link(void)
194 {
195   surfxml_call_cb_functions(STag_surfxml_link_cb_list);
196 }
197
198 void ETag_surfxml_link(void)
199 {
200   surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
201 }
202
203 void STag_surfxml_route(void)
204 {
205   surfxml_call_cb_functions(STag_surfxml_route_cb_list);
206 }
207
208 void ETag_surfxml_route(void)
209 {
210   surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
211 }
212
213 void STag_surfxml_link_c_ctn(void)
214 {
215   surfxml_call_cb_functions(STag_surfxml_link_c_ctn_cb_list);
216 }
217
218 void ETag_surfxml_link_c_ctn(void)
219 {
220   surfxml_call_cb_functions(ETag_surfxml_link_c_ctn_cb_list);
221 }
222
223 void STag_surfxml_process(void)
224 {
225   surfxml_call_cb_functions(STag_surfxml_process_cb_list);
226 }
227
228 void ETag_surfxml_process(void)
229 {
230   surfxml_call_cb_functions(ETag_surfxml_process_cb_list);
231 }
232
233 void STag_surfxml_argument(void)
234 {
235   surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
236 }
237
238 void ETag_surfxml_argument(void)
239 {
240   surfxml_call_cb_functions(ETag_surfxml_argument_cb_list);
241 }
242
243 void STag_surfxml_prop(void)
244 {
245   surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
246 }
247 void ETag_surfxml_prop(void)
248 {
249   surfxml_call_cb_functions(ETag_surfxml_prop_cb_list);
250 }
251
252
253 void surf_parse_open(const char *file)
254 {
255   static int warned = 0;        /* warn only once */
256   if (!file) {
257     if (!warned) {
258       WARN0
259           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. If it is not what you want, go fix your code.");
260       warned = 1;
261     }
262     return;
263   }
264   if (!surf_input_buffer_stack)
265     surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
266   if (!surf_file_to_parse_stack)
267     surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
268
269   surf_file_to_parse = surf_fopen(file, "r");
270   xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n", file);
271   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
272   surf_parse__switch_to_buffer(surf_input_buffer);
273   surf_parse_lineno = 1;
274 }
275
276 void surf_parse_close(void)
277 {
278   if (surf_input_buffer_stack)
279     xbt_dynar_free(&surf_input_buffer_stack);
280   if (surf_file_to_parse_stack)
281     xbt_dynar_free(&surf_file_to_parse_stack);
282
283   if (surf_file_to_parse) {
284     surf_parse__delete_buffer(surf_input_buffer);
285     fclose(surf_file_to_parse);
286   }
287 }
288
289
290 static int _surf_parse(void)
291 {
292   return surf_parse_lex();
293 }
294
295 int_f_void_t surf_parse = _surf_parse;
296
297 void surf_parse_get_double(double *value, const char *string)
298 {
299   int ret = 0;
300
301   ret = sscanf(string, "%lg", value);
302   xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
303               surf_parse_lineno, string);
304 }
305
306 void surf_parse_get_trace(tmgr_trace_t * trace, const char *string)
307 {
308   if ((!string) || (strcmp(string, "") == 0))
309     *trace = NULL;
310   else
311     *trace = tmgr_trace_new(string);
312 }
313
314 void parse_properties(void)
315 {
316   char *value = NULL;
317
318   if(!current_property_set) current_property_set = xbt_dict_new();
319
320    value = xbt_strdup(A_surfxml_prop_value);  
321    xbt_dict_set(current_property_set, A_surfxml_prop_id, value, free);
322
323 }
324
325 void free_string(void *d)
326 {
327   free(*(void**)d);
328 }
329
330 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
331 {
332   xbt_dynar_push(cb_list, &function);
333 }
334
335 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
336 {
337   unsigned int iterator;
338   void_f_void_t fun;
339   xbt_dynar_foreach(cb_list, iterator, fun){
340        DEBUG2("call %p %p",fun,*fun);
341        (*fun)();
342     }
343 }
344
345 void init_route_table(void)
346 {
347   xbt_dict_free(&route_table);
348   xbt_dynar_free(&route_link_list);
349   route_table = xbt_dict_new();
350 }
351
352 void parse_route_elem(void)
353 {
354   char *val;
355   val = xbt_strdup(A_surfxml_link_c_ctn_id);
356   xbt_dynar_push(route_link_list, &val);
357 }
358
359 void parse_platform_file(const char* file)
360 {
361   surf_parse_open(file);
362   xbt_assert1((!(*surf_parse)()), "Parse error in %s", file);
363   surf_parse_close();
364 }