Logo AND Algorithmique Numérique Distribuée

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