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 "src/surf/network_interface.hpp"
12 #include "surf/surf_routing.h"
20 #include <simgrid/host.h>
21 #include "src/surf/surf_private.h"
23 XBT_LOG_NEW_DEFAULT_CATEGORY(lua_platf, "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_seal", console_AS_seal},
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),"bandwidth of backbone",link.id);
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),"latency of backbone",link.id);
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_hostlink(&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)) {
165 XBT_ERROR("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) {
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_per_pstate = xbt_dynar_new(sizeof(double), NULL);
185 if (type == LUA_TNUMBER)
186 xbt_dynar_push_as(host.speed_per_pstate, double, lua_tointeger(L, -1));
188 xbt_dynar_push_as(host.speed_per_pstate, double, surf_parse_get_speed(lua_tostring(L, -1), "speed of host", host.id));
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_file");
205 const char *filename = lua_tostring(L, -1);
207 host.speed_trace = tmgr_trace_new_from_file(filename);
211 lua_pushstring(L, "state_file");
213 filename = lua_tostring(L, -1);
215 host.state_trace = tmgr_trace_new_from_file(filename);
218 sg_platf_new_host(&host);
219 xbt_dynar_free(&host.speed_per_pstate);
224 int console_add_link(lua_State *L) {
225 s_sg_platf_link_cbarg_t link;
226 memset(&link,0,sizeof(link));
231 if (! lua_istable(L, -1)) {
232 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
237 lua_pushstring(L, "id");
238 type = lua_gettable(L, -2);
239 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
240 XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
242 link.id = lua_tostring(L, -1);
245 // get bandwidth value
246 lua_pushstring(L, "bandwidth");
247 type = lua_gettable(L, -2);
248 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
249 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.");
251 if (type == LUA_TNUMBER)
252 link.bandwidth = lua_tonumber(L, -1);
254 link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1),"bandwidth of link", link.id);
258 lua_pushstring(L, "lat");
259 type = lua_gettable(L, -2);
260 if (type != LUA_TSTRING && type != LUA_TNUMBER) {
261 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.");
263 if (type == LUA_TNUMBER)
264 link.latency = lua_tonumber(L, -1);
266 link.latency = surf_parse_get_time(lua_tostring(L, -1),"latency of link", link.id);
269 /*Optional Arguments */
271 //get bandwidth_trace value
272 lua_pushstring(L, "bandwidth_file");
274 const char *filename = lua_tostring(L, -1);
276 link.bandwidth_trace = tmgr_trace_new_from_file(filename);
279 //get latency_trace value
280 lua_pushstring(L, "latency_file");
282 filename = lua_tostring(L, -1);
284 link.latency_trace = tmgr_trace_new_from_file(filename);
287 //get state_trace value
288 lua_pushstring(L, "state_file");
290 filename = lua_tostring(L, -1);
292 link.state_trace = tmgr_trace_new_from_file(filename);
296 lua_pushstring(L, "sharing_policy");
298 policy = lua_tostring(L, -1);
300 if (policy && !strcmp(policy,"FULLDUPLEX")) {
301 link.policy = SURF_LINK_FULLDUPLEX;
302 } else if (policy && !strcmp(policy,"FATPIPE")) {
303 link.policy = SURF_LINK_FATPIPE;
305 link.policy = SURF_LINK_SHARED;
308 sg_platf_new_link(&link);
313 * add Router to AS components
315 int console_add_router(lua_State* L) {
316 s_sg_platf_router_cbarg_t router;
317 memset(&router,0,sizeof(router));
320 if (! lua_istable(L, -1)) {
321 XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
325 lua_pushstring(L, "id");
326 type = lua_gettable(L, -2);
327 if (type != LUA_TSTRING) {
328 XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
330 router.id = lua_tostring(L, -1);
333 lua_pushstring(L,"coord");
335 router.coord = lua_tostring(L, -1);
338 sg_platf_new_router(&router);
343 int console_add_route(lua_State *L) {
344 XBT_DEBUG("Adding route");
345 s_sg_platf_route_cbarg_t route;
346 memset(&route,0,sizeof(route));
349 /* allocating memory for the buffer, I think 2kB should be enough */
350 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
352 if (! lua_istable(L, -1)) {
353 XBT_ERROR("Bad Arguments to add a route. Should be a table with named arguments");
357 lua_pushstring(L,"src");
358 type = lua_gettable(L,-2);
359 if (type != LUA_TSTRING) {
360 XBT_ERROR("Attribute 'src' must be specified for any route and must be a string.");
362 route.src = lua_tostring(L, -1);
365 lua_pushstring(L,"dest");
366 type = lua_gettable(L,-2);
367 if (type != LUA_TSTRING) {
368 XBT_ERROR("Attribute 'dest' must be specified for any route and must be a string.");
370 route.dst = lua_tostring(L, -1);
373 lua_pushstring(L,"links");
374 type = lua_gettable(L,-2);
375 if (type != LUA_TSTRING) {
376 XBT_ERROR("Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
378 route.link_list = new std::vector<Link*>();
379 xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
380 if (xbt_dynar_is_empty(names)) {
382 route.link_list->push_back(Link::byName(lua_tostring(L, -1)));
384 // Several names separated by , \t\r\n
387 xbt_dynar_foreach(names, cpt, name) {
388 if (strlen(name)>0) {
389 Link *link = Link::byName(name);
390 route.link_list->push_back(link);
396 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
397 * Et ouais mon pote. That's the way it goes. F34R.
399 * (Note that above this function, there is a #include statement. Is this
400 * comment related to that statement?)
402 lua_pushstring(L,"symmetrical");
404 if (lua_isstring(L, -1)) {
405 const char* value = lua_tostring(L, -1);
406 if (strcmp("YES", value) == 0) {
407 route.symmetrical = true;
410 route.symmetrical = false;
413 route.symmetrical = true;
420 sg_platf_new_route(&route);
425 int console_add_ASroute(lua_State *L) {
426 s_sg_platf_route_cbarg_t ASroute;
427 memset(&ASroute,0,sizeof(ASroute));
429 lua_pushstring(L, "src");
431 ASroute.src = lua_tostring(L, -1);
434 lua_pushstring(L, "dst");
436 ASroute.dst = lua_tostring(L, -1);
439 lua_pushstring(L, "gw_src");
441 const char *name = lua_tostring(L, -1);
442 ASroute.gw_src = sg_netcard_by_name_or_null(name);
443 if (ASroute.gw_src == NULL) {
444 XBT_ERROR("Attribute 'gw_src' of AS route does not name a valid machine: %s", name);
449 lua_pushstring(L, "gw_dst");
451 name = lua_tostring(L, -1);
452 ASroute.gw_dst = sg_netcard_by_name_or_null(name);
453 if (ASroute.gw_dst == NULL) {
454 XBT_ERROR("Attribute 'gw_dst' of AS route does not name a valid machine: %s", name);
459 lua_pushstring(L,"links");
461 ASroute.link_list = new std::vector<Link*>();
462 xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
463 if (xbt_dynar_is_empty(names)) {
465 ASroute.link_list->push_back(Link::byName(lua_tostring(L, -1)));
467 // Several names separated by , \t\r\n
470 xbt_dynar_foreach(names, cpt, name) {
471 if (strlen(name)>0) {
472 Link *link = Link::byName(name);
473 ASroute.link_list->push_back(link);
479 lua_pushstring(L,"symmetrical");
481 if (lua_isstring(L, -1)) {
482 const char* value = lua_tostring(L, -1);
483 if (strcmp("YES", value) == 0)
484 ASroute.symmetrical = true;
486 ASroute.symmetrical = false;
489 ASroute.symmetrical = true;
493 sg_platf_new_route(&ASroute);
498 int console_AS_open(lua_State *L) {
503 XBT_DEBUG("Opening AS");
505 if (! lua_istable(L, 1)) {
506 XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
510 lua_pushstring(L, "id");
511 type = lua_gettable(L, -2);
512 if (type != LUA_TSTRING) {
513 XBT_ERROR("Attribute 'id' must be specified for any AS and must be a string.");
515 id = lua_tostring(L, -1);
518 lua_pushstring(L, "mode");
520 mode = lua_tostring(L, -1);
523 int mode_int = A_surfxml_AS_routing_None;
524 if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
525 else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
526 else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
527 else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
528 else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
529 else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
530 else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
531 else xbt_die("Don't have the model name '%s'",mode);
533 s_sg_platf_AS_cbarg_t AS;
535 AS.routing = mode_int;
536 sg_platf_new_AS_begin(&AS);
540 int console_AS_seal(lua_State *L) {
541 XBT_DEBUG("Sealing AS");
542 sg_platf_new_AS_seal();
546 int console_host_set_property(lua_State *L) {
547 const char* name ="";
548 const char* prop_id = "";
549 const char* prop_value = "";
550 if (!lua_istable(L, -1)) {
551 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
557 lua_pushstring(L, "host");
559 name = lua_tostring(L, -1);
563 lua_pushstring(L, "prop");
565 prop_id = lua_tostring(L, -1);
568 lua_pushstring(L,"value");
570 prop_value = lua_tostring(L,-1);
573 sg_host_t host = sg_host_by_name(name);
575 XBT_ERROR("no host '%s' found",name);
578 xbt_dict_t props = sg_host_get_properties(host);
579 xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
585 * \brief Registers the platform functions into the table simgrid.platf.
586 * \param L a lua state
588 void sglua_register_platf_functions(lua_State* L)
590 lua_getglobal(L, "simgrid"); /* simgrid */
591 luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
592 lua_setfield(L, -2, "platf"); /* simgrid */
594 lua_pop(L, 1); /* -- */
597 //void sglua_register_routing_constants(lua_State* L)
599 // lua_getglobal(L, "simgrid"); /* simgrid */
600 // lua_newtable(L); /* simgrid simgrid.routing */
602 // lua_pushstring(L, "Cluster"); /* simgrid simgrid.routing Cluster */
603 // lua_setfield(L, -2, "CLUSTER"); /* simgrid simgrid.routing */
605 // lua_pushstring(L, "Dijkstra"); /* simgrid simgrid.routing Dijkstra */
606 // lua_setfield(L, -2, "DIJKSTRA"); /* simgrid simgrid.routing */
608 // lua_pushstring(L, "DijkstraCache"); /* simgrid simgrid.routing DijkstraCache */
609 // lua_setfield(L, -2, "DIJKSTRA_CACHE"); /* simgrid simgrid.routing */
611 // lua_pushstring(L, "Floyd"); /* simgrid simgrid.routing Floyd */
612 // lua_setfield(L, -2, "FLOYD"); /* simgrid simgrid.routing */
614 // lua_pushstring(L, "Full"); /* simgrid simgrid.routing Full */
615 // lua_setfield(L, -2, "FULL"); /* simgrid simgrid.routing */
617 // lua_pushstring(L, "None"); /* simgrid simgrid.routing None */
618 // lua_setfield(L, -2, "NONE"); /* simgrid simgrid.routing */
620 // lua_pushstring(L, "Vivaldi"); /* simgrid simgrid.routing Vivaldi */
621 // lua_setfield(L, -2, "FULL"); /* simgrid simgrid.routing */
623 // lua_setfield(L, -2, "routing"); /* simgrid */
625 // lua_pop(L, 1); /* -- */