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 <simgrid/host.h>
22 #include "src/surf/surf_private.h"
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 int console_open(lua_State *L) {
52 storage_register_callbacks();
57 int console_close(lua_State *L) {
63 int console_add_backbone(lua_State *L) {
64 LinkCreationArgs link;
66 link.properties = nullptr;
68 lua_ensure(lua_istable(L, -1),"Bad Arguments to create backbone in Lua. Should be a table with named arguments.");
70 lua_pushstring(L, "id");
71 int type = lua_gettable(L, -2);
72 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for backbone and must be a string.");
73 link.id = lua_tostring(L, -1);
76 lua_pushstring(L, "bandwidth");
77 type = lua_gettable(L, -2);
78 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
79 "Attribute 'bandwidth' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
80 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of backbone", link.id.c_str());
83 lua_pushstring(L, "lat");
84 type = lua_gettable(L, -2);
85 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
86 "Attribute 'lat' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
87 link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of backbone", link.id.c_str());
90 lua_pushstring(L, "sharing_policy");
91 type = lua_gettable(L, -2);
92 const char* policy = lua_tostring(L, -1);
93 if (policy && !strcmp(policy,"FULLDUPLEX")) {
94 link.policy = SURF_LINK_FULLDUPLEX;
95 } else if (policy && !strcmp(policy,"FATPIPE")) {
96 link.policy = SURF_LINK_FATPIPE;
98 link.policy = SURF_LINK_SHARED;
101 sg_platf_new_link(&link);
102 routing_cluster_add_backbone(simgrid::surf::LinkImpl::byName(link.id.c_str()));
107 int console_add_host___link(lua_State *L) {
108 s_sg_platf_host_link_cbarg_t hostlink;
109 memset(&hostlink,0,sizeof(hostlink));
112 lua_ensure(lua_istable(L, -1),
113 "Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
115 lua_pushstring(L, "id");
116 type = lua_gettable(L, -2);
117 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any host_link and must be a string.");
118 hostlink.id = lua_tostring(L, -1);
121 lua_pushstring(L, "up");
122 type = lua_gettable(L, -2);
123 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
124 "Attribute 'up' must be specified for host_link and must either be a string or a number.");
125 hostlink.link_up = lua_tostring(L, -1);
128 lua_pushstring(L, "down");
129 type = lua_gettable(L, -2);
130 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
131 "Attribute 'down' must be specified for host_link and must either be a string or a number.");
132 hostlink.link_down = lua_tostring(L, -1);
135 XBT_DEBUG("Create a host_link for host %s", hostlink.id);
136 sg_platf_new_hostlink(&hostlink);
141 int console_add_host(lua_State *L) {
142 s_sg_platf_host_cbarg_t host;
143 memset(&host,0,sizeof(host));
146 // we get values from the table passed as argument
147 lua_ensure(lua_istable(L, -1),
148 "Bad Arguments to create host. Should be a table with named arguments");
151 lua_pushstring(L, "id");
152 type = lua_gettable(L, -2);
153 lua_ensure(type == LUA_TSTRING,
154 "Attribute 'id' must be specified for any host and must be a string.");
155 host.id = lua_tostring(L, -1);
159 lua_pushstring(L, "speed");
160 type = lua_gettable(L, -2);
161 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
162 "Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
163 if (type == LUA_TNUMBER)
164 host.speed_per_pstate.push_back(lua_tointeger(L, -1));
166 host.speed_per_pstate.push_back(surf_parse_get_speed(lua_tostring(L, -1), "speed of host", host.id));
170 lua_pushstring(L, "core");
172 if(!lua_isnumber(L,-1))
173 host.core_amount = 1;// Default value
175 host.core_amount = lua_tonumber(L, -1);
176 if (host.core_amount == 0)
177 host.core_amount = 1;
181 lua_pushstring(L, "availability_file");
183 const char *filename = lua_tostring(L, -1);
185 host.speed_trace = tmgr_trace_new_from_file(filename);
189 lua_pushstring(L, "state_file");
191 filename = lua_tostring(L, -1);
193 host.state_trace = tmgr_trace_new_from_file(filename);
196 sg_platf_new_host(&host);
201 int console_add_link(lua_State *L) {
202 LinkCreationArgs link;
206 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
209 lua_pushstring(L, "id");
210 int type = lua_gettable(L, -2);
211 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
212 "Attribute 'id' must be specified for any link and must be a string.");
213 link.id = lua_tostring(L, -1);
216 // get bandwidth value
217 lua_pushstring(L, "bandwidth");
218 type = lua_gettable(L, -2);
219 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
220 "Attribute 'bandwidth' must be specified for any link and must either be either a string (in the right format; see docs) or a number.");
221 if (type == LUA_TNUMBER)
222 link.bandwidth = lua_tonumber(L, -1);
224 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of link", link.id.c_str());
228 lua_pushstring(L, "lat");
229 type = lua_gettable(L, -2);
230 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
231 "Attribute 'lat' must be specified for any link and must either be a string (in the right format; see docs) or a number.");
232 if (type == LUA_TNUMBER)
233 link.latency = lua_tonumber(L, -1);
235 link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of link", link.id.c_str());
238 /*Optional Arguments */
240 //get bandwidth_trace value
241 lua_pushstring(L, "bandwidth_file");
243 const char *filename = lua_tostring(L, -1);
245 link.bandwidth_trace = tmgr_trace_new_from_file(filename);
248 //get latency_trace value
249 lua_pushstring(L, "latency_file");
251 filename = lua_tostring(L, -1);
253 link.latency_trace = tmgr_trace_new_from_file(filename);
256 //get state_trace value
257 lua_pushstring(L, "state_file");
259 filename = lua_tostring(L, -1);
261 link.state_trace = tmgr_trace_new_from_file(filename);
265 lua_pushstring(L, "sharing_policy");
267 policy = lua_tostring(L, -1);
269 if (policy && !strcmp(policy,"FULLDUPLEX")) {
270 link.policy = SURF_LINK_FULLDUPLEX;
271 } else if (policy && !strcmp(policy,"FATPIPE")) {
272 link.policy = SURF_LINK_FATPIPE;
274 link.policy = SURF_LINK_SHARED;
277 sg_platf_new_link(&link);
282 * add Router to AS components
284 int console_add_router(lua_State* L) {
285 lua_ensure(lua_istable(L, -1),
286 "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 xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
334 if (xbt_dynar_is_empty(names)) {
336 route.link_list->push_back(simgrid::surf::LinkImpl::byName(lua_tostring(L, -1)));
338 // Several names separated by , \t\r\n
341 xbt_dynar_foreach(names, cpt, name) {
342 if (strlen(name)>0) {
343 simgrid::surf::LinkImpl* link = simgrid::surf::LinkImpl::byName(name);
344 route.link_list->push_back(link);
348 xbt_dynar_free(&names);
351 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
352 * Et ouais mon pote. That's the way it goes. F34R.
354 * (Note that above this function, there is a #include statement. Is this
355 * comment related to that statement?)
357 lua_pushstring(L,"symmetrical");
359 if (lua_isstring(L, -1)) {
360 const char* value = lua_tostring(L, -1);
361 if (strcmp("YES", value) == 0)
362 route.symmetrical = true;
364 route.symmetrical = false;
367 route.symmetrical = true;
371 route.gw_src = nullptr;
372 route.gw_dst = nullptr;
374 sg_platf_new_route(&route);
375 delete route.link_list;
380 int console_add_ASroute(lua_State *L) {
381 s_sg_platf_route_cbarg_t ASroute;
382 memset(&ASroute,0,sizeof(ASroute));
384 lua_pushstring(L, "src");
386 const char *srcName = lua_tostring(L, -1);
387 ASroute.src = sg_netpoint_by_name_or_null(srcName);
388 lua_ensure(ASroute.src != nullptr, "Attribute 'src=%s' of AS route does not name a node.", srcName);
391 lua_pushstring(L, "dst");
393 const char *dstName = lua_tostring(L, -1);
394 ASroute.dst = sg_netpoint_by_name_or_null(dstName);
395 lua_ensure(ASroute.dst != nullptr, "Attribute 'dst=%s' of AS route does not name a node.", dstName);
398 lua_pushstring(L, "gw_src");
400 const char *name = lua_tostring(L, -1);
401 ASroute.gw_src = sg_netpoint_by_name_or_null(name);
402 lua_ensure(ASroute.gw_src, "Attribute 'gw_src=%s' of AS route does not name a valid node", name);
405 lua_pushstring(L, "gw_dst");
407 name = lua_tostring(L, -1);
408 ASroute.gw_dst = sg_netpoint_by_name_or_null(name);
409 lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst=%s' of AS route does not name a valid node", name);
412 lua_pushstring(L,"links");
414 ASroute.link_list = new std::vector<simgrid::surf::LinkImpl*>();
415 xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
416 if (xbt_dynar_is_empty(names)) {
417 /* unique name with no comma */
418 ASroute.link_list->push_back(simgrid::surf::LinkImpl::byName(lua_tostring(L, -1)));
420 // Several names separated by , \t\r\n
423 xbt_dynar_foreach(names, cpt, name) {
424 if (strlen(name)>0) {
425 simgrid::surf::LinkImpl* link = simgrid::surf::LinkImpl::byName(name);
426 ASroute.link_list->push_back(link);
432 lua_pushstring(L,"symmetrical");
434 if (lua_isstring(L, -1)) {
435 const char* value = lua_tostring(L, -1);
436 if (strcmp("YES", value) == 0)
437 ASroute.symmetrical = true;
439 ASroute.symmetrical = false;
442 ASroute.symmetrical = true;
446 sg_platf_new_route(&ASroute);
447 delete ASroute.link_list;
452 int console_AS_open(lua_State *L) {
457 XBT_DEBUG("Opening AS");
459 lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
461 lua_pushstring(L, "id");
462 type = lua_gettable(L, -2);
463 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
464 id = lua_tostring(L, -1);
467 lua_pushstring(L, "mode");
469 mode = lua_tostring(L, -1);
472 int mode_int = A_surfxml_AS_routing_None;
473 if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
474 else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
475 else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
476 else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
477 else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
478 else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
479 else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
480 else xbt_die("Don't have the model name '%s'",mode);
482 s_sg_platf_AS_cbarg_t AS;
484 AS.routing = mode_int;
485 simgrid::s4u::NetZone* new_as = sg_platf_new_AS_begin(&AS);
487 /* Build a Lua representation of the new AS on the stack */
489 simgrid::s4u::NetZone** lua_as =
490 (simgrid::s4u::NetZone**)lua_newuserdata(L, sizeof(simgrid::s4u::NetZone*)); /* table userdatum */
492 luaL_getmetatable(L, PLATF_MODULE_NAME); /* table userdatum metatable */
493 lua_setmetatable(L, -2); /* table userdatum */
494 lua_setfield(L, -2, AS_FIELDNAME); /* table -- put the userdata as field of the table */
498 int console_AS_seal(lua_State *L) {
499 XBT_DEBUG("Sealing AS");
500 sg_platf_new_AS_seal();
504 int console_host_set_property(lua_State *L) {
505 const char* name ="";
506 const char* prop_id = "";
507 const char* prop_value = "";
508 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
511 lua_pushstring(L, "host");
513 name = lua_tostring(L, -1);
517 lua_pushstring(L, "prop");
519 prop_id = lua_tostring(L, -1);
522 lua_pushstring(L,"value");
524 prop_value = lua_tostring(L,-1);
527 sg_host_t host = sg_host_by_name(name);
528 lua_ensure(host, "no host '%s' found",name);
529 xbt_dict_t props = sg_host_get_properties(host);
530 xbt_dict_set(props,prop_id,xbt_strdup(prop_value),nullptr);
536 * \brief Registers the platform functions into the table simgrid.platf.
537 * \param L a lua state
539 void sglua_register_platf_functions(lua_State* L)
541 lua_getglobal(L, "simgrid"); /* simgrid */
542 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
543 lua_setfield(L, -2, "engine"); /* simgrid */
545 lua_pop(L, 1); /* -- */