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());
169 * Append new AS to the Platform
172 static int AS_new(lua_State * L)
175 if (xbt_dynar_is_empty(as_list_d))
176 as_list_d = xbt_dynar_new(sizeof(p_AS_attr), &xbt_free_ref);
181 if (lua_istable(L, 1)) {
182 lua_pushstring(L, "id");
184 id = lua_tostring(L, -1);
187 lua_pushstring(L, "mode");
189 mode = lua_tostring(L, -1);
193 ("Bad Arguments to AS.new, Should be a table with named arguments");
196 AS = malloc(sizeof(AS_attr));
199 AS->host_list_d = xbt_dynar_new(sizeof(p_host_attr),&xbt_free_ref);
200 AS->link_list_d = xbt_dynar_new(sizeof(p_link_attr),&xbt_free_ref);
201 AS->route_list_d = xbt_dynar_new(sizeof(p_route_attr),&xbt_free_ref);
202 AS->router_list_d = xbt_dynar_new(sizeof(p_router_attr),&xbt_free_ref);
203 AS->sub_as_list_id = xbt_dynar_new(sizeof(p_AS_attr),&xbt_free_ref);
204 xbt_dynar_push(as_list_d, &AS);
210 * add sub AS to the Parent AS
212 static int AS_add(lua_State *L)
216 p_AS_attr super_as,p_as;
217 const char *super_AS_id;
218 const char *sub_AS_id = NULL;
219 const char *sub_AS_routing= NULL;
220 if(lua_istable(L, -1))
222 lua_pushstring(L, "AS");
224 super_AS_id = lua_tostring(L, -1);
227 lua_pushstring(L, "id");
229 sub_AS_id = lua_tostring(L, -1);
234 xbt_dynar_foreach(as_list_d, i, p_as){
235 if (p_as->id == super_AS_id){
240 AS = malloc(sizeof(AS_attr));
242 AS->mode = sub_AS_routing;
243 AS->host_list_d = xbt_dynar_new(sizeof(p_host_attr),&xbt_free_ref);
244 AS->link_list_d = xbt_dynar_new(sizeof(p_link_attr),&xbt_free_ref);
245 AS->route_list_d = xbt_dynar_new(sizeof(p_route_attr),&xbt_free_ref);
246 AS->sub_as_list_id = xbt_dynar_new(sizeof(p_AS_attr),&xbt_free_ref);
247 xbt_dynar_push(super_as->sub_as_list_id, &AS);
254 * add new host to platform hosts list
256 static int Host_new(lua_State * L)
260 p_AS_attr p_as,current_as = NULL;
263 const char *power_trace;
264 const char *state_trace;
265 double power, power_scale;
266 int state_initial,core;
267 //get values from the table passed as argument
268 if (lua_istable(L, -1)) {
270 lua_pushstring(L, "AS");
272 AS_id = lua_tostring(L, -1);
276 lua_pushstring(L, "id");
278 id = lua_tostring(L, -1);
282 lua_pushstring(L, "power");
284 power = lua_tonumber(L, -1);
288 lua_pushstring(L, "power_scale");
290 power_scale = lua_tonumber(L, -1);
294 lua_pushstring(L, "power_trace");
296 power_trace = lua_tostring(L, -1);
299 lua_pushstring(L, "core");
301 core = lua_tonumber(L, -1);
305 lua_pushstring(L, "state_initial");
307 state_initial = lua_tonumber(L, -1);
311 lua_pushstring(L, "state_trace");
313 state_trace = lua_tostring(L, -1);
318 ("Bad Arguments to create host, Should be a table with named arguments");
321 xbt_dynar_foreach(as_list_d, i, p_as){
322 if (p_as->id == AS_id){
330 XBT_ERROR("No AS_id :%s found",AS_id);
334 host = malloc(sizeof(host_attr));
336 host->power_peak = power;
337 host->power_scale = power_scale;
338 host->power_trace = power_trace;
340 host->state_initial = state_initial;
341 host->state_trace = state_trace;
342 host->function = NULL;
343 host->properties = xbt_dict_new();
344 xbt_dynar_push(current_as->host_list_d, &host);
350 * add link to AS links list
352 static int Link_new(lua_State * L) // (id,bandwidth,latency)
356 p_AS_attr p_as,current_as = NULL;
358 double bandwidth, latency;
359 const char *bandwidth_trace;
360 const char *latency_trace;
361 const char *state_trace;
362 int state_initial, policy;
364 //get values from the table passed as argument
365 if (lua_istable(L, -1)) {
368 lua_pushstring(L, "AS");
370 AS_id = lua_tostring(L, -1);
374 lua_pushstring(L, "id");
376 id = lua_tostring(L, -1);
379 // get bandwidth value
380 lua_pushstring(L, "bandwidth");
382 bandwidth = lua_tonumber(L, -1);
386 lua_pushstring(L, "latency");
388 latency = lua_tonumber(L, -1);
391 /*Optional Arguments */
393 //get bandwidth_trace value
394 lua_pushstring(L, "bandwidth_trace");
396 bandwidth_trace = lua_tostring(L, -1);
399 //get latency_trace value
400 lua_pushstring(L, "latency_trace");
402 latency_trace = lua_tostring(L, -1);
405 //get state_trace value
406 lua_pushstring(L, "state_trace");
408 state_trace = lua_tostring(L, -1);
411 //get state_initial value
412 lua_pushstring(L, "state_initial");
414 state_initial = lua_tonumber(L, -1);
418 lua_pushstring(L, "policy");
420 policy = lua_tonumber(L, -1);
425 ("Bad Arguments to create link, Should be a table with named arguments");
428 xbt_dynar_foreach(as_list_d, i, p_as){
429 if (p_as->id == AS_id){
437 XBT_ERROR("No AS_id :%s found",AS_id);
440 p_link_attr link = malloc(sizeof(link_attr));
442 link->bandwidth = bandwidth;
443 link->latency = latency;
444 link->bandwidth_trace = bandwidth_trace;
445 link->latency_trace = latency_trace;
446 link->state_trace = state_trace;
447 link->state_initial = state_initial;
448 link->policy = policy;
449 xbt_dynar_push(current_as->link_list_d, &link);
455 * add route to AS routes list
457 static int Route_new(lua_State * L) // (src_id,dest_id,links_number,link_table)
461 p_AS_attr p_as,current_as = NULL;
464 p_route_attr route = malloc(sizeof(route_attr));
467 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)
470 lua_pushstring(L, "AS");
472 AS_id = lua_tostring(L, -1);
475 xbt_dynar_foreach(as_list_d, i, p_as){
476 if (p_as->id == AS_id){
484 XBT_ERROR("addRoute: No AS_id :%s found",AS_id);
488 lua_pushstring(L, "src");
490 route->src_id = lua_tostring(L, -1);
493 // get Destination Value
494 lua_pushstring(L, "dest");
496 route->dest_id = lua_tostring(L, -1);
499 // get Links Table (char* to be splited later)
500 lua_pushstring(L, "links");
502 links = lua_tostring(L, -1);
505 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
507 char *tmp_links = xbt_strdup(links);
508 link_id = strtok(tmp_links,","); //tmp_link = strtok((char*)links,",");
509 while(link_id != NULL)
511 xbt_dynar_push(route->links_id, &link_id);
512 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.
514 xbt_dynar_push(current_as->route_list_d, &route);
517 else { // Route.new is declared as a function
518 AS_id = luaL_checkstring(L, 1);
519 xbt_dynar_foreach(as_list_d, i, p_as){
520 if (p_as->id == AS_id){
528 XBT_ERROR("addRoute: No AS_id :%s found",AS_id);
531 route->src_id = luaL_checkstring(L, 2);
532 route->dest_id = luaL_checkstring(L, 3);
533 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
535 while (lua_next(L, 4) != 0)
537 link_id = lua_tostring(L, -1);
538 xbt_dynar_push(route->links_id, &link_id);
539 XBT_DEBUG("index = %f , Link_id = %s \n", lua_tonumber(L, -2),
540 lua_tostring(L, -1));
544 //add route to platform's route list
545 xbt_dynar_push(current_as->route_list_d, &route);
552 * add Router to AS components
554 static int Router_new(lua_State* L)
556 p_router_attr router;
559 p_AS_attr p_as,current_as = NULL;
561 if (lua_istable(L, -1)) {
563 lua_pushstring(L, "AS");
565 AS_id = lua_tostring(L, -1);
568 lua_pushstring(L, "id");
570 id = lua_tostring(L, -1);
573 xbt_dynar_foreach(as_list_d, i, p_as){
574 if (p_as->id == AS_id){
582 XBT_ERROR("No AS_id :%s found",AS_id);
585 router = malloc(sizeof(router_attr));
587 xbt_dynar_push(current_as->router_list_d, &router);
593 * set function to process
595 static int Host_set_function(lua_State * L) //(host,function,nb_args,list_args)
600 const char *host_id ;
601 const char *function_id;
605 if (lua_istable(L, -1)) {
607 lua_pushstring(L, "host");
609 host_id = lua_tostring(L, -1);
612 lua_pushstring(L, "fct");
614 function_id = lua_tostring(L, -1);
617 lua_pushstring(L,"args");
619 args = lua_tostring(L,-1);
623 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
627 // look for the index of host in host_list for each AS
628 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 p_host->function = function_id;
633 p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
634 // split & fill the args list
635 tmp_arg = strtok((char*)args,",");
636 while (tmp_arg != NULL) {
637 xbt_dynar_push(p_host->args_list, &tmp_arg);
638 tmp_arg = strtok(NULL,",");
644 XBT_ERROR("Host : %s Not Found !!", host_id);
649 static int Host_set_property(lua_State* L)
654 const char* host_id ="";
655 const char* prop_id = "";
656 const char* prop_value = "";
657 if (lua_istable(L, -1)) {
659 lua_pushstring(L, "host");
661 host_id = lua_tostring(L, -1);
664 lua_pushstring(L, "prop_id");
666 prop_id = lua_tostring(L, -1);
669 lua_pushstring(L,"prop_value");
671 prop_value = lua_tostring(L,-1);
674 xbt_dynar_foreach(as_list_d, i, p_as)
676 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
677 if (p_host->id == host_id) {
678 xbt_dict_set(p_host->properties, prop_id, xbt_strdup(prop_value), free);
686 * surf parse bypass platform
687 * through CPU/network Models
690 static int surf_parse_bypass_platform()
696 p_route_attr p_route;
700 xbt_dynar_foreach(as_list_d, i,p_as)
702 sg_platf_new_AS_open(p_as->id,p_as->mode);
703 // add associated Hosts
704 xbt_dynar_foreach(p_as->host_list_d, j, p_host){
705 create_host(p_host->id, p_host->power_peak, p_host->power_scale,
706 p_host->power_trace, p_host->core, p_host->state_initial,
707 p_host->state_trace, p_host->properties);
710 //add to routing model host list
711 surf_route_add_host((char *) p_host->id);
713 // add associated Links
714 xbt_dynar_foreach(p_as->link_list_d, j, p_link){
715 create_link(p_link->id, p_link->bandwidth, p_link->bandwidth_trace,
716 p_link->latency, p_link->latency_trace,
717 p_link->state_initial, p_link->state_trace,
720 // add associated Routes
721 xbt_dynar_foreach(p_as->route_list_d, j, p_route){
722 surf_routing_add_route((char *) p_route->src_id,
723 (char *) p_route->dest_id, p_route->links_id);
727 sg_platf_new_AS_close();
731 surf_add_host_traces();
732 surf_add_link_traces();
734 return 0; // must return 0 ?!!
740 * surf parse bypass platform
741 * through workstation_ptask_L07 Model
744 static int surf_wsL07_parse_bypass_platform()
747 p_AS_attr p_as, p_sub_as;
750 p_route_attr p_route;
752 xbt_dynar_foreach(as_list_d, i, p_as)
755 sg_platf_new_AS_open(p_as->id,p_as->mode);
760 xbt_dynar_foreach(p_as->sub_as_list_id, j, p_sub_as) {
763 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
764 create_host_wsL07(p_host->id, p_host->power_peak, p_host->power_scale,
765 p_host->power_trace, p_host->state_initial,
766 p_host->state_trace);
767 //add to routing model host list
768 surf_route_add_host((char *) p_host->id);
771 xbt_dynar_foreach(p_as->link_list_d, j, p_link) {
772 create_link_wsL07(p_link->id, p_link->bandwidth,
773 p_link->bandwidth_trace, p_link->latency,
774 p_link->latency_trace, p_link->state_initial,
775 p_link->state_trace, p_link->policy);
778 xbt_dynar_foreach(p_as->route_list_d, j, p_route) {
779 surf_routing_add_route((char *) p_route->src_id,
780 (char *) p_route->dest_id, p_route->links_id);
784 sg_platf_new_AS_close();
787 surf_wsL07_add_traces();
792 * surf parse bypass application for MSG Module
794 static int surf_parse_bypass_application()
799 xbt_dynar_foreach(as_list_d, i, p_as)
801 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
802 if (p_host->function)
803 MSG_set_function(p_host->id, p_host->function, p_host->args_list);
812 int console_add_host(lua_State *L)
817 int console_add_link(lua_State *L)
822 int console_add_route(lua_State *L)
827 int console_add_AS(lua_State *L)
832 int console_set_function(lua_State *L)
834 return Host_set_function(L);
837 int console_host_set_property(lua_State *L)
839 return Host_set_property(L);
842 int console_parse_platform()
844 return surf_parse_bypass_platform();
847 int console_parse_application()
849 return surf_parse_bypass_application();
852 int console_parse_platform_wsL07()
854 return surf_wsL07_parse_bypass_platform();