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 #define PLATF_MODULE_NAME "simgrid.engine"
28 #define 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 *L) {
65 simgrid::s4u::on_platform_creation();
70 int console_close(lua_State *L) {
71 simgrid::s4u::on_platform_created();
76 int console_add_backbone(lua_State *L) {
77 simgrid::kernel::routing::LinkCreationArgs link;
79 link.properties = nullptr;
81 lua_ensure(lua_istable(L, -1),"Bad Arguments to create backbone in Lua. Should be a table with named arguments.");
83 lua_pushstring(L, "id");
84 int type = lua_gettable(L, -2);
85 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for backbone and must be a string.");
86 link.id = lua_tostring(L, -1);
89 lua_pushstring(L, "bandwidth");
90 type = lua_gettable(L, -2);
91 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
92 "Attribute 'bandwidth' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
93 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of backbone", link.id.c_str());
96 lua_pushstring(L, "lat");
97 type = lua_gettable(L, -2);
98 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
99 "Attribute 'lat' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
100 link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of backbone", link.id.c_str());
103 lua_pushstring(L, "sharing_policy");
105 const char* policy = lua_tostring(L, -1);
107 link.policy = link_policy_get_by_name(policy);
109 sg_platf_new_link(&link);
110 routing_cluster_add_backbone(simgrid::s4u::Link::by_name(link.id)->get_impl());
115 int console_add_host___link(lua_State *L) {
116 simgrid::kernel::routing::HostLinkCreationArgs hostlink;
119 lua_ensure(lua_istable(L, -1), "Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
121 lua_pushstring(L, "id");
122 type = lua_gettable(L, -2);
123 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any host_link and must be a string.");
124 hostlink.id = lua_tostring(L, -1);
127 lua_pushstring(L, "up");
128 type = lua_gettable(L, -2);
129 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
130 "Attribute 'up' must be specified for host_link and must either be a string or a number.");
131 hostlink.link_up = lua_tostring(L, -1);
134 lua_pushstring(L, "down");
135 type = lua_gettable(L, -2);
136 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
137 "Attribute 'down' must be specified for host_link and must either be a string or a number.");
138 hostlink.link_down = lua_tostring(L, -1);
141 XBT_DEBUG("Create a host_link for host %s", hostlink.id.c_str());
142 sg_platf_new_hostlink(&hostlink);
147 int console_add_host(lua_State *L) {
148 simgrid::kernel::routing::HostCreationArgs host;
151 // we get values from the table passed as argument
152 lua_ensure(lua_istable(L, -1),
153 "Bad Arguments to create host. Should be a table with named arguments");
156 lua_pushstring(L, "id");
157 type = lua_gettable(L, -2);
158 lua_ensure(type == LUA_TSTRING,
159 "Attribute 'id' must be specified for any host and must be a string.");
160 host.id = lua_tostring(L, -1);
164 lua_pushstring(L, "speed");
165 type = lua_gettable(L, -2);
166 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
167 "Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
168 if (type == LUA_TNUMBER)
169 host.speed_per_pstate.push_back(lua_tointeger(L, -1));
171 host.speed_per_pstate.push_back(surf_parse_get_speed(lua_tostring(L, -1), "speed of host", host.id));
175 lua_pushstring(L, "core");
177 if (not lua_isnumber(L, -1))
178 host.core_amount = 1; // Default value
180 host.core_amount = lua_tonumber(L, -1);
181 if (host.core_amount == 0)
182 host.core_amount = 1;
186 lua_pushstring(L, "availability_file");
188 const char *filename = lua_tostring(L, -1);
190 host.speed_trace = tmgr_trace_new_from_file(filename);
194 lua_pushstring(L, "state_file");
196 filename = lua_tostring(L, -1);
198 host.state_trace = tmgr_trace_new_from_file(filename);
201 sg_platf_new_host(&host);
206 int console_add_link(lua_State *L) {
207 simgrid::kernel::routing::LinkCreationArgs link;
211 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
214 lua_pushstring(L, "id");
215 int type = lua_gettable(L, -2);
216 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
217 "Attribute 'id' must be specified for any link and must be a string.");
218 link.id = lua_tostring(L, -1);
221 // get bandwidth value
222 lua_pushstring(L, "bandwidth");
223 type = lua_gettable(L, -2);
224 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
225 "Attribute 'bandwidth' must be specified for any link and must either be either a string (in the right format; see docs) or a number.");
226 if (type == LUA_TNUMBER)
227 link.bandwidth = lua_tonumber(L, -1);
229 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of link", link.id.c_str());
233 lua_pushstring(L, "lat");
234 type = lua_gettable(L, -2);
235 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
236 "Attribute 'lat' must be specified for any link and must either be a string (in the right format; see docs) or a number.");
237 if (type == LUA_TNUMBER)
238 link.latency = lua_tonumber(L, -1);
240 link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of link", link.id.c_str());
243 /*Optional Arguments */
245 //get bandwidth_trace value
246 lua_pushstring(L, "bandwidth_file");
248 const char *filename = lua_tostring(L, -1);
250 link.bandwidth_trace = tmgr_trace_new_from_file(filename);
253 //get latency_trace value
254 lua_pushstring(L, "latency_file");
256 filename = lua_tostring(L, -1);
258 link.latency_trace = tmgr_trace_new_from_file(filename);
261 //get state_trace value
262 lua_pushstring(L, "state_file");
264 filename = lua_tostring(L, -1);
266 link.state_trace = tmgr_trace_new_from_file(filename);
270 lua_pushstring(L, "sharing_policy");
272 policy = lua_tostring(L, -1);
274 link.policy = link_policy_get_by_name(policy);
276 sg_platf_new_link(&link);
281 * add Router to AS components
283 int console_add_router(lua_State* L) {
284 lua_ensure(lua_istable(L, -1), "Bad Arguments to create router, Should be a table with named arguments");
286 lua_pushstring(L, "id");
287 int type = lua_gettable(L, -2);
288 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any link and must be a string.");
289 const char* name = lua_tostring(L, -1);
292 lua_pushstring(L,"coord");
294 const char* coords = lua_tostring(L, -1);
297 sg_platf_new_router(name, coords);
302 int console_add_route(lua_State *L) {
303 XBT_DEBUG("Adding route");
304 simgrid::kernel::routing::RouteCreationArgs route;
307 lua_ensure(lua_istable(L, -1), "Bad Arguments to add a route. Should be a table with named arguments");
309 lua_pushstring(L,"src");
310 type = lua_gettable(L,-2);
311 lua_ensure(type == LUA_TSTRING, "Attribute 'src' must be specified for any route and must be a string.");
312 const char *srcName = lua_tostring(L, -1);
313 route.src = sg_netpoint_by_name_or_null(srcName);
314 lua_ensure(route.src != nullptr, "Attribute 'src=%s' of route does not name a node.", srcName);
317 lua_pushstring(L,"dest");
318 type = lua_gettable(L,-2);
319 lua_ensure(type == LUA_TSTRING, "Attribute 'dest' must be specified for any route and must be a string.");
320 const char *dstName = lua_tostring(L, -1);
321 route.dst = sg_netpoint_by_name_or_null(dstName);
322 lua_ensure(route.dst != nullptr, "Attribute 'dst=%s' of route does not name a node.", dstName);
325 lua_pushstring(L,"links");
326 type = lua_gettable(L,-2);
327 lua_ensure(type == LUA_TSTRING,
328 "Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
329 std::vector<std::string> names;
330 const char* str = lua_tostring(L, -1);
331 boost::split(names, str, boost::is_any_of(", \t\r\n"));
334 route.link_list.push_back(simgrid::s4u::Link::by_name(lua_tostring(L, -1))->get_impl());
336 // Several names separated by , \t\r\n
337 for (auto const& name : names) {
338 if (name.length() > 0) {
339 simgrid::kernel::resource::LinkImpl* link = simgrid::s4u::Link::by_name(name)->get_impl();
340 route.link_list.push_back(link);
346 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
347 * Et ouais mon pote. That's the way it goes. F34R.
349 * (Note that above this function, there is a #include statement. Is this
350 * comment related to that statement?)
352 lua_pushstring(L,"symmetrical");
354 if (lua_isstring(L, -1)) {
355 const char* value = lua_tostring(L, -1);
356 if (strcmp("YES", value) == 0)
357 route.symmetrical = true;
359 route.symmetrical = false;
362 route.symmetrical = true;
366 route.gw_src = nullptr;
367 route.gw_dst = nullptr;
369 sg_platf_new_route(&route);
374 int console_add_ASroute(lua_State *L) {
375 simgrid::kernel::routing::RouteCreationArgs ASroute;
377 lua_pushstring(L, "src");
379 const char *srcName = lua_tostring(L, -1);
380 ASroute.src = sg_netpoint_by_name_or_null(srcName);
381 lua_ensure(ASroute.src != nullptr, "Attribute 'src=%s' of AS route does not name a node.", srcName);
384 lua_pushstring(L, "dst");
386 const char *dstName = lua_tostring(L, -1);
387 ASroute.dst = sg_netpoint_by_name_or_null(dstName);
388 lua_ensure(ASroute.dst != nullptr, "Attribute 'dst=%s' of AS route does not name a node.", dstName);
391 lua_pushstring(L, "gw_src");
393 const char *name = lua_tostring(L, -1);
394 ASroute.gw_src = sg_netpoint_by_name_or_null(name);
395 lua_ensure(ASroute.gw_src, "Attribute 'gw_src=%s' of AS route does not name a valid node", name);
398 lua_pushstring(L, "gw_dst");
400 name = lua_tostring(L, -1);
401 ASroute.gw_dst = sg_netpoint_by_name_or_null(name);
402 lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst=%s' of AS route does not name a valid node", name);
405 lua_pushstring(L,"links");
407 std::vector<std::string> names;
408 const char* str = lua_tostring(L, -1);
409 boost::split(names, str, boost::is_any_of(", \t\r\n"));
411 /* unique name with no comma */
412 ASroute.link_list.push_back(simgrid::s4u::Link::by_name(lua_tostring(L, -1))->get_impl());
414 // Several names separated by , \t\r\n
415 for (auto const& name : names) {
416 if (name.length() > 0) {
417 simgrid::kernel::resource::LinkImpl* link = simgrid::s4u::Link::by_name(name)->get_impl();
418 ASroute.link_list.push_back(link);
424 lua_pushstring(L,"symmetrical");
426 if (lua_isstring(L, -1)) {
427 const char* value = lua_tostring(L, -1);
428 if (strcmp("YES", value) == 0)
429 ASroute.symmetrical = true;
431 ASroute.symmetrical = false;
434 ASroute.symmetrical = true;
438 sg_platf_new_route(&ASroute);
443 int console_AS_open(lua_State *L) {
444 XBT_DEBUG("Opening AS");
446 lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
448 lua_pushstring(L, "id");
449 int type = lua_gettable(L, -2);
450 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
451 const char* id = lua_tostring(L, -1);
454 lua_pushstring(L, "mode");
456 const char* mode = lua_tostring(L, -1);
459 int mode_int = A_surfxml_AS_routing_None;
460 if (not strcmp(mode, "Full"))
461 mode_int = A_surfxml_AS_routing_Full;
462 else if (not strcmp(mode, "Floyd"))
463 mode_int = A_surfxml_AS_routing_Floyd;
464 else if (not strcmp(mode, "Dijkstra"))
465 mode_int = A_surfxml_AS_routing_Dijkstra;
466 else if (not strcmp(mode, "DijkstraCache"))
467 mode_int = A_surfxml_AS_routing_DijkstraCache;
468 else if (not strcmp(mode, "Vivaldi"))
469 mode_int = A_surfxml_AS_routing_Vivaldi;
470 else if (not strcmp(mode, "Cluster"))
471 mode_int = A_surfxml_AS_routing_Cluster;
472 else if (not strcmp(mode, "none"))
473 mode_int = A_surfxml_AS_routing_None;
474 else xbt_die("Don't have the model name '%s'",mode);
476 simgrid::kernel::routing::ZoneCreationArgs AS;
478 AS.routing = mode_int;
479 simgrid::kernel::routing::NetZoneImpl* new_as = sg_platf_new_Zone_begin(&AS);
481 /* Build a Lua representation of the new AS on the stack */
483 simgrid::kernel::routing::NetZoneImpl** lua_as = (simgrid::kernel::routing::NetZoneImpl**)lua_newuserdata(
484 L, sizeof(simgrid::kernel::routing::NetZoneImpl*)); /* table userdatum */
486 luaL_getmetatable(L, PLATF_MODULE_NAME); /* table userdatum metatable */
487 lua_setmetatable(L, -2); /* table userdatum */
488 lua_setfield(L, -2, AS_FIELDNAME); /* table -- put the userdata as field of the table */
492 int console_AS_seal(lua_State *L) {
493 XBT_DEBUG("Sealing AS");
494 sg_platf_new_Zone_seal();
498 int console_host_set_property(lua_State *L) {
499 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
502 lua_pushstring(L, "host");
504 const char* name = lua_tostring(L, -1);
508 lua_pushstring(L, "prop");
510 const char* prop_id = lua_tostring(L, -1);
513 lua_pushstring(L,"value");
515 const char* prop_value = lua_tostring(L, -1);
518 sg_host_t host = sg_host_by_name(name);
519 lua_ensure(host, "no host '%s' found",name);
520 host->set_property(prop_id, prop_value);
526 * @brief Registers the platform functions into the table simgrid.platf.
527 * @param L a lua state
529 void sglua_register_platf_functions(lua_State* L)
531 lua_getglobal(L, "simgrid"); /* simgrid */
532 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
533 lua_setfield(L, -2, "engine"); /* simgrid */
535 lua_pop(L, 1); /* -- */