Logo AND Algorithmique Numérique Distribuée

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