Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
simplify
[simgrid.git] / src / surf / xml / surfxml_parseplatf.cpp
1 /* Copyright (c) 2006-2021. The SimGrid Team. All rights reserved.          */
2
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. */
5
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"
11
12 #include <vector>
13
14
15 #if SIMGRID_HAVE_LUA
16 #include "src/bindings/lua/simgrid_lua.hpp"
17
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(), */
21 #endif
22
23 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
24
25 /* Trace related stuff */
26 XBT_PRIVATE std::unordered_map<std::string, simgrid::kernel::profile::Profile*> 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;
32
33 void sg_platf_trace_connect(simgrid::kernel::routing::TraceConnectCreationArgs* trace_connect)
34 {
35   surf_parse_assert(traces_set_list.find(trace_connect->trace) != traces_set_list.end(),
36          std::string("Cannot connect trace ")+ trace_connect->trace+ " to "+trace_connect->element+": trace unknown");
37
38   switch (trace_connect->kind) {
39     case simgrid::kernel::routing::TraceConnectKind::HOST_AVAIL:
40       trace_connect_list_host_avail.insert({trace_connect->trace, trace_connect->element});
41       break;
42     case simgrid::kernel::routing::TraceConnectKind::SPEED:
43       trace_connect_list_host_speed.insert({trace_connect->trace, trace_connect->element});
44       break;
45     case simgrid::kernel::routing::TraceConnectKind::LINK_AVAIL:
46       trace_connect_list_link_avail.insert({trace_connect->trace, trace_connect->element});
47       break;
48     case simgrid::kernel::routing::TraceConnectKind::BANDWIDTH:
49       trace_connect_list_link_bw.insert({trace_connect->trace, trace_connect->element});
50       break;
51     case simgrid::kernel::routing::TraceConnectKind::LATENCY:
52       trace_connect_list_link_lat.insert({trace_connect->trace, trace_connect->element});
53       break;
54     default:
55       surf_parse_error(std::string("Cannot connect trace ") + trace_connect->trace + " to " + trace_connect->element +
56                        ": unknown kind of trace");
57   }
58 }
59
60 /* This function acts as a main in the parsing area. */
61 void parse_platform_file(const std::string& file)
62 {
63   const char* cfile = file.c_str();
64   size_t len        = strlen(cfile);
65   bool is_lua       = len > 3 && file[len - 3] == 'l' && file[len - 2] == 'u' && file[len - 1] == 'a';
66
67   sg_platf_init();
68
69   /* Check if file extension is "lua". If so, we will use
70    * the lua bindings to parse the platform file (since it is
71    * written in lua). If not, we will use the (old?) XML parser
72    */
73   if (is_lua) {
74 #if SIMGRID_HAVE_LUA
75     static bool already_warned = false;
76     if (not already_warned) { // XBT_ATTRIB_DEPRECATED_v332
77       XBT_WARN("You are using a lua platform file. This feature is deprecated and will disappear after SimGrid v3.31.");
78       already_warned = true;
79     }
80     lua_State* L = luaL_newstate();
81     luaL_openlibs(L);
82
83     luaL_loadfile(L, cfile); // This loads the file without executing it.
84
85     /* Run the script */
86     xbt_assert(lua_pcall(L, 0, 0, 0) == 0, "FATAL ERROR:\n  %s: %s\n\n", "Lua call failed. Error message:",
87                lua_tostring(L, -1));
88     lua_close(L);
89     return;
90 #else
91     XBT_WARN("This looks like a lua platform file, but your SimGrid was not compiled with lua. Loading it as XML.");
92 #endif
93   }
94
95   // Use XML parser
96
97   /* init the flex parser */
98   surf_parse_open(file);
99
100   /* Do the actual parsing */
101   surf_parse();
102
103   /* connect all profiles relative to hosts */
104   for (auto const& elm : trace_connect_list_host_avail) {
105     surf_parse_assert(traces_set_list.find(elm.first) != traces_set_list.end(), std::string("<trace_connect kind=\"HOST_AVAIL\">: Trace ")+elm.first+" undefined.");
106     auto profile = traces_set_list.at(elm.first);
107
108     auto host = simgrid::s4u::Host::by_name_or_null(elm.second);
109     surf_parse_assert(host, std::string("<trace_connect kind=\"HOST_AVAIL\">: Host ") + elm.second + " undefined.");
110     host->set_state_profile(profile);
111   }
112
113   for (auto const& elm : trace_connect_list_host_speed) {
114     surf_parse_assert(traces_set_list.find(elm.first) != traces_set_list.end(), std::string("<trace_connect kind=\"SPEED\">: Trace ")+elm.first+" undefined.");
115     auto profile = traces_set_list.at(elm.first);
116
117     auto host = simgrid::s4u::Host::by_name_or_null(elm.second);
118     surf_parse_assert(host, std::string("<trace_connect kind=\"SPEED\">: Host ") + elm.second + " undefined.");
119     host->set_speed_profile(profile);
120   }
121
122   for (auto const& elm : trace_connect_list_link_avail) {
123     surf_parse_assert(traces_set_list.find(elm.first) != traces_set_list.end(), std::string("<trace_connect kind=\"LINK_AVAIL\">: Trace ")+elm.first+" undefined.");
124     auto profile = traces_set_list.at(elm.first);
125
126     auto link = simgrid::s4u::Link::by_name_or_null(elm.second);
127     surf_parse_assert(link, std::string("<trace_connect kind=\"LINK_AVAIL\">: Link ") + elm.second + " undefined.");
128     link->set_state_profile(profile);
129   }
130
131   for (auto const& elm : trace_connect_list_link_bw) {
132     surf_parse_assert(traces_set_list.find(elm.first) != traces_set_list.end(), std::string("<trace_connect kind=\"BANDWIDTH\">: Trace ")+elm.first+" undefined.");
133     auto profile = traces_set_list.at(elm.first);
134
135     auto link = simgrid::s4u::Link::by_name_or_null(elm.second);
136     surf_parse_assert(link, std::string("<trace_connect kind=\"BANDWIDTH\">: Link ") + elm.second + " undefined.");
137     link->set_bandwidth_profile(profile);
138   }
139
140   for (auto const& elm : trace_connect_list_link_lat) {
141     surf_parse_assert(traces_set_list.find(elm.first) != traces_set_list.end(), std::string("<trace_connect kind=\"LATENCY\">: Trace ")+elm.first+" undefined.");
142     auto profile = traces_set_list.at(elm.first);
143
144     auto link = simgrid::s4u::Link::by_name_or_null(elm.second);
145     surf_parse_assert(link, std::string("<trace_connect kind=\"LATENCY\">: Link ") + elm.second + " undefined.");
146     link->set_latency_profile(profile);
147   }
148
149   surf_parse_close();
150 }