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 <msg/msg_private.h>
18 #include <simix/smx_host_private.h>
19 #include <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();
51 gpu_register_callbacks();
56 int console_close(lua_State *L) {
60 xbt_lib_cursor_t cursor;
64 /* Initialize MSG and WKS hosts */
65 XBT_DEBUG("Initialize MSG and WKS hosts");
66 xbt_lib_foreach(host_lib, cursor, name, data) {
67 if(data[SURF_HOST_LEVEL]){
68 XBT_DEBUG("\tSee surf host %s",name);
69 SIMIX_host_create(name);
70 // THIS IS BRAINDEAD. There is no sg_host_t in that level, but a smx_host_priv. So commenting out for now.
71 // Lua is broken anyway. Christian will fix it
72 // __MSG_host_create((sg_host_t)data[SIMIX_HOST_LEVEL]);
79 int console_add_backbone(lua_State *L) {
80 s_sg_platf_link_cbarg_t link;
81 memset(&link,0,sizeof(link));
84 link.properties = NULL;
86 if (!lua_istable(L, -1)) {
88 ("Bad Arguments to create backbone in Lua. Should be a table with named arguments.");
92 lua_pushstring(L, "id");
93 type = lua_gettable(L, -2);
94 if (type != LUA_TSTRING) {
95 XBT_ERROR("Attribute 'id' must be specified for backbone and must be a string.");
97 link.id = lua_tostring(L, -1);
100 lua_pushstring(L, "bandwidth");
101 type = lua_gettable(L, -2);
102 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
103 XBT_ERROR("Attribute 'bandwidth' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
105 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1));
108 lua_pushstring(L, "lat");
109 type = lua_gettable(L, -2);
110 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
111 XBT_ERROR("Attribute 'lat' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
113 link.latency = surf_parse_get_time(lua_tostring(L, -1));
116 link.state = SURF_RESOURCE_ON;
118 lua_pushstring(L, "sharing_policy");
119 type = lua_gettable(L, -2);
120 char* policy = lua_tostring(L, -1);
121 if (policy && !strcmp(policy,"FULLDUPLEX")) {
122 link.policy = SURF_LINK_FULLDUPLEX;
123 } else if (policy && !strcmp(policy,"FATPIPE")) {
124 link.policy = SURF_LINK_FATPIPE;
126 link.policy = SURF_LINK_SHARED;
129 sg_platf_new_link(&link);
130 routing_cluster_add_backbone(xbt_lib_get_or_null(link_lib, link.id, SURF_LINK_LEVEL));
135 int console_add_host___link(lua_State *L) {
136 s_sg_platf_host_link_cbarg_t host_link;
137 memset(&host_link,0,sizeof(host_link));
140 // we get values from the table passed as argument
141 if (!lua_istable(L, -1)) {
143 ("Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
147 lua_pushstring(L, "id");
148 type = lua_gettable(L, -2);
149 if (type != LUA_TSTRING) {
150 XBT_ERROR("Attribute 'id' must be specified for any host_link and must be a string.");
152 host_link.id = lua_tostring(L, -1);
155 lua_pushstring(L, "up");
156 type = lua_gettable(L, -2);
157 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
158 XBT_ERROR("Attribute 'up' must be specified for host_link and must either be a string or a number.");
160 host_link.link_up = lua_tostring(L, -1);
163 lua_pushstring(L, "down");
164 type = lua_gettable(L, -2);
165 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
166 XBT_ERROR("Attribute 'down' must be specified for host_link and must either be a string or a number.");
168 host_link.link_down = lua_tostring(L, -1);
171 XBT_DEBUG("Create a host_link for host %s", host_link.id);
172 sg_platf_new_host_link(&host_link);
177 int console_add_host(lua_State *L) {
178 s_sg_platf_host_cbarg_t host;
179 memset(&host,0,sizeof(host));
182 // we get values from the table passed as argument
183 if (!lua_istable(L, -1)) {
185 ("Bad Arguments to create host, Should be a table with named arguments");
190 lua_pushstring(L, "id");
191 type = lua_gettable(L, -2);
192 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
193 XBT_ERROR("Attribute 'id' must be specified for any host and must be a string.");
195 host.id = lua_tostring(L, -1);
199 lua_pushstring(L, "power");
200 type = lua_gettable(L, -2);
201 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
202 XBT_ERROR("Attribute 'power' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
204 host.power_peak = xbt_dynar_new(sizeof(double), NULL);
205 xbt_dynar_push_as(host.power_peak, double, get_cpu_power(lua_tostring(L, -1)));
209 lua_pushstring(L, "core");
211 if(!lua_isnumber(L,-1)) {
212 host.core_amount = 1;// Default value
214 else host.core_amount = lua_tonumber(L, -1);
215 if (host.core_amount == 0)
216 host.core_amount = 1;
220 lua_pushstring(L, "availability");
222 if(!lua_isnumber(L,-1)) host.power_scale = 1;// Default value
223 else host.power_scale = lua_tonumber(L, -1);
227 lua_pushstring(L, "availability_file");
229 host.power_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
233 lua_pushstring(L, "state");
235 if(!lua_isnumber(L,-1)) state = 1;// Default value
236 else state = lua_tonumber(L, -1);
240 host.initial_state = SURF_RESOURCE_ON;
242 host.initial_state = SURF_RESOURCE_OFF;
245 lua_pushstring(L, "state_file");
247 host.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
250 sg_platf_new_host(&host);
255 int console_add_link(lua_State *L) {
256 s_sg_platf_link_cbarg_t link;
257 memset(&link,0,sizeof(link));
262 if (! lua_istable(L, -1)) {
263 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
268 lua_pushstring(L, "id");
269 type = lua_gettable(L, -2);
270 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
271 XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
273 link.id = lua_tostring(L, -1);
276 // get bandwidth value
277 lua_pushstring(L, "bandwidth");
278 type = lua_gettable(L, -2);
279 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
280 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.");
282 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1));
286 lua_pushstring(L, "lat");
287 type = lua_gettable(L, -2);
288 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
289 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.");
291 link.latency = surf_parse_get_time(lua_tostring(L, -1));
294 /*Optional Arguments */
296 //get bandwidth_trace value
297 lua_pushstring(L, "bandwidth_file");
299 link.bandwidth_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
302 //get latency_trace value
303 lua_pushstring(L, "latency_file");
305 link.latency_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
308 //get state_trace value
309 lua_pushstring(L, "state_file");
311 link.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
314 //get state_initial value
315 lua_pushstring(L, "state");
317 if (!lua_isnumber(L,-1) || lua_tonumber(L, -1))
318 link.state = SURF_RESOURCE_ON;
320 link.state = SURF_RESOURCE_OFF;
324 lua_pushstring(L, "sharing_policy");
326 policy = lua_tostring(L, -1);
328 if (policy && !strcmp(policy,"FULLDUPLEX")) {
329 link.policy = SURF_LINK_FULLDUPLEX;
330 } else if (policy && !strcmp(policy,"FATPIPE")) {
331 link.policy = SURF_LINK_FATPIPE;
333 link.policy = SURF_LINK_SHARED;
336 sg_platf_new_link(&link);
341 * add Router to AS components
343 int console_add_router(lua_State* L) {
344 s_sg_platf_router_cbarg_t router;
345 memset(&router,0,sizeof(router));
348 if (! lua_istable(L, -1)) {
349 XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
353 lua_pushstring(L, "id");
354 type = lua_gettable(L, -2);
355 if (type != LUA_TSTRING) {
356 XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
358 router.id = lua_tostring(L, -1);
361 lua_pushstring(L,"coord");
363 router.coord = lua_tostring(L, -1);
366 sg_platf_new_router(&router);
371 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
373 int console_add_route(lua_State *L) {
374 XBT_DEBUG("Adding route");
375 s_sg_platf_route_cbarg_t route;
376 memset(&route,0,sizeof(route));
379 /* allocating memory for the buffer, I think 2kB should be enough */
380 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
382 if (! lua_istable(L, -1)) {
383 XBT_ERROR("Bad Arguments to create a route. Should be a table with named arguments");
387 lua_pushstring(L,"src");
388 type = lua_gettable(L,-2);
389 if (type != LUA_TSTRING) {
390 XBT_ERROR("Attribute 'src' must be specified for any route and must be a string.");
392 route.src = lua_tostring(L, -1);
395 lua_pushstring(L,"dest");
396 type = lua_gettable(L,-2);
397 if (type != LUA_TSTRING) {
398 XBT_ERROR("Attribute 'dest' must be specified for any route and must be a string.");
400 route.dst = lua_tostring(L, -1);
403 lua_pushstring(L,"links");
404 type = lua_gettable(L,-2);
405 if (type != LUA_TSTRING) {
406 XBT_ERROR("Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
408 route.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
409 if (xbt_dynar_is_empty(route.link_list))
410 xbt_dynar_push_as(route.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
413 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
414 * Et ouais mon pote. That's the way it goes. F34R.
416 * (Note that above this function, there is a #include statement. Is this
417 * comment related to that statement?)
419 lua_pushstring(L,"symmetrical");
421 if (lua_isstring(L, -1)) {
422 const char* value = lua_tostring(L, -1);
423 if (strcmp("YES", value) == 0) {
424 route.symmetrical = TRUE;
427 route.symmetrical = FALSE;
430 route.symmetrical = TRUE;
437 sg_platf_new_route(&route);
442 int console_add_ASroute(lua_State *L) {
443 s_sg_platf_route_cbarg_t ASroute;
444 memset(&ASroute,0,sizeof(ASroute));
446 lua_pushstring(L, "src");
448 ASroute.src = lua_tostring(L, -1);
451 lua_pushstring(L, "dst");
453 ASroute.dst = lua_tostring(L, -1);
456 lua_pushstring(L, "gw_src");
458 ASroute.gw_src = sg_routing_edge_by_name_or_null(lua_tostring(L, -1));
461 lua_pushstring(L, "gw_dst");
463 ASroute.gw_dst = sg_routing_edge_by_name_or_null(lua_tostring(L, -1));
466 /*if (A_surfxml_ASroute_gw___src && !ASroute.gw_src)*/
467 /*surf_parse_error("gw_src=\"%s\" not found for ASroute from \"%s\" to \"%s\"",*/
468 /*A_surfxml_ASroute_gw___src, ASroute.src, ASroute.dst);*/
469 /*if (A_surfxml_ASroute_gw___dst && !ASroute.gw_dst)*/
470 /*surf_parse_error("gw_dst=\"%s\" not found for ASroute from \"%s\" to \"%s\"",*/
471 /*A_surfxml_ASroute_gw___dst, ASroute.src, ASroute.dst);*/
473 lua_pushstring(L,"links");
475 ASroute.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
476 if (xbt_dynar_is_empty(ASroute.link_list))
477 xbt_dynar_push_as(ASroute.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
480 lua_pushstring(L,"symmetrical");
482 if (lua_isstring(L, -1)) {
483 const char* value = lua_tostring(L, -1);
484 if (strcmp("YES", value) == 0) {
485 ASroute.symmetrical = TRUE;
488 ASroute.symmetrical = FALSE;
491 ASroute.symmetrical = TRUE;
495 sg_platf_new_ASroute(&ASroute);
500 int console_AS_open(lua_State *L) {
505 XBT_DEBUG("Opening AS");
507 if (! lua_istable(L, 1)) {
508 XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
512 lua_pushstring(L, "id");
513 type = lua_gettable(L, -2);
514 if (type != LUA_TSTRING) {
515 XBT_ERROR("Attribute 'id' must be specified for any AS and must be a string.");
517 id = lua_tostring(L, -1);
520 lua_pushstring(L, "mode");
522 mode = lua_tostring(L, -1);
525 int mode_int = A_surfxml_AS_routing_None;
526 if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
527 else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
528 else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
529 else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
530 else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
531 else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
532 else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
533 else xbt_die("Don't have the model name '%s'",mode);
535 s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
537 AS.routing = mode_int;
539 sg_platf_new_AS_begin(&AS);
543 int console_AS_close(lua_State *L) {
544 XBT_DEBUG("Closing AS");
545 sg_platf_new_AS_end();
549 int console_set_function(lua_State *L) {
551 const char *host_id ;
552 const char *function_id;
555 if (! lua_istable(L, 1)) {
556 XBT_ERROR("Bad Arguments to AS.new, Should be a table with named arguments");
561 lua_pushstring(L, "host");
563 host_id = lua_tostring(L, -1);
567 lua_pushstring(L, "fct");
569 function_id = lua_tostring(L, -1);
573 lua_pushstring(L,"args");
575 args = xbt_str_split_str( lua_tostring(L,-1) , ",");
578 msg_host_t host = MSG_host_by_name(host_id);
580 XBT_ERROR("no host '%s' found",host_id);
584 // FIXME: use sg_platf_new_process directly (warning: find a way to check hostname)
585 MSG_set_function(host_id, function_id, args);
590 int console_host_set_property(lua_State *L) {
591 const char* name ="";
592 const char* prop_id = "";
593 const char* prop_value = "";
594 if (!lua_istable(L, -1)) {
595 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
601 lua_pushstring(L, "host");
603 name = lua_tostring(L, -1);
607 lua_pushstring(L, "prop");
609 prop_id = lua_tostring(L, -1);
612 lua_pushstring(L,"value");
614 prop_value = lua_tostring(L,-1);
617 msg_host_t host = MSG_host_by_name(name);
619 XBT_ERROR("no host '%s' found",name);
622 xbt_dict_t props = MSG_host_get_properties(host);
623 xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
629 * \brief Registers the platform functions into the table simgrid.platf.
630 * \param L a lua state
632 void sglua_register_platf_functions(lua_State* L)
634 lua_getglobal(L, "simgrid"); /* simgrid */
635 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
636 lua_setfield(L, -2, "platf"); /* simgrid */
638 lua_pop(L, 1); /* -- */