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 "simgrid/platf_interface.h"
11 #include "surf/surfxml_parse.h"
12 #include "surf/surf_routing.h"
17 #include <simgrid/host.h>
18 #include "src/surf/surf_private.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_platf, bindings, "Lua bindings (platform module)");
22 #define PLATF_MODULE_NAME "simgrid.platf"
24 /* ********************************************************************************* */
25 /* simgrid.platf API */
26 /* ********************************************************************************* */
28 static const luaL_Reg platf_functions[] = {
29 {"open", console_open},
30 {"close", console_close},
31 {"AS_open", console_AS_open},
32 {"AS_close", console_AS_close},
33 {"backbone_new", console_add_backbone},
34 {"host_link_new", console_add_host___link},
35 {"host_new", console_add_host},
36 {"link_new", console_add_link},
37 {"router_new", console_add_router},
38 {"route_new", console_add_route},
39 {"ASroute_new", console_add_ASroute},
43 int console_open(lua_State *L) {
47 storage_register_callbacks();
48 routing_register_callbacks();
53 int console_close(lua_State *L) {
59 int console_add_backbone(lua_State *L) {
60 s_sg_platf_link_cbarg_t link;
61 memset(&link,0,sizeof(link));
64 link.properties = NULL;
66 if (!lua_istable(L, -1)) {
68 ("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 if (type != LUA_TSTRING) {
75 XBT_ERROR("Attribute 'id' must be specified for backbone and must be a string.");
77 link.id = lua_tostring(L, -1);
80 lua_pushstring(L, "bandwidth");
81 type = lua_gettable(L, -2);
82 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
83 XBT_ERROR("Attribute 'bandwidth' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
85 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1));
88 lua_pushstring(L, "lat");
89 type = lua_gettable(L, -2);
90 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
91 XBT_ERROR("Attribute 'lat' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
93 link.latency = surf_parse_get_time(lua_tostring(L, -1));
98 lua_pushstring(L, "sharing_policy");
99 type = lua_gettable(L, -2);
100 const char* policy = lua_tostring(L, -1);
101 if (policy && !strcmp(policy,"FULLDUPLEX")) {
102 link.policy = SURF_LINK_FULLDUPLEX;
103 } else if (policy && !strcmp(policy,"FATPIPE")) {
104 link.policy = SURF_LINK_FATPIPE;
106 link.policy = SURF_LINK_SHARED;
109 sg_platf_new_link(&link);
110 routing_cluster_add_backbone(sg_link_by_name(link.id));
115 int console_add_host___link(lua_State *L) {
116 s_sg_platf_host_link_cbarg_t netcard;
117 memset(&netcard,0,sizeof(netcard));
120 // we get values from the table passed as argument
121 if (!lua_istable(L, -1)) {
123 ("Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
127 lua_pushstring(L, "id");
128 type = lua_gettable(L, -2);
129 if (type != LUA_TSTRING) {
130 XBT_ERROR("Attribute 'id' must be specified for any host_link and must be a string.");
132 netcard.id = lua_tostring(L, -1);
135 lua_pushstring(L, "up");
136 type = lua_gettable(L, -2);
137 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
138 XBT_ERROR("Attribute 'up' must be specified for host_link and must either be a string or a number.");
140 netcard.link_up = lua_tostring(L, -1);
143 lua_pushstring(L, "down");
144 type = lua_gettable(L, -2);
145 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
146 XBT_ERROR("Attribute 'down' must be specified for host_link and must either be a string or a number.");
148 netcard.link_down = lua_tostring(L, -1);
151 XBT_DEBUG("Create a host_link for host %s", netcard.id);
152 sg_platf_new_netcard(&netcard);
157 int console_add_host(lua_State *L) {
158 s_sg_platf_host_cbarg_t host;
159 memset(&host,0,sizeof(host));
162 // we get values from the table passed as argument
163 if (!lua_istable(L, -1)) {
165 ("Bad Arguments to create host. Should be a table with named arguments");
170 lua_pushstring(L, "id");
171 type = lua_gettable(L, -2);
172 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
173 XBT_ERROR("Attribute 'id' must be specified for any host and must be a string.");
175 host.id = lua_tostring(L, -1);
179 lua_pushstring(L, "speed");
180 type = lua_gettable(L, -2);
181 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
182 XBT_ERROR("Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
184 host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
185 xbt_dynar_push_as(host.speed_peak, double, parse_cpu_speed(lua_tostring(L, -1)));
189 lua_pushstring(L, "core");
191 if(!lua_isnumber(L,-1)) {
192 host.core_amount = 1;// Default value
194 else host.core_amount = lua_tonumber(L, -1);
195 if (host.core_amount == 0)
196 host.core_amount = 1;
200 lua_pushstring(L, "availability");
202 if(!lua_isnumber(L,-1)) host.speed_scale = 1;// Default value
203 else host.speed_scale = lua_tonumber(L, -1);
207 lua_pushstring(L, "availability_file");
209 host.speed_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
213 lua_pushstring(L, "state");
215 if(!lua_isnumber(L,-1)) state = 1;// Default value
216 else state = lua_tonumber(L, -1);
220 host.initiallyOn = 1;
222 host.initiallyOn = 0;
225 lua_pushstring(L, "state_file");
227 host.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
230 sg_platf_new_host(&host);
231 xbt_dynar_free(&host.speed_peak);
236 int console_add_link(lua_State *L) {
237 s_sg_platf_link_cbarg_t link;
238 memset(&link,0,sizeof(link));
243 if (! lua_istable(L, -1)) {
244 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
249 lua_pushstring(L, "id");
250 type = lua_gettable(L, -2);
251 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
252 XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
254 link.id = lua_tostring(L, -1);
257 // get bandwidth value
258 lua_pushstring(L, "bandwidth");
259 type = lua_gettable(L, -2);
260 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
261 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.");
263 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1));
267 lua_pushstring(L, "lat");
268 type = lua_gettable(L, -2);
269 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
270 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.");
272 link.latency = surf_parse_get_time(lua_tostring(L, -1));
275 /*Optional Arguments */
277 //get bandwidth_trace value
278 lua_pushstring(L, "bandwidth_file");
280 link.bandwidth_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
283 //get latency_trace value
284 lua_pushstring(L, "latency_file");
286 link.latency_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
289 //get state_trace value
290 lua_pushstring(L, "state_file");
292 link.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
295 //get state_initial value
296 lua_pushstring(L, "state");
298 if (!lua_isnumber(L,-1) || lua_tonumber(L, -1))
299 link.initiallyOn = 1;
301 link.initiallyOn = 0;
305 lua_pushstring(L, "sharing_policy");
307 policy = lua_tostring(L, -1);
309 if (policy && !strcmp(policy,"FULLDUPLEX")) {
310 link.policy = SURF_LINK_FULLDUPLEX;
311 } else if (policy && !strcmp(policy,"FATPIPE")) {
312 link.policy = SURF_LINK_FATPIPE;
314 link.policy = SURF_LINK_SHARED;
317 sg_platf_new_link(&link);
322 * add Router to AS components
324 int console_add_router(lua_State* L) {
325 s_sg_platf_router_cbarg_t router;
326 memset(&router,0,sizeof(router));
329 if (! lua_istable(L, -1)) {
330 XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
334 lua_pushstring(L, "id");
335 type = lua_gettable(L, -2);
336 if (type != LUA_TSTRING) {
337 XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
339 router.id = lua_tostring(L, -1);
342 lua_pushstring(L,"coord");
344 router.coord = lua_tostring(L, -1);
347 sg_platf_new_router(&router);
352 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
354 int console_add_route(lua_State *L) {
355 XBT_DEBUG("Adding route");
356 s_sg_platf_route_cbarg_t route;
357 memset(&route,0,sizeof(route));
360 /* allocating memory for the buffer, I think 2kB should be enough */
361 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
363 if (! lua_istable(L, -1)) {
364 XBT_ERROR("Bad Arguments to create a route. Should be a table with named arguments");
368 lua_pushstring(L,"src");
369 type = lua_gettable(L,-2);
370 if (type != LUA_TSTRING) {
371 XBT_ERROR("Attribute 'src' must be specified for any route and must be a string.");
373 route.src = lua_tostring(L, -1);
376 lua_pushstring(L,"dest");
377 type = lua_gettable(L,-2);
378 if (type != LUA_TSTRING) {
379 XBT_ERROR("Attribute 'dest' must be specified for any route and must be a string.");
381 route.dst = lua_tostring(L, -1);
384 lua_pushstring(L,"links");
385 type = lua_gettable(L,-2);
386 if (type != LUA_TSTRING) {
387 XBT_ERROR("Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
389 route.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
390 if (xbt_dynar_is_empty(route.link_list))
391 xbt_dynar_push_as(route.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
394 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
395 * Et ouais mon pote. That's the way it goes. F34R.
397 * (Note that above this function, there is a #include statement. Is this
398 * comment related to that statement?)
400 lua_pushstring(L,"symmetrical");
402 if (lua_isstring(L, -1)) {
403 const char* value = lua_tostring(L, -1);
404 if (strcmp("YES", value) == 0) {
405 route.symmetrical = TRUE;
408 route.symmetrical = FALSE;
411 route.symmetrical = TRUE;
418 sg_platf_new_route(&route);
423 int console_add_ASroute(lua_State *L) {
424 s_sg_platf_route_cbarg_t ASroute;
425 memset(&ASroute,0,sizeof(ASroute));
427 lua_pushstring(L, "src");
429 ASroute.src = lua_tostring(L, -1);
432 lua_pushstring(L, "dst");
434 ASroute.dst = lua_tostring(L, -1);
437 lua_pushstring(L, "gw_src");
439 ASroute.gw_src = sg_netcard_by_name_or_null(lua_tostring(L, -1));
442 lua_pushstring(L, "gw_dst");
444 ASroute.gw_dst = sg_netcard_by_name_or_null(lua_tostring(L, -1));
447 /*if (A_surfxml_ASroute_gw___src && !ASroute.gw_src)*/
448 /*surf_parse_error("gw_src=\"%s\" not found for ASroute from \"%s\" to \"%s\"",*/
449 /*A_surfxml_ASroute_gw___src, ASroute.src, ASroute.dst);*/
450 /*if (A_surfxml_ASroute_gw___dst && !ASroute.gw_dst)*/
451 /*surf_parse_error("gw_dst=\"%s\" not found for ASroute from \"%s\" to \"%s\"",*/
452 /*A_surfxml_ASroute_gw___dst, ASroute.src, ASroute.dst);*/
454 lua_pushstring(L,"links");
456 ASroute.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
457 if (xbt_dynar_is_empty(ASroute.link_list))
458 xbt_dynar_push_as(ASroute.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
461 lua_pushstring(L,"symmetrical");
463 if (lua_isstring(L, -1)) {
464 const char* value = lua_tostring(L, -1);
465 if (strcmp("YES", value) == 0) {
466 ASroute.symmetrical = TRUE;
469 ASroute.symmetrical = FALSE;
472 ASroute.symmetrical = TRUE;
476 sg_platf_new_ASroute(&ASroute);
481 int console_AS_open(lua_State *L) {
486 XBT_DEBUG("Opening AS");
488 if (! lua_istable(L, 1)) {
489 XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
493 lua_pushstring(L, "id");
494 type = lua_gettable(L, -2);
495 if (type != LUA_TSTRING) {
496 XBT_ERROR("Attribute 'id' must be specified for any AS and must be a string.");
498 id = lua_tostring(L, -1);
501 lua_pushstring(L, "mode");
503 mode = lua_tostring(L, -1);
506 int mode_int = A_surfxml_AS_routing_None;
507 if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
508 else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
509 else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
510 else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
511 else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
512 else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
513 else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
514 else xbt_die("Don't have the model name '%s'",mode);
516 s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
518 AS.routing = mode_int;
520 sg_platf_new_AS_begin(&AS);
524 int console_AS_close(lua_State *L) {
525 XBT_DEBUG("Closing AS");
526 sg_platf_new_AS_end();
530 int console_host_set_property(lua_State *L) {
531 const char* name ="";
532 const char* prop_id = "";
533 const char* prop_value = "";
534 if (!lua_istable(L, -1)) {
535 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
541 lua_pushstring(L, "host");
543 name = lua_tostring(L, -1);
547 lua_pushstring(L, "prop");
549 prop_id = lua_tostring(L, -1);
552 lua_pushstring(L,"value");
554 prop_value = lua_tostring(L,-1);
557 sg_host_t host = sg_host_by_name(name);
559 XBT_ERROR("no host '%s' found",name);
562 xbt_dict_t props = sg_host_get_properties(host);
563 xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
569 * \brief Registers the platform functions into the table simgrid.platf.
570 * \param L a lua state
572 void sglua_register_platf_functions(lua_State* L)
574 lua_getglobal(L, "simgrid"); /* simgrid */
575 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
576 lua_setfield(L, -2, "platf"); /* simgrid */
578 lua_pop(L, 1); /* -- */
581 //void sglua_register_routing_constants(lua_State* L)
583 // lua_getglobal(L, "simgrid"); /* simgrid */
584 // lua_newtable(L); /* simgrid simgrid.routing */
586 // lua_pushstring(L, "Cluster"); /* simgrid simgrid.routing Cluster */
587 // lua_setfield(L, -2, "CLUSTER"); /* simgrid simgrid.routing */
589 // lua_pushstring(L, "Dijkstra"); /* simgrid simgrid.routing Dijkstra */
590 // lua_setfield(L, -2, "DIJKSTRA"); /* simgrid simgrid.routing */
592 // lua_pushstring(L, "DijkstraCache"); /* simgrid simgrid.routing DijkstraCache */
593 // lua_setfield(L, -2, "DIJKSTRA_CACHE"); /* simgrid simgrid.routing */
595 // lua_pushstring(L, "Floyd"); /* simgrid simgrid.routing Floyd */
596 // lua_setfield(L, -2, "FLOYD"); /* simgrid simgrid.routing */
598 // lua_pushstring(L, "Full"); /* simgrid simgrid.routing Full */
599 // lua_setfield(L, -2, "FULL"); /* simgrid simgrid.routing */
601 // lua_pushstring(L, "None"); /* simgrid simgrid.routing None */
602 // lua_setfield(L, -2, "NONE"); /* simgrid simgrid.routing */
604 // lua_pushstring(L, "Vivaldi"); /* simgrid simgrid.routing Vivaldi */
605 // lua_setfield(L, -2, "FULL"); /* simgrid simgrid.routing */
607 // lua_setfield(L, -2, "routing"); /* simgrid */
609 // lua_pop(L, 1); /* -- */