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 "surf/surf.hpp"
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"
16 #include "src/bindings/lua/simgrid_lua.hpp"
18 #include <lua.h> /* Always include this when calling Lua */
19 #include <lauxlib.h> /* Always include this when calling Lua */
20 #include <lualib.h> /* Prototype for luaL_openlibs(), */
23 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
25 /* Trace related stuff */
26 XBT_PRIVATE std::unordered_map<std::string, tmgr_trace_t> traces_set_list;
27 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_host_avail;
28 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_host_speed;
29 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_avail;
30 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_bw;
31 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_lat;
33 void sg_platf_trace_connect(simgrid::kernel::routing::TraceConnectCreationArgs* trace_connect)
35 xbt_assert(traces_set_list.find(trace_connect->trace) != traces_set_list.end(),
36 "Cannot connect trace %s to %s: trace unknown", trace_connect->trace.c_str(),
37 trace_connect->element.c_str());
39 switch (trace_connect->kind) {
40 case simgrid::kernel::routing::TraceConnectKind::HOST_AVAIL:
41 trace_connect_list_host_avail.insert({trace_connect->trace, trace_connect->element});
43 case simgrid::kernel::routing::TraceConnectKind::SPEED:
44 trace_connect_list_host_speed.insert({trace_connect->trace, trace_connect->element});
46 case simgrid::kernel::routing::TraceConnectKind::LINK_AVAIL:
47 trace_connect_list_link_avail.insert({trace_connect->trace, trace_connect->element});
49 case simgrid::kernel::routing::TraceConnectKind::BANDWIDTH:
50 trace_connect_list_link_bw.insert({trace_connect->trace, trace_connect->element});
52 case simgrid::kernel::routing::TraceConnectKind::LATENCY:
53 trace_connect_list_link_lat.insert({trace_connect->trace, trace_connect->element});
56 surf_parse_error(std::string("Cannot connect trace ") + trace_connect->trace + " to " + trace_connect->element +
57 ": unknown kind of trace");
62 /* This function acts as a main in the parsing area. */
63 void parse_platform_file(std::string file)
66 const char* cfile = file.c_str();
67 int len = strlen(cfile);
68 int is_lua = len > 3 && file[len - 3] == 'l' && file[len - 2] == 'u' && file[len - 1] == 'a';
74 /* Check if file extension is "lua". If so, we will use
75 * the lua bindings to parse the platform file (since it is
76 * written in lua). If not, we will use the (old?) XML parser
79 lua_State* L = luaL_newstate();
82 luaL_loadfile(L, cfile); // This loads the file without executing it.
85 if (lua_pcall(L, 0, 0, 0)) {
86 XBT_ERROR("FATAL ERROR:\n %s: %s\n\n", "Lua call failed. Error message:", lua_tostring(L, -1));
87 xbt_die("Lua call failed. See Log");
98 /* init the flex parser */
99 surf_parse_open(file);
101 /* Do the actual parsing */
102 parse_status = surf_parse();
104 /* connect all traces relative to hosts */
105 for (auto const& elm : trace_connect_list_host_avail) {
106 xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
107 tmgr_trace_t trace = traces_set_list.at(elm.first);
109 simgrid::s4u::Host* host = sg_host_by_name(elm.second.c_str());
110 xbt_assert(host, "Host %s undefined", elm.second.c_str());
111 simgrid::surf::Cpu* cpu = host->pimpl_cpu;
113 cpu->set_state_trace(trace);
116 for (auto const& elm : trace_connect_list_host_speed) {
117 xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
118 tmgr_trace_t trace = traces_set_list.at(elm.first);
120 simgrid::s4u::Host* host = sg_host_by_name(elm.second.c_str());
121 xbt_assert(host, "Host %s undefined", elm.second.c_str());
122 simgrid::surf::Cpu* cpu = host->pimpl_cpu;
124 cpu->set_speed_trace(trace);
127 for (auto const& elm : trace_connect_list_link_avail) {
128 xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
129 tmgr_trace_t trace = traces_set_list.at(elm.first);
131 sg_link_t link = simgrid::s4u::Link::by_name(elm.second.c_str());
132 xbt_assert(link, "Link %s undefined", elm.second.c_str());
133 link->set_state_trace(trace);
136 for (auto const& elm : trace_connect_list_link_bw) {
137 xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
138 tmgr_trace_t trace = traces_set_list.at(elm.first);
139 sg_link_t link = simgrid::s4u::Link::by_name(elm.second.c_str());
140 xbt_assert(link, "Link %s undefined", elm.second.c_str());
141 link->set_bandwidth_trace(trace);
144 for (auto const& elm : trace_connect_list_link_lat) {
145 xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
146 tmgr_trace_t trace = traces_set_list.at(elm.first);
147 sg_link_t link = simgrid::s4u::Link::by_name(elm.second.c_str());
148 xbt_assert(link, "Link %s undefined", elm.second.c_str());
149 link->set_latency_trace(trace);
155 surf_parse_error(std::string("Parse error in ") + file);