Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3e889ee5cb72c4eeff441ee1a3a2718186cf71ee
[simgrid.git] / src / surf / surfxml_parseplatf.cpp
1 /* Copyright (c) 2006-2015. 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 "src/surf/cpu_interface.hpp"
13 #include "src/surf/network_interface.hpp"
14
15 #ifdef HAVE_LUA
16 extern "C" {
17 #include "src/bindings/lua/simgrid_lua.h"
18
19 #include <lua.h>                /* Always include this when calling Lua */
20 #include <lauxlib.h>            /* Always include this when calling Lua */
21 #include <lualib.h>             /* Prototype for luaL_openlibs(), */
22 }
23 #endif
24
25 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
26
27 /*
28  *  Allow the cluster tag to mess with the parsing buffer.
29  * (this will probably become obsolete once the cluster tag do not mess with the parsing callbacks directly)
30  */
31
32 /* This buffer is used to store the original buffer before substituting it by out own buffer. Useful for the cluster tag */
33 static xbt_dynar_t surfxml_bufferstack_stack = NULL;
34 int surfxml_bufferstack_size = 2048;
35
36 static char *old_buff = NULL;
37
38 XBT_IMPORT_NO_EXPORT(unsigned int) surfxml_buffer_stack_stack_ptr;
39 XBT_IMPORT_NO_EXPORT(unsigned int) surfxml_buffer_stack_stack[1024];
40
41 void surfxml_bufferstack_push(int new_one)
42 {
43   if (!new_one)
44     old_buff = surfxml_bufferstack;
45   else {
46     xbt_dynar_push(surfxml_bufferstack_stack, &surfxml_bufferstack);
47     surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
48   }
49 }
50
51 void surfxml_bufferstack_pop(int new_one)
52 {
53   if (!new_one)
54     surfxml_bufferstack = old_buff;
55   else {
56     free(surfxml_bufferstack);
57     xbt_dynar_pop(surfxml_bufferstack_stack, &surfxml_bufferstack);
58   }
59 }
60
61 /*
62  * Trace related stuff
63  */
64
65 xbt_dict_t traces_set_list = NULL;
66 XBT_PRIVATE xbt_dict_t trace_connect_list_host_avail = NULL;
67 XBT_PRIVATE xbt_dict_t trace_connect_list_host_speed = NULL;
68 XBT_PRIVATE xbt_dict_t trace_connect_list_link_avail = NULL;
69 XBT_PRIVATE xbt_dict_t trace_connect_list_link_bw = NULL;
70 XBT_PRIVATE xbt_dict_t trace_connect_list_link_lat = NULL;
71
72 void sg_platf_trace_connect(sg_platf_trace_connect_cbarg_t trace_connect)
73 {
74   xbt_assert(xbt_dict_get_or_null(traces_set_list, trace_connect->trace),
75               "Cannot connect trace %s to %s: trace unknown",
76               trace_connect->trace,
77               trace_connect->element);
78
79   switch (trace_connect->kind) {
80   case SURF_TRACE_CONNECT_KIND_HOST_AVAIL:
81     xbt_dict_set(trace_connect_list_host_avail,
82         trace_connect->trace,
83         xbt_strdup(trace_connect->element), NULL);
84     break;
85   case SURF_TRACE_CONNECT_KIND_SPEED:
86     xbt_dict_set(trace_connect_list_host_speed, trace_connect->trace,
87         xbt_strdup(trace_connect->element), NULL);
88     break;
89   case SURF_TRACE_CONNECT_KIND_LINK_AVAIL:
90     xbt_dict_set(trace_connect_list_link_avail,
91         trace_connect->trace,
92         xbt_strdup(trace_connect->element), NULL);
93     break;
94   case SURF_TRACE_CONNECT_KIND_BANDWIDTH:
95     xbt_dict_set(trace_connect_list_link_bw,
96         trace_connect->trace,
97         xbt_strdup(trace_connect->element), NULL);
98     break;
99   case SURF_TRACE_CONNECT_KIND_LATENCY:
100     xbt_dict_set(trace_connect_list_link_lat, trace_connect->trace,
101         xbt_strdup(trace_connect->element), NULL);
102     break;
103   default:
104   surf_parse_error("Cannot connect trace %s to %s: kind of trace unknown",
105         trace_connect->trace, trace_connect->element);
106     break;
107   }
108 }
109
110
111 /* ***************************************** */
112
113 static int after_config_done;
114 void parse_after_config() {
115   if (!after_config_done) {
116     TRACE_start();
117
118     /* Register classical callbacks */
119     storage_register_callbacks();
120     routing_register_callbacks();
121
122     after_config_done = 1;
123   }
124 }
125
126 /* This function acts as a main in the parsing area. */
127 void parse_platform_file(const char *file)
128 {
129 #ifdef HAVE_LUA
130   int is_lua = (file != NULL && strlen(file) > 3 && file[strlen(file)-3] == 'l' && file[strlen(file)-2] == 'u'
131         && file[strlen(file)-1] == 'a');
132 #endif
133
134   surf_parse_init_callbacks();
135
136 #ifdef HAVE_LUA
137   /* Check if file extension is "lua". If so, we will use
138    * the lua bindings to parse the platform file (since it is
139    * written in lua). If not, we will use the (old?) XML parser
140    */
141   if (is_lua) {
142     lua_State* L = luaL_newstate();
143     luaL_openlibs(L);
144
145     luaL_loadfile(L, file); // This loads the file without executing it.
146
147     /* Run the script */
148     if (lua_pcall(L, 0, 0, 0)) {
149         XBT_ERROR("FATAL ERROR:\n  %s: %s\n\n", "Lua call failed. Errormessage:", lua_tostring(L, -1));
150         xbt_die("Lua call failed. See Log");
151     }
152   }
153   else
154 #endif
155   { // Use XML parser
156
157     int parse_status;
158
159     /* init the flex parser */
160     surfxml_buffer_stack_stack_ptr = 1;
161     surfxml_buffer_stack_stack[0] = 0;
162     after_config_done = 0;
163     surf_parse_open(file);
164
165     traces_set_list = xbt_dict_new_homogeneous(NULL);
166     trace_connect_list_host_avail = xbt_dict_new_homogeneous(free);
167     trace_connect_list_host_speed = xbt_dict_new_homogeneous(free);
168     trace_connect_list_link_avail = xbt_dict_new_homogeneous(free);
169     trace_connect_list_link_bw = xbt_dict_new_homogeneous(free);
170     trace_connect_list_link_lat = xbt_dict_new_homogeneous(free);
171
172     /* Init my data */
173     if (!surfxml_bufferstack_stack)
174       surfxml_bufferstack_stack = xbt_dynar_new(sizeof(char *), NULL);
175
176     /* Do the actual parsing */
177     parse_status = surf_parse();
178
179     /* connect all traces relative to hosts */
180     xbt_dict_cursor_t cursor = NULL;
181     char *trace_name, *elm;
182
183     xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
184       tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
185       xbt_assert(trace, "Trace %s undefined", trace_name);
186
187       simgrid::s4u::Host *host = sg_host_by_name(elm);
188       xbt_assert(host, "Host %s undefined", elm);
189       simgrid::surf::Cpu *cpu = host->pimpl_cpu;
190
191       cpu->set_state_trace(trace);
192     }
193     xbt_dict_foreach(trace_connect_list_host_speed, cursor, trace_name, elm) {
194       tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
195       xbt_assert(trace, "Trace %s undefined", trace_name);
196
197       simgrid::s4u::Host *host = sg_host_by_name(elm);
198       xbt_assert(host, "Host %s undefined", elm);
199       simgrid::surf::Cpu *cpu = host->pimpl_cpu;
200
201       cpu->set_speed_trace(trace);
202     }
203     xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
204       tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
205       xbt_assert(trace, "Trace %s undefined", trace_name);
206       Link *link = Link::byName(elm);
207       xbt_assert(link, "Link %s undefined", elm);
208       link->set_state_trace(trace);
209     }
210
211     xbt_dict_foreach(trace_connect_list_link_bw, cursor, trace_name, elm) {
212       tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
213       xbt_assert(trace, "Trace %s undefined", trace_name);
214       Link *link = Link::byName(elm);
215       xbt_assert(link, "Link %s undefined", elm);
216       link->set_bandwidth_trace(trace);
217     }
218
219     xbt_dict_foreach(trace_connect_list_link_lat, cursor, trace_name, elm) {
220       tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
221       xbt_assert(trace, "Trace %s undefined", trace_name);
222       Link *link = Link::byName(elm);
223       xbt_assert(link, "Link %s undefined", elm);
224       link->set_latency_trace(trace);
225     }
226
227     /* Free my data */
228     xbt_dict_free(&trace_connect_list_host_avail);
229     xbt_dict_free(&trace_connect_list_host_speed);
230     xbt_dict_free(&trace_connect_list_link_avail);
231     xbt_dict_free(&trace_connect_list_link_bw);
232     xbt_dict_free(&trace_connect_list_link_lat);
233     xbt_dict_free(&traces_set_list);
234     xbt_dynar_free(&surfxml_bufferstack_stack);
235
236     surf_parse_close();
237
238     if (parse_status)
239       surf_parse_error("Parse error in %s", file);
240
241   }
242
243
244 }