Logo AND Algorithmique Numérique Distribuée

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