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"
13 #include "surf/surf_routing.h"
21 #include "src/surf/surf_private.h"
22 #include <boost/algorithm/string/classification.hpp>
23 #include <boost/algorithm/string/split.hpp>
24 #include <simgrid/host.h>
28 XBT_LOG_NEW_DEFAULT_CATEGORY(lua_platf, "Lua bindings (platform module)");
30 #define PLATF_MODULE_NAME "simgrid.engine"
31 #define AS_FIELDNAME "__simgrid_as"
33 /* ********************************************************************************* */
34 /* simgrid.platf API */
35 /* ********************************************************************************* */
37 static const luaL_Reg platf_functions[] = {
38 {"open", console_open},
39 {"close", console_close},
40 {"AS_open", console_AS_open},
41 {"AS_seal", console_AS_seal},
42 {"backbone_new", console_add_backbone},
43 {"host_link_new", console_add_host___link},
44 {"host_new", console_add_host},
45 {"link_new", console_add_link},
46 {"router_new", console_add_router},
47 {"route_new", console_add_route},
48 {"ASroute_new", console_add_ASroute},
52 int console_open(lua_State *L) {
56 storage_register_callbacks();
61 int console_close(lua_State *L) {
67 int console_add_backbone(lua_State *L) {
68 LinkCreationArgs link;
70 link.properties = nullptr;
72 lua_ensure(lua_istable(L, -1),"Bad Arguments to create backbone in Lua. Should be a table with named arguments.");
74 lua_pushstring(L, "id");
75 int type = lua_gettable(L, -2);
76 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for backbone and must be a string.");
77 link.id = lua_tostring(L, -1);
80 lua_pushstring(L, "bandwidth");
81 type = lua_gettable(L, -2);
82 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
83 "Attribute 'bandwidth' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
84 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of backbone", link.id.c_str());
87 lua_pushstring(L, "lat");
88 type = lua_gettable(L, -2);
89 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
90 "Attribute 'lat' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
91 link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of backbone", link.id.c_str());
94 lua_pushstring(L, "sharing_policy");
95 type = lua_gettable(L, -2);
96 const char* policy = lua_tostring(L, -1);
97 if (policy && !strcmp(policy,"FULLDUPLEX")) {
98 link.policy = SURF_LINK_FULLDUPLEX;
99 } else if (policy && !strcmp(policy,"FATPIPE")) {
100 link.policy = SURF_LINK_FATPIPE;
102 link.policy = SURF_LINK_SHARED;
105 sg_platf_new_link(&link);
106 routing_cluster_add_backbone(simgrid::surf::LinkImpl::byName(link.id.c_str()));
111 int console_add_host___link(lua_State *L) {
112 s_sg_platf_host_link_cbarg_t hostlink;
113 memset(&hostlink,0,sizeof(hostlink));
116 lua_ensure(lua_istable(L, -1),
117 "Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
119 lua_pushstring(L, "id");
120 type = lua_gettable(L, -2);
121 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any host_link and must be a string.");
122 hostlink.id = lua_tostring(L, -1);
125 lua_pushstring(L, "up");
126 type = lua_gettable(L, -2);
127 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
128 "Attribute 'up' must be specified for host_link and must either be a string or a number.");
129 hostlink.link_up = lua_tostring(L, -1);
132 lua_pushstring(L, "down");
133 type = lua_gettable(L, -2);
134 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
135 "Attribute 'down' must be specified for host_link and must either be a string or a number.");
136 hostlink.link_down = lua_tostring(L, -1);
139 XBT_DEBUG("Create a host_link for host %s", hostlink.id);
140 sg_platf_new_hostlink(&hostlink);
145 int console_add_host(lua_State *L) {
146 s_sg_platf_host_cbarg_t host;
147 memset(&host,0,sizeof(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(!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 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 if (policy && !strcmp(policy,"FULLDUPLEX")) {
274 link.policy = SURF_LINK_FULLDUPLEX;
275 } else if (policy && !strcmp(policy,"FATPIPE")) {
276 link.policy = SURF_LINK_FATPIPE;
278 link.policy = SURF_LINK_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),
290 "Bad Arguments to create router, Should be a table with named arguments");
292 lua_pushstring(L, "id");
293 int type = lua_gettable(L, -2);
294 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any link and must be a string.");
295 const char* name = lua_tostring(L, -1);
298 lua_pushstring(L,"coord");
300 const char* coords = lua_tostring(L, -1);
303 sg_platf_new_router(name, coords);
308 int console_add_route(lua_State *L) {
309 XBT_DEBUG("Adding route");
310 s_sg_platf_route_cbarg_t route;
311 memset(&route,0,sizeof(route));
314 lua_ensure(lua_istable(L, -1), "Bad Arguments to add a route. Should be a table with named arguments");
316 lua_pushstring(L,"src");
317 type = lua_gettable(L,-2);
318 lua_ensure(type == LUA_TSTRING, "Attribute 'src' must be specified for any route and must be a string.");
319 const char *srcName = lua_tostring(L, -1);
320 route.src = sg_netpoint_by_name_or_null(srcName);
321 lua_ensure(route.src != nullptr, "Attribute 'src=%s' of route does not name a node.", srcName);
324 lua_pushstring(L,"dest");
325 type = lua_gettable(L,-2);
326 lua_ensure(type == LUA_TSTRING, "Attribute 'dest' must be specified for any route and must be a string.");
327 const char *dstName = lua_tostring(L, -1);
328 route.dst = sg_netpoint_by_name_or_null(dstName);
329 lua_ensure(route.dst != nullptr, "Attribute 'dst=%s' of route does not name a node.", dstName);
332 lua_pushstring(L,"links");
333 type = lua_gettable(L,-2);
334 lua_ensure(type == LUA_TSTRING,
335 "Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
336 route.link_list = new std::vector<simgrid::surf::LinkImpl*>();
337 std::vector<std::string> names;
338 const char* str = lua_tostring(L, -1);
339 boost::split(names, str, boost::is_any_of(", \t\r\n"));
342 route.link_list->push_back(simgrid::surf::LinkImpl::byName(lua_tostring(L, -1)));
344 // Several names separated by , \t\r\n
345 for (auto name : names) {
346 if (name.length() > 0) {
347 simgrid::surf::LinkImpl* link = simgrid::surf::LinkImpl::byName(name.c_str());
348 route.link_list->push_back(link);
354 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
355 * Et ouais mon pote. That's the way it goes. F34R.
357 * (Note that above this function, there is a #include statement. Is this
358 * comment related to that statement?)
360 lua_pushstring(L,"symmetrical");
362 if (lua_isstring(L, -1)) {
363 const char* value = lua_tostring(L, -1);
364 if (strcmp("YES", value) == 0)
365 route.symmetrical = true;
367 route.symmetrical = false;
370 route.symmetrical = true;
374 route.gw_src = nullptr;
375 route.gw_dst = nullptr;
377 sg_platf_new_route(&route);
378 delete route.link_list;
383 int console_add_ASroute(lua_State *L) {
384 s_sg_platf_route_cbarg_t ASroute;
385 memset(&ASroute,0,sizeof(ASroute));
387 lua_pushstring(L, "src");
389 const char *srcName = lua_tostring(L, -1);
390 ASroute.src = sg_netpoint_by_name_or_null(srcName);
391 lua_ensure(ASroute.src != nullptr, "Attribute 'src=%s' of AS route does not name a node.", srcName);
394 lua_pushstring(L, "dst");
396 const char *dstName = lua_tostring(L, -1);
397 ASroute.dst = sg_netpoint_by_name_or_null(dstName);
398 lua_ensure(ASroute.dst != nullptr, "Attribute 'dst=%s' of AS route does not name a node.", dstName);
401 lua_pushstring(L, "gw_src");
403 const char *name = lua_tostring(L, -1);
404 ASroute.gw_src = sg_netpoint_by_name_or_null(name);
405 lua_ensure(ASroute.gw_src, "Attribute 'gw_src=%s' of AS route does not name a valid node", name);
408 lua_pushstring(L, "gw_dst");
410 name = lua_tostring(L, -1);
411 ASroute.gw_dst = sg_netpoint_by_name_or_null(name);
412 lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst=%s' of AS route does not name a valid node", name);
415 lua_pushstring(L,"links");
417 ASroute.link_list = new std::vector<simgrid::surf::LinkImpl*>();
418 std::vector<std::string> names;
419 const char* str = lua_tostring(L, -1);
420 boost::split(names, str, boost::is_any_of(", \t\r\n"));
422 /* unique name with no comma */
423 ASroute.link_list->push_back(simgrid::surf::LinkImpl::byName(lua_tostring(L, -1)));
425 // Several names separated by , \t\r\n
426 for (auto name : names) {
427 if (name.length() > 0) {
428 simgrid::surf::LinkImpl* link = simgrid::surf::LinkImpl::byName(name.c_str());
429 ASroute.link_list->push_back(link);
435 lua_pushstring(L,"symmetrical");
437 if (lua_isstring(L, -1)) {
438 const char* value = lua_tostring(L, -1);
439 if (strcmp("YES", value) == 0)
440 ASroute.symmetrical = true;
442 ASroute.symmetrical = false;
445 ASroute.symmetrical = true;
449 sg_platf_new_route(&ASroute);
450 delete ASroute.link_list;
455 int console_AS_open(lua_State *L) {
460 XBT_DEBUG("Opening AS");
462 lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
464 lua_pushstring(L, "id");
465 type = lua_gettable(L, -2);
466 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
467 id = lua_tostring(L, -1);
470 lua_pushstring(L, "mode");
472 mode = lua_tostring(L, -1);
475 int mode_int = A_surfxml_AS_routing_None;
476 if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
477 else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
478 else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
479 else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
480 else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
481 else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
482 else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
483 else xbt_die("Don't have the model name '%s'",mode);
485 s_sg_platf_AS_cbarg_t AS;
487 AS.routing = mode_int;
488 simgrid::s4u::NetZone* new_as = sg_platf_new_AS_begin(&AS);
490 /* Build a Lua representation of the new AS on the stack */
492 simgrid::s4u::NetZone** lua_as =
493 (simgrid::s4u::NetZone**)lua_newuserdata(L, sizeof(simgrid::s4u::NetZone*)); /* table userdatum */
495 luaL_getmetatable(L, PLATF_MODULE_NAME); /* table userdatum metatable */
496 lua_setmetatable(L, -2); /* table userdatum */
497 lua_setfield(L, -2, AS_FIELDNAME); /* table -- put the userdata as field of the table */
501 int console_AS_seal(lua_State *L) {
502 XBT_DEBUG("Sealing AS");
503 sg_platf_new_AS_seal();
507 int console_host_set_property(lua_State *L) {
508 const char* name ="";
509 const char* prop_id = "";
510 const char* prop_value = "";
511 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
514 lua_pushstring(L, "host");
516 name = lua_tostring(L, -1);
520 lua_pushstring(L, "prop");
522 prop_id = lua_tostring(L, -1);
525 lua_pushstring(L,"value");
527 prop_value = lua_tostring(L,-1);
530 sg_host_t host = sg_host_by_name(name);
531 lua_ensure(host, "no host '%s' found",name);
532 xbt_dict_t props = sg_host_get_properties(host);
533 xbt_dict_set(props,prop_id,xbt_strdup(prop_value),nullptr);
539 * \brief Registers the platform functions into the table simgrid.platf.
540 * \param L a lua state
542 void sglua_register_platf_functions(lua_State* L)
544 lua_getglobal(L, "simgrid"); /* simgrid */
545 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
546 lua_setfield(L, -2, "engine"); /* simgrid */
548 lua_pop(L, 1); /* -- */