Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Define FairBottleneck and Lagrange as subclasses of lmm::System.
[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 "src/instr/instr_private.hpp" // TRACE_start(). FIXME: remove by subscribing tracing to the surf signals
7 #include "src/surf/cpu_interface.hpp"
8 #include "src/surf/network_interface.hpp"
9 #include "xbt/log.h"
10 #include "xbt/misc.h"
11 #include <vector>
12
13 #include "src/surf/xml/platf_private.hpp"
14
15 #if SIMGRID_HAVE_LUA
16 extern "C" {
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 }
23 #endif
24
25 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
26
27 /* Trace related stuff */
28 XBT_PRIVATE std::unordered_map<std::string, tmgr_trace_t> 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 extern "C" {
36 void sg_platf_trace_connect(simgrid::kernel::routing::TraceConnectCreationArgs* trace_connect)
37 {
38   xbt_assert(traces_set_list.find(trace_connect->trace) != traces_set_list.end(),
39              "Cannot connect trace %s to %s: trace unknown", trace_connect->trace.c_str(),
40              trace_connect->element.c_str());
41
42   switch (trace_connect->kind) {
43     case simgrid::kernel::routing::TraceConnectKind::HOST_AVAIL:
44       trace_connect_list_host_avail.insert({trace_connect->trace, trace_connect->element});
45       break;
46     case simgrid::kernel::routing::TraceConnectKind::SPEED:
47       trace_connect_list_host_speed.insert({trace_connect->trace, trace_connect->element});
48       break;
49     case simgrid::kernel::routing::TraceConnectKind::LINK_AVAIL:
50       trace_connect_list_link_avail.insert({trace_connect->trace, trace_connect->element});
51       break;
52     case simgrid::kernel::routing::TraceConnectKind::BANDWIDTH:
53       trace_connect_list_link_bw.insert({trace_connect->trace, trace_connect->element});
54       break;
55     case simgrid::kernel::routing::TraceConnectKind::LATENCY:
56       trace_connect_list_link_lat.insert({trace_connect->trace, trace_connect->element});
57       break;
58     default:
59       surf_parse_error(std::string("Cannot connect trace ") + trace_connect->trace + " to " + trace_connect->element +
60                        ": unknown kind of trace");
61       break;
62   }
63 }
64
65 static int after_config_done;
66 void parse_after_config() {
67   if (not after_config_done) {
68     TRACE_start();
69
70     /* Register classical callbacks */
71     storage_register_callbacks();
72
73     after_config_done = 1;
74   }
75 }
76
77 /* This function acts as a main in the parsing area. */
78 void parse_platform_file(const char *file)
79 {
80 #if SIMGRID_HAVE_LUA
81   int len    = (file == nullptr ? 0 : strlen(file));
82   int is_lua = (file != nullptr && len > 3 && file[len - 3] == 'l' && file[len - 2] == 'u' && file[len - 1] == 'a');
83 #endif
84
85   sg_platf_init();
86
87 #if SIMGRID_HAVE_LUA
88   /* Check if file extension is "lua". If so, we will use
89    * the lua bindings to parse the platform file (since it is
90    * written in lua). If not, we will use the (old?) XML parser
91    */
92   if (is_lua) {
93     lua_State* L = luaL_newstate();
94     luaL_openlibs(L);
95
96     luaL_loadfile(L, file); // This loads the file without executing it.
97
98     /* Run the script */
99     if (lua_pcall(L, 0, 0, 0)) {
100       XBT_ERROR("FATAL ERROR:\n  %s: %s\n\n", "Lua call failed. Error message:", lua_tostring(L, -1));
101       xbt_die("Lua call failed. See Log");
102     }
103     lua_close(L);
104     return;
105   }
106 #endif
107
108   // Use XML parser
109
110   int parse_status;
111
112   /* init the flex parser */
113   after_config_done = 0;
114   surf_parse_open(file);
115
116   /* Do the actual parsing */
117   parse_status = surf_parse();
118
119   /* connect all traces relative to hosts */
120   for (auto const& elm : trace_connect_list_host_avail) {
121     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
122     tmgr_trace_t trace = traces_set_list.at(elm.first);
123
124     simgrid::s4u::Host* host = sg_host_by_name(elm.second.c_str());
125     xbt_assert(host, "Host %s undefined", elm.second.c_str());
126     simgrid::surf::Cpu* cpu = host->pimpl_cpu;
127
128     cpu->setStateTrace(trace);
129   }
130
131   for (auto const& elm : trace_connect_list_host_speed) {
132     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
133     tmgr_trace_t trace = traces_set_list.at(elm.first);
134
135     simgrid::s4u::Host* host = sg_host_by_name(elm.second.c_str());
136     xbt_assert(host, "Host %s undefined", elm.second.c_str());
137     simgrid::surf::Cpu* cpu = host->pimpl_cpu;
138
139     cpu->setSpeedTrace(trace);
140   }
141
142   for (auto const& elm : trace_connect_list_link_avail) {
143     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
144     tmgr_trace_t trace = traces_set_list.at(elm.first);
145
146     sg_link_t link = simgrid::s4u::Link::byName(elm.second.c_str());
147     xbt_assert(link, "Link %s undefined", elm.second.c_str());
148     link->setStateTrace(trace);
149   }
150
151   for (auto const& elm : trace_connect_list_link_bw) {
152     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
153     tmgr_trace_t trace = traces_set_list.at(elm.first);
154     sg_link_t link     = simgrid::s4u::Link::byName(elm.second.c_str());
155     xbt_assert(link, "Link %s undefined", elm.second.c_str());
156     link->setBandwidthTrace(trace);
157   }
158
159   for (auto const& elm : trace_connect_list_link_lat) {
160     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
161     tmgr_trace_t trace = traces_set_list.at(elm.first);
162     sg_link_t link     = simgrid::s4u::Link::byName(elm.second.c_str());
163     xbt_assert(link, "Link %s undefined", elm.second.c_str());
164     link->setLatencyTrace(trace);
165   }
166
167   surf_parse_close();
168
169   if (parse_status)
170     surf_parse_error(std::string("Parse error in ") + file);
171 }
172 }