Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
055b5fee3be9ce2f321862b2b854d459c52cde30
[simgrid.git] / src / surf / xml / surfxml_parseplatf.cpp
1 /* Copyright (c) 2006-2015. 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.h" // 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 "xbt/str.h"
13 #include <vector>
14
15 #include "src/surf/xml/platf_private.hpp"
16
17 #if SIMGRID_HAVE_LUA
18 extern "C" {
19 #include "src/bindings/lua/simgrid_lua.h"
20
21 #include <lua.h>                /* Always include this when calling Lua */
22 #include <lauxlib.h>            /* Always include this when calling Lua */
23 #include <lualib.h>             /* Prototype for luaL_openlibs(), */
24 }
25 #endif
26
27 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
28
29 /* Trace related stuff */
30 XBT_PRIVATE std::unordered_map<std::string, tmgr_trace_t> traces_set_list;
31 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_host_avail;
32 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_host_speed;
33 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_avail;
34 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_bw;
35 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_lat;
36
37 SG_BEGIN_DECL()
38 void sg_platf_trace_connect(TraceConnectCreationArgs* trace_connect)
39 {
40   xbt_assert(traces_set_list.find(trace_connect->trace) != traces_set_list.end(),
41              "Cannot connect trace %s to %s: trace unknown", trace_connect->trace.c_str(),
42              trace_connect->element.c_str());
43
44   switch (trace_connect->kind) {
45   case SURF_TRACE_CONNECT_KIND_HOST_AVAIL:
46     trace_connect_list_host_avail.insert({trace_connect->trace, trace_connect->element});
47     break;
48   case SURF_TRACE_CONNECT_KIND_SPEED:
49     trace_connect_list_host_speed.insert({trace_connect->trace, trace_connect->element});
50     break;
51   case SURF_TRACE_CONNECT_KIND_LINK_AVAIL:
52     trace_connect_list_link_avail.insert({trace_connect->trace, trace_connect->element});
53     break;
54   case SURF_TRACE_CONNECT_KIND_BANDWIDTH:
55     trace_connect_list_link_bw.insert({trace_connect->trace, trace_connect->element});
56     break;
57   case SURF_TRACE_CONNECT_KIND_LATENCY:
58     trace_connect_list_link_lat.insert({trace_connect->trace, trace_connect->element});
59     break;
60   default:
61     surf_parse_error(std::string("Cannot connect trace ") + trace_connect->trace + " to " + trace_connect->element +
62                      ": unknown kind of trace");
63     break;
64   }
65 }
66
67 static int after_config_done;
68 void parse_after_config() {
69   if (not after_config_done) {
70     TRACE_start();
71
72     /* Register classical callbacks */
73     storage_register_callbacks();
74
75     after_config_done = 1;
76   }
77 }
78
79 /* This function acts as a main in the parsing area. */
80 void parse_platform_file(const char *file)
81 {
82 #if SIMGRID_HAVE_LUA
83   int len    = (file == nullptr ? 0 : strlen(file));
84   int is_lua = (file != nullptr && len > 3 && file[len - 3] == 'l' && file[len - 2] == 'u' && file[len - 1] == 'a');
85 #endif
86
87   sg_platf_init();
88
89 #if SIMGRID_HAVE_LUA
90   /* Check if file extension is "lua". If so, we will use
91    * the lua bindings to parse the platform file (since it is
92    * written in lua). If not, we will use the (old?) XML parser
93    */
94   if (is_lua) {
95     lua_State* L = luaL_newstate();
96     luaL_openlibs(L);
97
98     luaL_loadfile(L, file); // This loads the file without executing it.
99
100     /* Run the script */
101     if (lua_pcall(L, 0, 0, 0)) {
102       XBT_ERROR("FATAL ERROR:\n  %s: %s\n\n", "Lua call failed. Error message:", lua_tostring(L, -1));
103       xbt_die("Lua call failed. See Log");
104     }
105     lua_close(L);
106   }
107   else
108 #endif
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 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 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 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 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 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 }
174
175 SG_END_DECL()