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.hpp"
11 #include "surf/surf_routing.h"
19 #include <simgrid/host.h>
20 #include "src/surf/surf_private.h"
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_platf, bindings, "Lua bindings (platform module)");
24 #define PLATF_MODULE_NAME "simgrid.platf"
26 /* ********************************************************************************* */
27 /* simgrid.platf API */
28 /* ********************************************************************************* */
30 static const luaL_Reg platf_functions[] = {
31 {"open", console_open},
32 {"close", console_close},
33 {"AS_open", console_AS_open},
34 {"AS_close", console_AS_close},
35 {"backbone_new", console_add_backbone},
36 {"host_link_new", console_add_host___link},
37 {"host_new", console_add_host},
38 {"link_new", console_add_link},
39 {"router_new", console_add_router},
40 {"route_new", console_add_route},
41 {"ASroute_new", console_add_ASroute},
45 int console_open(lua_State *L) {
49 storage_register_callbacks();
50 routing_register_callbacks();
55 int console_close(lua_State *L) {
61 int console_add_backbone(lua_State *L) {
62 s_sg_platf_link_cbarg_t link;
63 memset(&link,0,sizeof(link));
66 link.properties = NULL;
68 if (!lua_istable(L, -1)) {
70 ("Bad Arguments to create backbone in Lua. Should be a table with named arguments.");
74 lua_pushstring(L, "id");
75 type = lua_gettable(L, -2);
76 if (type != LUA_TSTRING) {
77 XBT_ERROR("Attribute 'id' must be specified for backbone and must be a string.");
79 link.id = lua_tostring(L, -1);
82 lua_pushstring(L, "bandwidth");
83 type = lua_gettable(L, -2);
84 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
85 XBT_ERROR("Attribute 'bandwidth' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
87 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1),"bandwidth of backbone",link.id);
90 lua_pushstring(L, "lat");
91 type = lua_gettable(L, -2);
92 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
93 XBT_ERROR("Attribute 'lat' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
95 link.latency = surf_parse_get_time(lua_tostring(L, -1),"latency of backbone",link.id);
100 lua_pushstring(L, "sharing_policy");
101 type = lua_gettable(L, -2);
102 const char* policy = lua_tostring(L, -1);
103 if (policy && !strcmp(policy,"FULLDUPLEX")) {
104 link.policy = SURF_LINK_FULLDUPLEX;
105 } else if (policy && !strcmp(policy,"FATPIPE")) {
106 link.policy = SURF_LINK_FATPIPE;
108 link.policy = SURF_LINK_SHARED;
111 sg_platf_new_link(&link);
112 routing_cluster_add_backbone(sg_link_by_name(link.id));
117 int console_add_host___link(lua_State *L) {
118 s_sg_platf_host_link_cbarg_t netcard;
119 memset(&netcard,0,sizeof(netcard));
122 // we get values from the table passed as argument
123 if (!lua_istable(L, -1)) {
125 ("Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
129 lua_pushstring(L, "id");
130 type = lua_gettable(L, -2);
131 if (type != LUA_TSTRING) {
132 XBT_ERROR("Attribute 'id' must be specified for any host_link and must be a string.");
134 netcard.id = lua_tostring(L, -1);
137 lua_pushstring(L, "up");
138 type = lua_gettable(L, -2);
139 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
140 XBT_ERROR("Attribute 'up' must be specified for host_link and must either be a string or a number.");
142 netcard.link_up = lua_tostring(L, -1);
145 lua_pushstring(L, "down");
146 type = lua_gettable(L, -2);
147 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
148 XBT_ERROR("Attribute 'down' must be specified for host_link and must either be a string or a number.");
150 netcard.link_down = lua_tostring(L, -1);
153 XBT_DEBUG("Create a host_link for host %s", netcard.id);
154 sg_platf_new_hostlink(&netcard);
159 int console_add_host(lua_State *L) {
160 s_sg_platf_host_cbarg_t host;
161 memset(&host,0,sizeof(host));
164 // we get values from the table passed as argument
165 if (!lua_istable(L, -1)) {
167 ("Bad Arguments to create host. Should be a table with named arguments");
172 lua_pushstring(L, "id");
173 type = lua_gettable(L, -2);
174 if (type != LUA_TSTRING) {
175 XBT_ERROR("Attribute 'id' must be specified for any host and must be a string.");
177 host.id = lua_tostring(L, -1);
181 lua_pushstring(L, "speed");
182 type = lua_gettable(L, -2);
183 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
184 XBT_ERROR("Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
186 host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
187 if (type == LUA_TNUMBER)
188 xbt_dynar_push_as(host.speed_peak, double, lua_tointeger(L, -1));
190 xbt_dynar_push_as(host.speed_peak, double, surf_parse_get_speed(lua_tostring(L, -1), "speed of host", host.id));
194 lua_pushstring(L, "core");
196 if(!lua_isnumber(L,-1)) {
197 host.core_amount = 1;// Default value
199 else host.core_amount = lua_tonumber(L, -1);
200 if (host.core_amount == 0)
201 host.core_amount = 1;
205 lua_pushstring(L, "availability");
207 if(!lua_isnumber(L,-1)) host.speed_scale = 1;// Default value
208 else host.speed_scale = lua_tonumber(L, -1);
212 lua_pushstring(L, "availability_file");
214 host.speed_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
218 lua_pushstring(L, "state");
220 if(!lua_isnumber(L,-1)) state = 1;// Default value
221 else state = lua_tonumber(L, -1);
225 host.initiallyOn = 1;
227 host.initiallyOn = 0;
230 lua_pushstring(L, "state_file");
232 host.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
235 sg_platf_new_host(&host);
236 xbt_dynar_free(&host.speed_peak);
241 int console_add_link(lua_State *L) {
242 s_sg_platf_link_cbarg_t link;
243 memset(&link,0,sizeof(link));
248 if (! lua_istable(L, -1)) {
249 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
254 lua_pushstring(L, "id");
255 type = lua_gettable(L, -2);
256 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
257 XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
259 link.id = lua_tostring(L, -1);
262 // get bandwidth value
263 lua_pushstring(L, "bandwidth");
264 type = lua_gettable(L, -2);
265 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
266 XBT_ERROR("Attribute 'bandwidth' must be specified for any link and must either be either a string (in the right format; see docs) or a number.");
268 if (type == LUA_TNUMBER)
269 link.bandwidth = lua_tonumber(L, -1);
271 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1),"bandwidth of link", link.id);
275 lua_pushstring(L, "lat");
276 type = lua_gettable(L, -2);
277 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
278 XBT_ERROR("Attribute 'lat' must be specified for any link and must either be a string (in the right format; see docs) or a number.");
280 if (type == LUA_TNUMBER)
281 link.latency = lua_tonumber(L, -1);
283 link.latency = surf_parse_get_time(lua_tostring(L, -1),"latency of link", link.id);
286 /*Optional Arguments */
288 //get bandwidth_trace value
289 lua_pushstring(L, "bandwidth_file");
291 link.bandwidth_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
294 //get latency_trace value
295 lua_pushstring(L, "latency_file");
297 link.latency_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
300 //get state_trace value
301 lua_pushstring(L, "state_file");
303 link.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
306 //get state_initial value
307 lua_pushstring(L, "state");
309 if (!lua_isnumber(L,-1) || lua_tonumber(L, -1))
310 link.initiallyOn = 1;
312 link.initiallyOn = 0;
316 lua_pushstring(L, "sharing_policy");
318 policy = lua_tostring(L, -1);
320 if (policy && !strcmp(policy,"FULLDUPLEX")) {
321 link.policy = SURF_LINK_FULLDUPLEX;
322 } else if (policy && !strcmp(policy,"FATPIPE")) {
323 link.policy = SURF_LINK_FATPIPE;
325 link.policy = SURF_LINK_SHARED;
328 sg_platf_new_link(&link);
333 * add Router to AS components
335 int console_add_router(lua_State* L) {
336 s_sg_platf_router_cbarg_t router;
337 memset(&router,0,sizeof(router));
340 if (! lua_istable(L, -1)) {
341 XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
345 lua_pushstring(L, "id");
346 type = lua_gettable(L, -2);
347 if (type != LUA_TSTRING) {
348 XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
350 router.id = lua_tostring(L, -1);
353 lua_pushstring(L,"coord");
355 router.coord = lua_tostring(L, -1);
358 sg_platf_new_router(&router);
363 int console_add_route(lua_State *L) {
364 XBT_DEBUG("Adding route");
365 s_sg_platf_route_cbarg_t route;
366 memset(&route,0,sizeof(route));
369 /* allocating memory for the buffer, I think 2kB should be enough */
370 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
372 if (! lua_istable(L, -1)) {
373 XBT_ERROR("Bad Arguments to add a route. Should be a table with named arguments");
377 lua_pushstring(L,"src");
378 type = lua_gettable(L,-2);
379 if (type != LUA_TSTRING) {
380 XBT_ERROR("Attribute 'src' must be specified for any route and must be a string.");
382 route.src = lua_tostring(L, -1);
385 lua_pushstring(L,"dest");
386 type = lua_gettable(L,-2);
387 if (type != LUA_TSTRING) {
388 XBT_ERROR("Attribute 'dest' must be specified for any route and must be a string.");
390 route.dst = lua_tostring(L, -1);
393 lua_pushstring(L,"links");
394 type = lua_gettable(L,-2);
395 if (type != LUA_TSTRING) {
396 XBT_ERROR("Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
398 route.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
399 if (xbt_dynar_is_empty(route.link_list))
400 xbt_dynar_push_as(route.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
403 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
404 * Et ouais mon pote. That's the way it goes. F34R.
406 * (Note that above this function, there is a #include statement. Is this
407 * comment related to that statement?)
409 lua_pushstring(L,"symmetrical");
411 if (lua_isstring(L, -1)) {
412 const char* value = lua_tostring(L, -1);
413 if (strcmp("YES", value) == 0) {
414 route.symmetrical = TRUE;
417 route.symmetrical = FALSE;
420 route.symmetrical = TRUE;
427 sg_platf_new_route(&route);
432 int console_add_ASroute(lua_State *L) {
433 s_sg_platf_route_cbarg_t ASroute;
434 memset(&ASroute,0,sizeof(ASroute));
436 lua_pushstring(L, "src");
438 ASroute.src = lua_tostring(L, -1);
441 lua_pushstring(L, "dst");
443 ASroute.dst = lua_tostring(L, -1);
446 lua_pushstring(L, "gw_src");
448 const char *name = lua_tostring(L, -1);
449 ASroute.gw_src = sg_netcard_by_name_or_null(name);
450 if (ASroute.gw_src == NULL) {
451 XBT_ERROR("Attribute 'gw_src' of AS route does not name a valid machine: %s", name);
456 lua_pushstring(L, "gw_dst");
458 name = lua_tostring(L, -1);
459 ASroute.gw_dst = sg_netcard_by_name_or_null(name);
460 if (ASroute.gw_dst == NULL) {
461 XBT_ERROR("Attribute 'gw_dst' of AS route does not name a valid machine: %s", name);
466 lua_pushstring(L,"links");
468 ASroute.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
469 if (xbt_dynar_is_empty(ASroute.link_list))
470 xbt_dynar_push_as(ASroute.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
473 lua_pushstring(L,"symmetrical");
475 if (lua_isstring(L, -1)) {
476 const char* value = lua_tostring(L, -1);
477 if (strcmp("YES", value) == 0)
478 ASroute.symmetrical = TRUE;
480 ASroute.symmetrical = FALSE;
483 ASroute.symmetrical = TRUE;
487 sg_platf_new_route(&ASroute);
492 int console_AS_open(lua_State *L) {
497 XBT_DEBUG("Opening AS");
499 if (! lua_istable(L, 1)) {
500 XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
504 lua_pushstring(L, "id");
505 type = lua_gettable(L, -2);
506 if (type != LUA_TSTRING) {
507 XBT_ERROR("Attribute 'id' must be specified for any AS and must be a string.");
509 id = lua_tostring(L, -1);
512 lua_pushstring(L, "mode");
514 mode = lua_tostring(L, -1);
517 int mode_int = A_surfxml_AS_routing_None;
518 if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
519 else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
520 else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
521 else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
522 else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
523 else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
524 else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
525 else xbt_die("Don't have the model name '%s'",mode);
527 s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
529 AS.routing = mode_int;
531 sg_platf_new_AS_begin(&AS);
535 int console_AS_close(lua_State *L) {
536 XBT_DEBUG("Closing AS");
537 sg_platf_new_AS_end();
541 int console_host_set_property(lua_State *L) {
542 const char* name ="";
543 const char* prop_id = "";
544 const char* prop_value = "";
545 if (!lua_istable(L, -1)) {
546 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
552 lua_pushstring(L, "host");
554 name = lua_tostring(L, -1);
558 lua_pushstring(L, "prop");
560 prop_id = lua_tostring(L, -1);
563 lua_pushstring(L,"value");
565 prop_value = lua_tostring(L,-1);
568 sg_host_t host = sg_host_by_name(name);
570 XBT_ERROR("no host '%s' found",name);
573 xbt_dict_t props = sg_host_get_properties(host);
574 xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
580 * \brief Registers the platform functions into the table simgrid.platf.
581 * \param L a lua state
583 void sglua_register_platf_functions(lua_State* L)
585 lua_getglobal(L, "simgrid"); /* simgrid */
586 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
587 lua_setfield(L, -2, "platf"); /* simgrid */
589 lua_pop(L, 1); /* -- */
592 //void sglua_register_routing_constants(lua_State* L)
594 // lua_getglobal(L, "simgrid"); /* simgrid */
595 // lua_newtable(L); /* simgrid simgrid.routing */
597 // lua_pushstring(L, "Cluster"); /* simgrid simgrid.routing Cluster */
598 // lua_setfield(L, -2, "CLUSTER"); /* simgrid simgrid.routing */
600 // lua_pushstring(L, "Dijkstra"); /* simgrid simgrid.routing Dijkstra */
601 // lua_setfield(L, -2, "DIJKSTRA"); /* simgrid simgrid.routing */
603 // lua_pushstring(L, "DijkstraCache"); /* simgrid simgrid.routing DijkstraCache */
604 // lua_setfield(L, -2, "DIJKSTRA_CACHE"); /* simgrid simgrid.routing */
606 // lua_pushstring(L, "Floyd"); /* simgrid simgrid.routing Floyd */
607 // lua_setfield(L, -2, "FLOYD"); /* simgrid simgrid.routing */
609 // lua_pushstring(L, "Full"); /* simgrid simgrid.routing Full */
610 // lua_setfield(L, -2, "FULL"); /* simgrid simgrid.routing */
612 // lua_pushstring(L, "None"); /* simgrid simgrid.routing None */
613 // lua_setfield(L, -2, "NONE"); /* simgrid simgrid.routing */
615 // lua_pushstring(L, "Vivaldi"); /* simgrid simgrid.routing Vivaldi */
616 // lua_setfield(L, -2, "FULL"); /* simgrid simgrid.routing */
618 // lua_setfield(L, -2, "routing"); /* simgrid */
620 // lua_pop(L, 1); /* -- */