1 /* Copyright (c) 2010-2019. 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/surf/network_interface.hpp"
11 #include "src/surf/xml/platf_private.hpp"
17 #include "src/surf/surf_private.hpp"
18 #include <boost/algorithm/string/classification.hpp>
19 #include <boost/algorithm/string/split.hpp>
20 #include <simgrid/s4u/Engine.hpp>
21 #include <simgrid/s4u/Host.hpp>
25 XBT_LOG_NEW_DEFAULT_CATEGORY(lua_platf, "Lua bindings (platform module)");
27 constexpr char PLATF_MODULE_NAME[] = "simgrid.engine";
28 constexpr char AS_FIELDNAME[] = "__simgrid_as";
30 /* ********************************************************************************* */
31 /* simgrid.platf API */
32 /* ********************************************************************************* */
34 static const luaL_Reg platf_functions[] = {
35 {"open", console_open},
36 {"close", console_close},
37 {"AS_open", console_AS_open},
38 {"AS_seal", console_AS_seal},
39 {"backbone_new", console_add_backbone},
40 {"host_link_new", console_add_host___link},
41 {"host_new", console_add_host},
42 {"link_new", console_add_link},
43 {"router_new", console_add_router},
44 {"route_new", console_add_route},
45 {"ASroute_new", console_add_ASroute},
49 static simgrid::s4u::Link::SharingPolicy link_policy_get_by_name(const char* policy)
51 if (policy && not strcmp(policy, "FULLDUPLEX")) {
52 XBT_WARN("Please update your platform to use SPLITDUPLEX instead of FULLDUPLEX");
53 return simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
54 } else if (policy && not strcmp(policy, "SPLITDUPLEX")) {
55 return simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
56 } else if (policy && not strcmp(policy, "FATPIPE")) {
57 return simgrid::s4u::Link::SharingPolicy::FATPIPE;
59 return simgrid::s4u::Link::SharingPolicy::SHARED;
63 int console_open(lua_State*)
66 simgrid::s4u::on_platform_creation();
71 int console_close(lua_State*)
73 simgrid::s4u::on_platform_created();
78 int console_add_backbone(lua_State *L) {
79 simgrid::kernel::routing::LinkCreationArgs link;
81 link.properties = nullptr;
83 lua_ensure(lua_istable(L, -1),"Bad Arguments to create backbone in Lua. Should be a table with named arguments.");
85 lua_pushstring(L, "id");
86 int type = lua_gettable(L, -2);
87 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for backbone and must be a string.");
88 link.id = lua_tostring(L, -1);
91 lua_pushstring(L, "bandwidth");
92 type = lua_gettable(L, -2);
93 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
94 "Attribute 'bandwidth' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
95 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of backbone", link.id.c_str());
98 lua_pushstring(L, "lat");
99 type = lua_gettable(L, -2);
100 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
101 "Attribute 'lat' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
102 link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of backbone", link.id.c_str());
105 lua_pushstring(L, "sharing_policy");
107 const char* policy = lua_tostring(L, -1);
109 link.policy = link_policy_get_by_name(policy);
111 sg_platf_new_link(&link);
112 routing_cluster_add_backbone(simgrid::s4u::Link::by_name(link.id)->get_impl());
117 int console_add_host___link(lua_State *L) {
118 simgrid::kernel::routing::HostLinkCreationArgs hostlink;
121 lua_ensure(lua_istable(L, -1), "Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
123 lua_pushstring(L, "id");
124 type = lua_gettable(L, -2);
125 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any host_link and must be a string.");
126 hostlink.id = lua_tostring(L, -1);
129 lua_pushstring(L, "up");
130 type = lua_gettable(L, -2);
131 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
132 "Attribute 'up' must be specified for host_link and must either be a string or a number.");
133 hostlink.link_up = lua_tostring(L, -1);
136 lua_pushstring(L, "down");
137 type = lua_gettable(L, -2);
138 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
139 "Attribute 'down' must be specified for host_link and must either be a string or a number.");
140 hostlink.link_down = lua_tostring(L, -1);
143 XBT_DEBUG("Create a host_link for host %s", hostlink.id.c_str());
144 sg_platf_new_hostlink(&hostlink);
149 int console_add_host(lua_State *L) {
150 simgrid::kernel::routing::HostCreationArgs host;
153 // we get values from the table passed as argument
154 lua_ensure(lua_istable(L, -1),
155 "Bad Arguments to create host. Should be a table with named arguments");
158 lua_pushstring(L, "id");
159 type = lua_gettable(L, -2);
160 lua_ensure(type == LUA_TSTRING,
161 "Attribute 'id' must be specified for any host and must be a string.");
162 host.id = lua_tostring(L, -1);
166 lua_pushstring(L, "speed");
167 type = lua_gettable(L, -2);
168 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
169 "Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
170 if (type == LUA_TNUMBER)
171 host.speed_per_pstate.push_back(lua_tointeger(L, -1));
173 host.speed_per_pstate.push_back(surf_parse_get_speed(lua_tostring(L, -1), "speed of host", host.id));
177 lua_pushstring(L, "core");
179 if (not lua_isnumber(L, -1))
180 host.core_amount = 1; // Default value
182 host.core_amount = lua_tonumber(L, -1);
183 if (host.core_amount == 0)
184 host.core_amount = 1;
188 lua_pushstring(L, "availability_file");
190 const char *filename = lua_tostring(L, -1);
192 host.speed_trace = simgrid::kernel::profile::Profile::from_file(filename);
196 lua_pushstring(L, "state_file");
198 filename = lua_tostring(L, -1);
200 host.state_trace = simgrid::kernel::profile::Profile::from_file(filename);
203 sg_platf_new_host(&host);
208 int console_add_link(lua_State *L) {
209 simgrid::kernel::routing::LinkCreationArgs link;
213 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
216 lua_pushstring(L, "id");
217 int type = lua_gettable(L, -2);
218 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
219 "Attribute 'id' must be specified for any link and must be a string.");
220 link.id = lua_tostring(L, -1);
223 // get bandwidth value
224 lua_pushstring(L, "bandwidth");
225 type = lua_gettable(L, -2);
226 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
227 "Attribute 'bandwidth' must be specified for any link and must either be either a string (in the right format; see docs) or a number.");
228 if (type == LUA_TNUMBER)
229 link.bandwidth = lua_tonumber(L, -1);
231 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of link", link.id.c_str());
235 lua_pushstring(L, "lat");
236 type = lua_gettable(L, -2);
237 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
238 "Attribute 'lat' must be specified for any link and must either be a string (in the right format; see docs) or a number.");
239 if (type == LUA_TNUMBER)
240 link.latency = lua_tonumber(L, -1);
242 link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of link", link.id.c_str());
245 /*Optional Arguments */
247 //get bandwidth_trace value
248 lua_pushstring(L, "bandwidth_file");
250 const char *filename = lua_tostring(L, -1);
252 link.bandwidth_trace = simgrid::kernel::profile::Profile::from_file(filename);
255 //get latency_trace value
256 lua_pushstring(L, "latency_file");
258 filename = lua_tostring(L, -1);
260 link.latency_trace = simgrid::kernel::profile::Profile::from_file(filename);
263 //get state_trace value
264 lua_pushstring(L, "state_file");
266 filename = lua_tostring(L, -1);
268 link.state_trace = simgrid::kernel::profile::Profile::from_file(filename);
272 lua_pushstring(L, "sharing_policy");
274 policy = lua_tostring(L, -1);
276 link.policy = link_policy_get_by_name(policy);
278 sg_platf_new_link(&link);
283 * add Router to AS components
285 int console_add_router(lua_State* L) {
286 lua_ensure(lua_istable(L, -1), "Bad Arguments to create router, Should be a table with named arguments");
288 lua_pushstring(L, "id");
289 int type = lua_gettable(L, -2);
290 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any link and must be a string.");
291 const char* name = lua_tostring(L, -1);
294 lua_pushstring(L,"coord");
296 const char* coords = lua_tostring(L, -1);
299 sg_platf_new_router(name, coords);
304 int console_add_route(lua_State *L) {
305 XBT_DEBUG("Adding route");
306 simgrid::kernel::routing::RouteCreationArgs route;
309 lua_ensure(lua_istable(L, -1), "Bad Arguments to add a route. Should be a table with named arguments");
311 lua_pushstring(L,"src");
312 type = lua_gettable(L,-2);
313 lua_ensure(type == LUA_TSTRING, "Attribute 'src' must be specified for any route and must be a string.");
314 const char *srcName = lua_tostring(L, -1);
315 route.src = sg_netpoint_by_name_or_null(srcName);
316 lua_ensure(route.src != nullptr, "Attribute 'src=%s' of route does not name a node.", srcName);
319 lua_pushstring(L,"dest");
320 type = lua_gettable(L,-2);
321 lua_ensure(type == LUA_TSTRING, "Attribute 'dest' must be specified for any route and must be a string.");
322 const char *dstName = lua_tostring(L, -1);
323 route.dst = sg_netpoint_by_name_or_null(dstName);
324 lua_ensure(route.dst != nullptr, "Attribute 'dst=%s' of route does not name a node.", dstName);
327 lua_pushstring(L,"links");
328 type = lua_gettable(L,-2);
329 lua_ensure(type == LUA_TSTRING,
330 "Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
331 std::vector<std::string> names;
332 const char* str = lua_tostring(L, -1);
333 boost::split(names, str, boost::is_any_of(", \t\r\n"));
336 route.link_list.push_back(simgrid::s4u::Link::by_name(lua_tostring(L, -1))->get_impl());
338 // Several names separated by , \t\r\n
339 for (auto const& name : names) {
340 if (name.length() > 0) {
341 simgrid::kernel::resource::LinkImpl* link = simgrid::s4u::Link::by_name(name)->get_impl();
342 route.link_list.push_back(link);
348 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
349 * Et ouais mon pote. That's the way it goes. F34R.
351 * (Note that above this function, there is a #include statement. Is this
352 * comment related to that statement?)
354 lua_pushstring(L,"symmetrical");
356 if (lua_isstring(L, -1)) {
357 const char* value = lua_tostring(L, -1);
358 if (strcmp("YES", value) == 0)
359 route.symmetrical = true;
361 route.symmetrical = false;
364 route.symmetrical = true;
368 route.gw_src = nullptr;
369 route.gw_dst = nullptr;
371 sg_platf_new_route(&route);
376 int console_add_ASroute(lua_State *L) {
377 simgrid::kernel::routing::RouteCreationArgs ASroute;
379 lua_pushstring(L, "src");
381 const char *srcName = lua_tostring(L, -1);
382 ASroute.src = sg_netpoint_by_name_or_null(srcName);
383 lua_ensure(ASroute.src != nullptr, "Attribute 'src=%s' of AS route does not name a node.", srcName);
386 lua_pushstring(L, "dst");
388 const char *dstName = lua_tostring(L, -1);
389 ASroute.dst = sg_netpoint_by_name_or_null(dstName);
390 lua_ensure(ASroute.dst != nullptr, "Attribute 'dst=%s' of AS route does not name a node.", dstName);
393 lua_pushstring(L, "gw_src");
395 const char* pname = lua_tostring(L, -1);
396 ASroute.gw_src = sg_netpoint_by_name_or_null(pname);
397 lua_ensure(ASroute.gw_src, "Attribute 'gw_src=%s' of AS route does not name a valid node", pname);
400 lua_pushstring(L, "gw_dst");
402 pname = lua_tostring(L, -1);
403 ASroute.gw_dst = sg_netpoint_by_name_or_null(pname);
404 lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst=%s' of AS route does not name a valid node", pname);
407 lua_pushstring(L,"links");
409 std::vector<std::string> names;
410 const char* str = lua_tostring(L, -1);
411 boost::split(names, str, boost::is_any_of(", \t\r\n"));
413 /* unique name with no comma */
414 ASroute.link_list.push_back(simgrid::s4u::Link::by_name(lua_tostring(L, -1))->get_impl());
416 // Several names separated by , \t\r\n
417 for (auto const& name : names) {
418 if (name.length() > 0) {
419 simgrid::kernel::resource::LinkImpl* link = simgrid::s4u::Link::by_name(name)->get_impl();
420 ASroute.link_list.push_back(link);
426 lua_pushstring(L,"symmetrical");
428 if (lua_isstring(L, -1)) {
429 const char* value = lua_tostring(L, -1);
430 if (strcmp("YES", value) == 0)
431 ASroute.symmetrical = true;
433 ASroute.symmetrical = false;
436 ASroute.symmetrical = true;
440 sg_platf_new_route(&ASroute);
445 int console_AS_open(lua_State *L) {
446 XBT_DEBUG("Opening AS");
448 lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
450 lua_pushstring(L, "id");
451 int type = lua_gettable(L, -2);
452 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
453 const char* id = lua_tostring(L, -1);
456 lua_pushstring(L, "mode");
458 const char* mode = lua_tostring(L, -1);
461 int mode_int = A_surfxml_AS_routing_None;
462 if (not strcmp(mode, "Full"))
463 mode_int = A_surfxml_AS_routing_Full;
464 else if (not strcmp(mode, "Floyd"))
465 mode_int = A_surfxml_AS_routing_Floyd;
466 else if (not strcmp(mode, "Dijkstra"))
467 mode_int = A_surfxml_AS_routing_Dijkstra;
468 else if (not strcmp(mode, "DijkstraCache"))
469 mode_int = A_surfxml_AS_routing_DijkstraCache;
470 else if (not strcmp(mode, "Vivaldi"))
471 mode_int = A_surfxml_AS_routing_Vivaldi;
472 else if (not strcmp(mode, "Cluster"))
473 mode_int = A_surfxml_AS_routing_Cluster;
474 else if (not strcmp(mode, "none"))
475 mode_int = A_surfxml_AS_routing_None;
476 else xbt_die("Don't have the model name '%s'",mode);
478 simgrid::kernel::routing::ZoneCreationArgs AS;
480 AS.routing = mode_int;
481 simgrid::kernel::routing::NetZoneImpl* new_as = sg_platf_new_Zone_begin(&AS);
483 /* Build a Lua representation of the new AS on the stack */
485 simgrid::kernel::routing::NetZoneImpl** lua_as = (simgrid::kernel::routing::NetZoneImpl**)lua_newuserdata(
486 L, sizeof(simgrid::kernel::routing::NetZoneImpl*)); /* table userdatum */
488 luaL_getmetatable(L, PLATF_MODULE_NAME); /* table userdatum metatable */
489 lua_setmetatable(L, -2); /* table userdatum */
490 lua_setfield(L, -2, AS_FIELDNAME); /* table -- put the userdata as field of the table */
495 int console_AS_seal(lua_State*)
497 XBT_DEBUG("Sealing AS");
498 sg_platf_new_Zone_seal();
502 int console_host_set_property(lua_State *L) {
503 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
506 lua_pushstring(L, "host");
508 const char* name = lua_tostring(L, -1);
512 lua_pushstring(L, "prop");
514 const char* prop_id = lua_tostring(L, -1);
517 lua_pushstring(L,"value");
519 const char* prop_value = lua_tostring(L, -1);
522 sg_host_t host = sg_host_by_name(name);
523 lua_ensure(host, "no host '%s' found",name);
524 host->set_property(prop_id, prop_value);
530 * @brief Registers the platform functions into the table simgrid.platf.
531 * @param L a lua state
533 void sglua_register_platf_functions(lua_State* L)
535 lua_getglobal(L, "simgrid"); /* simgrid */
536 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
537 lua_setfield(L, -2, "engine"); /* simgrid */
539 lua_pop(L, 1); /* -- */