1 /* Copyright (c) 2006-2018. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "instr/instr_interface.h" // TRACE_start(). FIXME: remove by subscribing tracing to the surf signals
7 #include "src/surf/cpu_interface.hpp"
8 #include "src/surf/network_interface.hpp"
9 #include "src/surf/surf_interface.hpp"
10 #include "src/surf/xml/platf_private.hpp"
11 #include "surf/surf.hpp"
17 #include "src/bindings/lua/simgrid_lua.hpp"
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(), */
24 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
26 /* Trace related stuff */
27 XBT_PRIVATE std::unordered_map<std::string, tmgr_trace_t> traces_set_list;
28 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_host_avail;
29 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_host_speed;
30 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_avail;
31 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_bw;
32 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_lat;
34 void sg_platf_trace_connect(simgrid::kernel::routing::TraceConnectCreationArgs* trace_connect)
36 xbt_assert(traces_set_list.find(trace_connect->trace) != traces_set_list.end(),
37 "Cannot connect trace %s to %s: trace unknown", trace_connect->trace.c_str(),
38 trace_connect->element.c_str());
40 switch (trace_connect->kind) {
41 case simgrid::kernel::routing::TraceConnectKind::HOST_AVAIL:
42 trace_connect_list_host_avail.insert({trace_connect->trace, trace_connect->element});
44 case simgrid::kernel::routing::TraceConnectKind::SPEED:
45 trace_connect_list_host_speed.insert({trace_connect->trace, trace_connect->element});
47 case simgrid::kernel::routing::TraceConnectKind::LINK_AVAIL:
48 trace_connect_list_link_avail.insert({trace_connect->trace, trace_connect->element});
50 case simgrid::kernel::routing::TraceConnectKind::BANDWIDTH:
51 trace_connect_list_link_bw.insert({trace_connect->trace, trace_connect->element});
53 case simgrid::kernel::routing::TraceConnectKind::LATENCY:
54 trace_connect_list_link_lat.insert({trace_connect->trace, trace_connect->element});
57 surf_parse_error(std::string("Cannot connect trace ") + trace_connect->trace + " to " + trace_connect->element +
58 ": unknown kind of trace");
63 static int after_config_done;
64 void parse_after_config() {
65 if (not after_config_done) {
68 /* Register classical callbacks */
69 storage_register_callbacks();
71 after_config_done = 1;
75 /* This function acts as a main in the parsing area. */
76 void parse_platform_file(const char *file)
79 int len = (file == nullptr ? 0 : strlen(file));
80 int is_lua = (file != nullptr && len > 3 && file[len - 3] == 'l' && file[len - 2] == 'u' && file[len - 1] == 'a');
86 /* Check if file extension is "lua". If so, we will use
87 * the lua bindings to parse the platform file (since it is
88 * written in lua). If not, we will use the (old?) XML parser
91 lua_State* L = luaL_newstate();
94 luaL_loadfile(L, file); // This loads the file without executing it.
97 if (lua_pcall(L, 0, 0, 0)) {
98 XBT_ERROR("FATAL ERROR:\n %s: %s\n\n", "Lua call failed. Error message:", lua_tostring(L, -1));
99 xbt_die("Lua call failed. See Log");
110 /* init the flex parser */
111 after_config_done = 0;
112 surf_parse_open(file);
114 /* Do the actual parsing */
115 parse_status = surf_parse();
117 /* connect all traces relative to hosts */
118 for (auto const& elm : trace_connect_list_host_avail) {
119 xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
120 tmgr_trace_t trace = traces_set_list.at(elm.first);
122 simgrid::s4u::Host* host = sg_host_by_name(elm.second.c_str());
123 xbt_assert(host, "Host %s undefined", elm.second.c_str());
124 simgrid::surf::Cpu* cpu = host->pimpl_cpu;
126 cpu->setStateTrace(trace);
129 for (auto const& elm : trace_connect_list_host_speed) {
130 xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
131 tmgr_trace_t trace = traces_set_list.at(elm.first);
133 simgrid::s4u::Host* host = sg_host_by_name(elm.second.c_str());
134 xbt_assert(host, "Host %s undefined", elm.second.c_str());
135 simgrid::surf::Cpu* cpu = host->pimpl_cpu;
137 cpu->set_speed_trace(trace);
140 for (auto const& elm : trace_connect_list_link_avail) {
141 xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
142 tmgr_trace_t trace = traces_set_list.at(elm.first);
144 sg_link_t link = simgrid::s4u::Link::byName(elm.second.c_str());
145 xbt_assert(link, "Link %s undefined", elm.second.c_str());
146 link->setStateTrace(trace);
149 for (auto const& elm : trace_connect_list_link_bw) {
150 xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
151 tmgr_trace_t trace = traces_set_list.at(elm.first);
152 sg_link_t link = simgrid::s4u::Link::byName(elm.second.c_str());
153 xbt_assert(link, "Link %s undefined", elm.second.c_str());
154 link->setBandwidthTrace(trace);
157 for (auto const& elm : trace_connect_list_link_lat) {
158 xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
159 tmgr_trace_t trace = traces_set_list.at(elm.first);
160 sg_link_t link = simgrid::s4u::Link::byName(elm.second.c_str());
161 xbt_assert(link, "Link %s undefined", elm.second.c_str());
162 link->setLatencyTrace(trace);
168 surf_parse_error(std::string("Parse error in ") + file);