1 /* Copyright (c) 2010, 2012-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 /* SimGrid Lua bindings */
9 #include "lua_private.h"
10 #include "src/kernel/routing/NetPoint.hpp"
11 #include "src/surf/network_interface.hpp"
12 #include "src/surf/xml/platf_private.hpp"
20 #include "src/surf/surf_private.h"
21 #include <boost/algorithm/string/classification.hpp>
22 #include <boost/algorithm/string/split.hpp>
23 #include <simgrid/host.h>
27 XBT_LOG_NEW_DEFAULT_CATEGORY(lua_platf, "Lua bindings (platform module)");
29 #define PLATF_MODULE_NAME "simgrid.engine"
30 #define 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 int console_open(lua_State *L) {
55 storage_register_callbacks();
60 int console_close(lua_State *L) {
66 int console_add_backbone(lua_State *L) {
67 LinkCreationArgs link;
69 link.properties = nullptr;
71 lua_ensure(lua_istable(L, -1),"Bad Arguments to create backbone in Lua. Should be a table with named arguments.");
73 lua_pushstring(L, "id");
74 int type = lua_gettable(L, -2);
75 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for backbone and must be a string.");
76 link.id = lua_tostring(L, -1);
79 lua_pushstring(L, "bandwidth");
80 type = lua_gettable(L, -2);
81 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
82 "Attribute 'bandwidth' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
83 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of backbone", link.id.c_str());
86 lua_pushstring(L, "lat");
87 type = lua_gettable(L, -2);
88 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
89 "Attribute 'lat' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
90 link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of backbone", link.id.c_str());
93 lua_pushstring(L, "sharing_policy");
94 type = lua_gettable(L, -2);
95 const char* policy = lua_tostring(L, -1);
96 if (policy && not strcmp(policy, "FULLDUPLEX")) {
97 link.policy = SURF_LINK_FULLDUPLEX;
98 } else if (policy && not strcmp(policy, "FATPIPE")) {
99 link.policy = SURF_LINK_FATPIPE;
101 link.policy = SURF_LINK_SHARED;
104 sg_platf_new_link(&link);
105 routing_cluster_add_backbone(simgrid::surf::LinkImpl::byName(link.id.c_str()));
110 int console_add_host___link(lua_State *L) {
111 s_sg_platf_host_link_cbarg_t hostlink;
112 memset(&hostlink,0,sizeof(hostlink));
115 lua_ensure(lua_istable(L, -1),
116 "Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
118 lua_pushstring(L, "id");
119 type = lua_gettable(L, -2);
120 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any host_link and must be a string.");
121 hostlink.id = lua_tostring(L, -1);
124 lua_pushstring(L, "up");
125 type = lua_gettable(L, -2);
126 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
127 "Attribute 'up' must be specified for host_link and must either be a string or a number.");
128 hostlink.link_up = lua_tostring(L, -1);
131 lua_pushstring(L, "down");
132 type = lua_gettable(L, -2);
133 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
134 "Attribute 'down' must be specified for host_link and must either be a string or a number.");
135 hostlink.link_down = lua_tostring(L, -1);
138 XBT_DEBUG("Create a host_link for host %s", hostlink.id);
139 sg_platf_new_hostlink(&hostlink);
144 int console_add_host(lua_State *L) {
145 s_sg_platf_host_cbarg_t host;
146 memset(&host,0,sizeof(host));
149 // we get values from the table passed as argument
150 lua_ensure(lua_istable(L, -1),
151 "Bad Arguments to create host. Should be a table with named arguments");
154 lua_pushstring(L, "id");
155 type = lua_gettable(L, -2);
156 lua_ensure(type == LUA_TSTRING,
157 "Attribute 'id' must be specified for any host and must be a string.");
158 host.id = lua_tostring(L, -1);
162 lua_pushstring(L, "speed");
163 type = lua_gettable(L, -2);
164 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
165 "Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
166 if (type == LUA_TNUMBER)
167 host.speed_per_pstate.push_back(lua_tointeger(L, -1));
169 host.speed_per_pstate.push_back(surf_parse_get_speed(lua_tostring(L, -1), "speed of host", host.id));
173 lua_pushstring(L, "core");
175 if (not lua_isnumber(L, -1))
176 host.core_amount = 1; // Default value
178 host.core_amount = lua_tonumber(L, -1);
179 if (host.core_amount == 0)
180 host.core_amount = 1;
184 lua_pushstring(L, "availability_file");
186 const char *filename = lua_tostring(L, -1);
188 host.speed_trace = tmgr_trace_new_from_file(filename);
192 lua_pushstring(L, "state_file");
194 filename = lua_tostring(L, -1);
196 host.state_trace = tmgr_trace_new_from_file(filename);
199 sg_platf_new_host(&host);
204 int console_add_link(lua_State *L) {
205 LinkCreationArgs link;
209 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
212 lua_pushstring(L, "id");
213 int type = lua_gettable(L, -2);
214 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
215 "Attribute 'id' must be specified for any link and must be a string.");
216 link.id = lua_tostring(L, -1);
219 // get bandwidth value
220 lua_pushstring(L, "bandwidth");
221 type = lua_gettable(L, -2);
222 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
223 "Attribute 'bandwidth' must be specified for any link and must either be either a string (in the right format; see docs) or a number.");
224 if (type == LUA_TNUMBER)
225 link.bandwidth = lua_tonumber(L, -1);
227 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of link", link.id.c_str());
231 lua_pushstring(L, "lat");
232 type = lua_gettable(L, -2);
233 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
234 "Attribute 'lat' must be specified for any link and must either be a string (in the right format; see docs) or a number.");
235 if (type == LUA_TNUMBER)
236 link.latency = lua_tonumber(L, -1);
238 link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of link", link.id.c_str());
241 /*Optional Arguments */
243 //get bandwidth_trace value
244 lua_pushstring(L, "bandwidth_file");
246 const char *filename = lua_tostring(L, -1);
248 link.bandwidth_trace = tmgr_trace_new_from_file(filename);
251 //get latency_trace value
252 lua_pushstring(L, "latency_file");
254 filename = lua_tostring(L, -1);
256 link.latency_trace = tmgr_trace_new_from_file(filename);
259 //get state_trace value
260 lua_pushstring(L, "state_file");
262 filename = lua_tostring(L, -1);
264 link.state_trace = tmgr_trace_new_from_file(filename);
268 lua_pushstring(L, "sharing_policy");
270 policy = lua_tostring(L, -1);
272 if (policy && not strcmp(policy, "FULLDUPLEX")) {
273 link.policy = SURF_LINK_FULLDUPLEX;
274 } else if (policy && not strcmp(policy, "FATPIPE")) {
275 link.policy = SURF_LINK_FATPIPE;
277 link.policy = SURF_LINK_SHARED;
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),
289 "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 s_sg_platf_route_cbarg_t route;
310 memset(&route,0,sizeof(route));
313 lua_ensure(lua_istable(L, -1), "Bad Arguments to add a route. Should be a table with named arguments");
315 lua_pushstring(L,"src");
316 type = lua_gettable(L,-2);
317 lua_ensure(type == LUA_TSTRING, "Attribute 'src' must be specified for any route and must be a string.");
318 const char *srcName = lua_tostring(L, -1);
319 route.src = sg_netpoint_by_name_or_null(srcName);
320 lua_ensure(route.src != nullptr, "Attribute 'src=%s' of route does not name a node.", srcName);
323 lua_pushstring(L,"dest");
324 type = lua_gettable(L,-2);
325 lua_ensure(type == LUA_TSTRING, "Attribute 'dest' must be specified for any route and must be a string.");
326 const char *dstName = lua_tostring(L, -1);
327 route.dst = sg_netpoint_by_name_or_null(dstName);
328 lua_ensure(route.dst != nullptr, "Attribute 'dst=%s' of route does not name a node.", dstName);
331 lua_pushstring(L,"links");
332 type = lua_gettable(L,-2);
333 lua_ensure(type == LUA_TSTRING,
334 "Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
335 route.link_list = new std::vector<simgrid::surf::LinkImpl*>();
336 std::vector<std::string> names;
337 const char* str = lua_tostring(L, -1);
338 boost::split(names, str, boost::is_any_of(", \t\r\n"));
341 route.link_list->push_back(simgrid::surf::LinkImpl::byName(lua_tostring(L, -1)));
343 // Several names separated by , \t\r\n
344 for (auto name : names) {
345 if (name.length() > 0) {
346 simgrid::surf::LinkImpl* link = simgrid::surf::LinkImpl::byName(name.c_str());
347 route.link_list->push_back(link);
353 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
354 * Et ouais mon pote. That's the way it goes. F34R.
356 * (Note that above this function, there is a #include statement. Is this
357 * comment related to that statement?)
359 lua_pushstring(L,"symmetrical");
361 if (lua_isstring(L, -1)) {
362 const char* value = lua_tostring(L, -1);
363 if (strcmp("YES", value) == 0)
364 route.symmetrical = true;
366 route.symmetrical = false;
369 route.symmetrical = true;
373 route.gw_src = nullptr;
374 route.gw_dst = nullptr;
376 sg_platf_new_route(&route);
377 delete route.link_list;
382 int console_add_ASroute(lua_State *L) {
383 s_sg_platf_route_cbarg_t ASroute;
384 memset(&ASroute,0,sizeof(ASroute));
386 lua_pushstring(L, "src");
388 const char *srcName = lua_tostring(L, -1);
389 ASroute.src = sg_netpoint_by_name_or_null(srcName);
390 lua_ensure(ASroute.src != nullptr, "Attribute 'src=%s' of AS route does not name a node.", srcName);
393 lua_pushstring(L, "dst");
395 const char *dstName = lua_tostring(L, -1);
396 ASroute.dst = sg_netpoint_by_name_or_null(dstName);
397 lua_ensure(ASroute.dst != nullptr, "Attribute 'dst=%s' of AS route does not name a node.", dstName);
400 lua_pushstring(L, "gw_src");
402 const char *name = lua_tostring(L, -1);
403 ASroute.gw_src = sg_netpoint_by_name_or_null(name);
404 lua_ensure(ASroute.gw_src, "Attribute 'gw_src=%s' of AS route does not name a valid node", name);
407 lua_pushstring(L, "gw_dst");
409 name = lua_tostring(L, -1);
410 ASroute.gw_dst = sg_netpoint_by_name_or_null(name);
411 lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst=%s' of AS route does not name a valid node", name);
414 lua_pushstring(L,"links");
416 ASroute.link_list = new std::vector<simgrid::surf::LinkImpl*>();
417 std::vector<std::string> names;
418 const char* str = lua_tostring(L, -1);
419 boost::split(names, str, boost::is_any_of(", \t\r\n"));
421 /* unique name with no comma */
422 ASroute.link_list->push_back(simgrid::surf::LinkImpl::byName(lua_tostring(L, -1)));
424 // Several names separated by , \t\r\n
425 for (auto name : names) {
426 if (name.length() > 0) {
427 simgrid::surf::LinkImpl* link = simgrid::surf::LinkImpl::byName(name.c_str());
428 ASroute.link_list->push_back(link);
434 lua_pushstring(L,"symmetrical");
436 if (lua_isstring(L, -1)) {
437 const char* value = lua_tostring(L, -1);
438 if (strcmp("YES", value) == 0)
439 ASroute.symmetrical = true;
441 ASroute.symmetrical = false;
444 ASroute.symmetrical = true;
448 sg_platf_new_route(&ASroute);
449 delete ASroute.link_list;
454 int console_AS_open(lua_State *L) {
459 XBT_DEBUG("Opening AS");
461 lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
463 lua_pushstring(L, "id");
464 type = lua_gettable(L, -2);
465 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
466 id = lua_tostring(L, -1);
469 lua_pushstring(L, "mode");
471 mode = lua_tostring(L, -1);
474 int mode_int = A_surfxml_AS_routing_None;
475 if (not strcmp(mode, "Full"))
476 mode_int = A_surfxml_AS_routing_Full;
477 else if (not strcmp(mode, "Floyd"))
478 mode_int = A_surfxml_AS_routing_Floyd;
479 else if (not strcmp(mode, "Dijkstra"))
480 mode_int = A_surfxml_AS_routing_Dijkstra;
481 else if (not strcmp(mode, "DijkstraCache"))
482 mode_int = A_surfxml_AS_routing_DijkstraCache;
483 else if (not strcmp(mode, "Vivaldi"))
484 mode_int = A_surfxml_AS_routing_Vivaldi;
485 else if (not strcmp(mode, "Cluster"))
486 mode_int = A_surfxml_AS_routing_Cluster;
487 else if (not strcmp(mode, "none"))
488 mode_int = A_surfxml_AS_routing_None;
489 else xbt_die("Don't have the model name '%s'",mode);
491 s_sg_platf_AS_cbarg_t AS;
493 AS.routing = mode_int;
494 simgrid::s4u::NetZone* new_as = sg_platf_new_AS_begin(&AS);
496 /* Build a Lua representation of the new AS on the stack */
498 simgrid::s4u::NetZone** lua_as =
499 (simgrid::s4u::NetZone**)lua_newuserdata(L, sizeof(simgrid::s4u::NetZone*)); /* table userdatum */
501 luaL_getmetatable(L, PLATF_MODULE_NAME); /* table userdatum metatable */
502 lua_setmetatable(L, -2); /* table userdatum */
503 lua_setfield(L, -2, AS_FIELDNAME); /* table -- put the userdata as field of the table */
507 int console_AS_seal(lua_State *L) {
508 XBT_DEBUG("Sealing AS");
509 sg_platf_new_AS_seal();
513 int console_host_set_property(lua_State *L) {
514 const char* name ="";
515 const char* prop_id = "";
516 const char* prop_value = "";
517 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
520 lua_pushstring(L, "host");
522 name = lua_tostring(L, -1);
526 lua_pushstring(L, "prop");
528 prop_id = lua_tostring(L, -1);
531 lua_pushstring(L,"value");
533 prop_value = lua_tostring(L,-1);
536 sg_host_t host = sg_host_by_name(name);
537 lua_ensure(host, "no host '%s' found",name);
538 xbt_dict_t props = sg_host_get_properties(host);
539 xbt_dict_set(props,prop_id,xbt_strdup(prop_value),nullptr);
545 * \brief Registers the platform functions into the table simgrid.platf.
546 * \param L a lua state
548 void sglua_register_platf_functions(lua_State* L)
550 lua_getglobal(L, "simgrid"); /* simgrid */
551 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
552 lua_setfield(L, -2, "engine"); /* simgrid */
554 lua_pop(L, 1); /* -- */