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 "src/msg/msg_private.h"
18 #include "src/simix/smx_host_private.h"
19 #include "src/surf/surf_private.h"
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_platf, bindings, "Lua bindings (platform module)");
23 #define PLATF_MODULE_NAME "simgrid.platf"
25 /* ********************************************************************************* */
26 /* simgrid.platf API */
27 /* ********************************************************************************* */
29 static const luaL_Reg platf_functions[] = {
30 {"open", console_open},
31 {"close", console_close},
32 {"AS_open", console_AS_open},
33 {"AS_close", console_AS_close},
34 {"backbone_new", console_add_backbone},
35 {"host_link_new", console_add_host___link},
36 {"host_new", console_add_host},
37 {"link_new", console_add_link},
38 {"router_new", console_add_router},
39 {"route_new", console_add_route},
40 {"ASroute_new", console_add_ASroute},
44 int console_open(lua_State *L) {
48 storage_register_callbacks();
49 routing_register_callbacks();
54 int console_close(lua_State *L) {
60 int console_add_backbone(lua_State *L) {
61 s_sg_platf_link_cbarg_t link;
62 memset(&link,0,sizeof(link));
65 link.properties = NULL;
67 if (!lua_istable(L, -1)) {
69 ("Bad Arguments to create backbone in Lua. Should be a table with named arguments.");
73 lua_pushstring(L, "id");
74 type = lua_gettable(L, -2);
75 if (type != LUA_TSTRING) {
76 XBT_ERROR("Attribute 'id' must be specified for backbone and must be a string.");
78 link.id = lua_tostring(L, -1);
81 lua_pushstring(L, "bandwidth");
82 type = lua_gettable(L, -2);
83 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
84 XBT_ERROR("Attribute 'bandwidth' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
86 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1));
89 lua_pushstring(L, "lat");
90 type = lua_gettable(L, -2);
91 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
92 XBT_ERROR("Attribute 'lat' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
94 link.latency = surf_parse_get_time(lua_tostring(L, -1));
99 lua_pushstring(L, "sharing_policy");
100 type = lua_gettable(L, -2);
101 const char* policy = lua_tostring(L, -1);
102 if (policy && !strcmp(policy,"FULLDUPLEX")) {
103 link.policy = SURF_LINK_FULLDUPLEX;
104 } else if (policy && !strcmp(policy,"FATPIPE")) {
105 link.policy = SURF_LINK_FATPIPE;
107 link.policy = SURF_LINK_SHARED;
110 sg_platf_new_link(&link);
111 routing_cluster_add_backbone(sg_link_by_name(link.id));
116 int console_add_host___link(lua_State *L) {
117 s_sg_platf_host_link_cbarg_t netcard;
118 memset(&netcard,0,sizeof(netcard));
121 // we get values from the table passed as argument
122 if (!lua_istable(L, -1)) {
124 ("Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
128 lua_pushstring(L, "id");
129 type = lua_gettable(L, -2);
130 if (type != LUA_TSTRING) {
131 XBT_ERROR("Attribute 'id' must be specified for any host_link and must be a string.");
133 netcard.id = lua_tostring(L, -1);
136 lua_pushstring(L, "up");
137 type = lua_gettable(L, -2);
138 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
139 XBT_ERROR("Attribute 'up' must be specified for host_link and must either be a string or a number.");
141 netcard.link_up = lua_tostring(L, -1);
144 lua_pushstring(L, "down");
145 type = lua_gettable(L, -2);
146 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
147 XBT_ERROR("Attribute 'down' must be specified for host_link and must either be a string or a number.");
149 netcard.link_down = lua_tostring(L, -1);
152 XBT_DEBUG("Create a host_link for host %s", netcard.id);
153 sg_platf_new_netcard(&netcard);
158 int console_add_host(lua_State *L) {
159 s_sg_platf_host_cbarg_t host;
160 memset(&host,0,sizeof(host));
163 // we get values from the table passed as argument
164 if (!lua_istable(L, -1)) {
166 ("Bad Arguments to create host. Should be a table with named arguments");
171 lua_pushstring(L, "id");
172 type = lua_gettable(L, -2);
173 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
174 XBT_ERROR("Attribute 'id' must be specified for any host and must be a string.");
176 host.id = lua_tostring(L, -1);
180 lua_pushstring(L, "speed");
181 type = lua_gettable(L, -2);
182 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
183 XBT_ERROR("Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
185 host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
186 xbt_dynar_push_as(host.speed_peak, double, get_cpu_speed(lua_tostring(L, -1)));
190 lua_pushstring(L, "core");
192 if(!lua_isnumber(L,-1)) {
193 host.core_amount = 1;// Default value
195 else host.core_amount = lua_tonumber(L, -1);
196 if (host.core_amount == 0)
197 host.core_amount = 1;
201 lua_pushstring(L, "availability");
203 if(!lua_isnumber(L,-1)) host.speed_scale = 1;// Default value
204 else host.speed_scale = lua_tonumber(L, -1);
208 lua_pushstring(L, "availability_file");
210 host.speed_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
214 lua_pushstring(L, "state");
216 if(!lua_isnumber(L,-1)) state = 1;// Default value
217 else state = lua_tonumber(L, -1);
221 host.initiallyOn = 1;
223 host.initiallyOn = 0;
226 lua_pushstring(L, "state_file");
228 host.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
231 sg_platf_new_host(&host);
232 xbt_dynar_free(&host.speed_peak);
237 int console_add_link(lua_State *L) {
238 s_sg_platf_link_cbarg_t link;
239 memset(&link,0,sizeof(link));
244 if (! lua_istable(L, -1)) {
245 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
250 lua_pushstring(L, "id");
251 type = lua_gettable(L, -2);
252 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
253 XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
255 link.id = lua_tostring(L, -1);
258 // get bandwidth value
259 lua_pushstring(L, "bandwidth");
260 type = lua_gettable(L, -2);
261 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
262 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.");
264 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1));
268 lua_pushstring(L, "lat");
269 type = lua_gettable(L, -2);
270 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
271 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.");
273 link.latency = surf_parse_get_time(lua_tostring(L, -1));
276 /*Optional Arguments */
278 //get bandwidth_trace value
279 lua_pushstring(L, "bandwidth_file");
281 link.bandwidth_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
284 //get latency_trace value
285 lua_pushstring(L, "latency_file");
287 link.latency_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
290 //get state_trace value
291 lua_pushstring(L, "state_file");
293 link.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
296 //get state_initial value
297 lua_pushstring(L, "state");
299 if (!lua_isnumber(L,-1) || lua_tonumber(L, -1))
300 link.initiallyOn = 1;
302 link.initiallyOn = 0;
306 lua_pushstring(L, "sharing_policy");
308 policy = lua_tostring(L, -1);
310 if (policy && !strcmp(policy,"FULLDUPLEX")) {
311 link.policy = SURF_LINK_FULLDUPLEX;
312 } else if (policy && !strcmp(policy,"FATPIPE")) {
313 link.policy = SURF_LINK_FATPIPE;
315 link.policy = SURF_LINK_SHARED;
318 sg_platf_new_link(&link);
323 * add Router to AS components
325 int console_add_router(lua_State* L) {
326 s_sg_platf_router_cbarg_t router;
327 memset(&router,0,sizeof(router));
330 if (! lua_istable(L, -1)) {
331 XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
335 lua_pushstring(L, "id");
336 type = lua_gettable(L, -2);
337 if (type != LUA_TSTRING) {
338 XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
340 router.id = lua_tostring(L, -1);
343 lua_pushstring(L,"coord");
345 router.coord = lua_tostring(L, -1);
348 sg_platf_new_router(&router);
353 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
355 int console_add_route(lua_State *L) {
356 XBT_DEBUG("Adding route");
357 s_sg_platf_route_cbarg_t route;
358 memset(&route,0,sizeof(route));
361 /* allocating memory for the buffer, I think 2kB should be enough */
362 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
364 if (! lua_istable(L, -1)) {
365 XBT_ERROR("Bad Arguments to create a route. Should be a table with named arguments");
369 lua_pushstring(L,"src");
370 type = lua_gettable(L,-2);
371 if (type != LUA_TSTRING) {
372 XBT_ERROR("Attribute 'src' must be specified for any route and must be a string.");
374 route.src = lua_tostring(L, -1);
377 lua_pushstring(L,"dest");
378 type = lua_gettable(L,-2);
379 if (type != LUA_TSTRING) {
380 XBT_ERROR("Attribute 'dest' must be specified for any route and must be a string.");
382 route.dst = lua_tostring(L, -1);
385 lua_pushstring(L,"links");
386 type = lua_gettable(L,-2);
387 if (type != LUA_TSTRING) {
388 XBT_ERROR("Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
390 route.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
391 if (xbt_dynar_is_empty(route.link_list))
392 xbt_dynar_push_as(route.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
395 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
396 * Et ouais mon pote. That's the way it goes. F34R.
398 * (Note that above this function, there is a #include statement. Is this
399 * comment related to that statement?)
401 lua_pushstring(L,"symmetrical");
403 if (lua_isstring(L, -1)) {
404 const char* value = lua_tostring(L, -1);
405 if (strcmp("YES", value) == 0) {
406 route.symmetrical = TRUE;
409 route.symmetrical = FALSE;
412 route.symmetrical = TRUE;
419 sg_platf_new_route(&route);
424 int console_add_ASroute(lua_State *L) {
425 s_sg_platf_route_cbarg_t ASroute;
426 memset(&ASroute,0,sizeof(ASroute));
428 lua_pushstring(L, "src");
430 ASroute.src = lua_tostring(L, -1);
433 lua_pushstring(L, "dst");
435 ASroute.dst = lua_tostring(L, -1);
438 lua_pushstring(L, "gw_src");
440 ASroute.gw_src = sg_netcard_by_name_or_null(lua_tostring(L, -1));
443 lua_pushstring(L, "gw_dst");
445 ASroute.gw_dst = sg_netcard_by_name_or_null(lua_tostring(L, -1));
448 /*if (A_surfxml_ASroute_gw___src && !ASroute.gw_src)*/
449 /*surf_parse_error("gw_src=\"%s\" not found for ASroute from \"%s\" to \"%s\"",*/
450 /*A_surfxml_ASroute_gw___src, ASroute.src, ASroute.dst);*/
451 /*if (A_surfxml_ASroute_gw___dst && !ASroute.gw_dst)*/
452 /*surf_parse_error("gw_dst=\"%s\" not found for ASroute from \"%s\" to \"%s\"",*/
453 /*A_surfxml_ASroute_gw___dst, ASroute.src, ASroute.dst);*/
455 lua_pushstring(L,"links");
457 ASroute.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
458 if (xbt_dynar_is_empty(ASroute.link_list))
459 xbt_dynar_push_as(ASroute.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
462 lua_pushstring(L,"symmetrical");
464 if (lua_isstring(L, -1)) {
465 const char* value = lua_tostring(L, -1);
466 if (strcmp("YES", value) == 0) {
467 ASroute.symmetrical = TRUE;
470 ASroute.symmetrical = FALSE;
473 ASroute.symmetrical = TRUE;
477 sg_platf_new_ASroute(&ASroute);
482 int console_AS_open(lua_State *L) {
487 XBT_DEBUG("Opening AS");
489 if (! lua_istable(L, 1)) {
490 XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
494 lua_pushstring(L, "id");
495 type = lua_gettable(L, -2);
496 if (type != LUA_TSTRING) {
497 XBT_ERROR("Attribute 'id' must be specified for any AS and must be a string.");
499 id = lua_tostring(L, -1);
502 lua_pushstring(L, "mode");
504 mode = lua_tostring(L, -1);
507 int mode_int = A_surfxml_AS_routing_None;
508 if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
509 else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
510 else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
511 else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
512 else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
513 else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
514 else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
515 else xbt_die("Don't have the model name '%s'",mode);
517 s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
519 AS.routing = mode_int;
521 sg_platf_new_AS_begin(&AS);
525 int console_AS_close(lua_State *L) {
526 XBT_DEBUG("Closing AS");
527 sg_platf_new_AS_end();
531 int console_set_function(lua_State *L) {
533 const char *host_id ;
534 const char *function_id;
537 if (! lua_istable(L, 1)) {
538 XBT_ERROR("Bad Arguments to AS.new, Should be a table with named arguments");
543 lua_pushstring(L, "host");
545 host_id = lua_tostring(L, -1);
549 lua_pushstring(L, "fct");
551 function_id = lua_tostring(L, -1);
555 lua_pushstring(L,"args");
557 args = xbt_str_split_str( lua_tostring(L,-1) , ",");
560 msg_host_t host = MSG_host_by_name(host_id);
562 XBT_ERROR("no host '%s' found",host_id);
566 // FIXME: use sg_platf_new_process directly (warning: find a way to check hostname)
567 MSG_set_function(host_id, function_id, args);
572 int console_host_set_property(lua_State *L) {
573 const char* name ="";
574 const char* prop_id = "";
575 const char* prop_value = "";
576 if (!lua_istable(L, -1)) {
577 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
583 lua_pushstring(L, "host");
585 name = lua_tostring(L, -1);
589 lua_pushstring(L, "prop");
591 prop_id = lua_tostring(L, -1);
594 lua_pushstring(L,"value");
596 prop_value = lua_tostring(L,-1);
599 msg_host_t host = MSG_host_by_name(name);
601 XBT_ERROR("no host '%s' found",name);
604 xbt_dict_t props = MSG_host_get_properties(host);
605 xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
611 * \brief Registers the platform functions into the table simgrid.platf.
612 * \param L a lua state
614 void sglua_register_platf_functions(lua_State* L)
616 lua_getglobal(L, "simgrid"); /* simgrid */
617 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
618 lua_setfield(L, -2, "platf"); /* simgrid */
620 lua_pop(L, 1); /* -- */
623 void sglua_register_routing_constants(lua_State* L)
625 lua_getglobal(L, "simgrid"); /* simgrid */
626 lua_newtable(L); /* simgrid simgrid.routing */
628 lua_pushstring(L, "Cluster"); /* simgrid simgrid.routing Cluster */
629 lua_setfield(L, -2, "CLUSTER"); /* simgrid simgrid.routing */
631 lua_pushstring(L, "Dijkstra"); /* simgrid simgrid.routing Dijkstra */
632 lua_setfield(L, -2, "DIJKSTRA"); /* simgrid simgrid.routing */
634 lua_pushstring(L, "DijkstraCache"); /* simgrid simgrid.routing DijkstraCache */
635 lua_setfield(L, -2, "DIJKSTRA_CACHE"); /* simgrid simgrid.routing */
637 lua_pushstring(L, "Floyd"); /* simgrid simgrid.routing Floyd */
638 lua_setfield(L, -2, "FLOYD"); /* simgrid simgrid.routing */
640 lua_pushstring(L, "Full"); /* simgrid simgrid.routing Full */
641 lua_setfield(L, -2, "FULL"); /* simgrid simgrid.routing */
643 lua_pushstring(L, "None"); /* simgrid simgrid.routing None */
644 lua_setfield(L, -2, "NONE"); /* simgrid simgrid.routing */
646 lua_pushstring(L, "Vivaldi"); /* simgrid simgrid.routing Vivaldi */
647 lua_setfield(L, -2, "FULL"); /* simgrid simgrid.routing */
649 lua_setfield(L, -2, "routing"); /* simgrid */
651 lua_pop(L, 1); /* -- */