1 /* Copyright (c) 2010-2020. The SimGrid Team. All rights reserved. */
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. */
6 /* SimGrid Lua bindings */
8 #include "lua_private.hpp"
9 #include "simgrid/kernel/routing/NetPoint.hpp"
10 #include "src/kernel/resource/profile/Profile.hpp"
11 #include "src/surf/network_interface.hpp"
12 #include "src/surf/surf_private.hpp"
13 #include "src/surf/xml/platf_private.hpp"
14 #include "xbt/parse_units.hpp"
16 #include <boost/algorithm/string/classification.hpp>
17 #include <boost/algorithm/string/split.hpp>
18 #include <simgrid/s4u/Engine.hpp>
19 #include <simgrid/s4u/Host.hpp>
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_platf, lua, "Lua bindings (platform module)");
30 constexpr char PLATF_MODULE_NAME[] = "simgrid.engine";
31 constexpr char AS_FIELDNAME[] = "__simgrid_as";
33 /* ********************************************************************************* */
34 /* simgrid.platf API */
35 /* ********************************************************************************* */
37 static const luaL_Reg platf_functions[] = {
38 {"open", console_open},
39 {"close", console_close},
40 {"AS_open", console_AS_open},
41 {"AS_seal", console_AS_seal},
42 {"backbone_new", console_add_backbone},
43 {"host_link_new", console_add_host___link},
44 {"host_new", console_add_host},
45 {"link_new", console_add_link},
46 {"router_new", console_add_router},
47 {"route_new", console_add_route},
48 {"ASroute_new", console_add_ASroute},
52 static simgrid::s4u::Link::SharingPolicy link_policy_get_by_name(const char* policy)
54 if (policy && not strcmp(policy, "FULLDUPLEX")) {
55 XBT_WARN("Please update your platform to use SPLITDUPLEX instead of FULLDUPLEX");
56 return simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
57 } else if (policy && not strcmp(policy, "SPLITDUPLEX")) {
58 return simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
59 } else if (policy && not strcmp(policy, "FATPIPE")) {
60 return simgrid::s4u::Link::SharingPolicy::FATPIPE;
62 return simgrid::s4u::Link::SharingPolicy::SHARED;
66 int console_open(lua_State*)
69 simgrid::s4u::Engine::on_platform_creation();
74 int console_close(lua_State*)
76 simgrid::s4u::Engine::on_platform_created();
81 int console_add_backbone(lua_State *L) {
82 simgrid::kernel::routing::LinkCreationArgs link;
84 lua_getstack(L, 1, &ar);
85 lua_getinfo(L, "Sl", &ar);
87 lua_ensure(lua_istable(L, -1),"Bad Arguments to create backbone in Lua. Should be a table with named arguments.");
89 lua_pushstring(L, "id");
90 int type = lua_gettable(L, -2);
91 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for backbone and must be a string.");
92 link.id = lua_tostring(L, -1);
95 lua_pushstring(L, "bandwidth");
96 type = lua_gettable(L, -2);
97 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
98 "Attribute 'bandwidth' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
99 link.bandwidths.push_back(xbt_parse_get_bandwidth(ar.short_src, ar.currentline, lua_tostring(L, -1),
100 "bandwidth of backbone", link.id.c_str()));
103 lua_pushstring(L, "lat");
104 type = lua_gettable(L, -2);
105 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
106 "Attribute 'lat' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
108 xbt_parse_get_time(ar.short_src, ar.currentline, lua_tostring(L, -1), "latency of backbone", link.id.c_str());
111 lua_pushstring(L, "sharing_policy");
113 const char* policy = lua_tostring(L, -1);
115 link.policy = link_policy_get_by_name(policy);
117 sg_platf_new_link(&link);
118 routing_cluster_add_backbone(simgrid::s4u::Link::by_name(link.id)->get_impl());
123 int console_add_host___link(lua_State *L) {
124 simgrid::kernel::routing::HostLinkCreationArgs hostlink;
127 lua_ensure(lua_istable(L, -1), "Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
129 lua_pushstring(L, "id");
130 type = lua_gettable(L, -2);
131 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any host_link and must be a string.");
132 hostlink.id = lua_tostring(L, -1);
135 lua_pushstring(L, "up");
136 type = lua_gettable(L, -2);
137 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
138 "Attribute 'up' must be specified for host_link and must either be a string or a number.");
139 hostlink.link_up = lua_tostring(L, -1);
142 lua_pushstring(L, "down");
143 type = lua_gettable(L, -2);
144 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
145 "Attribute 'down' must be specified for host_link and must either be a string or a number.");
146 hostlink.link_down = lua_tostring(L, -1);
149 XBT_DEBUG("Create a host_link for host %s", hostlink.id.c_str());
150 sg_platf_new_hostlink(&hostlink);
155 int console_add_host(lua_State *L) {
156 simgrid::kernel::routing::HostCreationArgs host;
159 lua_getstack(L, 1, &ar);
160 lua_getinfo(L, "Sl", &ar);
162 // we get values from the table passed as argument
163 lua_ensure(lua_istable(L, -1),
164 "Bad Arguments to create host. Should be a table with named arguments");
167 lua_pushstring(L, "id");
168 type = lua_gettable(L, -2);
169 lua_ensure(type == LUA_TSTRING,
170 "Attribute 'id' must be specified for any host and must be a string.");
171 host.id = lua_tostring(L, -1);
175 lua_pushstring(L, "speed");
176 type = lua_gettable(L, -2);
177 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
178 "Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
179 if (type == LUA_TNUMBER)
180 host.speed_per_pstate.push_back(lua_tonumber(L, -1));
182 host.speed_per_pstate.push_back(
183 xbt_parse_get_speed(ar.short_src, ar.currentline, lua_tostring(L, -1), "speed of host", host.id));
187 lua_pushstring(L, "core");
189 if (not lua_isnumber(L, -1))
190 host.core_amount = 1; // Default value
192 host.core_amount = static_cast<int>(lua_tointeger(L, -1));
193 if (host.core_amount == 0)
194 host.core_amount = 1;
198 lua_pushstring(L, "availability_file");
200 const char *filename = lua_tostring(L, -1);
202 host.speed_trace = simgrid::kernel::profile::Profile::from_file(filename);
206 lua_pushstring(L, "state_file");
208 filename = lua_tostring(L, -1);
210 host.state_trace = simgrid::kernel::profile::Profile::from_file(filename);
213 sg_platf_new_host(&host);
218 int console_add_link(lua_State *L) {
219 simgrid::kernel::routing::LinkCreationArgs link;
221 lua_getstack(L, 1, &ar);
222 lua_getinfo(L, "Sl", &ar);
226 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
229 lua_pushstring(L, "id");
230 int type = lua_gettable(L, -2);
231 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
232 "Attribute 'id' must be specified for any link and must be a string.");
233 link.id = lua_tostring(L, -1);
236 // get bandwidth value
237 lua_pushstring(L, "bandwidth");
238 type = lua_gettable(L, -2);
239 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
240 "Attribute 'bandwidth' must be specified for any link and must either be either a string (in the right format; see docs) or a number.");
241 if (type == LUA_TNUMBER)
242 link.bandwidths.push_back(lua_tonumber(L, -1));
244 link.bandwidths.push_back(xbt_parse_get_bandwidth(ar.short_src, ar.currentline, lua_tostring(L, -1),
245 "bandwidth of link", link.id.c_str()));
249 lua_pushstring(L, "lat");
250 type = lua_gettable(L, -2);
251 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
252 "Attribute 'lat' must be specified for any link and must either be a string (in the right format; see docs) or a number.");
253 if (type == LUA_TNUMBER)
254 link.latency = lua_tonumber(L, -1);
257 xbt_parse_get_time(ar.short_src, ar.currentline, lua_tostring(L, -1), "latency of link", link.id.c_str());
260 /*Optional Arguments */
262 //get bandwidth_trace value
263 lua_pushstring(L, "bandwidth_file");
265 const char *filename = lua_tostring(L, -1);
267 link.bandwidth_trace = simgrid::kernel::profile::Profile::from_file(filename);
270 //get latency_trace value
271 lua_pushstring(L, "latency_file");
273 filename = lua_tostring(L, -1);
275 link.latency_trace = simgrid::kernel::profile::Profile::from_file(filename);
278 //get state_trace value
279 lua_pushstring(L, "state_file");
281 filename = lua_tostring(L, -1);
283 link.state_trace = simgrid::kernel::profile::Profile::from_file(filename);
287 lua_pushstring(L, "sharing_policy");
289 policy = lua_tostring(L, -1);
291 link.policy = link_policy_get_by_name(policy);
293 sg_platf_new_link(&link);
298 * add Router to AS components
300 int console_add_router(lua_State* L) {
301 lua_ensure(lua_istable(L, -1), "Bad Arguments to create router, Should be a table with named arguments");
303 lua_pushstring(L, "id");
304 int type = lua_gettable(L, -2);
305 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any link and must be a string.");
306 const char* name = lua_tostring(L, -1);
309 lua_pushstring(L,"coord");
311 const char* coords = lua_tostring(L, -1);
314 sg_platf_new_router(name, coords);
319 int console_add_route(lua_State *L) {
320 XBT_DEBUG("Adding route");
321 simgrid::kernel::routing::RouteCreationArgs route;
324 lua_ensure(lua_istable(L, -1), "Bad Arguments to add a route. Should be a table with named arguments");
326 lua_pushstring(L,"src");
327 type = lua_gettable(L,-2);
328 lua_ensure(type == LUA_TSTRING, "Attribute 'src' must be specified for any route and must be a string.");
329 const char *srcName = lua_tostring(L, -1);
330 route.src = sg_netpoint_by_name_or_null(srcName);
331 lua_ensure(route.src != nullptr, "Attribute 'src=%s' of route does not name a node.", srcName);
334 lua_pushstring(L,"dest");
335 type = lua_gettable(L,-2);
336 lua_ensure(type == LUA_TSTRING, "Attribute 'dest' must be specified for any route and must be a string.");
337 const char *dstName = lua_tostring(L, -1);
338 route.dst = sg_netpoint_by_name_or_null(dstName);
339 lua_ensure(route.dst != nullptr, "Attribute 'dst=%s' of route does not name a node.", dstName);
342 lua_pushstring(L,"links");
343 type = lua_gettable(L,-2);
344 lua_ensure(type == LUA_TSTRING,
345 "Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
346 std::vector<std::string> names;
347 const char* str = lua_tostring(L, -1);
348 boost::split(names, str, boost::is_any_of(", \t\r\n"));
351 route.link_list.push_back(simgrid::s4u::Link::by_name(lua_tostring(L, -1))->get_impl());
353 // Several names separated by , \t\r\n
354 for (auto const& name : names) {
355 if (name.length() > 0) {
356 simgrid::kernel::resource::LinkImpl* link = simgrid::s4u::Link::by_name(name)->get_impl();
357 route.link_list.push_back(link);
363 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
364 * Et ouais mon pote. That's the way it goes. F34R.
366 * (Note that above this function, there is a #include statement. Is this
367 * comment related to that statement?)
369 lua_pushstring(L,"symmetrical");
371 route.symmetrical = (not lua_isstring(L, -1) || strcasecmp("YES", lua_tostring(L, -1)) == 0);
374 sg_platf_new_route(&route);
379 int console_add_ASroute(lua_State *L) {
380 simgrid::kernel::routing::RouteCreationArgs ASroute;
382 lua_pushstring(L, "src");
384 const char *srcName = lua_tostring(L, -1);
385 ASroute.src = sg_netpoint_by_name_or_null(srcName);
386 lua_ensure(ASroute.src != nullptr, "Attribute 'src=%s' of AS route does not name a node.", srcName);
389 lua_pushstring(L, "dst");
391 const char *dstName = lua_tostring(L, -1);
392 ASroute.dst = sg_netpoint_by_name_or_null(dstName);
393 lua_ensure(ASroute.dst != nullptr, "Attribute 'dst=%s' of AS route does not name a node.", dstName);
396 lua_pushstring(L, "gw_src");
398 const char* pname = lua_tostring(L, -1);
399 ASroute.gw_src = sg_netpoint_by_name_or_null(pname);
400 lua_ensure(ASroute.gw_src, "Attribute 'gw_src=%s' of AS route does not name a valid node", pname);
403 lua_pushstring(L, "gw_dst");
405 pname = lua_tostring(L, -1);
406 ASroute.gw_dst = sg_netpoint_by_name_or_null(pname);
407 lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst=%s' of AS route does not name a valid node", pname);
410 lua_pushstring(L,"links");
412 std::vector<std::string> names;
413 const char* str = lua_tostring(L, -1);
414 boost::split(names, str, boost::is_any_of(", \t\r\n"));
416 /* unique name with no comma */
417 ASroute.link_list.push_back(simgrid::s4u::Link::by_name(lua_tostring(L, -1))->get_impl());
419 // Several names separated by , \t\r\n
420 for (auto const& name : names) {
421 if (name.length() > 0) {
422 simgrid::kernel::resource::LinkImpl* link = simgrid::s4u::Link::by_name(name)->get_impl();
423 ASroute.link_list.push_back(link);
429 lua_pushstring(L,"symmetrical");
431 ASroute.symmetrical = (not lua_isstring(L, -1) || strcasecmp("YES", lua_tostring(L, -1)) == 0);
434 sg_platf_new_route(&ASroute);
439 int console_AS_open(lua_State *L) {
440 XBT_DEBUG("Opening AS");
442 lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
444 lua_pushstring(L, "id");
445 int type = lua_gettable(L, -2);
446 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
447 const char* id = lua_tostring(L, -1);
450 lua_pushstring(L, "mode");
452 const char* mode = lua_tostring(L, -1);
455 simgrid::kernel::routing::ZoneCreationArgs AS;
458 simgrid::kernel::routing::NetZoneImpl* new_as = sg_platf_new_Zone_begin(&AS);
460 /* Build a Lua representation of the new AS on the stack */
462 auto* lua_as = static_cast<simgrid::kernel::routing::NetZoneImpl**>(
463 lua_newuserdata(L, sizeof(simgrid::kernel::routing::NetZoneImpl*))); /* table userdatum */
465 luaL_getmetatable(L, PLATF_MODULE_NAME); /* table userdatum metatable */
466 lua_setmetatable(L, -2); /* table userdatum */
467 lua_setfield(L, -2, AS_FIELDNAME); /* table -- put the userdata as field of the table */
472 int console_AS_seal(lua_State*)
474 XBT_DEBUG("Sealing AS");
475 sg_platf_new_Zone_seal();
479 int console_host_set_property(lua_State *L) {
480 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
483 lua_pushstring(L, "host");
485 const char* name = lua_tostring(L, -1);
489 lua_pushstring(L, "prop");
491 const char* prop_id = lua_tostring(L, -1);
494 lua_pushstring(L,"value");
496 const char* prop_value = lua_tostring(L, -1);
499 sg_host_t host = sg_host_by_name(name);
500 lua_ensure(host, "no host '%s' found",name);
501 host->set_property(prop_id, prop_value);
507 * @brief Registers the platform functions into the table simgrid.platf.
508 * @param L a lua state
510 void sglua_register_platf_functions(lua_State* L)
512 lua_getglobal(L, "simgrid"); /* simgrid */
513 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
514 lua_setfield(L, -2, "engine"); /* simgrid */
516 lua_pop(L, 1); /* -- */