Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
21d9a558dd1972a4bf9e775ec4b4b225ad6a8b31
[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 "src/instr/instr_private.h" // TRACE_start(). FIXME: remove by subscribing tracing to the surf signals
8 #include "src/surf/cpu_interface.hpp"
9 #include "src/surf/network_interface.hpp"
10 #include "xbt/log.h"
11 #include "xbt/misc.h"
12 #include "xbt/str.h"
13 #include <vector>
14
15 #include "src/surf/xml/platf_private.hpp"
16
17 #if SIMGRID_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 /* Trace related stuff */
30 XBT_PRIVATE std::unordered_map<std::string, tmgr_trace_t> traces_set_list;
31 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_host_avail;
32 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_host_speed;
33 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_avail;
34 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_bw;
35 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_lat;
36
37 SG_BEGIN_DECL()
38 void sg_platf_trace_connect(sg_platf_trace_connect_cbarg_t trace_connect)
39 {
40   xbt_assert(traces_set_list.find(trace_connect->trace) != traces_set_list.end(),
41              "Cannot connect trace %s to %s: trace unknown", trace_connect->trace, trace_connect->element);
42
43   switch (trace_connect->kind) {
44   case SURF_TRACE_CONNECT_KIND_HOST_AVAIL:
45     trace_connect_list_host_avail.insert({trace_connect->trace, trace_connect->element});
46     break;
47   case SURF_TRACE_CONNECT_KIND_SPEED:
48     trace_connect_list_host_speed.insert({trace_connect->trace, trace_connect->element});
49     break;
50   case SURF_TRACE_CONNECT_KIND_LINK_AVAIL:
51     trace_connect_list_link_avail.insert({trace_connect->trace, trace_connect->element});
52     break;
53   case SURF_TRACE_CONNECT_KIND_BANDWIDTH:
54     trace_connect_list_link_bw.insert({trace_connect->trace, trace_connect->element});
55     break;
56   case SURF_TRACE_CONNECT_KIND_LATENCY:
57     trace_connect_list_link_lat.insert({trace_connect->trace, trace_connect->element});
58     break;
59   default:
60     surf_parse_error("Cannot connect trace %s to %s: kind of trace unknown", trace_connect->trace,
61                      trace_connect->element);
62     break;
63   }
64 }
65
66 static int after_config_done;
67 void parse_after_config() {
68   if (not after_config_done) {
69     TRACE_start();
70
71     /* Register classical callbacks */
72     storage_register_callbacks();
73
74     after_config_done = 1;
75   }
76 }
77
78 /* This function acts as a main in the parsing area. */
79 void parse_platform_file(const char *file)
80 {
81 #if SIMGRID_HAVE_LUA
82   int len    = (file == nullptr ? 0 : strlen(file));
83   int is_lua = (file != nullptr && len > 3 && file[len - 3] == 'l' && file[len - 2] == 'u' && file[len - 1] == 'a');
84 #endif
85
86   sg_platf_init();
87
88 #if SIMGRID_HAVE_LUA
89   /* Check if file extension is "lua". If so, we will use
90    * the lua bindings to parse the platform file (since it is
91    * written in lua). If not, we will use the (old?) XML parser
92    */
93   if (is_lua) {
94     lua_State* L = luaL_newstate();
95     luaL_openlibs(L);
96
97     luaL_loadfile(L, file); // This loads the file without executing it.
98
99     /* Run the script */
100     if (lua_pcall(L, 0, 0, 0)) {
101       XBT_ERROR("FATAL ERROR:\n  %s: %s\n\n", "Lua call failed. Error message:", lua_tostring(L, -1));
102       xbt_die("Lua call failed. See Log");
103     }
104     lua_close(L);
105   }
106   else
107 #endif
108   { // Use XML parser
109
110     int parse_status;
111
112     /* init the flex parser */
113     after_config_done = 0;
114     surf_parse_open(file);
115
116     /* Do the actual parsing */
117     parse_status = surf_parse();
118
119     /* connect all traces relative to hosts */
120     for (auto elm : trace_connect_list_host_avail) {
121       xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
122       tmgr_trace_t trace = traces_set_list.at(elm.first);
123
124       simgrid::s4u::Host* host = sg_host_by_name(elm.second.c_str());
125       xbt_assert(host, "Host %s undefined", elm.second.c_str());
126       simgrid::surf::Cpu *cpu = host->pimpl_cpu;
127
128       cpu->setStateTrace(trace);
129     }
130
131     for (auto elm : trace_connect_list_host_speed) {
132       xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
133       tmgr_trace_t trace = traces_set_list.at(elm.first);
134
135       simgrid::s4u::Host* host = sg_host_by_name(elm.second.c_str());
136       xbt_assert(host, "Host %s undefined", elm.second.c_str());
137       simgrid::surf::Cpu *cpu = host->pimpl_cpu;
138
139       cpu->setSpeedTrace(trace);
140     }
141
142     for (auto elm : trace_connect_list_link_avail) {
143       xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
144       tmgr_trace_t trace = traces_set_list.at(elm.first);
145
146       sg_link_t link = simgrid::s4u::Link::byName(elm.second.c_str());
147       xbt_assert(link, "Link %s undefined", elm.second.c_str());
148       link->setStateTrace(trace);
149     }
150
151     for (auto elm : trace_connect_list_link_bw) {
152       xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
153       tmgr_trace_t trace = traces_set_list.at(elm.first);
154       sg_link_t link = simgrid::s4u::Link::byName(elm.second.c_str());
155       xbt_assert(link, "Link %s undefined", elm.second.c_str());
156       link->setBandwidthTrace(trace);
157     }
158
159     for (auto elm : trace_connect_list_link_lat) {
160       xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
161       tmgr_trace_t trace = traces_set_list.at(elm.first);
162       sg_link_t link = simgrid::s4u::Link::byName(elm.second.c_str());
163       xbt_assert(link, "Link %s undefined", elm.second.c_str());
164       link->setLatencyTrace(trace);
165     }
166
167     surf_parse_close();
168
169     if (parse_status)
170       surf_parse_error("Parse error in %s", file);
171   }
172 }
173
174 SG_END_DECL()