Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #259 from simgrid/configfix
[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 #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, tmgr_trace_t> 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       break;
59   }
60 }
61
62 static int after_config_done;
63 void parse_after_config() {
64   if (not after_config_done) {
65     TRACE_start();
66
67     /* Register classical callbacks */
68     storage_register_callbacks();
69
70     after_config_done = 1;
71   }
72 }
73
74 /* This function acts as a main in the parsing area. */
75 void parse_platform_file(const char *file)
76 {
77 #if SIMGRID_HAVE_LUA
78   int len    = (file == nullptr ? 0 : strlen(file));
79   int is_lua = (file != nullptr && len > 3 && file[len - 3] == 'l' && file[len - 2] == 'u' && file[len - 1] == 'a');
80 #endif
81
82   sg_platf_init();
83
84 #if SIMGRID_HAVE_LUA
85   /* Check if file extension is "lua". If so, we will use
86    * the lua bindings to parse the platform file (since it is
87    * written in lua). If not, we will use the (old?) XML parser
88    */
89   if (is_lua) {
90     lua_State* L = luaL_newstate();
91     luaL_openlibs(L);
92
93     luaL_loadfile(L, file); // This loads the file without executing it.
94
95     /* Run the script */
96     if (lua_pcall(L, 0, 0, 0)) {
97       XBT_ERROR("FATAL ERROR:\n  %s: %s\n\n", "Lua call failed. Error message:", lua_tostring(L, -1));
98       xbt_die("Lua call failed. See Log");
99     }
100     lua_close(L);
101     return;
102   }
103 #endif
104
105   // Use XML parser
106
107   int parse_status;
108
109   /* init the flex parser */
110   after_config_done = 0;
111   surf_parse_open(file);
112
113   /* Do the actual parsing */
114   parse_status = surf_parse();
115
116   /* connect all traces relative to hosts */
117   for (auto const& elm : trace_connect_list_host_avail) {
118     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
119     tmgr_trace_t trace = traces_set_list.at(elm.first);
120
121     simgrid::s4u::Host* host = sg_host_by_name(elm.second.c_str());
122     xbt_assert(host, "Host %s undefined", elm.second.c_str());
123     simgrid::surf::Cpu* cpu = host->pimpl_cpu;
124
125     cpu->setStateTrace(trace);
126   }
127
128   for (auto const& elm : trace_connect_list_host_speed) {
129     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
130     tmgr_trace_t trace = traces_set_list.at(elm.first);
131
132     simgrid::s4u::Host* host = sg_host_by_name(elm.second.c_str());
133     xbt_assert(host, "Host %s undefined", elm.second.c_str());
134     simgrid::surf::Cpu* cpu = host->pimpl_cpu;
135
136     cpu->setSpeedTrace(trace);
137   }
138
139   for (auto const& elm : trace_connect_list_link_avail) {
140     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
141     tmgr_trace_t trace = traces_set_list.at(elm.first);
142
143     sg_link_t link = simgrid::s4u::Link::byName(elm.second.c_str());
144     xbt_assert(link, "Link %s undefined", elm.second.c_str());
145     link->setStateTrace(trace);
146   }
147
148   for (auto const& elm : trace_connect_list_link_bw) {
149     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
150     tmgr_trace_t trace = traces_set_list.at(elm.first);
151     sg_link_t link     = simgrid::s4u::Link::byName(elm.second.c_str());
152     xbt_assert(link, "Link %s undefined", elm.second.c_str());
153     link->setBandwidthTrace(trace);
154   }
155
156   for (auto const& elm : trace_connect_list_link_lat) {
157     xbt_assert(traces_set_list.find(elm.first) != traces_set_list.end(), "Trace %s undefined", elm.first.c_str());
158     tmgr_trace_t trace = traces_set_list.at(elm.first);
159     sg_link_t link     = simgrid::s4u::Link::byName(elm.second.c_str());
160     xbt_assert(link, "Link %s undefined", elm.second.c_str());
161     link->setLatencyTrace(trace);
162   }
163
164   surf_parse_close();
165
166   if (parse_status)
167     surf_parse_error(std::string("Parse error in ") + file);
168 }