1 /* Copyright (c) 2010-2018. 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/Host.hpp>
24 XBT_LOG_NEW_DEFAULT_CATEGORY(lua_platf, "Lua bindings (platform module)");
26 #define PLATF_MODULE_NAME "simgrid.engine"
27 #define AS_FIELDNAME "__simgrid_as"
29 /* ********************************************************************************* */
30 /* simgrid.platf API */
31 /* ********************************************************************************* */
33 static const luaL_Reg platf_functions[] = {
34 {"open", console_open},
35 {"close", console_close},
36 {"AS_open", console_AS_open},
37 {"AS_seal", console_AS_seal},
38 {"backbone_new", console_add_backbone},
39 {"host_link_new", console_add_host___link},
40 {"host_new", console_add_host},
41 {"link_new", console_add_link},
42 {"router_new", console_add_router},
43 {"route_new", console_add_route},
44 {"ASroute_new", console_add_ASroute},
48 int console_open(lua_State *L) {
52 storage_register_callbacks();
57 int console_close(lua_State *L) {
63 int console_add_backbone(lua_State *L) {
64 simgrid::kernel::routing::LinkCreationArgs link;
66 link.properties = nullptr;
68 lua_ensure(lua_istable(L, -1),"Bad Arguments to create backbone in Lua. Should be a table with named arguments.");
70 lua_pushstring(L, "id");
71 int type = lua_gettable(L, -2);
72 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for backbone and must be a string.");
73 link.id = lua_tostring(L, -1);
76 lua_pushstring(L, "bandwidth");
77 type = lua_gettable(L, -2);
78 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
79 "Attribute 'bandwidth' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
80 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of backbone", link.id.c_str());
83 lua_pushstring(L, "lat");
84 type = lua_gettable(L, -2);
85 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
86 "Attribute 'lat' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
87 link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of backbone", link.id.c_str());
90 lua_pushstring(L, "sharing_policy");
92 const char* policy = lua_tostring(L, -1);
94 if (policy && not strcmp(policy, "FULLDUPLEX")) {
95 XBT_WARN("Please update your platform to use SPLITDUPLEX instead of FULLDUPLEX");
96 link.policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
97 } else if (policy && not strcmp(policy, "SPLITDUPLEX")) {
98 link.policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
99 } else if (policy && not strcmp(policy, "FATPIPE")) {
100 link.policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
102 link.policy = simgrid::s4u::Link::SharingPolicy::SHARED;
105 sg_platf_new_link(&link);
106 routing_cluster_add_backbone(simgrid::kernel::resource::LinkImpl::byName(link.id));
111 int console_add_host___link(lua_State *L) {
112 simgrid::kernel::routing::HostLinkCreationArgs hostlink;
115 lua_ensure(lua_istable(L, -1), "Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
117 lua_pushstring(L, "id");
118 type = lua_gettable(L, -2);
119 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any host_link and must be a string.");
120 hostlink.id = lua_tostring(L, -1);
123 lua_pushstring(L, "up");
124 type = lua_gettable(L, -2);
125 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
126 "Attribute 'up' must be specified for host_link and must either be a string or a number.");
127 hostlink.link_up = lua_tostring(L, -1);
130 lua_pushstring(L, "down");
131 type = lua_gettable(L, -2);
132 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
133 "Attribute 'down' must be specified for host_link and must either be a string or a number.");
134 hostlink.link_down = lua_tostring(L, -1);
137 XBT_DEBUG("Create a host_link for host %s", hostlink.id.c_str());
138 sg_platf_new_hostlink(&hostlink);
143 int console_add_host(lua_State *L) {
144 simgrid::kernel::routing::HostCreationArgs host;
147 // we get values from the table passed as argument
148 lua_ensure(lua_istable(L, -1),
149 "Bad Arguments to create host. Should be a table with named arguments");
152 lua_pushstring(L, "id");
153 type = lua_gettable(L, -2);
154 lua_ensure(type == LUA_TSTRING,
155 "Attribute 'id' must be specified for any host and must be a string.");
156 host.id = lua_tostring(L, -1);
160 lua_pushstring(L, "speed");
161 type = lua_gettable(L, -2);
162 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
163 "Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
164 if (type == LUA_TNUMBER)
165 host.speed_per_pstate.push_back(lua_tointeger(L, -1));
167 host.speed_per_pstate.push_back(surf_parse_get_speed(lua_tostring(L, -1), "speed of host", host.id));
171 lua_pushstring(L, "core");
173 if (not lua_isnumber(L, -1))
174 host.core_amount = 1; // Default value
176 host.core_amount = lua_tonumber(L, -1);
177 if (host.core_amount == 0)
178 host.core_amount = 1;
182 lua_pushstring(L, "availability_file");
184 const char *filename = lua_tostring(L, -1);
186 host.speed_trace = tmgr_trace_new_from_file(filename);
190 lua_pushstring(L, "state_file");
192 filename = lua_tostring(L, -1);
194 host.state_trace = tmgr_trace_new_from_file(filename);
197 sg_platf_new_host(&host);
202 int console_add_link(lua_State *L) {
203 simgrid::kernel::routing::LinkCreationArgs link;
207 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
210 lua_pushstring(L, "id");
211 int type = lua_gettable(L, -2);
212 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
213 "Attribute 'id' must be specified for any link and must be a string.");
214 link.id = lua_tostring(L, -1);
217 // get bandwidth value
218 lua_pushstring(L, "bandwidth");
219 type = lua_gettable(L, -2);
220 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
221 "Attribute 'bandwidth' must be specified for any link and must either be either a string (in the right format; see docs) or a number.");
222 if (type == LUA_TNUMBER)
223 link.bandwidth = lua_tonumber(L, -1);
225 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of link", link.id.c_str());
229 lua_pushstring(L, "lat");
230 type = lua_gettable(L, -2);
231 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
232 "Attribute 'lat' must be specified for any link and must either be a string (in the right format; see docs) or a number.");
233 if (type == LUA_TNUMBER)
234 link.latency = lua_tonumber(L, -1);
236 link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of link", link.id.c_str());
239 /*Optional Arguments */
241 //get bandwidth_trace value
242 lua_pushstring(L, "bandwidth_file");
244 const char *filename = lua_tostring(L, -1);
246 link.bandwidth_trace = tmgr_trace_new_from_file(filename);
249 //get latency_trace value
250 lua_pushstring(L, "latency_file");
252 filename = lua_tostring(L, -1);
254 link.latency_trace = tmgr_trace_new_from_file(filename);
257 //get state_trace value
258 lua_pushstring(L, "state_file");
260 filename = lua_tostring(L, -1);
262 link.state_trace = tmgr_trace_new_from_file(filename);
266 lua_pushstring(L, "sharing_policy");
268 policy = lua_tostring(L, -1);
270 if (policy && not strcmp(policy, "FULLDUPLEX")) {
271 XBT_WARN("Please update your platform to use SPLITDUPLEX instead of FULLDUPLEX");
272 link.policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
273 } else if (policy && not strcmp(policy, "SPLITDUPLEX")) {
274 link.policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
275 } else if (policy && not strcmp(policy, "FATPIPE")) {
276 link.policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
278 link.policy = simgrid::s4u::Link::SharingPolicy::SHARED;
281 sg_platf_new_link(&link);
286 * add Router to AS components
288 int console_add_router(lua_State* L) {
289 lua_ensure(lua_istable(L, -1), "Bad Arguments to create router, Should be a table with named arguments");
291 lua_pushstring(L, "id");
292 int type = lua_gettable(L, -2);
293 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any link and must be a string.");
294 const char* name = lua_tostring(L, -1);
297 lua_pushstring(L,"coord");
299 const char* coords = lua_tostring(L, -1);
302 sg_platf_new_router(name, coords);
307 int console_add_route(lua_State *L) {
308 XBT_DEBUG("Adding route");
309 simgrid::kernel::routing::RouteCreationArgs route;
312 lua_ensure(lua_istable(L, -1), "Bad Arguments to add a route. Should be a table with named arguments");
314 lua_pushstring(L,"src");
315 type = lua_gettable(L,-2);
316 lua_ensure(type == LUA_TSTRING, "Attribute 'src' must be specified for any route and must be a string.");
317 const char *srcName = lua_tostring(L, -1);
318 route.src = sg_netpoint_by_name_or_null(srcName);
319 lua_ensure(route.src != nullptr, "Attribute 'src=%s' of route does not name a node.", srcName);
322 lua_pushstring(L,"dest");
323 type = lua_gettable(L,-2);
324 lua_ensure(type == LUA_TSTRING, "Attribute 'dest' must be specified for any route and must be a string.");
325 const char *dstName = lua_tostring(L, -1);
326 route.dst = sg_netpoint_by_name_or_null(dstName);
327 lua_ensure(route.dst != nullptr, "Attribute 'dst=%s' of route does not name a node.", dstName);
330 lua_pushstring(L,"links");
331 type = lua_gettable(L,-2);
332 lua_ensure(type == LUA_TSTRING,
333 "Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
334 std::vector<std::string> names;
335 const char* str = lua_tostring(L, -1);
336 boost::split(names, str, boost::is_any_of(", \t\r\n"));
339 route.link_list.push_back(simgrid::kernel::resource::LinkImpl::byName(lua_tostring(L, -1)));
341 // Several names separated by , \t\r\n
342 for (auto const& name : names) {
343 if (name.length() > 0) {
344 simgrid::kernel::resource::LinkImpl* link = simgrid::kernel::resource::LinkImpl::byName(name);
345 route.link_list.push_back(link);
351 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
352 * Et ouais mon pote. That's the way it goes. F34R.
354 * (Note that above this function, there is a #include statement. Is this
355 * comment related to that statement?)
357 lua_pushstring(L,"symmetrical");
359 if (lua_isstring(L, -1)) {
360 const char* value = lua_tostring(L, -1);
361 if (strcmp("YES", value) == 0)
362 route.symmetrical = true;
364 route.symmetrical = false;
367 route.symmetrical = true;
371 route.gw_src = nullptr;
372 route.gw_dst = nullptr;
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 *name = lua_tostring(L, -1);
399 ASroute.gw_src = sg_netpoint_by_name_or_null(name);
400 lua_ensure(ASroute.gw_src, "Attribute 'gw_src=%s' of AS route does not name a valid node", name);
403 lua_pushstring(L, "gw_dst");
405 name = lua_tostring(L, -1);
406 ASroute.gw_dst = sg_netpoint_by_name_or_null(name);
407 lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst=%s' of AS route does not name a valid node", name);
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::kernel::resource::LinkImpl::byName(lua_tostring(L, -1)));
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::kernel::resource::LinkImpl::byName(name);
423 ASroute.link_list.push_back(link);
429 lua_pushstring(L,"symmetrical");
431 if (lua_isstring(L, -1)) {
432 const char* value = lua_tostring(L, -1);
433 if (strcmp("YES", value) == 0)
434 ASroute.symmetrical = true;
436 ASroute.symmetrical = false;
439 ASroute.symmetrical = true;
443 sg_platf_new_route(&ASroute);
448 int console_AS_open(lua_State *L) {
449 XBT_DEBUG("Opening AS");
451 lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
453 lua_pushstring(L, "id");
454 int type = lua_gettable(L, -2);
455 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
456 const char* id = lua_tostring(L, -1);
459 lua_pushstring(L, "mode");
461 const char* mode = lua_tostring(L, -1);
464 int mode_int = A_surfxml_AS_routing_None;
465 if (not strcmp(mode, "Full"))
466 mode_int = A_surfxml_AS_routing_Full;
467 else if (not strcmp(mode, "Floyd"))
468 mode_int = A_surfxml_AS_routing_Floyd;
469 else if (not strcmp(mode, "Dijkstra"))
470 mode_int = A_surfxml_AS_routing_Dijkstra;
471 else if (not strcmp(mode, "DijkstraCache"))
472 mode_int = A_surfxml_AS_routing_DijkstraCache;
473 else if (not strcmp(mode, "Vivaldi"))
474 mode_int = A_surfxml_AS_routing_Vivaldi;
475 else if (not strcmp(mode, "Cluster"))
476 mode_int = A_surfxml_AS_routing_Cluster;
477 else if (not strcmp(mode, "none"))
478 mode_int = A_surfxml_AS_routing_None;
479 else xbt_die("Don't have the model name '%s'",mode);
481 simgrid::kernel::routing::ZoneCreationArgs AS;
483 AS.routing = mode_int;
484 simgrid::s4u::NetZone* new_as = sg_platf_new_Zone_begin(&AS);
486 /* Build a Lua representation of the new AS on the stack */
488 simgrid::s4u::NetZone** lua_as =
489 (simgrid::s4u::NetZone**)lua_newuserdata(L, sizeof(simgrid::s4u::NetZone*)); /* table userdatum */
491 luaL_getmetatable(L, PLATF_MODULE_NAME); /* table userdatum metatable */
492 lua_setmetatable(L, -2); /* table userdatum */
493 lua_setfield(L, -2, AS_FIELDNAME); /* table -- put the userdata as field of the table */
497 int console_AS_seal(lua_State *L) {
498 XBT_DEBUG("Sealing AS");
499 sg_platf_new_Zone_seal();
503 int console_host_set_property(lua_State *L) {
504 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
507 lua_pushstring(L, "host");
509 const char* name = lua_tostring(L, -1);
513 lua_pushstring(L, "prop");
515 const char* prop_id = lua_tostring(L, -1);
518 lua_pushstring(L,"value");
520 const char* prop_value = lua_tostring(L, -1);
523 sg_host_t host = sg_host_by_name(name);
524 lua_ensure(host, "no host '%s' found",name);
525 host->setProperty(prop_id, prop_value);
531 * \brief Registers the platform functions into the table simgrid.platf.
532 * \param L a lua state
534 void sglua_register_platf_functions(lua_State* L)
536 lua_getglobal(L, "simgrid"); /* simgrid */
537 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
538 lua_setfield(L, -2, "engine"); /* simgrid */
540 lua_pop(L, 1); /* -- */