1 /* Copyright (c) 2006-2017. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "src/instr/instr_private.hpp" // 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"
14 #include "src/surf/xml/platf_private.hpp"
18 #include "src/bindings/lua/simgrid_lua.hpp"
20 #include <lua.h> /* Always include this when calling Lua */
21 #include <lauxlib.h> /* Always include this when calling Lua */
22 #include <lualib.h> /* Prototype for luaL_openlibs(), */
26 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
28 /* Trace related stuff */
29 XBT_PRIVATE std::unordered_map<std::string, tmgr_trace_t> traces_set_list;
30 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_host_avail;
31 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_host_speed;
32 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_avail;
33 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_bw;
34 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_lat;
37 void sg_platf_trace_connect(TraceConnectCreationArgs* trace_connect)
39 xbt_assert(traces_set_list.find(trace_connect->trace) != traces_set_list.end(),
40 "Cannot connect trace %s to %s: trace unknown", trace_connect->trace.c_str(),
41 trace_connect->element.c_str());
43 switch (trace_connect->kind) {
44 case TraceConnectKind::HOST_AVAIL:
45 trace_connect_list_host_avail.insert({trace_connect->trace, trace_connect->element});
47 case TraceConnectKind::SPEED:
48 trace_connect_list_host_speed.insert({trace_connect->trace, trace_connect->element});
50 case TraceConnectKind::LINK_AVAIL:
51 trace_connect_list_link_avail.insert({trace_connect->trace, trace_connect->element});
53 case TraceConnectKind::BANDWIDTH:
54 trace_connect_list_link_bw.insert({trace_connect->trace, trace_connect->element});
56 case TraceConnectKind::LATENCY:
57 trace_connect_list_link_lat.insert({trace_connect->trace, trace_connect->element});
60 surf_parse_error(std::string("Cannot connect trace ") + trace_connect->trace + " to " + trace_connect->element +
61 ": unknown kind of trace");
66 static int after_config_done;
67 void parse_after_config() {
68 if (not after_config_done) {
71 /* Register classical callbacks */
72 storage_register_callbacks();
74 after_config_done = 1;
78 /* This function acts as a main in the parsing area. */
79 void parse_platform_file(const char *file)
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');
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
94 lua_State* L = luaL_newstate();
97 luaL_loadfile(L, file); // This loads the file without executing it.
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");
113 /* init the flex parser */
114 after_config_done = 0;
115 surf_parse_open(file);
117 /* Do the actual parsing */
118 parse_status = surf_parse();
120 /* connect all traces relative to hosts */
121 for (auto const& elm : trace_connect_list_host_avail) {
122 xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
123 tmgr_trace_t trace = traces_set_list.at(elm.first);
125 simgrid::s4u::Host* host = sg_host_by_name(elm.second.c_str());
126 xbt_assert(host, "Host %s undefined", elm.second.c_str());
127 simgrid::surf::Cpu* cpu = host->pimpl_cpu;
129 cpu->setStateTrace(trace);
132 for (auto const& elm : trace_connect_list_host_speed) {
133 xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
134 tmgr_trace_t trace = traces_set_list.at(elm.first);
136 simgrid::s4u::Host* host = sg_host_by_name(elm.second.c_str());
137 xbt_assert(host, "Host %s undefined", elm.second.c_str());
138 simgrid::surf::Cpu* cpu = host->pimpl_cpu;
140 cpu->setSpeedTrace(trace);
143 for (auto const& elm : trace_connect_list_link_avail) {
144 xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
145 tmgr_trace_t trace = traces_set_list.at(elm.first);
147 sg_link_t link = simgrid::s4u::Link::byName(elm.second.c_str());
148 xbt_assert(link, "Link %s undefined", elm.second.c_str());
149 link->setStateTrace(trace);
152 for (auto const& elm : trace_connect_list_link_bw) {
153 xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
154 tmgr_trace_t trace = traces_set_list.at(elm.first);
155 sg_link_t link = simgrid::s4u::Link::byName(elm.second.c_str());
156 xbt_assert(link, "Link %s undefined", elm.second.c_str());
157 link->setBandwidthTrace(trace);
160 for (auto const& elm : trace_connect_list_link_lat) {
161 xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
162 tmgr_trace_t trace = traces_set_list.at(elm.first);
163 sg_link_t link = simgrid::s4u::Link::byName(elm.second.c_str());
164 xbt_assert(link, "Link %s undefined", elm.second.c_str());
165 link->setLatencyTrace(trace);
171 surf_parse_error(std::string("Parse error in ") + file);