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 "src/instr/instr_private.hpp" // 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"
13 #include "src/surf/xml/platf_private.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(), */
25 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
27 /* Trace related stuff */
28 XBT_PRIVATE std::unordered_map<std::string, tmgr_trace_t> traces_set_list;
29 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_host_avail;
30 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_host_speed;
31 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_avail;
32 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_bw;
33 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_lat;
36 void sg_platf_trace_connect(simgrid::kernel::routing::TraceConnectCreationArgs* trace_connect)
38 xbt_assert(traces_set_list.find(trace_connect->trace) != traces_set_list.end(),
39 "Cannot connect trace %s to %s: trace unknown", trace_connect->trace.c_str(),
40 trace_connect->element.c_str());
42 switch (trace_connect->kind) {
43 case simgrid::kernel::routing::TraceConnectKind::HOST_AVAIL:
44 trace_connect_list_host_avail.insert({trace_connect->trace, trace_connect->element});
46 case simgrid::kernel::routing::TraceConnectKind::SPEED:
47 trace_connect_list_host_speed.insert({trace_connect->trace, trace_connect->element});
49 case simgrid::kernel::routing::TraceConnectKind::LINK_AVAIL:
50 trace_connect_list_link_avail.insert({trace_connect->trace, trace_connect->element});
52 case simgrid::kernel::routing::TraceConnectKind::BANDWIDTH:
53 trace_connect_list_link_bw.insert({trace_connect->trace, trace_connect->element});
55 case simgrid::kernel::routing::TraceConnectKind::LATENCY:
56 trace_connect_list_link_lat.insert({trace_connect->trace, trace_connect->element});
59 surf_parse_error(std::string("Cannot connect trace ") + trace_connect->trace + " to " + trace_connect->element +
60 ": unknown kind of trace");
65 static int after_config_done;
66 void parse_after_config() {
67 if (not after_config_done) {
70 /* Register classical callbacks */
71 storage_register_callbacks();
73 after_config_done = 1;
77 /* This function acts as a main in the parsing area. */
78 void parse_platform_file(const char *file)
81 int len = (file == nullptr ? 0 : strlen(file));
82 int is_lua = (file != nullptr && len > 3 && file[len - 3] == 'l' && file[len - 2] == 'u' && file[len - 1] == 'a');
88 /* Check if file extension is "lua". If so, we will use
89 * the lua bindings to parse the platform file (since it is
90 * written in lua). If not, we will use the (old?) XML parser
93 lua_State* L = luaL_newstate();
96 luaL_loadfile(L, file); // This loads the file without executing it.
99 if (lua_pcall(L, 0, 0, 0)) {
100 XBT_ERROR("FATAL ERROR:\n %s: %s\n\n", "Lua call failed. Error message:", lua_tostring(L, -1));
101 xbt_die("Lua call failed. See Log");
112 /* init the flex parser */
113 after_config_done = 0;
114 surf_parse_open(file);
116 /* Do the actual parsing */
117 parse_status = surf_parse();
119 /* connect all traces relative to hosts */
120 for (auto const& 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);
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;
128 cpu->setStateTrace(trace);
131 for (auto const& 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);
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;
139 cpu->setSpeedTrace(trace);
142 for (auto const& 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);
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);
151 for (auto const& 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);
159 for (auto const& 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);
170 surf_parse_error(std::string("Parse error in ") + file);