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/NetCard.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 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 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 /* allocating memory for the buffer, I think 2kB should be enough */
315 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
317 lua_ensure(lua_istable(L, -1), "Bad Arguments to add a route. Should be a table with named arguments");
319 lua_pushstring(L,"src");
320 type = lua_gettable(L,-2);
321 lua_ensure(type == LUA_TSTRING, "Attribute 'src' must be specified for any route and must be a string.");
322 const char *srcName = lua_tostring(L, -1);
323 route.src = sg_netcard_by_name_or_null(srcName);
324 lua_ensure(route.src != nullptr, "Attribute 'src=%s' of route does not name a node.", srcName);
327 lua_pushstring(L,"dest");
328 type = lua_gettable(L,-2);
329 lua_ensure(type == LUA_TSTRING, "Attribute 'dest' must be specified for any route and must be a string.");
330 const char *dstName = lua_tostring(L, -1);
331 route.dst = sg_netcard_by_name_or_null(dstName);
332 lua_ensure(route.dst != nullptr, "Attribute 'dst=%s' of route does not name a node.", dstName);
335 lua_pushstring(L,"links");
336 type = lua_gettable(L,-2);
337 lua_ensure(type == LUA_TSTRING,
338 "Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
339 route.link_list = new std::vector<Link*>();
340 xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
341 if (xbt_dynar_is_empty(names)) {
343 route.link_list->push_back(Link::byName(lua_tostring(L, -1)));
345 // Several names separated by , \t\r\n
348 xbt_dynar_foreach(names, cpt, name) {
349 if (strlen(name)>0) {
350 Link *link = Link::byName(name);
351 route.link_list->push_back(link);
357 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
358 * Et ouais mon pote. That's the way it goes. F34R.
360 * (Note that above this function, there is a #include statement. Is this
361 * comment related to that statement?)
363 lua_pushstring(L,"symmetrical");
365 if (lua_isstring(L, -1)) {
366 const char* value = lua_tostring(L, -1);
367 if (strcmp("YES", value) == 0)
368 route.symmetrical = true;
370 route.symmetrical = false;
373 route.symmetrical = true;
377 route.gw_src = nullptr;
378 route.gw_dst = nullptr;
380 sg_platf_new_route(&route);
385 int console_add_ASroute(lua_State *L) {
386 s_sg_platf_route_cbarg_t ASroute;
387 memset(&ASroute,0,sizeof(ASroute));
389 lua_pushstring(L, "src");
391 const char *srcName = lua_tostring(L, -1);
392 ASroute.src = sg_netcard_by_name_or_null(srcName);
393 lua_ensure(ASroute.src != nullptr, "Attribute 'src=%s' of AS route does not name a node.", srcName);
396 lua_pushstring(L, "dst");
398 const char *dstName = lua_tostring(L, -1);
399 ASroute.dst = sg_netcard_by_name_or_null(dstName);
400 lua_ensure(ASroute.dst != nullptr, "Attribute 'dst=%s' of AS route does not name a node.", dstName);
403 lua_pushstring(L, "gw_src");
405 const char *name = lua_tostring(L, -1);
406 ASroute.gw_src = sg_netcard_by_name_or_null(name);
407 lua_ensure(ASroute.gw_src, "Attribute 'gw_src=%s' of AS route does not name a valid node", name);
410 lua_pushstring(L, "gw_dst");
412 name = lua_tostring(L, -1);
413 ASroute.gw_dst = sg_netcard_by_name_or_null(name);
414 lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst=%s' of AS route does not name a valid node", name);
417 lua_pushstring(L,"links");
419 ASroute.link_list = new std::vector<Link*>();
420 xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
421 if (xbt_dynar_is_empty(names)) {
422 /* unique name with no comma */
423 ASroute.link_list->push_back(Link::byName(lua_tostring(L, -1)));
425 // Several names separated by , \t\r\n
428 xbt_dynar_foreach(names, cpt, name) {
429 if (strlen(name)>0) {
430 Link *link = Link::byName(name);
431 ASroute.link_list->push_back(link);
437 lua_pushstring(L,"symmetrical");
439 if (lua_isstring(L, -1)) {
440 const char* value = lua_tostring(L, -1);
441 if (strcmp("YES", value) == 0)
442 ASroute.symmetrical = true;
444 ASroute.symmetrical = false;
447 ASroute.symmetrical = true;
451 sg_platf_new_route(&ASroute);
456 int console_AS_open(lua_State *L) {
461 XBT_DEBUG("Opening AS");
463 lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
465 lua_pushstring(L, "id");
466 type = lua_gettable(L, -2);
467 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
468 id = lua_tostring(L, -1);
471 lua_pushstring(L, "mode");
473 mode = lua_tostring(L, -1);
476 int mode_int = A_surfxml_AS_routing_None;
477 if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
478 else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
479 else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
480 else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
481 else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
482 else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
483 else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
484 else xbt_die("Don't have the model name '%s'",mode);
486 s_sg_platf_AS_cbarg_t AS;
488 AS.routing = mode_int;
489 simgrid::s4u::NetZone* new_as = sg_platf_new_AS_begin(&AS);
491 /* Build a Lua representation of the new AS on the stack */
493 simgrid::s4u::NetZone** lua_as =
494 (simgrid::s4u::NetZone**)lua_newuserdata(L, sizeof(simgrid::s4u::NetZone*)); /* table userdatum */
496 luaL_getmetatable(L, PLATF_MODULE_NAME); /* table userdatum metatable */
497 lua_setmetatable(L, -2); /* table userdatum */
498 lua_setfield(L, -2, AS_FIELDNAME); /* table -- put the userdata as field of the table */
502 int console_AS_seal(lua_State *L) {
503 XBT_DEBUG("Sealing AS");
504 sg_platf_new_AS_seal();
508 int console_host_set_property(lua_State *L) {
509 const char* name ="";
510 const char* prop_id = "";
511 const char* prop_value = "";
512 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
515 lua_pushstring(L, "host");
517 name = lua_tostring(L, -1);
521 lua_pushstring(L, "prop");
523 prop_id = lua_tostring(L, -1);
526 lua_pushstring(L,"value");
528 prop_value = lua_tostring(L,-1);
531 sg_host_t host = sg_host_by_name(name);
532 lua_ensure(host, "no host '%s' found",name);
533 xbt_dict_t props = sg_host_get_properties(host);
534 xbt_dict_set(props,prop_id,xbt_strdup(prop_value),nullptr);
540 * \brief Registers the platform functions into the table simgrid.platf.
541 * \param L a lua state
543 void sglua_register_platf_functions(lua_State* L)
545 lua_getglobal(L, "simgrid"); /* simgrid */
546 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
547 lua_setfield(L, -2, "engine"); /* simgrid */
549 lua_pop(L, 1); /* -- */