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/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"
15 #include <boost/algorithm/string/classification.hpp>
16 #include <boost/algorithm/string/split.hpp>
17 #include <simgrid/s4u/Engine.hpp>
18 #include <simgrid/s4u/Host.hpp>
27 XBT_LOG_NEW_DEFAULT_CATEGORY(lua_platf, "Lua bindings (platform module)");
29 constexpr char PLATF_MODULE_NAME[] = "simgrid.engine";
30 constexpr char AS_FIELDNAME[] = "__simgrid_as";
32 /* ********************************************************************************* */
33 /* simgrid.platf API */
34 /* ********************************************************************************* */
36 static const luaL_Reg platf_functions[] = {
37 {"open", console_open},
38 {"close", console_close},
39 {"AS_open", console_AS_open},
40 {"AS_seal", console_AS_seal},
41 {"backbone_new", console_add_backbone},
42 {"host_link_new", console_add_host___link},
43 {"host_new", console_add_host},
44 {"link_new", console_add_link},
45 {"router_new", console_add_router},
46 {"route_new", console_add_route},
47 {"ASroute_new", console_add_ASroute},
51 static simgrid::s4u::Link::SharingPolicy link_policy_get_by_name(const char* policy)
53 if (policy && not strcmp(policy, "FULLDUPLEX")) {
54 XBT_WARN("Please update your platform to use SPLITDUPLEX instead of FULLDUPLEX");
55 return simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
56 } else if (policy && not strcmp(policy, "SPLITDUPLEX")) {
57 return simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
58 } else if (policy && not strcmp(policy, "FATPIPE")) {
59 return simgrid::s4u::Link::SharingPolicy::FATPIPE;
61 return simgrid::s4u::Link::SharingPolicy::SHARED;
65 int console_open(lua_State*)
68 simgrid::s4u::on_platform_creation();
73 int console_close(lua_State*)
75 simgrid::s4u::on_platform_created();
80 int console_add_backbone(lua_State *L) {
81 simgrid::kernel::routing::LinkCreationArgs link;
83 link.properties = nullptr;
85 lua_ensure(lua_istable(L, -1),"Bad Arguments to create backbone in Lua. Should be a table with named arguments.");
87 lua_pushstring(L, "id");
88 int type = lua_gettable(L, -2);
89 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for backbone and must be a string.");
90 link.id = lua_tostring(L, -1);
93 lua_pushstring(L, "bandwidth");
94 type = lua_gettable(L, -2);
95 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
96 "Attribute 'bandwidth' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
97 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of backbone", link.id.c_str());
100 lua_pushstring(L, "lat");
101 type = lua_gettable(L, -2);
102 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
103 "Attribute 'lat' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
104 link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of backbone", link.id.c_str());
107 lua_pushstring(L, "sharing_policy");
109 const char* policy = lua_tostring(L, -1);
111 link.policy = link_policy_get_by_name(policy);
113 sg_platf_new_link(&link);
114 routing_cluster_add_backbone(simgrid::s4u::Link::by_name(link.id)->get_impl());
119 int console_add_host___link(lua_State *L) {
120 simgrid::kernel::routing::HostLinkCreationArgs hostlink;
123 lua_ensure(lua_istable(L, -1), "Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
125 lua_pushstring(L, "id");
126 type = lua_gettable(L, -2);
127 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any host_link and must be a string.");
128 hostlink.id = lua_tostring(L, -1);
131 lua_pushstring(L, "up");
132 type = lua_gettable(L, -2);
133 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
134 "Attribute 'up' must be specified for host_link and must either be a string or a number.");
135 hostlink.link_up = lua_tostring(L, -1);
138 lua_pushstring(L, "down");
139 type = lua_gettable(L, -2);
140 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
141 "Attribute 'down' must be specified for host_link and must either be a string or a number.");
142 hostlink.link_down = lua_tostring(L, -1);
145 XBT_DEBUG("Create a host_link for host %s", hostlink.id.c_str());
146 sg_platf_new_hostlink(&hostlink);
151 int console_add_host(lua_State *L) {
152 simgrid::kernel::routing::HostCreationArgs host;
155 // we get values from the table passed as argument
156 lua_ensure(lua_istable(L, -1),
157 "Bad Arguments to create host. Should be a table with named arguments");
160 lua_pushstring(L, "id");
161 type = lua_gettable(L, -2);
162 lua_ensure(type == LUA_TSTRING,
163 "Attribute 'id' must be specified for any host and must be a string.");
164 host.id = lua_tostring(L, -1);
168 lua_pushstring(L, "speed");
169 type = lua_gettable(L, -2);
170 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
171 "Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
172 if (type == LUA_TNUMBER)
173 host.speed_per_pstate.push_back(lua_tointeger(L, -1));
175 host.speed_per_pstate.push_back(surf_parse_get_speed(lua_tostring(L, -1), "speed of host", host.id));
179 lua_pushstring(L, "core");
181 if (not lua_isnumber(L, -1))
182 host.core_amount = 1; // Default value
184 host.core_amount = lua_tonumber(L, -1);
185 if (host.core_amount == 0)
186 host.core_amount = 1;
190 lua_pushstring(L, "availability_file");
192 const char *filename = lua_tostring(L, -1);
194 host.speed_trace = simgrid::kernel::profile::Profile::from_file(filename);
198 lua_pushstring(L, "state_file");
200 filename = lua_tostring(L, -1);
202 host.state_trace = simgrid::kernel::profile::Profile::from_file(filename);
205 sg_platf_new_host(&host);
210 int console_add_link(lua_State *L) {
211 simgrid::kernel::routing::LinkCreationArgs link;
215 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
218 lua_pushstring(L, "id");
219 int type = lua_gettable(L, -2);
220 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
221 "Attribute 'id' must be specified for any link and must be a string.");
222 link.id = lua_tostring(L, -1);
225 // get bandwidth value
226 lua_pushstring(L, "bandwidth");
227 type = lua_gettable(L, -2);
228 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
229 "Attribute 'bandwidth' must be specified for any link and must either be either a string (in the right format; see docs) or a number.");
230 if (type == LUA_TNUMBER)
231 link.bandwidth = lua_tonumber(L, -1);
233 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of link", link.id.c_str());
237 lua_pushstring(L, "lat");
238 type = lua_gettable(L, -2);
239 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
240 "Attribute 'lat' must be specified for any link and must either be a string (in the right format; see docs) or a number.");
241 if (type == LUA_TNUMBER)
242 link.latency = lua_tonumber(L, -1);
244 link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of link", link.id.c_str());
247 /*Optional Arguments */
249 //get bandwidth_trace value
250 lua_pushstring(L, "bandwidth_file");
252 const char *filename = lua_tostring(L, -1);
254 link.bandwidth_trace = simgrid::kernel::profile::Profile::from_file(filename);
257 //get latency_trace value
258 lua_pushstring(L, "latency_file");
260 filename = lua_tostring(L, -1);
262 link.latency_trace = simgrid::kernel::profile::Profile::from_file(filename);
265 //get state_trace value
266 lua_pushstring(L, "state_file");
268 filename = lua_tostring(L, -1);
270 link.state_trace = simgrid::kernel::profile::Profile::from_file(filename);
274 lua_pushstring(L, "sharing_policy");
276 policy = lua_tostring(L, -1);
278 link.policy = link_policy_get_by_name(policy);
280 sg_platf_new_link(&link);
285 * add Router to AS components
287 int console_add_router(lua_State* L) {
288 lua_ensure(lua_istable(L, -1), "Bad Arguments to create router, Should be a table with named arguments");
290 lua_pushstring(L, "id");
291 int type = lua_gettable(L, -2);
292 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any link and must be a string.");
293 const char* name = lua_tostring(L, -1);
296 lua_pushstring(L,"coord");
298 const char* coords = lua_tostring(L, -1);
301 sg_platf_new_router(name, coords);
306 int console_add_route(lua_State *L) {
307 XBT_DEBUG("Adding route");
308 simgrid::kernel::routing::RouteCreationArgs route;
311 lua_ensure(lua_istable(L, -1), "Bad Arguments to add a route. Should be a table with named arguments");
313 lua_pushstring(L,"src");
314 type = lua_gettable(L,-2);
315 lua_ensure(type == LUA_TSTRING, "Attribute 'src' must be specified for any route and must be a string.");
316 const char *srcName = lua_tostring(L, -1);
317 route.src = sg_netpoint_by_name_or_null(srcName);
318 lua_ensure(route.src != nullptr, "Attribute 'src=%s' of route does not name a node.", srcName);
321 lua_pushstring(L,"dest");
322 type = lua_gettable(L,-2);
323 lua_ensure(type == LUA_TSTRING, "Attribute 'dest' must be specified for any route and must be a string.");
324 const char *dstName = lua_tostring(L, -1);
325 route.dst = sg_netpoint_by_name_or_null(dstName);
326 lua_ensure(route.dst != nullptr, "Attribute 'dst=%s' of route does not name a node.", dstName);
329 lua_pushstring(L,"links");
330 type = lua_gettable(L,-2);
331 lua_ensure(type == LUA_TSTRING,
332 "Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
333 std::vector<std::string> names;
334 const char* str = lua_tostring(L, -1);
335 boost::split(names, str, boost::is_any_of(", \t\r\n"));
338 route.link_list.push_back(simgrid::s4u::Link::by_name(lua_tostring(L, -1))->get_impl());
340 // Several names separated by , \t\r\n
341 for (auto const& name : names) {
342 if (name.length() > 0) {
343 simgrid::kernel::resource::LinkImpl* link = simgrid::s4u::Link::by_name(name)->get_impl();
344 route.link_list.push_back(link);
350 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
351 * Et ouais mon pote. That's the way it goes. F34R.
353 * (Note that above this function, there is a #include statement. Is this
354 * comment related to that statement?)
356 lua_pushstring(L,"symmetrical");
358 if (lua_isstring(L, -1)) {
359 const char* value = lua_tostring(L, -1);
360 if (strcmp("YES", value) == 0)
361 route.symmetrical = true;
363 route.symmetrical = false;
366 route.symmetrical = true;
370 route.gw_src = nullptr;
371 route.gw_dst = nullptr;
373 sg_platf_new_route(&route);
378 int console_add_ASroute(lua_State *L) {
379 simgrid::kernel::routing::RouteCreationArgs ASroute;
381 lua_pushstring(L, "src");
383 const char *srcName = lua_tostring(L, -1);
384 ASroute.src = sg_netpoint_by_name_or_null(srcName);
385 lua_ensure(ASroute.src != nullptr, "Attribute 'src=%s' of AS route does not name a node.", srcName);
388 lua_pushstring(L, "dst");
390 const char *dstName = lua_tostring(L, -1);
391 ASroute.dst = sg_netpoint_by_name_or_null(dstName);
392 lua_ensure(ASroute.dst != nullptr, "Attribute 'dst=%s' of AS route does not name a node.", dstName);
395 lua_pushstring(L, "gw_src");
397 const char* pname = lua_tostring(L, -1);
398 ASroute.gw_src = sg_netpoint_by_name_or_null(pname);
399 lua_ensure(ASroute.gw_src, "Attribute 'gw_src=%s' of AS route does not name a valid node", pname);
402 lua_pushstring(L, "gw_dst");
404 pname = lua_tostring(L, -1);
405 ASroute.gw_dst = sg_netpoint_by_name_or_null(pname);
406 lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst=%s' of AS route does not name a valid node", pname);
409 lua_pushstring(L,"links");
411 std::vector<std::string> names;
412 const char* str = lua_tostring(L, -1);
413 boost::split(names, str, boost::is_any_of(", \t\r\n"));
415 /* unique name with no comma */
416 ASroute.link_list.push_back(simgrid::s4u::Link::by_name(lua_tostring(L, -1))->get_impl());
418 // Several names separated by , \t\r\n
419 for (auto const& name : names) {
420 if (name.length() > 0) {
421 simgrid::kernel::resource::LinkImpl* link = simgrid::s4u::Link::by_name(name)->get_impl();
422 ASroute.link_list.push_back(link);
428 lua_pushstring(L,"symmetrical");
430 if (lua_isstring(L, -1)) {
431 const char* value = lua_tostring(L, -1);
432 if (strcmp("YES", value) == 0)
433 ASroute.symmetrical = true;
435 ASroute.symmetrical = false;
438 ASroute.symmetrical = true;
442 sg_platf_new_route(&ASroute);
447 int console_AS_open(lua_State *L) {
448 XBT_DEBUG("Opening AS");
450 lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
452 lua_pushstring(L, "id");
453 int type = lua_gettable(L, -2);
454 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
455 const char* id = lua_tostring(L, -1);
458 lua_pushstring(L, "mode");
460 const char* mode = lua_tostring(L, -1);
463 int mode_int = A_surfxml_AS_routing_None;
464 if (not strcmp(mode, "Full"))
465 mode_int = A_surfxml_AS_routing_Full;
466 else if (not strcmp(mode, "Floyd"))
467 mode_int = A_surfxml_AS_routing_Floyd;
468 else if (not strcmp(mode, "Dijkstra"))
469 mode_int = A_surfxml_AS_routing_Dijkstra;
470 else if (not strcmp(mode, "DijkstraCache"))
471 mode_int = A_surfxml_AS_routing_DijkstraCache;
472 else if (not strcmp(mode, "Vivaldi"))
473 mode_int = A_surfxml_AS_routing_Vivaldi;
474 else if (not strcmp(mode, "Cluster"))
475 mode_int = A_surfxml_AS_routing_Cluster;
476 else if (not strcmp(mode, "none"))
477 mode_int = A_surfxml_AS_routing_None;
478 else xbt_die("Don't have the model name '%s'",mode);
480 simgrid::kernel::routing::ZoneCreationArgs AS;
482 AS.routing = mode_int;
483 simgrid::kernel::routing::NetZoneImpl* new_as = sg_platf_new_Zone_begin(&AS);
485 /* Build a Lua representation of the new AS on the stack */
487 simgrid::kernel::routing::NetZoneImpl** lua_as = (simgrid::kernel::routing::NetZoneImpl**)lua_newuserdata(
488 L, sizeof(simgrid::kernel::routing::NetZoneImpl*)); /* table userdatum */
490 luaL_getmetatable(L, PLATF_MODULE_NAME); /* table userdatum metatable */
491 lua_setmetatable(L, -2); /* table userdatum */
492 lua_setfield(L, -2, AS_FIELDNAME); /* table -- put the userdata as field of the table */
497 int console_AS_seal(lua_State*)
499 XBT_DEBUG("Sealing AS");
500 sg_platf_new_Zone_seal();
504 int console_host_set_property(lua_State *L) {
505 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
508 lua_pushstring(L, "host");
510 const char* name = lua_tostring(L, -1);
514 lua_pushstring(L, "prop");
516 const char* prop_id = lua_tostring(L, -1);
519 lua_pushstring(L,"value");
521 const char* prop_value = lua_tostring(L, -1);
524 sg_host_t host = sg_host_by_name(name);
525 lua_ensure(host, "no host '%s' found",name);
526 host->set_property(prop_id, prop_value);
532 * @brief Registers the platform functions into the table simgrid.platf.
533 * @param L a lua state
535 void sglua_register_platf_functions(lua_State* L)
537 lua_getglobal(L, "simgrid"); /* simgrid */
538 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
539 lua_setfield(L, -2, "engine"); /* simgrid */
541 lua_pop(L, 1); /* -- */