Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
33498c33c78d539bd8b52798ba35d65d0b73b2e8
[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  * Trace related stuff
54  */
55
56 xbt_dict_t traces_set_list = NULL;
57 xbt_dict_t trace_connect_list_host_avail = NULL;
58 xbt_dict_t trace_connect_list_power = NULL;
59 xbt_dict_t trace_connect_list_link_avail = NULL;
60 xbt_dict_t trace_connect_list_bandwidth = NULL;
61 xbt_dict_t trace_connect_list_latency = NULL;
62
63 static double trace_periodicity = -1.0;
64 static char *trace_file = NULL;
65 static char *trace_id = NULL;
66
67 static void parse_Stag_trace(void)
68 {
69   trace_id = xbt_strdup(A_surfxml_trace_id);
70   trace_file = xbt_strdup(A_surfxml_trace_file);
71   trace_periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
72 }
73
74 static void parse_Etag_trace(void)
75 {
76   tmgr_trace_t trace;
77   if (!trace_file || strcmp(trace_file, "") != 0) {
78     trace = tmgr_trace_new(trace_file);
79   } else {
80     if (strcmp(surfxml_pcdata, "") == 0)
81       trace = NULL;
82     else
83       trace =
84           tmgr_trace_new_from_string(trace_id, surfxml_pcdata,
85                                      trace_periodicity);
86   }
87   xbt_dict_set(traces_set_list, trace_id, (void *) trace, NULL);
88   xbt_free(trace_file);
89   trace_file = NULL;
90   xbt_free(trace_id);
91   trace_id = NULL;
92 }
93
94 static void parse_Stag_trace_connect(void)
95 {
96   xbt_assert(xbt_dict_get_or_null
97               (traces_set_list, A_surfxml_trace_connect_trace),
98               "Cannot connect trace %s to %s: trace unknown",
99               A_surfxml_trace_connect_trace,
100               A_surfxml_trace_connect_element);
101
102   switch (A_surfxml_trace_connect_kind) {
103   case A_surfxml_trace_connect_kind_HOST_AVAIL:
104     xbt_dict_set(trace_connect_list_host_avail,
105                  A_surfxml_trace_connect_trace,
106                  xbt_strdup(A_surfxml_trace_connect_element), free);
107     break;
108   case A_surfxml_trace_connect_kind_POWER:
109     xbt_dict_set(trace_connect_list_power, A_surfxml_trace_connect_trace,
110                  xbt_strdup(A_surfxml_trace_connect_element), free);
111     break;
112   case A_surfxml_trace_connect_kind_LINK_AVAIL:
113     xbt_dict_set(trace_connect_list_link_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_BANDWIDTH:
118     xbt_dict_set(trace_connect_list_bandwidth,
119                  A_surfxml_trace_connect_trace,
120                  xbt_strdup(A_surfxml_trace_connect_element), free);
121     break;
122   case A_surfxml_trace_connect_kind_LATENCY:
123     xbt_dict_set(trace_connect_list_latency, A_surfxml_trace_connect_trace,
124                  xbt_strdup(A_surfxml_trace_connect_element), free);
125     break;
126   default:
127     xbt_die("Cannot connect trace %s to %s: kind of trace unknown",
128             A_surfxml_trace_connect_trace, A_surfxml_trace_connect_element);
129     break;
130   }
131 }
132
133 /* This function acts as a main in the parsing area. */
134 void parse_platform_file(const char *file)
135 {
136   int parse_status;
137
138   surf_parse_reset_callbacks();
139
140   /* Register classical callbacks */
141   surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties_XML);
142   routing_register_callbacks();
143
144   /* init the flex parser */
145   surfxml_buffer_stack_stack_ptr = 1;
146   surfxml_buffer_stack_stack[0] = 0;
147
148   surf_parse_open(file);
149
150   /* Init my data */
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   /* we care about the ASes while parsing the platf. Incredible, isnt it? */
167   sg_platf_AS_end_add_cb(routing_AS_end);
168   sg_platf_AS_begin_add_cb(routing_AS_begin);
169
170
171   /* Do the actual parsing */
172   parse_status = surf_parse();
173
174   /* Free my data */
175   xbt_dict_free(&trace_connect_list_host_avail);
176   xbt_dict_free(&trace_connect_list_power);
177   xbt_dict_free(&trace_connect_list_link_avail);
178   xbt_dict_free(&trace_connect_list_bandwidth);
179   xbt_dict_free(&trace_connect_list_latency);
180   xbt_dict_free(&traces_set_list);
181   xbt_dict_free(&random_data_list);
182   xbt_dynar_free(&surfxml_bufferstack_stack);
183
184   /* Stop the flex parser */
185   surf_parse_close();
186   if (parse_status)
187     xbt_die("Parse error in %s", file);
188 }
189