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.platf"
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 = NULL;
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 host.speed_per_pstate = xbt_dynar_new(sizeof(double), NULL);
166 if (type == LUA_TNUMBER)
167 xbt_dynar_push_as(host.speed_per_pstate, double, lua_tointeger(L, -1));
169 xbt_dynar_push_as(host.speed_per_pstate, double, surf_parse_get_speed(lua_tostring(L, -1), "speed of host", host.id));
173 lua_pushstring(L, "core");
175 if(!lua_isnumber(L,-1))
176 host.core_amount = 1;// Default value
178 host.core_amount = lua_tonumber(L, -1);
179 if (host.core_amount == 0)
180 host.core_amount = 1;
184 lua_pushstring(L, "availability_file");
186 const char *filename = lua_tostring(L, -1);
188 host.speed_trace = tmgr_trace_new_from_file(filename);
192 lua_pushstring(L, "state_file");
194 filename = lua_tostring(L, -1);
196 host.state_trace = tmgr_trace_new_from_file(filename);
199 sg_platf_new_host(&host);
200 xbt_dynar_free(&host.speed_per_pstate);
205 int console_add_link(lua_State *L) {
206 s_sg_platf_link_cbarg_t link;
207 memset(&link,0,sizeof(link));
212 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
215 lua_pushstring(L, "id");
216 type = lua_gettable(L, -2);
217 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
218 "Attribute 'id' must be specified for any link and must be a string.");
219 link.id = lua_tostring(L, -1);
222 // get bandwidth value
223 lua_pushstring(L, "bandwidth");
224 type = lua_gettable(L, -2);
225 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
226 "Attribute 'bandwidth' must be specified for any link and must either be either a string (in the right format; see docs) or a number.");
227 if (type == LUA_TNUMBER)
228 link.bandwidth = lua_tonumber(L, -1);
230 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1),"bandwidth of link", link.id);
234 lua_pushstring(L, "lat");
235 type = lua_gettable(L, -2);
236 lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
237 "Attribute 'lat' must be specified for any link and must either be a string (in the right format; see docs) or a number.");
238 if (type == LUA_TNUMBER)
239 link.latency = lua_tonumber(L, -1);
241 link.latency = surf_parse_get_time(lua_tostring(L, -1),"latency of link", link.id);
244 /*Optional Arguments */
246 //get bandwidth_trace value
247 lua_pushstring(L, "bandwidth_file");
249 const char *filename = lua_tostring(L, -1);
251 link.bandwidth_trace = tmgr_trace_new_from_file(filename);
254 //get latency_trace value
255 lua_pushstring(L, "latency_file");
257 filename = lua_tostring(L, -1);
259 link.latency_trace = tmgr_trace_new_from_file(filename);
262 //get state_trace value
263 lua_pushstring(L, "state_file");
265 filename = lua_tostring(L, -1);
267 link.state_trace = tmgr_trace_new_from_file(filename);
271 lua_pushstring(L, "sharing_policy");
273 policy = lua_tostring(L, -1);
275 if (policy && !strcmp(policy,"FULLDUPLEX")) {
276 link.policy = SURF_LINK_FULLDUPLEX;
277 } else if (policy && !strcmp(policy,"FATPIPE")) {
278 link.policy = SURF_LINK_FATPIPE;
280 link.policy = SURF_LINK_SHARED;
283 sg_platf_new_link(&link);
288 * add Router to AS components
290 int console_add_router(lua_State* L) {
291 s_sg_platf_router_cbarg_t router;
292 memset(&router,0,sizeof(router));
295 lua_ensure(lua_istable(L, -1),
296 "Bad Arguments to create router, Should be a table with named arguments");
298 lua_pushstring(L, "id");
299 type = lua_gettable(L, -2);
300 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any link and must be a string.");
301 router.id = lua_tostring(L, -1);
304 lua_pushstring(L,"coord");
306 router.coord = lua_tostring(L, -1);
309 sg_platf_new_router(&router);
314 int console_add_route(lua_State *L) {
315 XBT_DEBUG("Adding route");
316 s_sg_platf_route_cbarg_t route;
317 memset(&route,0,sizeof(route));
320 /* allocating memory for the buffer, I think 2kB should be enough */
321 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
323 lua_ensure(lua_istable(L, -1), "Bad Arguments to add a route. Should be a table with named arguments");
325 lua_pushstring(L,"src");
326 type = lua_gettable(L,-2);
327 lua_ensure(type == LUA_TSTRING, "Attribute 'src' must be specified for any route and must be a string.");
328 route.src = lua_tostring(L, -1);
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 route.dst = lua_tostring(L, -1);
337 lua_pushstring(L,"links");
338 type = lua_gettable(L,-2);
339 lua_ensure(type == LUA_TSTRING,
340 "Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
341 route.link_list = new std::vector<Link*>();
342 xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
343 if (xbt_dynar_is_empty(names)) {
345 route.link_list->push_back(Link::byName(lua_tostring(L, -1)));
347 // Several names separated by , \t\r\n
350 xbt_dynar_foreach(names, cpt, name) {
351 if (strlen(name)>0) {
352 Link *link = Link::byName(name);
353 route.link_list->push_back(link);
359 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
360 * Et ouais mon pote. That's the way it goes. F34R.
362 * (Note that above this function, there is a #include statement. Is this
363 * comment related to that statement?)
365 lua_pushstring(L,"symmetrical");
367 if (lua_isstring(L, -1)) {
368 const char* value = lua_tostring(L, -1);
369 if (strcmp("YES", value) == 0)
370 route.symmetrical = true;
372 route.symmetrical = false;
375 route.symmetrical = true;
382 sg_platf_new_route(&route);
387 int console_add_ASroute(lua_State *L) {
388 s_sg_platf_route_cbarg_t ASroute;
389 memset(&ASroute,0,sizeof(ASroute));
391 lua_pushstring(L, "src");
393 ASroute.src = lua_tostring(L, -1);
396 lua_pushstring(L, "dst");
398 ASroute.dst = lua_tostring(L, -1);
401 lua_pushstring(L, "gw_src");
403 const char *name = lua_tostring(L, -1);
404 ASroute.gw_src = sg_netcard_by_name_or_null(name);
405 lua_ensure(ASroute.gw_src, "Attribute 'gw_src' of AS route does not name a valid machine: %s", name);
408 lua_pushstring(L, "gw_dst");
410 name = lua_tostring(L, -1);
411 ASroute.gw_dst = sg_netcard_by_name_or_null(name);
412 lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst' of AS route does not name a valid machine: %s", name);
415 lua_pushstring(L,"links");
417 ASroute.link_list = new std::vector<Link*>();
418 xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
419 if (xbt_dynar_is_empty(names)) {
420 /* unique name with no comma */
421 ASroute.link_list->push_back(Link::byName(lua_tostring(L, -1)));
423 // Several names separated by , \t\r\n
426 xbt_dynar_foreach(names, cpt, name) {
427 if (strlen(name)>0) {
428 Link *link = Link::byName(name);
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);
454 int console_AS_open(lua_State *L) {
459 XBT_DEBUG("Opening AS");
461 lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
463 lua_pushstring(L, "id");
464 type = lua_gettable(L, -2);
465 lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
466 id = lua_tostring(L, -1);
469 lua_pushstring(L, "mode");
471 mode = lua_tostring(L, -1);
474 int mode_int = A_surfxml_AS_routing_None;
475 if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
476 else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
477 else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
478 else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
479 else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
480 else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
481 else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
482 else xbt_die("Don't have the model name '%s'",mode);
484 s_sg_platf_AS_cbarg_t AS;
486 AS.routing = mode_int;
487 sg_platf_new_AS_begin(&AS);
491 int console_AS_seal(lua_State *L) {
492 XBT_DEBUG("Sealing AS");
493 sg_platf_new_AS_seal();
497 int console_host_set_property(lua_State *L) {
498 const char* name ="";
499 const char* prop_id = "";
500 const char* prop_value = "";
501 lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
504 lua_pushstring(L, "host");
506 name = lua_tostring(L, -1);
510 lua_pushstring(L, "prop");
512 prop_id = lua_tostring(L, -1);
515 lua_pushstring(L,"value");
517 prop_value = lua_tostring(L,-1);
520 sg_host_t host = sg_host_by_name(name);
521 lua_ensure(host, "no host '%s' found",name);
522 xbt_dict_t props = sg_host_get_properties(host);
523 xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
529 * \brief Registers the platform functions into the table simgrid.platf.
530 * \param L a lua state
532 void sglua_register_platf_functions(lua_State* L)
534 lua_getglobal(L, "simgrid"); /* simgrid */
535 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
536 lua_setfield(L, -2, "platf"); /* simgrid */
538 lua_pop(L, 1); /* -- */