Logo AND Algorithmique Numérique Distribuée

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