Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
split raw parsing functions from platform parsing, which is somehow similar to the...
[simgrid.git] / src / surf / surfxml_parseplatf.c
1 /* Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "xbt/misc.h"
8 #include "xbt/log.h"
9 #include "xbt/str.h"
10 #include "xbt/dict.h"
11 #include "surf/surfxml_parse_private.h"
12 #include "surf/surf_private.h"
13
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
15
16 /*
17  *  Allow the cluster tag to mess with the parsing buffer.
18  * (this will probably become obsolete once the cluster tag do not mess with the parsing callbacks directly)
19  */
20
21 /* This buffer is used to store the original buffer before substituting it by out own buffer. Useful for the cluster tag */
22 static xbt_dynar_t surfxml_bufferstack_stack = NULL;
23 int surfxml_bufferstack_size = 2048;
24
25 static char *old_buff = NULL;
26
27 unsigned int surfxml_buffer_stack_stack_ptr;
28 unsigned int surfxml_buffer_stack_stack[1024];
29
30
31 void surfxml_bufferstack_push(int new)
32 {
33   if (!new)
34     old_buff = surfxml_bufferstack;
35   else {
36     xbt_dynar_push(surfxml_bufferstack_stack, &surfxml_bufferstack);
37     surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
38   }
39 }
40
41 void surfxml_bufferstack_pop(int new)
42 {
43   if (!new)
44     surfxml_bufferstack = old_buff;
45   else {
46     free(surfxml_bufferstack);
47     xbt_dynar_pop(surfxml_bufferstack_stack, &surfxml_bufferstack);
48   }
49 }
50
51 /*
52  * Pass arguments to parsing callback as structures to save some time (and allow callbacks to ignore some)
53  */
54
55 hostSG_t struct_host = NULL;
56 router_t struct_router = NULL;
57 cluster_t struct_cluster = NULL;
58 peer_t struct_peer = NULL;
59 link_t struct_lnk = NULL;
60
61
62 /*
63  * Trace related stuff
64  */
65
66 xbt_dict_t traces_set_list = NULL;
67 xbt_dict_t trace_connect_list_host_avail = NULL;
68 xbt_dict_t trace_connect_list_power = NULL;
69 xbt_dict_t trace_connect_list_link_avail = NULL;
70 xbt_dict_t trace_connect_list_bandwidth = NULL;
71 xbt_dict_t trace_connect_list_latency = NULL;
72
73 static double trace_periodicity = -1.0;
74 static char *trace_file = NULL;
75 static char *trace_id = NULL;
76
77 static void parse_Stag_trace(void)
78 {
79   surf_parse_models_setup(); /* ensure that the models are created after the last <config> tag. See comment in simgrid.dtd */
80   trace_id = xbt_strdup(A_surfxml_trace_id);
81   trace_file = xbt_strdup(A_surfxml_trace_file);
82   surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
83 }
84
85 static void parse_Etag_trace(void)
86 {
87   tmgr_trace_t trace;
88   if (!trace_file || strcmp(trace_file, "") != 0) {
89     trace = tmgr_trace_new(trace_file);
90   } else {
91     if (strcmp(surfxml_pcdata, "") == 0)
92       trace = NULL;
93     else
94       trace =
95           tmgr_trace_new_from_string(trace_id, surfxml_pcdata,
96                                      trace_periodicity);
97   }
98   xbt_dict_set(traces_set_list, trace_id, (void *) trace, NULL);
99   xbt_free(trace_file);
100   trace_file = NULL;
101   xbt_free(trace_id);
102   trace_id = NULL;
103 }
104
105 static void parse_Stag_trace_connect(void)
106 {
107   surf_parse_models_setup(); /* ensure that the models are created after the last <config> tag. See comment in simgrid.dtd */
108
109   xbt_assert(xbt_dict_get_or_null
110               (traces_set_list, A_surfxml_trace_connect_trace),
111               "Cannot connect trace %s to %s: trace unknown",
112               A_surfxml_trace_connect_trace,
113               A_surfxml_trace_connect_element);
114
115   switch (A_surfxml_trace_connect_kind) {
116   case A_surfxml_trace_connect_kind_HOST_AVAIL:
117     xbt_dict_set(trace_connect_list_host_avail,
118                  A_surfxml_trace_connect_trace,
119                  xbt_strdup(A_surfxml_trace_connect_element), free);
120     break;
121   case A_surfxml_trace_connect_kind_POWER:
122     xbt_dict_set(trace_connect_list_power, A_surfxml_trace_connect_trace,
123                  xbt_strdup(A_surfxml_trace_connect_element), free);
124     break;
125   case A_surfxml_trace_connect_kind_LINK_AVAIL:
126     xbt_dict_set(trace_connect_list_link_avail,
127                  A_surfxml_trace_connect_trace,
128                  xbt_strdup(A_surfxml_trace_connect_element), free);
129     break;
130   case A_surfxml_trace_connect_kind_BANDWIDTH:
131     xbt_dict_set(trace_connect_list_bandwidth,
132                  A_surfxml_trace_connect_trace,
133                  xbt_strdup(A_surfxml_trace_connect_element), free);
134     break;
135   case A_surfxml_trace_connect_kind_LATENCY:
136     xbt_dict_set(trace_connect_list_latency, A_surfxml_trace_connect_trace,
137                  xbt_strdup(A_surfxml_trace_connect_element), free);
138     break;
139   default:
140     xbt_die("Cannot connect trace %s to %s: kind of trace unknown",
141             A_surfxml_trace_connect_trace, A_surfxml_trace_connect_element);
142     break;
143   }
144 }
145
146
147 /* Init and free parse data */
148
149 static void init_data(void)
150 {
151   if (!surfxml_bufferstack_stack)
152     surfxml_bufferstack_stack = xbt_dynar_new(sizeof(char *), NULL);
153
154   traces_set_list = xbt_dict_new();
155   trace_connect_list_host_avail = xbt_dict_new();
156   trace_connect_list_power = xbt_dict_new();
157   trace_connect_list_link_avail = xbt_dict_new();
158   trace_connect_list_bandwidth = xbt_dict_new();
159   trace_connect_list_latency = xbt_dict_new();
160
161   surfxml_add_callback(STag_surfxml_trace_cb_list, &parse_Stag_trace);
162   surfxml_add_callback(ETag_surfxml_trace_cb_list, &parse_Etag_trace);
163   surfxml_add_callback(STag_surfxml_trace_connect_cb_list,
164              &parse_Stag_trace_connect);
165 }
166
167 static void free_data(void)
168 {
169   xbt_dict_free(&trace_connect_list_host_avail);
170   xbt_dict_free(&trace_connect_list_power);
171   xbt_dict_free(&trace_connect_list_link_avail);
172   xbt_dict_free(&trace_connect_list_bandwidth);
173   xbt_dict_free(&trace_connect_list_latency);
174   xbt_dict_free(&traces_set_list);
175   xbt_dict_free(&random_data_list);
176   xbt_dynar_free(&surfxml_bufferstack_stack);
177 }
178
179 /* This function acts as a main in the parsing area. */
180 void parse_platform_file(const char *file)
181 {
182   int parse_status;
183
184
185   surf_parse_reset_callbacks();
186   surf_parse_add_callback_config();
187
188   surfxml_buffer_stack_stack_ptr = 1;
189   surfxml_buffer_stack_stack[0] = 0;
190
191   surf_parse_open(file);
192   init_data();
193   parse_status = surf_parse();
194   free_data();
195   surf_parse_close();
196   xbt_assert(!parse_status, "Parse error in %s", file);
197
198   surf_config_models_create_elms();
199 }
200