1 /* Copyright (c) 2010-2017. 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 "src/kernel/routing/NetPoint.hpp"
10 #include "src/surf/network_interface.hpp"
11 #include "src/surf/xml/platf_private.hpp"
19 #include "src/surf/surf_private.hpp"
20 #include <boost/algorithm/string/classification.hpp>
21 #include <boost/algorithm/string/split.hpp>
22 #include <simgrid/s4u/Host.hpp>
26 XBT_LOG_NEW_DEFAULT_CATEGORY(lua_platf, "Lua bindings (platform module)");
28 #define PLATF_MODULE_NAME "simgrid.engine"
29 #define AS_FIELDNAME "__simgrid_as"
31 /* ********************************************************************************* */
32 /* simgrid.platf API */
33 /* ********************************************************************************* */
35 static const luaL_Reg platf_functions[] = {
36 {"open", console_open},
37 {"close", console_close},
38 {"AS_open", console_AS_open},
39 {"AS_seal", console_AS_seal},
40 {"backbone_new", console_add_backbone},
41 {"host_link_new", console_add_host___link},
42 {"host_new", console_add_host},
43 {"link_new", console_add_link},
44 {"router_new", console_add_router},
45 {"route_new", console_add_route},
46 {"ASroute_new", console_add_ASroute},
50 int console_open(lua_State *L) {
54 storage_register_callbacks();
59 int console_close(lua_State *L) {
65 int console_add_backbone(lua_State *L) {
66 LinkCreationArgs link;
68 link.properties = nullptr;
70 lua_ensure(lua_istable(L, -1),"Bad Arguments to create backbone in Lua. Should be a table with named arguments.");
72 lua_pushstring(L, "id");
73 int type = lua_gettable(L, -2);
74 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for backbone and must be a string.");
75 link.id = lua_tostring(L, -1);
78 lua_pushstring(L, "bandwidth");
79 type = lua_gettable(L, -2);
80 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
81 "Attribute 'bandwidth' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
82 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of backbone", link.id.c_str());
85 lua_pushstring(L, "lat");
86 type = lua_gettable(L, -2);
87 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
88 "Attribute 'lat' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
89 link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of backbone", link.id.c_str());
92 lua_pushstring(L, "sharing_policy");
94 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));
110 int console_add_host___link(lua_State *L) {
111 HostLinkCreationArgs hostlink;
114 lua_ensure(lua_istable(L, -1), "Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
116 lua_pushstring(L, "id");
117 type = lua_gettable(L, -2);
118 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any host_link and must be a string.");
119 hostlink.id = lua_tostring(L, -1);
122 lua_pushstring(L, "up");
123 type = lua_gettable(L, -2);
124 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
125 "Attribute 'up' must be specified for host_link and must either be a string or a number.");
126 hostlink.link_up = lua_tostring(L, -1);
129 lua_pushstring(L, "down");
130 type = lua_gettable(L, -2);
131 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
132 "Attribute 'down' must be specified for host_link and must either be a string or a number.");
133 hostlink.link_down = lua_tostring(L, -1);
136 XBT_DEBUG("Create a host_link for host %s", hostlink.id.c_str());
137 sg_platf_new_hostlink(&hostlink);
142 int console_add_host(lua_State *L) {
143 s_sg_platf_host_cbarg_t host;
144 memset(&host,0,sizeof(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 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 link.policy = SURF_LINK_FULLDUPLEX;
272 } else if (policy && not strcmp(policy, "FATPIPE")) {
273 link.policy = SURF_LINK_FATPIPE;
275 link.policy = SURF_LINK_SHARED;
278 sg_platf_new_link(&link);
283 * add Router to AS components
285 int console_add_router(lua_State* L) {
286 lua_ensure(lua_istable(L, -1), "Bad Arguments to create router, Should be a table with named arguments");
288 lua_pushstring(L, "id");
289 int type = lua_gettable(L, -2);
290 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any link and must be a string.");
291 const char* name = lua_tostring(L, -1);
294 lua_pushstring(L,"coord");
296 const char* coords = lua_tostring(L, -1);
299 sg_platf_new_router(name, coords);
304 int console_add_route(lua_State *L) {
305 XBT_DEBUG("Adding route");
306 s_sg_platf_route_cbarg_t route;
307 memset(&route,0,sizeof(route));
310 lua_ensure(lua_istable(L, -1), "Bad Arguments to add a route. Should be a table with named arguments");
312 lua_pushstring(L,"src");
313 type = lua_gettable(L,-2);
314 lua_ensure(type == LUA_TSTRING, "Attribute 'src' must be specified for any route and must be a string.");
315 const char *srcName = lua_tostring(L, -1);
316 route.src = sg_netpoint_by_name_or_null(srcName);
317 lua_ensure(route.src != nullptr, "Attribute 'src=%s' of route does not name a node.", srcName);
320 lua_pushstring(L,"dest");
321 type = lua_gettable(L,-2);
322 lua_ensure(type == LUA_TSTRING, "Attribute 'dest' must be specified for any route and must be a string.");
323 const char *dstName = lua_tostring(L, -1);
324 route.dst = sg_netpoint_by_name_or_null(dstName);
325 lua_ensure(route.dst != nullptr, "Attribute 'dst=%s' of route does not name a node.", dstName);
328 lua_pushstring(L,"links");
329 type = lua_gettable(L,-2);
330 lua_ensure(type == LUA_TSTRING,
331 "Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
332 route.link_list = new std::vector<simgrid::surf::LinkImpl*>();
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::surf::LinkImpl::byName(lua_tostring(L, -1)));
340 // Several names separated by , \t\r\n
341 for (auto const& name : names) {
342 if (name.length() > 0) {
343 simgrid::surf::LinkImpl* link = simgrid::surf::LinkImpl::byName(name);
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);
374 delete route.link_list;
379 int console_add_ASroute(lua_State *L) {
380 s_sg_platf_route_cbarg_t ASroute;
381 memset(&ASroute,0,sizeof(ASroute));
383 lua_pushstring(L, "src");
385 const char *srcName = lua_tostring(L, -1);
386 ASroute.src = sg_netpoint_by_name_or_null(srcName);
387 lua_ensure(ASroute.src != nullptr, "Attribute 'src=%s' of AS route does not name a node.", srcName);
390 lua_pushstring(L, "dst");
392 const char *dstName = lua_tostring(L, -1);
393 ASroute.dst = sg_netpoint_by_name_or_null(dstName);
394 lua_ensure(ASroute.dst != nullptr, "Attribute 'dst=%s' of AS route does not name a node.", dstName);
397 lua_pushstring(L, "gw_src");
399 const char *name = lua_tostring(L, -1);
400 ASroute.gw_src = sg_netpoint_by_name_or_null(name);
401 lua_ensure(ASroute.gw_src, "Attribute 'gw_src=%s' of AS route does not name a valid node", name);
404 lua_pushstring(L, "gw_dst");
406 name = lua_tostring(L, -1);
407 ASroute.gw_dst = sg_netpoint_by_name_or_null(name);
408 lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst=%s' of AS route does not name a valid node", name);
411 lua_pushstring(L,"links");
413 ASroute.link_list = new std::vector<simgrid::surf::LinkImpl*>();
414 std::vector<std::string> names;
415 const char* str = lua_tostring(L, -1);
416 boost::split(names, str, boost::is_any_of(", \t\r\n"));
418 /* unique name with no comma */
419 ASroute.link_list->push_back(simgrid::surf::LinkImpl::byName(lua_tostring(L, -1)));
421 // Several names separated by , \t\r\n
422 for (auto const& name : names) {
423 if (name.length() > 0) {
424 simgrid::surf::LinkImpl* link = simgrid::surf::LinkImpl::byName(name);
425 ASroute.link_list->push_back(link);
431 lua_pushstring(L,"symmetrical");
433 if (lua_isstring(L, -1)) {
434 const char* value = lua_tostring(L, -1);
435 if (strcmp("YES", value) == 0)
436 ASroute.symmetrical = true;
438 ASroute.symmetrical = false;
441 ASroute.symmetrical = true;
445 sg_platf_new_route(&ASroute);
446 delete ASroute.link_list;
451 int console_AS_open(lua_State *L) {
452 XBT_DEBUG("Opening AS");
454 lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
456 lua_pushstring(L, "id");
457 int type = lua_gettable(L, -2);
458 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
459 const char* id = lua_tostring(L, -1);
462 lua_pushstring(L, "mode");
464 const char* mode = lua_tostring(L, -1);
467 int mode_int = A_surfxml_AS_routing_None;
468 if (not strcmp(mode, "Full"))
469 mode_int = A_surfxml_AS_routing_Full;
470 else if (not strcmp(mode, "Floyd"))
471 mode_int = A_surfxml_AS_routing_Floyd;
472 else if (not strcmp(mode, "Dijkstra"))
473 mode_int = A_surfxml_AS_routing_Dijkstra;
474 else if (not strcmp(mode, "DijkstraCache"))
475 mode_int = A_surfxml_AS_routing_DijkstraCache;
476 else if (not strcmp(mode, "Vivaldi"))
477 mode_int = A_surfxml_AS_routing_Vivaldi;
478 else if (not strcmp(mode, "Cluster"))
479 mode_int = A_surfxml_AS_routing_Cluster;
480 else if (not strcmp(mode, "none"))
481 mode_int = A_surfxml_AS_routing_None;
482 else xbt_die("Don't have the model name '%s'",mode);
486 AS.routing = mode_int;
487 simgrid::s4u::NetZone* new_as = sg_platf_new_Zone_begin(&AS);
489 /* Build a Lua representation of the new AS on the stack */
491 simgrid::s4u::NetZone** lua_as =
492 (simgrid::s4u::NetZone**)lua_newuserdata(L, sizeof(simgrid::s4u::NetZone*)); /* table userdatum */
494 luaL_getmetatable(L, PLATF_MODULE_NAME); /* table userdatum metatable */
495 lua_setmetatable(L, -2); /* table userdatum */
496 lua_setfield(L, -2, AS_FIELDNAME); /* table -- put the userdata as field of the table */
500 int console_AS_seal(lua_State *L) {
501 XBT_DEBUG("Sealing AS");
502 sg_platf_new_Zone_seal();
506 int console_host_set_property(lua_State *L) {
507 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
510 lua_pushstring(L, "host");
512 const char* name = lua_tostring(L, -1);
516 lua_pushstring(L, "prop");
518 const char* prop_id = lua_tostring(L, -1);
521 lua_pushstring(L,"value");
523 const char* prop_value = lua_tostring(L, -1);
526 sg_host_t host = sg_host_by_name(name);
527 lua_ensure(host, "no host '%s' found",name);
528 host->setProperty(prop_id, prop_value);
534 * \brief Registers the platform functions into the table simgrid.platf.
535 * \param L a lua state
537 void sglua_register_platf_functions(lua_State* L)
539 lua_getglobal(L, "simgrid"); /* simgrid */
540 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
541 lua_setfield(L, -2, "engine"); /* simgrid */
543 lua_pop(L, 1); /* -- */