Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Call surf_parse_models_setup() from the right level
[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 surf_parsing_router_arg_t struct_router = NULL;
56 surf_parsing_cluster_arg_t struct_cluster = NULL;
57 surf_parsing_peer_arg_t struct_peer = NULL;
58 surf_parsing_link_arg_t struct_lnk = NULL;
59
60
61 /*
62  * Trace related stuff
63  */
64
65 xbt_dict_t traces_set_list = NULL;
66 xbt_dict_t trace_connect_list_host_avail = NULL;
67 xbt_dict_t trace_connect_list_power = NULL;
68 xbt_dict_t trace_connect_list_link_avail = NULL;
69 xbt_dict_t trace_connect_list_bandwidth = NULL;
70 xbt_dict_t trace_connect_list_latency = NULL;
71
72 static double trace_periodicity = -1.0;
73 static char *trace_file = NULL;
74 static char *trace_id = NULL;
75
76 static void parse_Stag_trace(void)
77 {
78   trace_id = xbt_strdup(A_surfxml_trace_id);
79   trace_file = xbt_strdup(A_surfxml_trace_file);
80   surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
81 }
82
83 static void parse_Etag_trace(void)
84 {
85   tmgr_trace_t trace;
86   if (!trace_file || strcmp(trace_file, "") != 0) {
87     trace = tmgr_trace_new(trace_file);
88   } else {
89     if (strcmp(surfxml_pcdata, "") == 0)
90       trace = NULL;
91     else
92       trace =
93           tmgr_trace_new_from_string(trace_id, surfxml_pcdata,
94                                      trace_periodicity);
95   }
96   xbt_dict_set(traces_set_list, trace_id, (void *) trace, NULL);
97   xbt_free(trace_file);
98   trace_file = NULL;
99   xbt_free(trace_id);
100   trace_id = NULL;
101 }
102
103 static void parse_Stag_trace_connect(void)
104 {
105   xbt_assert(xbt_dict_get_or_null
106               (traces_set_list, A_surfxml_trace_connect_trace),
107               "Cannot connect trace %s to %s: trace unknown",
108               A_surfxml_trace_connect_trace,
109               A_surfxml_trace_connect_element);
110
111   switch (A_surfxml_trace_connect_kind) {
112   case A_surfxml_trace_connect_kind_HOST_AVAIL:
113     xbt_dict_set(trace_connect_list_host_avail,
114                  A_surfxml_trace_connect_trace,
115                  xbt_strdup(A_surfxml_trace_connect_element), free);
116     break;
117   case A_surfxml_trace_connect_kind_POWER:
118     xbt_dict_set(trace_connect_list_power, A_surfxml_trace_connect_trace,
119                  xbt_strdup(A_surfxml_trace_connect_element), free);
120     break;
121   case A_surfxml_trace_connect_kind_LINK_AVAIL:
122     xbt_dict_set(trace_connect_list_link_avail,
123                  A_surfxml_trace_connect_trace,
124                  xbt_strdup(A_surfxml_trace_connect_element), free);
125     break;
126   case A_surfxml_trace_connect_kind_BANDWIDTH:
127     xbt_dict_set(trace_connect_list_bandwidth,
128                  A_surfxml_trace_connect_trace,
129                  xbt_strdup(A_surfxml_trace_connect_element), free);
130     break;
131   case A_surfxml_trace_connect_kind_LATENCY:
132     xbt_dict_set(trace_connect_list_latency, A_surfxml_trace_connect_trace,
133                  xbt_strdup(A_surfxml_trace_connect_element), free);
134     break;
135   default:
136     xbt_die("Cannot connect trace %s to %s: kind of trace unknown",
137             A_surfxml_trace_connect_trace, A_surfxml_trace_connect_element);
138     break;
139   }
140 }
141
142 /* Call the right C function when we see the <AS> tags */
143 static void parse_S_AS(void) {
144   routing_AS_init(A_surfxml_AS_id, A_surfxml_AS_routing);
145 }
146 static void parse_E_AS(void) {
147   routing_AS_end(A_surfxml_AS_id);
148 }
149
150
151 /* Init and free parse data */
152
153 static void init_data(void)
154 {
155   if (!surfxml_bufferstack_stack)
156     surfxml_bufferstack_stack = xbt_dynar_new(sizeof(char *), NULL);
157
158   traces_set_list = xbt_dict_new();
159   trace_connect_list_host_avail = xbt_dict_new();
160   trace_connect_list_power = xbt_dict_new();
161   trace_connect_list_link_avail = xbt_dict_new();
162   trace_connect_list_bandwidth = xbt_dict_new();
163   trace_connect_list_latency = xbt_dict_new();
164
165   surfxml_add_callback(STag_surfxml_trace_cb_list, &parse_Stag_trace);
166   surfxml_add_callback(ETag_surfxml_trace_cb_list, &parse_Etag_trace);
167   surfxml_add_callback(STag_surfxml_trace_connect_cb_list,
168              &parse_Stag_trace_connect);
169
170   /* we care about the ASes while parsing the platf. Incredible, isnt it? */
171   surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS);
172   surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS);
173 }
174
175 static void free_data(void)
176 {
177   xbt_dict_free(&trace_connect_list_host_avail);
178   xbt_dict_free(&trace_connect_list_power);
179   xbt_dict_free(&trace_connect_list_link_avail);
180   xbt_dict_free(&trace_connect_list_bandwidth);
181   xbt_dict_free(&trace_connect_list_latency);
182   xbt_dict_free(&traces_set_list);
183   xbt_dict_free(&random_data_list);
184   xbt_dynar_free(&surfxml_bufferstack_stack);
185 }
186
187 /* This function acts as a main in the parsing area. */
188 void parse_platform_file(const char *file)
189 {
190   int parse_status;
191
192   surf_parse_reset_callbacks();
193   surf_parse_add_callback_config();
194
195   surfxml_buffer_stack_stack_ptr = 1;
196   surfxml_buffer_stack_stack[0] = 0;
197
198   surf_parse_open(file);
199   init_data();
200   parse_status = surf_parse();
201   free_data();
202   surf_parse_close();
203   if (parse_status)
204     xbt_die("Parse error in %s", file);
205
206   surf_config_models_create_elms();
207 }
208