Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix hash update.
[simgrid.git] / src / surf / xml / surfxml_parseplatf.cpp
1 /* Copyright (c) 2006-2019. 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   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());
38
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});
42       break;
43     case simgrid::kernel::routing::TraceConnectKind::SPEED:
44       trace_connect_list_host_speed.insert({trace_connect->trace, trace_connect->element});
45       break;
46     case simgrid::kernel::routing::TraceConnectKind::LINK_AVAIL:
47       trace_connect_list_link_avail.insert({trace_connect->trace, trace_connect->element});
48       break;
49     case simgrid::kernel::routing::TraceConnectKind::BANDWIDTH:
50       trace_connect_list_link_bw.insert({trace_connect->trace, trace_connect->element});
51       break;
52     case simgrid::kernel::routing::TraceConnectKind::LATENCY:
53       trace_connect_list_link_lat.insert({trace_connect->trace, trace_connect->element});
54       break;
55     default:
56       surf_parse_error(std::string("Cannot connect trace ") + trace_connect->trace + " to " + trace_connect->element +
57                        ": unknown kind of trace");
58   }
59 }
60
61 /* This function acts as a main in the parsing area. */
62 void parse_platform_file(const std::string& file)
63 {
64   const char* cfile = file.c_str();
65   int len           = strlen(cfile);
66   int is_lua        = len > 3 && file[len - 3] == 'l' && file[len - 2] == 'u' && file[len - 1] == 'a';
67
68   sg_platf_init();
69
70   /* Check if file extension is "lua". If so, we will use
71    * the lua bindings to parse the platform file (since it is
72    * written in lua). If not, we will use the (old?) XML parser
73    */
74   if (is_lua) {
75 #if SIMGRID_HAVE_LUA
76     lua_State* L = luaL_newstate();
77     luaL_openlibs(L);
78
79     luaL_loadfile(L, cfile); // This loads the file without executing it.
80
81     /* Run the script */
82     if (lua_pcall(L, 0, 0, 0)) {
83       XBT_ERROR("FATAL ERROR:\n  %s: %s\n\n", "Lua call failed. Error message:", lua_tostring(L, -1));
84       xbt_die("Lua call failed. See Log");
85     }
86     lua_close(L);
87     return;
88 #else
89     XBT_WARN("This looks like a lua platform file, but your SimGrid was not compiled with lua. Loading it as XML.");
90 #endif
91   }
92
93   // Use XML parser
94
95   int parse_status;
96
97   /* init the flex parser */
98   surf_parse_open(file);
99
100   /* Do the actual parsing */
101   parse_status = surf_parse();
102
103   /* connect all profiles relative to hosts */
104   for (auto const& elm : trace_connect_list_host_avail) {
105     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
106     simgrid::kernel::profile::Profile* profile = traces_set_list.at(elm.first);
107
108     simgrid::s4u::Host* host = simgrid::s4u::Host::by_name_or_null(elm.second);
109     xbt_assert(host, "Host %s undefined", elm.second.c_str());
110     simgrid::surf::Cpu* cpu = host->pimpl_cpu;
111
112     cpu->set_state_profile(profile);
113   }
114
115   for (auto const& elm : trace_connect_list_host_speed) {
116     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
117     simgrid::kernel::profile::Profile* profile = traces_set_list.at(elm.first);
118
119     simgrid::s4u::Host* host = simgrid::s4u::Host::by_name_or_null(elm.second);
120     xbt_assert(host, "Host %s undefined", elm.second.c_str());
121     simgrid::surf::Cpu* cpu = host->pimpl_cpu;
122
123     cpu->set_speed_profile(profile);
124   }
125
126   for (auto const& elm : trace_connect_list_link_avail) {
127     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
128     simgrid::kernel::profile::Profile* profile = traces_set_list.at(elm.first);
129
130     sg_link_t link = simgrid::s4u::Link::by_name(elm.second);
131     xbt_assert(link, "Link %s undefined", elm.second.c_str());
132     link->set_state_profile(profile);
133   }
134
135   for (auto const& elm : trace_connect_list_link_bw) {
136     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
137     simgrid::kernel::profile::Profile* profile = traces_set_list.at(elm.first);
138     sg_link_t link                             = simgrid::s4u::Link::by_name(elm.second);
139     xbt_assert(link, "Link %s undefined", elm.second.c_str());
140     link->set_bandwidth_profile(profile);
141   }
142
143   for (auto const& elm : trace_connect_list_link_lat) {
144     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
145     simgrid::kernel::profile::Profile* profile = traces_set_list.at(elm.first);
146     sg_link_t link                             = simgrid::s4u::Link::by_name(elm.second);
147     xbt_assert(link, "Link %s undefined", elm.second.c_str());
148     link->set_latency_profile(profile);
149   }
150
151   surf_parse_close();
152
153   if (parse_status)
154     surf_parse_error(std::string("Parse error in ") + file);
155 }