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"
20 #include <simgrid/host.h>
21 #include "src/surf/surf_private.h"
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_platf, bindings, "Lua bindings (platform module)");
25 #define PLATF_MODULE_NAME "simgrid.platf"
27 /* ********************************************************************************* */
28 /* simgrid.platf API */
29 /* ********************************************************************************* */
31 static const luaL_Reg platf_functions[] = {
32 {"open", console_open},
33 {"close", console_close},
34 {"AS_open", console_AS_open},
35 {"AS_close", console_AS_close},
36 {"backbone_new", console_add_backbone},
37 {"host_link_new", console_add_host___link},
38 {"host_new", console_add_host},
39 {"link_new", console_add_link},
40 {"router_new", console_add_router},
41 {"route_new", console_add_route},
42 {"ASroute_new", console_add_ASroute},
46 int console_open(lua_State *L) {
50 storage_register_callbacks();
51 routing_register_callbacks();
56 int console_close(lua_State *L) {
62 int console_add_backbone(lua_State *L) {
63 s_sg_platf_link_cbarg_t link;
64 memset(&link,0,sizeof(link));
67 link.properties = NULL;
69 if (!lua_istable(L, -1)) {
71 ("Bad Arguments to create backbone in Lua. Should be a table with named arguments.");
75 lua_pushstring(L, "id");
76 type = lua_gettable(L, -2);
77 if (type != LUA_TSTRING) {
78 XBT_ERROR("Attribute 'id' must be specified for backbone and must be a string.");
80 link.id = lua_tostring(L, -1);
83 lua_pushstring(L, "bandwidth");
84 type = lua_gettable(L, -2);
85 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
86 XBT_ERROR("Attribute 'bandwidth' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
88 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1));
91 lua_pushstring(L, "lat");
92 type = lua_gettable(L, -2);
93 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
94 XBT_ERROR("Attribute 'lat' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
96 link.latency = surf_parse_get_time(lua_tostring(L, -1));
101 lua_pushstring(L, "sharing_policy");
102 type = lua_gettable(L, -2);
103 const char* policy = lua_tostring(L, -1);
104 if (policy && !strcmp(policy,"FULLDUPLEX")) {
105 link.policy = SURF_LINK_FULLDUPLEX;
106 } else if (policy && !strcmp(policy,"FATPIPE")) {
107 link.policy = SURF_LINK_FATPIPE;
109 link.policy = SURF_LINK_SHARED;
112 sg_platf_new_link(&link);
113 routing_cluster_add_backbone(sg_link_by_name(link.id));
118 int console_add_host___link(lua_State *L) {
119 s_sg_platf_host_link_cbarg_t netcard;
120 memset(&netcard,0,sizeof(netcard));
123 // we get values from the table passed as argument
124 if (!lua_istable(L, -1)) {
126 ("Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
130 lua_pushstring(L, "id");
131 type = lua_gettable(L, -2);
132 if (type != LUA_TSTRING) {
133 XBT_ERROR("Attribute 'id' must be specified for any host_link and must be a string.");
135 netcard.id = lua_tostring(L, -1);
138 lua_pushstring(L, "up");
139 type = lua_gettable(L, -2);
140 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
141 XBT_ERROR("Attribute 'up' must be specified for host_link and must either be a string or a number.");
143 netcard.link_up = lua_tostring(L, -1);
146 lua_pushstring(L, "down");
147 type = lua_gettable(L, -2);
148 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
149 XBT_ERROR("Attribute 'down' must be specified for host_link and must either be a string or a number.");
151 netcard.link_down = lua_tostring(L, -1);
154 XBT_DEBUG("Create a host_link for host %s", netcard.id);
155 sg_platf_new_netcard(&netcard);
160 int console_add_host(lua_State *L) {
161 s_sg_platf_host_cbarg_t host;
162 memset(&host,0,sizeof(host));
165 // we get values from the table passed as argument
166 if (!lua_istable(L, -1)) {
168 ("Bad Arguments to create host. Should be a table with named arguments");
173 lua_pushstring(L, "id");
174 type = lua_gettable(L, -2);
175 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
176 XBT_ERROR("Attribute 'id' must be specified for any host and must be a string.");
178 host.id = lua_tostring(L, -1);
182 lua_pushstring(L, "speed");
183 type = lua_gettable(L, -2);
184 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
185 XBT_ERROR("Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
187 host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
188 xbt_dynar_push_as(host.speed_peak, double, parse_cpu_speed(lua_tostring(L, -1)));
192 lua_pushstring(L, "core");
194 if(!lua_isnumber(L,-1)) {
195 host.core_amount = 1;// Default value
197 else host.core_amount = lua_tonumber(L, -1);
198 if (host.core_amount == 0)
199 host.core_amount = 1;
203 lua_pushstring(L, "availability");
205 if(!lua_isnumber(L,-1)) host.speed_scale = 1;// Default value
206 else host.speed_scale = lua_tonumber(L, -1);
210 lua_pushstring(L, "availability_file");
212 host.speed_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
216 lua_pushstring(L, "state");
218 if(!lua_isnumber(L,-1)) state = 1;// Default value
219 else state = lua_tonumber(L, -1);
223 host.initiallyOn = 1;
225 host.initiallyOn = 0;
228 lua_pushstring(L, "state_file");
230 host.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
233 sg_platf_new_host(&host);
234 xbt_dynar_free(&host.speed_peak);
239 int console_add_link(lua_State *L) {
240 s_sg_platf_link_cbarg_t link;
241 memset(&link,0,sizeof(link));
246 if (! lua_istable(L, -1)) {
247 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
252 lua_pushstring(L, "id");
253 type = lua_gettable(L, -2);
254 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
255 XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
257 link.id = lua_tostring(L, -1);
260 // get bandwidth value
261 lua_pushstring(L, "bandwidth");
262 type = lua_gettable(L, -2);
263 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
264 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.");
266 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1));
270 lua_pushstring(L, "lat");
271 type = lua_gettable(L, -2);
272 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
273 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.");
275 link.latency = surf_parse_get_time(lua_tostring(L, -1));
278 /*Optional Arguments */
280 //get bandwidth_trace value
281 lua_pushstring(L, "bandwidth_file");
283 link.bandwidth_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
286 //get latency_trace value
287 lua_pushstring(L, "latency_file");
289 link.latency_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
292 //get state_trace value
293 lua_pushstring(L, "state_file");
295 link.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
298 //get state_initial value
299 lua_pushstring(L, "state");
301 if (!lua_isnumber(L,-1) || lua_tonumber(L, -1))
302 link.initiallyOn = 1;
304 link.initiallyOn = 0;
308 lua_pushstring(L, "sharing_policy");
310 policy = lua_tostring(L, -1);
312 if (policy && !strcmp(policy,"FULLDUPLEX")) {
313 link.policy = SURF_LINK_FULLDUPLEX;
314 } else if (policy && !strcmp(policy,"FATPIPE")) {
315 link.policy = SURF_LINK_FATPIPE;
317 link.policy = SURF_LINK_SHARED;
320 sg_platf_new_link(&link);
325 * add Router to AS components
327 int console_add_router(lua_State* L) {
328 s_sg_platf_router_cbarg_t router;
329 memset(&router,0,sizeof(router));
332 if (! lua_istable(L, -1)) {
333 XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
337 lua_pushstring(L, "id");
338 type = lua_gettable(L, -2);
339 if (type != LUA_TSTRING) {
340 XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
342 router.id = lua_tostring(L, -1);
345 lua_pushstring(L,"coord");
347 router.coord = lua_tostring(L, -1);
350 sg_platf_new_router(&router);
355 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
357 int console_add_route(lua_State *L) {
358 XBT_DEBUG("Adding route");
359 s_sg_platf_route_cbarg_t route;
360 memset(&route,0,sizeof(route));
363 /* allocating memory for the buffer, I think 2kB should be enough */
364 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
366 if (! lua_istable(L, -1)) {
367 XBT_ERROR("Bad Arguments to create a route. Should be a table with named arguments");
371 lua_pushstring(L,"src");
372 type = lua_gettable(L,-2);
373 if (type != LUA_TSTRING) {
374 XBT_ERROR("Attribute 'src' must be specified for any route and must be a string.");
376 route.src = lua_tostring(L, -1);
379 lua_pushstring(L,"dest");
380 type = lua_gettable(L,-2);
381 if (type != LUA_TSTRING) {
382 XBT_ERROR("Attribute 'dest' must be specified for any route and must be a string.");
384 route.dst = lua_tostring(L, -1);
387 lua_pushstring(L,"links");
388 type = lua_gettable(L,-2);
389 if (type != LUA_TSTRING) {
390 XBT_ERROR("Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
392 route.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
393 if (xbt_dynar_is_empty(route.link_list))
394 xbt_dynar_push_as(route.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
397 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
398 * Et ouais mon pote. That's the way it goes. F34R.
400 * (Note that above this function, there is a #include statement. Is this
401 * comment related to that statement?)
403 lua_pushstring(L,"symmetrical");
405 if (lua_isstring(L, -1)) {
406 const char* value = lua_tostring(L, -1);
407 if (strcmp("YES", value) == 0) {
408 route.symmetrical = TRUE;
411 route.symmetrical = FALSE;
414 route.symmetrical = TRUE;
421 sg_platf_new_route(&route);
426 int console_add_ASroute(lua_State *L) {
427 s_sg_platf_route_cbarg_t ASroute;
428 memset(&ASroute,0,sizeof(ASroute));
430 lua_pushstring(L, "src");
432 ASroute.src = lua_tostring(L, -1);
435 lua_pushstring(L, "dst");
437 ASroute.dst = lua_tostring(L, -1);
440 lua_pushstring(L, "gw_src");
442 ASroute.gw_src = sg_netcard_by_name_or_null(lua_tostring(L, -1));
445 lua_pushstring(L, "gw_dst");
447 ASroute.gw_dst = sg_netcard_by_name_or_null(lua_tostring(L, -1));
450 /*if (A_surfxml_ASroute_gw___src && !ASroute.gw_src)*/
451 /*surf_parse_error("gw_src=\"%s\" not found for ASroute from \"%s\" to \"%s\"",*/
452 /*A_surfxml_ASroute_gw___src, ASroute.src, ASroute.dst);*/
453 /*if (A_surfxml_ASroute_gw___dst && !ASroute.gw_dst)*/
454 /*surf_parse_error("gw_dst=\"%s\" not found for ASroute from \"%s\" to \"%s\"",*/
455 /*A_surfxml_ASroute_gw___dst, ASroute.src, ASroute.dst);*/
457 lua_pushstring(L,"links");
459 ASroute.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
460 if (xbt_dynar_is_empty(ASroute.link_list))
461 xbt_dynar_push_as(ASroute.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
464 lua_pushstring(L,"symmetrical");
466 if (lua_isstring(L, -1)) {
467 const char* value = lua_tostring(L, -1);
468 if (strcmp("YES", value) == 0) {
469 ASroute.symmetrical = TRUE;
472 ASroute.symmetrical = FALSE;
475 ASroute.symmetrical = TRUE;
479 sg_platf_new_ASroute(&ASroute);
484 int console_AS_open(lua_State *L) {
489 XBT_DEBUG("Opening AS");
491 if (! lua_istable(L, 1)) {
492 XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
496 lua_pushstring(L, "id");
497 type = lua_gettable(L, -2);
498 if (type != LUA_TSTRING) {
499 XBT_ERROR("Attribute 'id' must be specified for any AS and must be a string.");
501 id = lua_tostring(L, -1);
504 lua_pushstring(L, "mode");
506 mode = lua_tostring(L, -1);
509 int mode_int = A_surfxml_AS_routing_None;
510 if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
511 else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
512 else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
513 else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
514 else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
515 else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
516 else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
517 else xbt_die("Don't have the model name '%s'",mode);
519 s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
521 AS.routing = mode_int;
523 sg_platf_new_AS_begin(&AS);
527 int console_AS_close(lua_State *L) {
528 XBT_DEBUG("Closing AS");
529 sg_platf_new_AS_end();
533 int console_host_set_property(lua_State *L) {
534 const char* name ="";
535 const char* prop_id = "";
536 const char* prop_value = "";
537 if (!lua_istable(L, -1)) {
538 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
544 lua_pushstring(L, "host");
546 name = lua_tostring(L, -1);
550 lua_pushstring(L, "prop");
552 prop_id = lua_tostring(L, -1);
555 lua_pushstring(L,"value");
557 prop_value = lua_tostring(L,-1);
560 sg_host_t host = sg_host_by_name(name);
562 XBT_ERROR("no host '%s' found",name);
565 xbt_dict_t props = sg_host_get_properties(host);
566 xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
572 * \brief Registers the platform functions into the table simgrid.platf.
573 * \param L a lua state
575 void sglua_register_platf_functions(lua_State* L)
577 lua_getglobal(L, "simgrid"); /* simgrid */
578 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
579 lua_setfield(L, -2, "platf"); /* simgrid */
581 lua_pop(L, 1); /* -- */
584 //void sglua_register_routing_constants(lua_State* L)
586 // lua_getglobal(L, "simgrid"); /* simgrid */
587 // lua_newtable(L); /* simgrid simgrid.routing */
589 // lua_pushstring(L, "Cluster"); /* simgrid simgrid.routing Cluster */
590 // lua_setfield(L, -2, "CLUSTER"); /* simgrid simgrid.routing */
592 // lua_pushstring(L, "Dijkstra"); /* simgrid simgrid.routing Dijkstra */
593 // lua_setfield(L, -2, "DIJKSTRA"); /* simgrid simgrid.routing */
595 // lua_pushstring(L, "DijkstraCache"); /* simgrid simgrid.routing DijkstraCache */
596 // lua_setfield(L, -2, "DIJKSTRA_CACHE"); /* simgrid simgrid.routing */
598 // lua_pushstring(L, "Floyd"); /* simgrid simgrid.routing Floyd */
599 // lua_setfield(L, -2, "FLOYD"); /* simgrid simgrid.routing */
601 // lua_pushstring(L, "Full"); /* simgrid simgrid.routing Full */
602 // lua_setfield(L, -2, "FULL"); /* simgrid simgrid.routing */
604 // lua_pushstring(L, "None"); /* simgrid simgrid.routing None */
605 // lua_setfield(L, -2, "NONE"); /* simgrid simgrid.routing */
607 // lua_pushstring(L, "Vivaldi"); /* simgrid simgrid.routing Vivaldi */
608 // lua_setfield(L, -2, "FULL"); /* simgrid simgrid.routing */
610 // lua_setfield(L, -2, "routing"); /* simgrid */
612 // lua_pop(L, 1); /* -- */