Logo AND Algorithmique Numérique Distribuée

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