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_private.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);
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_hostlink(&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)) {
164 XBT_ERROR("Bad Arguments to create host. Should be a table with named arguments");
169 lua_pushstring(L, "id");
170 type = lua_gettable(L, -2);
171 if (type != LUA_TSTRING) {
172 XBT_ERROR("Attribute 'id' must be specified for any host and must be a string.");
174 host.id = lua_tostring(L, -1);
178 lua_pushstring(L, "speed");
179 type = lua_gettable(L, -2);
180 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
181 XBT_ERROR("Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
183 host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
184 if (type == LUA_TNUMBER)
185 xbt_dynar_push_as(host.speed_peak, double, lua_tointeger(L, -1));
187 xbt_dynar_push_as(host.speed_peak, double, surf_parse_get_speed(lua_tostring(L, -1), "speed of host", host.id));
191 lua_pushstring(L, "core");
193 if(!lua_isnumber(L,-1)) {
194 host.core_amount = 1;// Default value
196 else host.core_amount = lua_tonumber(L, -1);
197 if (host.core_amount == 0)
198 host.core_amount = 1;
202 lua_pushstring(L, "availability_file");
204 host.speed_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
208 lua_pushstring(L, "state_file");
210 host.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
213 sg_platf_new_host(&host);
214 xbt_dynar_free(&host.speed_peak);
219 int console_add_link(lua_State *L) {
220 s_sg_platf_link_cbarg_t link;
221 memset(&link,0,sizeof(link));
226 if (! lua_istable(L, -1)) {
227 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
232 lua_pushstring(L, "id");
233 type = lua_gettable(L, -2);
234 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
235 XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
237 link.id = lua_tostring(L, -1);
240 // get bandwidth value
241 lua_pushstring(L, "bandwidth");
242 type = lua_gettable(L, -2);
243 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
244 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.");
246 if (type == LUA_TNUMBER)
247 link.bandwidth = lua_tonumber(L, -1);
249 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1),"bandwidth of link", link.id);
253 lua_pushstring(L, "lat");
254 type = lua_gettable(L, -2);
255 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
256 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.");
258 if (type == LUA_TNUMBER)
259 link.latency = lua_tonumber(L, -1);
261 link.latency = surf_parse_get_time(lua_tostring(L, -1),"latency of link", link.id);
264 /*Optional Arguments */
266 //get bandwidth_trace value
267 lua_pushstring(L, "bandwidth_file");
269 link.bandwidth_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
272 //get latency_trace value
273 lua_pushstring(L, "latency_file");
275 link.latency_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
278 //get state_trace value
279 lua_pushstring(L, "state_file");
281 link.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
285 lua_pushstring(L, "sharing_policy");
287 policy = lua_tostring(L, -1);
289 if (policy && !strcmp(policy,"FULLDUPLEX")) {
290 link.policy = SURF_LINK_FULLDUPLEX;
291 } else if (policy && !strcmp(policy,"FATPIPE")) {
292 link.policy = SURF_LINK_FATPIPE;
294 link.policy = SURF_LINK_SHARED;
297 sg_platf_new_link(&link);
302 * add Router to AS components
304 int console_add_router(lua_State* L) {
305 s_sg_platf_router_cbarg_t router;
306 memset(&router,0,sizeof(router));
309 if (! lua_istable(L, -1)) {
310 XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
314 lua_pushstring(L, "id");
315 type = lua_gettable(L, -2);
316 if (type != LUA_TSTRING) {
317 XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
319 router.id = lua_tostring(L, -1);
322 lua_pushstring(L,"coord");
324 router.coord = lua_tostring(L, -1);
327 sg_platf_new_router(&router);
332 int console_add_route(lua_State *L) {
333 XBT_DEBUG("Adding route");
334 s_sg_platf_route_cbarg_t route;
335 memset(&route,0,sizeof(route));
338 /* allocating memory for the buffer, I think 2kB should be enough */
339 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
341 if (! lua_istable(L, -1)) {
342 XBT_ERROR("Bad Arguments to add a route. Should be a table with named arguments");
346 lua_pushstring(L,"src");
347 type = lua_gettable(L,-2);
348 if (type != LUA_TSTRING) {
349 XBT_ERROR("Attribute 'src' must be specified for any route and must be a string.");
351 route.src = lua_tostring(L, -1);
354 lua_pushstring(L,"dest");
355 type = lua_gettable(L,-2);
356 if (type != LUA_TSTRING) {
357 XBT_ERROR("Attribute 'dest' must be specified for any route and must be a string.");
359 route.dst = lua_tostring(L, -1);
362 lua_pushstring(L,"links");
363 type = lua_gettable(L,-2);
364 if (type != LUA_TSTRING) {
365 XBT_ERROR("Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
367 route.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
368 if (xbt_dynar_is_empty(route.link_list))
369 xbt_dynar_push_as(route.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
372 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
373 * Et ouais mon pote. That's the way it goes. F34R.
375 * (Note that above this function, there is a #include statement. Is this
376 * comment related to that statement?)
378 lua_pushstring(L,"symmetrical");
380 if (lua_isstring(L, -1)) {
381 const char* value = lua_tostring(L, -1);
382 if (strcmp("YES", value) == 0) {
383 route.symmetrical = TRUE;
386 route.symmetrical = FALSE;
389 route.symmetrical = TRUE;
396 sg_platf_new_route(&route);
401 int console_add_ASroute(lua_State *L) {
402 s_sg_platf_route_cbarg_t ASroute;
403 memset(&ASroute,0,sizeof(ASroute));
405 lua_pushstring(L, "src");
407 ASroute.src = lua_tostring(L, -1);
410 lua_pushstring(L, "dst");
412 ASroute.dst = lua_tostring(L, -1);
415 lua_pushstring(L, "gw_src");
417 const char *name = lua_tostring(L, -1);
418 ASroute.gw_src = sg_netcard_by_name_or_null(name);
419 if (ASroute.gw_src == NULL) {
420 XBT_ERROR("Attribute 'gw_src' of AS route does not name a valid machine: %s", name);
425 lua_pushstring(L, "gw_dst");
427 name = lua_tostring(L, -1);
428 ASroute.gw_dst = sg_netcard_by_name_or_null(name);
429 if (ASroute.gw_dst == NULL) {
430 XBT_ERROR("Attribute 'gw_dst' of AS route does not name a valid machine: %s", name);
435 lua_pushstring(L,"links");
437 ASroute.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
438 if (xbt_dynar_is_empty(ASroute.link_list))
439 xbt_dynar_push_as(ASroute.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
442 lua_pushstring(L,"symmetrical");
444 if (lua_isstring(L, -1)) {
445 const char* value = lua_tostring(L, -1);
446 if (strcmp("YES", value) == 0)
447 ASroute.symmetrical = TRUE;
449 ASroute.symmetrical = FALSE;
452 ASroute.symmetrical = TRUE;
456 sg_platf_new_route(&ASroute);
461 int console_AS_open(lua_State *L) {
466 XBT_DEBUG("Opening AS");
468 if (! lua_istable(L, 1)) {
469 XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
473 lua_pushstring(L, "id");
474 type = lua_gettable(L, -2);
475 if (type != LUA_TSTRING) {
476 XBT_ERROR("Attribute 'id' must be specified for any AS and must be a string.");
478 id = lua_tostring(L, -1);
481 lua_pushstring(L, "mode");
483 mode = lua_tostring(L, -1);
486 int mode_int = A_surfxml_AS_routing_None;
487 if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
488 else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
489 else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
490 else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
491 else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
492 else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
493 else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
494 else xbt_die("Don't have the model name '%s'",mode);
496 s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
498 AS.routing = mode_int;
500 sg_platf_new_AS_begin(&AS);
504 int console_AS_close(lua_State *L) {
505 XBT_DEBUG("Closing AS");
506 sg_platf_new_AS_end();
510 int console_host_set_property(lua_State *L) {
511 const char* name ="";
512 const char* prop_id = "";
513 const char* prop_value = "";
514 if (!lua_istable(L, -1)) {
515 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
521 lua_pushstring(L, "host");
523 name = lua_tostring(L, -1);
527 lua_pushstring(L, "prop");
529 prop_id = lua_tostring(L, -1);
532 lua_pushstring(L,"value");
534 prop_value = lua_tostring(L,-1);
537 sg_host_t host = sg_host_by_name(name);
539 XBT_ERROR("no host '%s' found",name);
542 xbt_dict_t props = sg_host_get_properties(host);
543 xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
549 * \brief Registers the platform functions into the table simgrid.platf.
550 * \param L a lua state
552 void sglua_register_platf_functions(lua_State* L)
554 lua_getglobal(L, "simgrid"); /* simgrid */
555 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
556 lua_setfield(L, -2, "platf"); /* simgrid */
558 lua_pop(L, 1); /* -- */
561 //void sglua_register_routing_constants(lua_State* L)
563 // lua_getglobal(L, "simgrid"); /* simgrid */
564 // lua_newtable(L); /* simgrid simgrid.routing */
566 // lua_pushstring(L, "Cluster"); /* simgrid simgrid.routing Cluster */
567 // lua_setfield(L, -2, "CLUSTER"); /* simgrid simgrid.routing */
569 // lua_pushstring(L, "Dijkstra"); /* simgrid simgrid.routing Dijkstra */
570 // lua_setfield(L, -2, "DIJKSTRA"); /* simgrid simgrid.routing */
572 // lua_pushstring(L, "DijkstraCache"); /* simgrid simgrid.routing DijkstraCache */
573 // lua_setfield(L, -2, "DIJKSTRA_CACHE"); /* simgrid simgrid.routing */
575 // lua_pushstring(L, "Floyd"); /* simgrid simgrid.routing Floyd */
576 // lua_setfield(L, -2, "FLOYD"); /* simgrid simgrid.routing */
578 // lua_pushstring(L, "Full"); /* simgrid simgrid.routing Full */
579 // lua_setfield(L, -2, "FULL"); /* simgrid simgrid.routing */
581 // lua_pushstring(L, "None"); /* simgrid simgrid.routing None */
582 // lua_setfield(L, -2, "NONE"); /* simgrid simgrid.routing */
584 // lua_pushstring(L, "Vivaldi"); /* simgrid simgrid.routing Vivaldi */
585 // lua_setfield(L, -2, "FULL"); /* simgrid simgrid.routing */
587 // lua_setfield(L, -2, "routing"); /* simgrid */
589 // lua_pop(L, 1); /* -- */