1 /* SimGrid Lua Console */
3 /* Copyright (c) 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "simgrid_lua.h"
10 #include "simgrid/platf.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_console, bindings, "Lua Bindings");
18 static xbt_dynar_t as_list_d;
21 * create host resource via CPU model [for MSG]
24 static void create_host(const char *id, double power_peak, double power_sc,
25 const char *power_tr,int core,int state_init,
26 const char *state_tr,xbt_dict_t properties)
28 double power_scale = 1.0;
29 int core_nb = 1; //default value
30 tmgr_trace_t power_trace = NULL;
31 e_surf_resource_state_t state_initial;
32 tmgr_trace_t state_trace;
34 power_scale = power_sc;
36 core_nb = core; //default value
38 state_initial = SURF_RESOURCE_OFF;
40 state_initial = SURF_RESOURCE_ON;
42 power_trace = tmgr_trace_new(power_tr);
44 power_trace = tmgr_trace_new("");
46 state_trace = tmgr_trace_new(state_tr);
48 state_trace = tmgr_trace_new("");
50 surf_host_create_resource(xbt_strdup(id), power_peak, power_scale,
51 power_trace, core_nb, state_initial, state_trace,
56 * create link resource via network model
58 static void create_link(const char *name,
59 double bw_initial, const char *trace,
60 double lat_initial, const char *latency_trace,
61 int state_init, const char *state_trace,
64 tmgr_trace_t bw_trace;
65 tmgr_trace_t lat_trace;
66 e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
67 e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
68 tmgr_trace_t st_trace;
70 bw_trace = tmgr_trace_new(trace);
72 bw_trace = tmgr_trace_new("");
75 lat_trace = tmgr_trace_new(latency_trace);
77 lat_trace = tmgr_trace_new("");
80 st_trace = tmgr_trace_new(state_trace);
82 st_trace = tmgr_trace_new("");
85 state_initial_link = SURF_RESOURCE_OFF;
87 policy_initial_link = SURF_LINK_FATPIPE;
89 surf_link_create_resource(xbt_strdup(name), bw_initial, bw_trace,
90 lat_initial, lat_trace, state_initial_link,
91 st_trace, policy_initial_link, xbt_dict_new());
95 *create host resource via workstation_ptask_L07 model [for SimDag]
97 static void create_host_wsL07(const char *id, double power_peak,
98 double power_sc, const char *power_tr,
99 int state_init, const char *state_tr)
101 double power_scale = 1.0;
102 tmgr_trace_t power_trace = NULL;
103 e_surf_resource_state_t state_initial;
104 tmgr_trace_t state_trace;
106 power_scale = power_sc;
107 if (state_init == -1)
108 state_initial = SURF_RESOURCE_OFF;
110 state_initial = SURF_RESOURCE_ON;
112 power_trace = tmgr_trace_new(power_tr);
114 power_trace = tmgr_trace_new("");
116 state_trace = tmgr_trace_new(state_tr);
118 state_trace = tmgr_trace_new("");
120 surf_wsL07_host_create_resource(xbt_strdup(id), power_peak, power_scale,
121 power_trace, state_initial, state_trace,
122 current_property_set);
123 current_property_set = NULL;
127 * create link resource via workstation_ptask_L07 model [for SimDag]
130 static void create_link_wsL07(const char *name,
131 double bw_initial, const char *trace,
133 const char *latency_trace, int state_init,
134 const char *state_trace, int policy)
136 tmgr_trace_t bw_trace;
137 tmgr_trace_t lat_trace;
138 e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
139 e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
140 tmgr_trace_t st_trace;
142 bw_trace = tmgr_trace_new(trace);
144 bw_trace = tmgr_trace_new("");
147 lat_trace = tmgr_trace_new(latency_trace);
149 lat_trace = tmgr_trace_new("");
152 st_trace = tmgr_trace_new(state_trace);
154 st_trace = tmgr_trace_new("");
156 if (state_init == -1)
157 state_initial_link = SURF_RESOURCE_OFF;
159 policy_initial_link = SURF_LINK_FATPIPE;
161 surf_wsL07_link_create_resource(xbt_strdup(name), bw_initial, bw_trace,
162 lat_initial, lat_trace,
163 state_initial_link, st_trace,
164 policy_initial_link, xbt_dict_new());
168 * surf parse bypass application for MSG Module
170 static int surf_parse_bypass_application()
175 xbt_dynar_foreach(as_list_d, i, p_as)
177 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
178 if (p_host->function)
179 MSG_set_function(p_host->id, p_host->function, p_host->args_list);
188 int console_add_host(lua_State *L)
192 p_AS_attr p_as,current_as = NULL;
195 const char *power_trace;
196 const char *state_trace;
197 double power, power_scale;
198 int state_initial,core;
199 //get values from the table passed as argument
200 if (lua_istable(L, -1)) {
202 lua_pushstring(L, "AS");
204 AS_id = lua_tostring(L, -1);
208 lua_pushstring(L, "id");
210 id = lua_tostring(L, -1);
214 lua_pushstring(L, "power");
216 power = lua_tonumber(L, -1);
220 lua_pushstring(L, "power_scale");
222 power_scale = lua_tonumber(L, -1);
226 lua_pushstring(L, "power_trace");
228 power_trace = lua_tostring(L, -1);
231 lua_pushstring(L, "core");
233 core = lua_tonumber(L, -1);
237 lua_pushstring(L, "state_initial");
239 state_initial = lua_tonumber(L, -1);
243 lua_pushstring(L, "state_trace");
245 state_trace = lua_tostring(L, -1);
250 ("Bad Arguments to create host, Should be a table with named arguments");
253 xbt_dynar_foreach(as_list_d, i, p_as){
254 if (p_as->id == AS_id){
262 XBT_ERROR("No AS_id :%s found",AS_id);
266 host = malloc(sizeof(host_attr));
268 host->power_peak = power;
269 host->power_scale = power_scale;
270 host->power_trace = power_trace;
272 host->state_initial = state_initial;
273 host->state_trace = state_trace;
274 host->function = NULL;
275 host->properties = xbt_dict_new();
276 xbt_dynar_push(current_as->host_list_d, &host);
281 int console_add_link(lua_State *L) {
284 p_AS_attr p_as,current_as = NULL;
286 double bandwidth, latency;
287 const char *bandwidth_trace;
288 const char *latency_trace;
289 const char *state_trace;
290 int state_initial, policy;
292 //get values from the table passed as argument
293 if (lua_istable(L, -1)) {
296 lua_pushstring(L, "AS");
298 AS_id = lua_tostring(L, -1);
302 lua_pushstring(L, "id");
304 id = lua_tostring(L, -1);
307 // get bandwidth value
308 lua_pushstring(L, "bandwidth");
310 bandwidth = lua_tonumber(L, -1);
314 lua_pushstring(L, "latency");
316 latency = lua_tonumber(L, -1);
319 /*Optional Arguments */
321 //get bandwidth_trace value
322 lua_pushstring(L, "bandwidth_trace");
324 bandwidth_trace = lua_tostring(L, -1);
327 //get latency_trace value
328 lua_pushstring(L, "latency_trace");
330 latency_trace = lua_tostring(L, -1);
333 //get state_trace value
334 lua_pushstring(L, "state_trace");
336 state_trace = lua_tostring(L, -1);
339 //get state_initial value
340 lua_pushstring(L, "state_initial");
342 state_initial = lua_tonumber(L, -1);
346 lua_pushstring(L, "policy");
348 policy = lua_tonumber(L, -1);
353 ("Bad Arguments to create link, Should be a table with named arguments");
356 xbt_dynar_foreach(as_list_d, i, p_as){
357 if (p_as->id == AS_id){
365 XBT_ERROR("No AS_id :%s found",AS_id);
368 p_link_attr link = malloc(sizeof(link_attr));
370 link->bandwidth = bandwidth;
371 link->latency = latency;
372 link->bandwidth_trace = bandwidth_trace;
373 link->latency_trace = latency_trace;
374 link->state_trace = state_trace;
375 link->state_initial = state_initial;
376 link->policy = policy;
377 xbt_dynar_push(current_as->link_list_d, &link);
382 * add Router to AS components
384 int console_add_router(lua_State* L) {
385 p_router_attr router;
388 p_AS_attr p_as,current_as = NULL;
390 if (lua_istable(L, -1)) {
392 lua_pushstring(L, "AS");
394 AS_id = lua_tostring(L, -1);
397 lua_pushstring(L, "id");
399 id = lua_tostring(L, -1);
402 xbt_dynar_foreach(as_list_d, i, p_as){
403 if (p_as->id == AS_id){
411 XBT_ERROR("No AS_id '%s' found",AS_id);
414 router = malloc(sizeof(router_attr));
416 xbt_dynar_push(current_as->router_list_d, &router);
422 int console_add_route(lua_State *L)
426 p_AS_attr p_as,current_as = NULL;
429 p_route_attr route = malloc(sizeof(route_attr));
432 if (!lua_istable(L, 4)) { // if Route.new is declared as an indexed table (FIXME : we check the third arg if it's not a table)
435 lua_pushstring(L, "AS");
437 AS_id = lua_tostring(L, -1);
440 xbt_dynar_foreach(as_list_d, i, p_as){
441 if (p_as->id == AS_id){
449 XBT_ERROR("addRoute: No AS_id :%s found",AS_id);
453 lua_pushstring(L, "src");
455 route->src_id = lua_tostring(L, -1);
458 // get Destination Value
459 lua_pushstring(L, "dest");
461 route->dest_id = lua_tostring(L, -1);
464 // get Links Table (char* to be splited later)
465 lua_pushstring(L, "links");
467 links = lua_tostring(L, -1);
470 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
472 char *tmp_links = xbt_strdup(links);
473 link_id = strtok(tmp_links,","); //tmp_link = strtok((char*)links,",");
474 while(link_id != NULL)
476 xbt_dynar_push(route->links_id, &link_id);
477 link_id = strtok(NULL,","); //Alternatively, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.
479 xbt_dynar_push(current_as->route_list_d, &route);
482 else { // Route.new is declared as a function
483 AS_id = luaL_checkstring(L, 1);
484 xbt_dynar_foreach(as_list_d, i, p_as){
485 if (p_as->id == AS_id){
493 XBT_ERROR("addRoute: No AS_id :%s found",AS_id);
496 route->src_id = luaL_checkstring(L, 2);
497 route->dest_id = luaL_checkstring(L, 3);
498 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
500 while (lua_next(L, 4) != 0)
502 link_id = lua_tostring(L, -1);
503 xbt_dynar_push(route->links_id, &link_id);
504 XBT_DEBUG("index = %f , Link_id = %s \n", lua_tonumber(L, -2),
505 lua_tostring(L, -1));
509 //add route to platform's route list
510 xbt_dynar_push(current_as->route_list_d, &route);
517 int console_add_AS(lua_State *L)
519 if (xbt_dynar_is_empty(as_list_d))
520 as_list_d = xbt_dynar_new(sizeof(p_AS_attr), &xbt_free_ref);
525 if (lua_istable(L, 1)) {
526 lua_pushstring(L, "id");
528 id = lua_tostring(L, -1);
531 lua_pushstring(L, "mode");
533 mode = lua_tostring(L, -1);
537 ("Bad Arguments to AS.new, Should be a table with named arguments");
540 AS = malloc(sizeof(AS_attr));
543 AS->host_list_d = xbt_dynar_new(sizeof(p_host_attr),&xbt_free_ref);
544 AS->link_list_d = xbt_dynar_new(sizeof(p_link_attr),&xbt_free_ref);
545 AS->route_list_d = xbt_dynar_new(sizeof(p_route_attr),&xbt_free_ref);
546 AS->router_list_d = xbt_dynar_new(sizeof(p_router_attr),&xbt_free_ref);
547 AS->sub_as_list_id = xbt_dynar_new(sizeof(p_AS_attr),&xbt_free_ref);
548 xbt_dynar_push(as_list_d, &AS);
553 int console_set_function(lua_State *L) {
557 const char *host_id ;
558 const char *function_id;
562 if (lua_istable(L, -1)) {
564 lua_pushstring(L, "host");
566 host_id = lua_tostring(L, -1);
569 lua_pushstring(L, "fct");
571 function_id = lua_tostring(L, -1);
574 lua_pushstring(L,"args");
576 args = lua_tostring(L,-1);
580 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
584 // look for the index of host in host_list for each AS
585 xbt_dynar_foreach(as_list_d, i, p_as)
587 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
588 if (p_host->id == host_id) {
589 p_host->function = function_id;
590 p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
591 // split & fill the args list
592 tmp_arg = strtok((char*)args,",");
593 while (tmp_arg != NULL) {
594 xbt_dynar_push(p_host->args_list, &tmp_arg);
595 tmp_arg = strtok(NULL,",");
601 XBT_ERROR("Host : %s Not Found !!", host_id);
605 int console_host_set_property(lua_State *L) {
609 const char* host_id ="";
610 const char* prop_id = "";
611 const char* prop_value = "";
612 if (lua_istable(L, -1)) {
614 lua_pushstring(L, "host");
616 host_id = lua_tostring(L, -1);
619 lua_pushstring(L, "prop_id");
621 prop_id = lua_tostring(L, -1);
624 lua_pushstring(L,"prop_value");
626 prop_value = lua_tostring(L,-1);
629 xbt_dynar_foreach(as_list_d, i, p_as) {
630 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
631 if (p_host->id == host_id) {
632 xbt_dict_set(p_host->properties, prop_id, xbt_strdup(prop_value), free);
639 int console_parse_platform() {
644 p_route_attr p_route;
648 xbt_dynar_foreach(as_list_d, i,p_as)
650 sg_platf_new_AS_open(p_as->id,p_as->mode);
651 // add associated Hosts
652 xbt_dynar_foreach(p_as->host_list_d, j, p_host){
653 create_host(p_host->id, p_host->power_peak, p_host->power_scale,
654 p_host->power_trace, p_host->core, p_host->state_initial,
655 p_host->state_trace, p_host->properties);
658 //FIXME: should use sg_platf instead. That would add to routing model host list, amongst other benefits
660 // add associated Links
661 xbt_dynar_foreach(p_as->link_list_d, j, p_link){
662 create_link(p_link->id, p_link->bandwidth, p_link->bandwidth_trace,
663 p_link->latency, p_link->latency_trace,
664 p_link->state_initial, p_link->state_trace,
667 // add associated Routes
668 xbt_dynar_foreach(p_as->route_list_d, j, p_route){
669 surf_routing_add_route((char *) p_route->src_id,
670 (char *) p_route->dest_id, p_route->links_id);
674 sg_platf_new_AS_close();
678 surf_add_host_traces();
679 surf_add_link_traces();
681 return 0; // must return 0 ?!!
684 int console_parse_application()
686 return surf_parse_bypass_application();
689 int console_parse_platform_wsL07()
692 p_AS_attr p_as, p_sub_as;
695 p_route_attr p_route;
697 xbt_dynar_foreach(as_list_d, i, p_as)
700 sg_platf_new_AS_open(p_as->id,p_as->mode);
705 xbt_dynar_foreach(p_as->sub_as_list_id, j, p_sub_as) {
708 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
709 create_host_wsL07(p_host->id, p_host->power_peak, p_host->power_scale,
710 p_host->power_trace, p_host->state_initial,
711 p_host->state_trace);
712 //add to routing model host list
715 xbt_dynar_foreach(p_as->link_list_d, j, p_link) {
716 create_link_wsL07(p_link->id, p_link->bandwidth,
717 p_link->bandwidth_trace, p_link->latency,
718 p_link->latency_trace, p_link->state_initial,
719 p_link->state_trace, p_link->policy);
722 xbt_dynar_foreach(p_as->route_list_d, j, p_route) {
723 surf_routing_add_route((char *) p_route->src_id,
724 (char *) p_route->dest_id, p_route->links_id);
728 sg_platf_new_AS_close();
731 surf_wsL07_add_traces();