Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
14767edce39d5e5e964b23b0979dc987c6824f28
[simgrid.git] / src / surf / xml / surfxml_parseplatf.cpp
1 /* Copyright (c) 2006-2018. 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 "instr/instr_interface.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"
10 #include "src/surf/surf_interface.hpp"
11 #include "src/surf/xml/platf_private.hpp"
12
13 #include <vector>
14
15
16 #if SIMGRID_HAVE_LUA
17 #include "src/bindings/lua/simgrid_lua.hpp"
18
19 #include <lua.h>                /* Always include this when calling Lua */
20 #include <lauxlib.h>            /* Always include this when calling Lua */
21 #include <lualib.h>             /* Prototype for luaL_openlibs(), */
22 #endif
23
24 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
25
26 /* Trace related stuff */
27 XBT_PRIVATE std::unordered_map<std::string, tmgr_trace_t> traces_set_list;
28 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_host_avail;
29 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_host_speed;
30 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_avail;
31 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_bw;
32 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_lat;
33
34 void sg_platf_trace_connect(simgrid::kernel::routing::TraceConnectCreationArgs* trace_connect)
35 {
36   xbt_assert(traces_set_list.find(trace_connect->trace) != traces_set_list.end(),
37              "Cannot connect trace %s to %s: trace unknown", trace_connect->trace.c_str(),
38              trace_connect->element.c_str());
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       break;
60   }
61 }
62
63 static int after_config_done;
64 void parse_after_config() {
65   if (not after_config_done) {
66     TRACE_start();
67
68     after_config_done = 1;
69   }
70 }
71
72 /* This function acts as a main in the parsing area. */
73 void parse_platform_file(const char *file)
74 {
75 #if SIMGRID_HAVE_LUA
76   int len    = (file == nullptr ? 0 : strlen(file));
77   int is_lua = (file != nullptr && len > 3 && file[len - 3] == 'l' && file[len - 2] == 'u' && file[len - 1] == 'a');
78 #endif
79
80   sg_platf_init();
81
82 #if SIMGRID_HAVE_LUA
83   /* Check if file extension is "lua". If so, we will use
84    * the lua bindings to parse the platform file (since it is
85    * written in lua). If not, we will use the (old?) XML parser
86    */
87   if (is_lua) {
88     lua_State* L = luaL_newstate();
89     luaL_openlibs(L);
90
91     luaL_loadfile(L, file); // This loads the file without executing it.
92
93     /* Run the script */
94     if (lua_pcall(L, 0, 0, 0)) {
95       XBT_ERROR("FATAL ERROR:\n  %s: %s\n\n", "Lua call failed. Error message:", lua_tostring(L, -1));
96       xbt_die("Lua call failed. See Log");
97     }
98     lua_close(L);
99     return;
100   }
101 #endif
102
103   // Use XML parser
104
105   int parse_status;
106
107   /* init the flex parser */
108   after_config_done = 0;
109   surf_parse_open(file);
110
111   /* Do the actual parsing */
112   parse_status = surf_parse();
113
114   /* connect all traces relative to hosts */
115   for (auto const& elm : trace_connect_list_host_avail) {
116     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
117     tmgr_trace_t trace = traces_set_list.at(elm.first);
118
119     simgrid::s4u::Host* host = sg_host_by_name(elm.second.c_str());
120     xbt_assert(host, "Host %s undefined", elm.second.c_str());
121     simgrid::surf::Cpu* cpu = host->pimpl_cpu;
122
123     cpu->setStateTrace(trace);
124   }
125
126   for (auto const& elm : trace_connect_list_host_speed) {
127     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
128     tmgr_trace_t trace = traces_set_list.at(elm.first);
129
130     simgrid::s4u::Host* host = sg_host_by_name(elm.second.c_str());
131     xbt_assert(host, "Host %s undefined", elm.second.c_str());
132     simgrid::surf::Cpu* cpu = host->pimpl_cpu;
133
134     cpu->set_speed_trace(trace);
135   }
136
137   for (auto const& elm : trace_connect_list_link_avail) {
138     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
139     tmgr_trace_t trace = traces_set_list.at(elm.first);
140
141     sg_link_t link = simgrid::s4u::Link::by_name(elm.second.c_str());
142     xbt_assert(link, "Link %s undefined", elm.second.c_str());
143     link->setStateTrace(trace);
144   }
145
146   for (auto const& elm : trace_connect_list_link_bw) {
147     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
148     tmgr_trace_t trace = traces_set_list.at(elm.first);
149     sg_link_t link     = simgrid::s4u::Link::by_name(elm.second.c_str());
150     xbt_assert(link, "Link %s undefined", elm.second.c_str());
151     link->setBandwidthTrace(trace);
152   }
153
154   for (auto const& elm : trace_connect_list_link_lat) {
155     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
156     tmgr_trace_t trace = traces_set_list.at(elm.first);
157     sg_link_t link     = simgrid::s4u::Link::by_name(elm.second.c_str());
158     xbt_assert(link, "Link %s undefined", elm.second.c_str());
159     link->setLatencyTrace(trace);
160   }
161
162   surf_parse_close();
163
164   if (parse_status)
165     surf_parse_error(std::string("Parse error in ") + file);
166 }