1 /* Copyright (c) 2006-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
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"
16 #include "src/surf/xml/platf_private.hpp"
20 #include "src/bindings/lua/simgrid_lua.h"
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(), */
28 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
32 /* Trace related stuff */
34 xbt_dict_t traces_set_list = nullptr;
35 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_host_avail;
36 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_host_speed;
37 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_avail;
38 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_bw;
39 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_lat;
41 void sg_platf_trace_connect(sg_platf_trace_connect_cbarg_t trace_connect)
43 xbt_assert(xbt_dict_get_or_null(traces_set_list, trace_connect->trace),
44 "Cannot connect trace %s to %s: trace unknown", trace_connect->trace, trace_connect->element);
46 switch (trace_connect->kind) {
47 case SURF_TRACE_CONNECT_KIND_HOST_AVAIL:
48 trace_connect_list_host_avail.insert({trace_connect->trace, trace_connect->element});
50 case SURF_TRACE_CONNECT_KIND_SPEED:
51 trace_connect_list_host_speed.insert({trace_connect->trace, trace_connect->element});
53 case SURF_TRACE_CONNECT_KIND_LINK_AVAIL:
54 trace_connect_list_link_avail.insert({trace_connect->trace, trace_connect->element});
56 case SURF_TRACE_CONNECT_KIND_BANDWIDTH:
57 trace_connect_list_link_bw.insert({trace_connect->trace, trace_connect->element});
59 case SURF_TRACE_CONNECT_KIND_LATENCY:
60 trace_connect_list_link_lat.insert({trace_connect->trace, trace_connect->element});
63 surf_parse_error("Cannot connect trace %s to %s: kind of trace unknown", trace_connect->trace,
64 trace_connect->element);
69 static int after_config_done;
70 void parse_after_config() {
71 if (not after_config_done) {
74 /* Register classical callbacks */
75 storage_register_callbacks();
77 after_config_done = 1;
81 /* This function acts as a main in the parsing area. */
82 void parse_platform_file(const char *file)
85 int len = (file == nullptr ? 0 : strlen(file));
86 int is_lua = (file != nullptr && len > 3 && file[len - 3] == 'l' && file[len - 2] == 'u' && file[len - 1] == 'a');
92 /* Check if file extension is "lua". If so, we will use
93 * the lua bindings to parse the platform file (since it is
94 * written in lua). If not, we will use the (old?) XML parser
97 lua_State* L = luaL_newstate();
100 luaL_loadfile(L, file); // This loads the file without executing it.
103 if (lua_pcall(L, 0, 0, 0)) {
104 XBT_ERROR("FATAL ERROR:\n %s: %s\n\n", "Lua call failed. Error message:", lua_tostring(L, -1));
105 xbt_die("Lua call failed. See Log");
115 /* init the flex parser */
116 after_config_done = 0;
117 surf_parse_open(file);
119 traces_set_list = xbt_dict_new_homogeneous(nullptr);
121 /* Do the actual parsing */
122 parse_status = surf_parse();
124 /* connect all traces relative to hosts */
125 for (auto elm : trace_connect_list_host_avail) {
126 tmgr_trace_t trace = (tmgr_trace_t)xbt_dict_get_or_null(traces_set_list, elm.first.c_str());
127 xbt_assert(trace, "Trace %s undefined", elm.first.c_str());
129 simgrid::s4u::Host* host = sg_host_by_name(elm.second.c_str());
130 xbt_assert(host, "Host %s undefined", elm.second.c_str());
131 simgrid::surf::Cpu *cpu = host->pimpl_cpu;
133 cpu->setStateTrace(trace);
136 for (auto elm : trace_connect_list_host_speed) {
137 tmgr_trace_t trace = (tmgr_trace_t)xbt_dict_get_or_null(traces_set_list, elm.first.c_str());
138 xbt_assert(trace, "Trace %s undefined", elm.first.c_str());
140 simgrid::s4u::Host* host = sg_host_by_name(elm.second.c_str());
141 xbt_assert(host, "Host %s undefined", elm.second.c_str());
142 simgrid::surf::Cpu *cpu = host->pimpl_cpu;
144 cpu->setSpeedTrace(trace);
147 for (auto elm : trace_connect_list_link_avail) {
148 tmgr_trace_t trace = (tmgr_trace_t)xbt_dict_get_or_null(traces_set_list, elm.first.c_str());
149 xbt_assert(trace, "Trace %s undefined", elm.first.c_str());
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->setStateTrace(trace);
156 for (auto elm : trace_connect_list_link_bw) {
157 tmgr_trace_t trace = (tmgr_trace_t)xbt_dict_get_or_null(traces_set_list, elm.first.c_str());
158 xbt_assert(trace, "Trace %s undefined", elm.first.c_str());
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->setBandwidthTrace(trace);
164 for (auto elm : trace_connect_list_link_lat) {
165 tmgr_trace_t trace = (tmgr_trace_t)xbt_dict_get_or_null(traces_set_list, elm.first.c_str());
166 xbt_assert(trace, "Trace %s undefined", elm.first.c_str());
167 sg_link_t link = simgrid::s4u::Link::byName(elm.second.c_str());
168 xbt_assert(link, "Link %s undefined", elm.second.c_str());
169 link->setLatencyTrace(trace);
173 xbt_dict_free(&traces_set_list);
178 surf_parse_error("Parse error in %s", file);