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/surf/xml/platf_private.hpp"
11 #include "src/surf/network_interface.hpp"
12 #include "surf/surf_routing.h"
20 #include <simgrid/host.h>
21 #include "src/surf/surf_private.h"
23 XBT_LOG_NEW_DEFAULT_CATEGORY(lua_platf, "Lua bindings (platform module)");
25 #define PLATF_MODULE_NAME "simgrid.engine"
26 #define AS_FIELDNAME "__simgrid_as"
28 /* ********************************************************************************* */
29 /* simgrid.platf API */
30 /* ********************************************************************************* */
32 static const luaL_Reg platf_functions[] = {
33 {"open", console_open},
34 {"close", console_close},
35 {"AS_open", console_AS_open},
36 {"AS_seal", console_AS_seal},
37 {"backbone_new", console_add_backbone},
38 {"host_link_new", console_add_host___link},
39 {"host_new", console_add_host},
40 {"link_new", console_add_link},
41 {"router_new", console_add_router},
42 {"route_new", console_add_route},
43 {"ASroute_new", console_add_ASroute},
47 int console_open(lua_State *L) {
51 storage_register_callbacks();
52 routing_register_callbacks();
57 int console_close(lua_State *L) {
63 int console_add_backbone(lua_State *L) {
64 s_sg_platf_link_cbarg_t link;
65 memset(&link,0,sizeof(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 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);
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);
92 lua_pushstring(L, "sharing_policy");
93 type = lua_gettable(L, -2);
94 const char* policy = lua_tostring(L, -1);
95 if (policy && !strcmp(policy,"FULLDUPLEX")) {
96 link.policy = SURF_LINK_FULLDUPLEX;
97 } else if (policy && !strcmp(policy,"FATPIPE")) {
98 link.policy = SURF_LINK_FATPIPE;
100 link.policy = SURF_LINK_SHARED;
103 sg_platf_new_link(&link);
104 routing_cluster_add_backbone(sg_link_by_name(link.id));
109 int console_add_host___link(lua_State *L) {
110 s_sg_platf_host_link_cbarg_t hostlink;
111 memset(&hostlink,0,sizeof(hostlink));
114 lua_ensure(lua_istable(L, -1),
115 "Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
117 lua_pushstring(L, "id");
118 type = lua_gettable(L, -2);
119 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any host_link and must be a string.");
120 hostlink.id = lua_tostring(L, -1);
123 lua_pushstring(L, "up");
124 type = lua_gettable(L, -2);
125 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
126 "Attribute 'up' must be specified for host_link and must either be a string or a number.");
127 hostlink.link_up = lua_tostring(L, -1);
130 lua_pushstring(L, "down");
131 type = lua_gettable(L, -2);
132 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
133 "Attribute 'down' must be specified for host_link and must either be a string or a number.");
134 hostlink.link_down = lua_tostring(L, -1);
137 XBT_DEBUG("Create a host_link for host %s", hostlink.id);
138 sg_platf_new_hostlink(&hostlink);
143 int console_add_host(lua_State *L) {
144 s_sg_platf_host_cbarg_t host;
145 memset(&host,0,sizeof(host));
148 // we get values from the table passed as argument
149 lua_ensure(lua_istable(L, -1),
150 "Bad Arguments to create host. Should be a table with named arguments");
153 lua_pushstring(L, "id");
154 type = lua_gettable(L, -2);
155 lua_ensure(type == LUA_TSTRING,
156 "Attribute 'id' must be specified for any host and must be a string.");
157 host.id = lua_tostring(L, -1);
161 lua_pushstring(L, "speed");
162 type = lua_gettable(L, -2);
163 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
164 "Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
165 if (type == LUA_TNUMBER)
166 host.speed_per_pstate.push_back(lua_tointeger(L, -1));
168 host.speed_per_pstate.push_back(surf_parse_get_speed(lua_tostring(L, -1), "speed of host", host.id));
172 lua_pushstring(L, "core");
174 if(!lua_isnumber(L,-1))
175 host.core_amount = 1;// Default value
177 host.core_amount = lua_tonumber(L, -1);
178 if (host.core_amount == 0)
179 host.core_amount = 1;
183 lua_pushstring(L, "availability_file");
185 const char *filename = lua_tostring(L, -1);
187 host.speed_trace = tmgr_trace_new_from_file(filename);
191 lua_pushstring(L, "state_file");
193 filename = lua_tostring(L, -1);
195 host.state_trace = tmgr_trace_new_from_file(filename);
198 sg_platf_new_host(&host);
203 int console_add_link(lua_State *L) {
204 s_sg_platf_link_cbarg_t link;
205 memset(&link,0,sizeof(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 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);
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);
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 s_sg_platf_router_cbarg_t router;
290 memset(&router,0,sizeof(router));
293 lua_ensure(lua_istable(L, -1),
294 "Bad Arguments to create router, Should be a table with named arguments");
296 lua_pushstring(L, "id");
297 type = lua_gettable(L, -2);
298 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any link and must be a string.");
299 router.id = lua_tostring(L, -1);
302 lua_pushstring(L,"coord");
304 router.coord = lua_tostring(L, -1);
307 sg_platf_new_router(&router);
312 int console_add_route(lua_State *L) {
313 XBT_DEBUG("Adding route");
314 s_sg_platf_route_cbarg_t route;
315 memset(&route,0,sizeof(route));
318 /* allocating memory for the buffer, I think 2kB should be enough */
319 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
321 lua_ensure(lua_istable(L, -1), "Bad Arguments to add a route. Should be a table with named arguments");
323 lua_pushstring(L,"src");
324 type = lua_gettable(L,-2);
325 lua_ensure(type == LUA_TSTRING, "Attribute 'src' must be specified for any route and must be a string.");
326 const char *srcName = lua_tostring(L, -1);
327 route.src = sg_netcard_by_name_or_null(srcName);
328 lua_ensure(route.src != nullptr, "Attribute 'src=%s' of route does not name a node.", srcName);
331 lua_pushstring(L,"dest");
332 type = lua_gettable(L,-2);
333 lua_ensure(type == LUA_TSTRING, "Attribute 'dest' must be specified for any route and must be a string.");
334 const char *dstName = lua_tostring(L, -1);
335 route.dst = sg_netcard_by_name_or_null(dstName);
336 lua_ensure(route.dst != nullptr, "Attribute 'dst=%s' of route does not name a node.", dstName);
339 lua_pushstring(L,"links");
340 type = lua_gettable(L,-2);
341 lua_ensure(type == LUA_TSTRING,
342 "Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
343 route.link_list = new std::vector<Link*>();
344 xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
345 if (xbt_dynar_is_empty(names)) {
347 route.link_list->push_back(Link::byName(lua_tostring(L, -1)));
349 // Several names separated by , \t\r\n
352 xbt_dynar_foreach(names, cpt, name) {
353 if (strlen(name)>0) {
354 Link *link = Link::byName(name);
355 route.link_list->push_back(link);
361 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
362 * Et ouais mon pote. That's the way it goes. F34R.
364 * (Note that above this function, there is a #include statement. Is this
365 * comment related to that statement?)
367 lua_pushstring(L,"symmetrical");
369 if (lua_isstring(L, -1)) {
370 const char* value = lua_tostring(L, -1);
371 if (strcmp("YES", value) == 0)
372 route.symmetrical = true;
374 route.symmetrical = false;
377 route.symmetrical = true;
381 route.gw_src = nullptr;
382 route.gw_dst = nullptr;
384 sg_platf_new_route(&route);
389 int console_add_ASroute(lua_State *L) {
390 s_sg_platf_route_cbarg_t ASroute;
391 memset(&ASroute,0,sizeof(ASroute));
393 lua_pushstring(L, "src");
395 const char *srcName = lua_tostring(L, -1);
396 ASroute.src = sg_netcard_by_name_or_null(srcName);
397 lua_ensure(ASroute.src != nullptr, "Attribute 'src=%s' of AS route does not name a node.", srcName);
400 lua_pushstring(L, "dst");
402 const char *dstName = lua_tostring(L, -1);
403 ASroute.dst = sg_netcard_by_name_or_null(dstName);
404 lua_ensure(ASroute.dst != nullptr, "Attribute 'dst=%s' of AS route does not name a node.", dstName);
407 lua_pushstring(L, "gw_src");
409 const char *name = lua_tostring(L, -1);
410 ASroute.gw_src = sg_netcard_by_name_or_null(name);
411 lua_ensure(ASroute.gw_src, "Attribute 'gw_src=%s' of AS route does not name a valid node", name);
414 lua_pushstring(L, "gw_dst");
416 name = lua_tostring(L, -1);
417 ASroute.gw_dst = sg_netcard_by_name_or_null(name);
418 lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst=%s' of AS route does not name a valid node", name);
421 lua_pushstring(L,"links");
423 ASroute.link_list = new std::vector<Link*>();
424 xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
425 if (xbt_dynar_is_empty(names)) {
426 /* unique name with no comma */
427 ASroute.link_list->push_back(Link::byName(lua_tostring(L, -1)));
429 // Several names separated by , \t\r\n
432 xbt_dynar_foreach(names, cpt, name) {
433 if (strlen(name)>0) {
434 Link *link = Link::byName(name);
435 ASroute.link_list->push_back(link);
441 lua_pushstring(L,"symmetrical");
443 if (lua_isstring(L, -1)) {
444 const char* value = lua_tostring(L, -1);
445 if (strcmp("YES", value) == 0)
446 ASroute.symmetrical = true;
448 ASroute.symmetrical = false;
451 ASroute.symmetrical = true;
455 sg_platf_new_route(&ASroute);
460 int console_AS_open(lua_State *L) {
465 XBT_DEBUG("Opening AS");
467 lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
469 lua_pushstring(L, "id");
470 type = lua_gettable(L, -2);
471 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
472 id = lua_tostring(L, -1);
475 lua_pushstring(L, "mode");
477 mode = lua_tostring(L, -1);
480 int mode_int = A_surfxml_AS_routing_None;
481 if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
482 else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
483 else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
484 else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
485 else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
486 else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
487 else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
488 else xbt_die("Don't have the model name '%s'",mode);
490 s_sg_platf_AS_cbarg_t AS;
492 AS.routing = mode_int;
493 simgrid::s4u::As *new_as = sg_platf_new_AS_begin(&AS);
495 /* Build a Lua representation of the new AS on the stack */
497 simgrid::s4u::As **lua_as = (simgrid::s4u::As **) lua_newuserdata(L, sizeof(simgrid::s4u::As *)); /* table userdatum */
499 luaL_getmetatable(L, PLATF_MODULE_NAME); /* table userdatum metatable */
500 lua_setmetatable(L, -2); /* table userdatum */
501 lua_setfield(L, -2, AS_FIELDNAME); /* table -- put the userdata as field of the table */
505 int console_AS_seal(lua_State *L) {
506 XBT_DEBUG("Sealing AS");
507 sg_platf_new_AS_seal();
511 int console_host_set_property(lua_State *L) {
512 const char* name ="";
513 const char* prop_id = "";
514 const char* prop_value = "";
515 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
518 lua_pushstring(L, "host");
520 name = lua_tostring(L, -1);
524 lua_pushstring(L, "prop");
526 prop_id = lua_tostring(L, -1);
529 lua_pushstring(L,"value");
531 prop_value = lua_tostring(L,-1);
534 sg_host_t host = sg_host_by_name(name);
535 lua_ensure(host, "no host '%s' found",name);
536 xbt_dict_t props = sg_host_get_properties(host);
537 xbt_dict_set(props,prop_id,xbt_strdup(prop_value),nullptr);
543 * \brief Registers the platform functions into the table simgrid.platf.
544 * \param L a lua state
546 void sglua_register_platf_functions(lua_State* L)
548 lua_getglobal(L, "simgrid"); /* simgrid */
549 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
550 lua_setfield(L, -2, "engine"); /* simgrid */
552 lua_pop(L, 1); /* -- */