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 static simgrid::s4u::Link::SharingPolicy link_policy_get_by_name(const char* policy)
50 if (policy && not strcmp(policy, "FULLDUPLEX")) {
51 XBT_WARN("Please update your platform to use SPLITDUPLEX instead of FULLDUPLEX");
52 return simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
53 } else if (policy && not strcmp(policy, "SPLITDUPLEX")) {
54 return simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
55 } else if (policy && not strcmp(policy, "FATPIPE")) {
56 return simgrid::s4u::Link::SharingPolicy::FATPIPE;
58 return simgrid::s4u::Link::SharingPolicy::SHARED;
62 int console_open(lua_State *L) {
64 simgrid::s4u::on_platform_creation();
69 int console_close(lua_State *L) {
70 simgrid::s4u::on_platform_created();
75 int console_add_backbone(lua_State *L) {
76 simgrid::kernel::routing::LinkCreationArgs link;
78 link.properties = nullptr;
80 lua_ensure(lua_istable(L, -1),"Bad Arguments to create backbone in Lua. Should be a table with named arguments.");
82 lua_pushstring(L, "id");
83 int type = lua_gettable(L, -2);
84 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for backbone and must be a string.");
85 link.id = lua_tostring(L, -1);
88 lua_pushstring(L, "bandwidth");
89 type = lua_gettable(L, -2);
90 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
91 "Attribute 'bandwidth' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
92 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of backbone", link.id.c_str());
95 lua_pushstring(L, "lat");
96 type = lua_gettable(L, -2);
97 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
98 "Attribute 'lat' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
99 link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of backbone", link.id.c_str());
102 lua_pushstring(L, "sharing_policy");
104 const char* policy = lua_tostring(L, -1);
106 link.policy = link_policy_get_by_name(policy);
108 sg_platf_new_link(&link);
109 routing_cluster_add_backbone(simgrid::kernel::resource::LinkImpl::byName(link.id));
114 int console_add_host___link(lua_State *L) {
115 simgrid::kernel::routing::HostLinkCreationArgs hostlink;
118 lua_ensure(lua_istable(L, -1), "Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
120 lua_pushstring(L, "id");
121 type = lua_gettable(L, -2);
122 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any host_link and must be a string.");
123 hostlink.id = lua_tostring(L, -1);
126 lua_pushstring(L, "up");
127 type = lua_gettable(L, -2);
128 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
129 "Attribute 'up' must be specified for host_link and must either be a string or a number.");
130 hostlink.link_up = lua_tostring(L, -1);
133 lua_pushstring(L, "down");
134 type = lua_gettable(L, -2);
135 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
136 "Attribute 'down' must be specified for host_link and must either be a string or a number.");
137 hostlink.link_down = lua_tostring(L, -1);
140 XBT_DEBUG("Create a host_link for host %s", hostlink.id.c_str());
141 sg_platf_new_hostlink(&hostlink);
146 int console_add_host(lua_State *L) {
147 simgrid::kernel::routing::HostCreationArgs host;
150 // we get values from the table passed as argument
151 lua_ensure(lua_istable(L, -1),
152 "Bad Arguments to create host. Should be a table with named arguments");
155 lua_pushstring(L, "id");
156 type = lua_gettable(L, -2);
157 lua_ensure(type == LUA_TSTRING,
158 "Attribute 'id' must be specified for any host and must be a string.");
159 host.id = lua_tostring(L, -1);
163 lua_pushstring(L, "speed");
164 type = lua_gettable(L, -2);
165 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
166 "Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
167 if (type == LUA_TNUMBER)
168 host.speed_per_pstate.push_back(lua_tointeger(L, -1));
170 host.speed_per_pstate.push_back(surf_parse_get_speed(lua_tostring(L, -1), "speed of host", host.id));
174 lua_pushstring(L, "core");
176 if (not lua_isnumber(L, -1))
177 host.core_amount = 1; // Default value
179 host.core_amount = lua_tonumber(L, -1);
180 if (host.core_amount == 0)
181 host.core_amount = 1;
185 lua_pushstring(L, "availability_file");
187 const char *filename = lua_tostring(L, -1);
189 host.speed_trace = tmgr_trace_new_from_file(filename);
193 lua_pushstring(L, "state_file");
195 filename = lua_tostring(L, -1);
197 host.state_trace = tmgr_trace_new_from_file(filename);
200 sg_platf_new_host(&host);
205 int console_add_link(lua_State *L) {
206 simgrid::kernel::routing::LinkCreationArgs link;
210 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
213 lua_pushstring(L, "id");
214 int type = lua_gettable(L, -2);
215 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
216 "Attribute 'id' must be specified for any link and must be a string.");
217 link.id = lua_tostring(L, -1);
220 // get bandwidth value
221 lua_pushstring(L, "bandwidth");
222 type = lua_gettable(L, -2);
223 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
224 "Attribute 'bandwidth' must be specified for any link and must either be either a string (in the right format; see docs) or a number.");
225 if (type == LUA_TNUMBER)
226 link.bandwidth = lua_tonumber(L, -1);
228 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of link", link.id.c_str());
232 lua_pushstring(L, "lat");
233 type = lua_gettable(L, -2);
234 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
235 "Attribute 'lat' must be specified for any link and must either be a string (in the right format; see docs) or a number.");
236 if (type == LUA_TNUMBER)
237 link.latency = lua_tonumber(L, -1);
239 link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of link", link.id.c_str());
242 /*Optional Arguments */
244 //get bandwidth_trace value
245 lua_pushstring(L, "bandwidth_file");
247 const char *filename = lua_tostring(L, -1);
249 link.bandwidth_trace = tmgr_trace_new_from_file(filename);
252 //get latency_trace value
253 lua_pushstring(L, "latency_file");
255 filename = lua_tostring(L, -1);
257 link.latency_trace = tmgr_trace_new_from_file(filename);
260 //get state_trace value
261 lua_pushstring(L, "state_file");
263 filename = lua_tostring(L, -1);
265 link.state_trace = tmgr_trace_new_from_file(filename);
269 lua_pushstring(L, "sharing_policy");
271 policy = lua_tostring(L, -1);
273 link.policy = link_policy_get_by_name(policy);
275 sg_platf_new_link(&link);
280 * add Router to AS components
282 int console_add_router(lua_State* L) {
283 lua_ensure(lua_istable(L, -1), "Bad Arguments to create router, Should be a table with named arguments");
285 lua_pushstring(L, "id");
286 int type = lua_gettable(L, -2);
287 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any link and must be a string.");
288 const char* name = lua_tostring(L, -1);
291 lua_pushstring(L,"coord");
293 const char* coords = lua_tostring(L, -1);
296 sg_platf_new_router(name, coords);
301 int console_add_route(lua_State *L) {
302 XBT_DEBUG("Adding route");
303 simgrid::kernel::routing::RouteCreationArgs route;
306 lua_ensure(lua_istable(L, -1), "Bad Arguments to add a route. Should be a table with named arguments");
308 lua_pushstring(L,"src");
309 type = lua_gettable(L,-2);
310 lua_ensure(type == LUA_TSTRING, "Attribute 'src' must be specified for any route and must be a string.");
311 const char *srcName = lua_tostring(L, -1);
312 route.src = sg_netpoint_by_name_or_null(srcName);
313 lua_ensure(route.src != nullptr, "Attribute 'src=%s' of route does not name a node.", srcName);
316 lua_pushstring(L,"dest");
317 type = lua_gettable(L,-2);
318 lua_ensure(type == LUA_TSTRING, "Attribute 'dest' must be specified for any route and must be a string.");
319 const char *dstName = lua_tostring(L, -1);
320 route.dst = sg_netpoint_by_name_or_null(dstName);
321 lua_ensure(route.dst != nullptr, "Attribute 'dst=%s' of route does not name a node.", dstName);
324 lua_pushstring(L,"links");
325 type = lua_gettable(L,-2);
326 lua_ensure(type == LUA_TSTRING,
327 "Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
328 std::vector<std::string> names;
329 const char* str = lua_tostring(L, -1);
330 boost::split(names, str, boost::is_any_of(", \t\r\n"));
333 route.link_list.push_back(simgrid::kernel::resource::LinkImpl::byName(lua_tostring(L, -1)));
335 // Several names separated by , \t\r\n
336 for (auto const& name : names) {
337 if (name.length() > 0) {
338 simgrid::kernel::resource::LinkImpl* link = simgrid::kernel::resource::LinkImpl::byName(name);
339 route.link_list.push_back(link);
345 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
346 * Et ouais mon pote. That's the way it goes. F34R.
348 * (Note that above this function, there is a #include statement. Is this
349 * comment related to that statement?)
351 lua_pushstring(L,"symmetrical");
353 if (lua_isstring(L, -1)) {
354 const char* value = lua_tostring(L, -1);
355 if (strcmp("YES", value) == 0)
356 route.symmetrical = true;
358 route.symmetrical = false;
361 route.symmetrical = true;
365 route.gw_src = nullptr;
366 route.gw_dst = nullptr;
368 sg_platf_new_route(&route);
373 int console_add_ASroute(lua_State *L) {
374 simgrid::kernel::routing::RouteCreationArgs ASroute;
376 lua_pushstring(L, "src");
378 const char *srcName = lua_tostring(L, -1);
379 ASroute.src = sg_netpoint_by_name_or_null(srcName);
380 lua_ensure(ASroute.src != nullptr, "Attribute 'src=%s' of AS route does not name a node.", srcName);
383 lua_pushstring(L, "dst");
385 const char *dstName = lua_tostring(L, -1);
386 ASroute.dst = sg_netpoint_by_name_or_null(dstName);
387 lua_ensure(ASroute.dst != nullptr, "Attribute 'dst=%s' of AS route does not name a node.", dstName);
390 lua_pushstring(L, "gw_src");
392 const char *name = lua_tostring(L, -1);
393 ASroute.gw_src = sg_netpoint_by_name_or_null(name);
394 lua_ensure(ASroute.gw_src, "Attribute 'gw_src=%s' of AS route does not name a valid node", name);
397 lua_pushstring(L, "gw_dst");
399 name = lua_tostring(L, -1);
400 ASroute.gw_dst = sg_netpoint_by_name_or_null(name);
401 lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst=%s' of AS route does not name a valid node", name);
404 lua_pushstring(L,"links");
406 std::vector<std::string> names;
407 const char* str = lua_tostring(L, -1);
408 boost::split(names, str, boost::is_any_of(", \t\r\n"));
410 /* unique name with no comma */
411 ASroute.link_list.push_back(simgrid::kernel::resource::LinkImpl::byName(lua_tostring(L, -1)));
413 // Several names separated by , \t\r\n
414 for (auto const& name : names) {
415 if (name.length() > 0) {
416 simgrid::kernel::resource::LinkImpl* link = simgrid::kernel::resource::LinkImpl::byName(name);
417 ASroute.link_list.push_back(link);
423 lua_pushstring(L,"symmetrical");
425 if (lua_isstring(L, -1)) {
426 const char* value = lua_tostring(L, -1);
427 if (strcmp("YES", value) == 0)
428 ASroute.symmetrical = true;
430 ASroute.symmetrical = false;
433 ASroute.symmetrical = true;
437 sg_platf_new_route(&ASroute);
442 int console_AS_open(lua_State *L) {
443 XBT_DEBUG("Opening AS");
445 lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
447 lua_pushstring(L, "id");
448 int type = lua_gettable(L, -2);
449 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
450 const char* id = lua_tostring(L, -1);
453 lua_pushstring(L, "mode");
455 const char* mode = lua_tostring(L, -1);
458 int mode_int = A_surfxml_AS_routing_None;
459 if (not strcmp(mode, "Full"))
460 mode_int = A_surfxml_AS_routing_Full;
461 else if (not strcmp(mode, "Floyd"))
462 mode_int = A_surfxml_AS_routing_Floyd;
463 else if (not strcmp(mode, "Dijkstra"))
464 mode_int = A_surfxml_AS_routing_Dijkstra;
465 else if (not strcmp(mode, "DijkstraCache"))
466 mode_int = A_surfxml_AS_routing_DijkstraCache;
467 else if (not strcmp(mode, "Vivaldi"))
468 mode_int = A_surfxml_AS_routing_Vivaldi;
469 else if (not strcmp(mode, "Cluster"))
470 mode_int = A_surfxml_AS_routing_Cluster;
471 else if (not strcmp(mode, "none"))
472 mode_int = A_surfxml_AS_routing_None;
473 else xbt_die("Don't have the model name '%s'",mode);
475 simgrid::kernel::routing::ZoneCreationArgs AS;
477 AS.routing = mode_int;
478 simgrid::s4u::NetZone* new_as = sg_platf_new_Zone_begin(&AS);
480 /* Build a Lua representation of the new AS on the stack */
482 simgrid::s4u::NetZone** lua_as =
483 (simgrid::s4u::NetZone**)lua_newuserdata(L, sizeof(simgrid::s4u::NetZone*)); /* table userdatum */
485 luaL_getmetatable(L, PLATF_MODULE_NAME); /* table userdatum metatable */
486 lua_setmetatable(L, -2); /* table userdatum */
487 lua_setfield(L, -2, AS_FIELDNAME); /* table -- put the userdata as field of the table */
491 int console_AS_seal(lua_State *L) {
492 XBT_DEBUG("Sealing AS");
493 sg_platf_new_Zone_seal();
497 int console_host_set_property(lua_State *L) {
498 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
501 lua_pushstring(L, "host");
503 const char* name = lua_tostring(L, -1);
507 lua_pushstring(L, "prop");
509 const char* prop_id = lua_tostring(L, -1);
512 lua_pushstring(L,"value");
514 const char* prop_value = lua_tostring(L, -1);
517 sg_host_t host = sg_host_by_name(name);
518 lua_ensure(host, "no host '%s' found",name);
519 host->setProperty(prop_id, prop_value);
525 * \brief Registers the platform functions into the table simgrid.platf.
526 * \param L a lua state
528 void sglua_register_platf_functions(lua_State* L)
530 lua_getglobal(L, "simgrid"); /* simgrid */
531 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
532 lua_setfield(L, -2, "engine"); /* simgrid */
534 lua_pop(L, 1); /* -- */