Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
using pointer to scalar is a strange way to return a value...
[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 "simgrid/platf.h"
12 #include "surf/surfxml_parse_private.h"
13 #include "surf/surf_private.h"
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
16
17 /*
18  *  Allow the cluster tag to mess with the parsing buffer.
19  * (this will probably become obsolete once the cluster tag do not mess with the parsing callbacks directly)
20  */
21
22 /* This buffer is used to store the original buffer before substituting it by out own buffer. Useful for the cluster tag */
23 static xbt_dynar_t surfxml_bufferstack_stack = NULL;
24 int surfxml_bufferstack_size = 2048;
25
26 static char *old_buff = NULL;
27
28 unsigned int surfxml_buffer_stack_stack_ptr;
29 unsigned int surfxml_buffer_stack_stack[1024];
30
31
32 void surfxml_bufferstack_push(int new)
33 {
34   if (!new)
35     old_buff = surfxml_bufferstack;
36   else {
37     xbt_dynar_push(surfxml_bufferstack_stack, &surfxml_bufferstack);
38     surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
39   }
40 }
41
42 void surfxml_bufferstack_pop(int new)
43 {
44   if (!new)
45     surfxml_bufferstack = old_buff;
46   else {
47     free(surfxml_bufferstack);
48     xbt_dynar_pop(surfxml_bufferstack_stack, &surfxml_bufferstack);
49   }
50 }
51
52 /*
53  * Pass arguments to parsing callback as structures to save some time (and allow callbacks to ignore some)
54  */
55
56 surf_parsing_router_arg_t struct_router = NULL;
57 surf_parsing_cluster_arg_t struct_cluster = NULL;
58 surf_parsing_peer_arg_t struct_peer = NULL;
59 surf_parsing_link_arg_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   trace_id = xbt_strdup(A_surfxml_trace_id);
80   trace_file = xbt_strdup(A_surfxml_trace_file);
81   trace_periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
82 }
83
84 static void parse_Etag_trace(void)
85 {
86   tmgr_trace_t trace;
87   if (!trace_file || strcmp(trace_file, "") != 0) {
88     trace = tmgr_trace_new(trace_file);
89   } else {
90     if (strcmp(surfxml_pcdata, "") == 0)
91       trace = NULL;
92     else
93       trace =
94           tmgr_trace_new_from_string(trace_id, surfxml_pcdata,
95                                      trace_periodicity);
96   }
97   xbt_dict_set(traces_set_list, trace_id, (void *) trace, NULL);
98   xbt_free(trace_file);
99   trace_file = NULL;
100   xbt_free(trace_id);
101   trace_id = NULL;
102 }
103
104 static void parse_Stag_trace_connect(void)
105 {
106   xbt_assert(xbt_dict_get_or_null
107               (traces_set_list, A_surfxml_trace_connect_trace),
108               "Cannot connect trace %s to %s: trace unknown",
109               A_surfxml_trace_connect_trace,
110               A_surfxml_trace_connect_element);
111
112   switch (A_surfxml_trace_connect_kind) {
113   case A_surfxml_trace_connect_kind_HOST_AVAIL:
114     xbt_dict_set(trace_connect_list_host_avail,
115                  A_surfxml_trace_connect_trace,
116                  xbt_strdup(A_surfxml_trace_connect_element), free);
117     break;
118   case A_surfxml_trace_connect_kind_POWER:
119     xbt_dict_set(trace_connect_list_power, A_surfxml_trace_connect_trace,
120                  xbt_strdup(A_surfxml_trace_connect_element), free);
121     break;
122   case A_surfxml_trace_connect_kind_LINK_AVAIL:
123     xbt_dict_set(trace_connect_list_link_avail,
124                  A_surfxml_trace_connect_trace,
125                  xbt_strdup(A_surfxml_trace_connect_element), free);
126     break;
127   case A_surfxml_trace_connect_kind_BANDWIDTH:
128     xbt_dict_set(trace_connect_list_bandwidth,
129                  A_surfxml_trace_connect_trace,
130                  xbt_strdup(A_surfxml_trace_connect_element), free);
131     break;
132   case A_surfxml_trace_connect_kind_LATENCY:
133     xbt_dict_set(trace_connect_list_latency, A_surfxml_trace_connect_trace,
134                  xbt_strdup(A_surfxml_trace_connect_element), free);
135     break;
136   default:
137     xbt_die("Cannot connect trace %s to %s: kind of trace unknown",
138             A_surfxml_trace_connect_trace, A_surfxml_trace_connect_element);
139     break;
140   }
141 }
142
143 /* Call the right C function when we see the <AS> tags */
144 static void parse_S_AS(void) {
145   sg_platf_new_AS_open(A_surfxml_AS_id, A_surfxml_AS_routing);
146 }
147 static void parse_E_AS(void) {
148   sg_platf_new_AS_close();
149 }
150
151
152 /* Init and free parse data */
153
154 static void init_data(void)
155 {
156   if (!surfxml_bufferstack_stack)
157     surfxml_bufferstack_stack = xbt_dynar_new(sizeof(char *), NULL);
158
159   traces_set_list = xbt_dict_new();
160   trace_connect_list_host_avail = xbt_dict_new();
161   trace_connect_list_power = xbt_dict_new();
162   trace_connect_list_link_avail = xbt_dict_new();
163   trace_connect_list_bandwidth = xbt_dict_new();
164   trace_connect_list_latency = xbt_dict_new();
165
166   surfxml_add_callback(STag_surfxml_trace_cb_list, &parse_Stag_trace);
167   surfxml_add_callback(ETag_surfxml_trace_cb_list, &parse_Etag_trace);
168   surfxml_add_callback(STag_surfxml_trace_connect_cb_list,
169              &parse_Stag_trace_connect);
170
171   /* we care about the ASes while parsing the platf. Incredible, isnt it? */
172   surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS);
173   surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS);
174 }
175
176 static void free_data(void)
177 {
178   xbt_dict_free(&trace_connect_list_host_avail);
179   xbt_dict_free(&trace_connect_list_power);
180   xbt_dict_free(&trace_connect_list_link_avail);
181   xbt_dict_free(&trace_connect_list_bandwidth);
182   xbt_dict_free(&trace_connect_list_latency);
183   xbt_dict_free(&traces_set_list);
184   xbt_dict_free(&random_data_list);
185   xbt_dynar_free(&surfxml_bufferstack_stack);
186 }
187
188 /* This function acts as a main in the parsing area. */
189 void parse_platform_file(const char *file)
190 {
191   int parse_status;
192
193   surf_parse_reset_callbacks();
194   surf_parse_add_callback_config();
195
196   surfxml_buffer_stack_stack_ptr = 1;
197   surfxml_buffer_stack_stack[0] = 0;
198
199   surf_parse_open(file);
200   init_data();
201   parse_status = surf_parse();
202   free_data();
203   surf_parse_close();
204   if (parse_status)
205     xbt_die("Parse error in %s", file);
206
207   surf_config_models_create_elms();
208 }
209