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);
253 * set function to process
255 static int Host_set_function(lua_State * L) //(host,function,nb_args,list_args)
260 const char *host_id ;
261 const char *function_id;
265 if (lua_istable(L, -1)) {
267 lua_pushstring(L, "host");
269 host_id = lua_tostring(L, -1);
272 lua_pushstring(L, "fct");
274 function_id = lua_tostring(L, -1);
277 lua_pushstring(L,"args");
279 args = lua_tostring(L,-1);
283 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
287 // look for the index of host in host_list for each AS
288 xbt_dynar_foreach(as_list_d, i, p_as)
290 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
291 if (p_host->id == host_id) {
292 p_host->function = function_id;
293 p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
294 // split & fill the args list
295 tmp_arg = strtok((char*)args,",");
296 while (tmp_arg != NULL) {
297 xbt_dynar_push(p_host->args_list, &tmp_arg);
298 tmp_arg = strtok(NULL,",");
304 XBT_ERROR("Host : %s Not Found !!", host_id);
309 static int Host_set_property(lua_State* L)
314 const char* host_id ="";
315 const char* prop_id = "";
316 const char* prop_value = "";
317 if (lua_istable(L, -1)) {
319 lua_pushstring(L, "host");
321 host_id = lua_tostring(L, -1);
324 lua_pushstring(L, "prop_id");
326 prop_id = lua_tostring(L, -1);
329 lua_pushstring(L,"prop_value");
331 prop_value = lua_tostring(L,-1);
334 xbt_dynar_foreach(as_list_d, i, p_as)
336 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
337 if (p_host->id == host_id) {
338 xbt_dict_set(p_host->properties, prop_id, xbt_strdup(prop_value), free);
346 * surf parse bypass platform
347 * through CPU/network Models
350 static int surf_parse_bypass_platform()
356 p_route_attr p_route;
360 xbt_dynar_foreach(as_list_d, i,p_as)
362 sg_platf_new_AS_open(p_as->id,p_as->mode);
363 // add associated Hosts
364 xbt_dynar_foreach(p_as->host_list_d, j, p_host){
365 create_host(p_host->id, p_host->power_peak, p_host->power_scale,
366 p_host->power_trace, p_host->core, p_host->state_initial,
367 p_host->state_trace, p_host->properties);
370 //FIXME: should use sg_platf instead. That would add to routing model host list, amongst other benefits
372 // add associated Links
373 xbt_dynar_foreach(p_as->link_list_d, j, p_link){
374 create_link(p_link->id, p_link->bandwidth, p_link->bandwidth_trace,
375 p_link->latency, p_link->latency_trace,
376 p_link->state_initial, p_link->state_trace,
379 // add associated Routes
380 xbt_dynar_foreach(p_as->route_list_d, j, p_route){
381 surf_routing_add_route((char *) p_route->src_id,
382 (char *) p_route->dest_id, p_route->links_id);
386 sg_platf_new_AS_close();
390 surf_add_host_traces();
391 surf_add_link_traces();
393 return 0; // must return 0 ?!!
399 * surf parse bypass platform
400 * through workstation_ptask_L07 Model
403 static int surf_wsL07_parse_bypass_platform()
406 p_AS_attr p_as, p_sub_as;
409 p_route_attr p_route;
411 xbt_dynar_foreach(as_list_d, i, p_as)
414 sg_platf_new_AS_open(p_as->id,p_as->mode);
419 xbt_dynar_foreach(p_as->sub_as_list_id, j, p_sub_as) {
422 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
423 create_host_wsL07(p_host->id, p_host->power_peak, p_host->power_scale,
424 p_host->power_trace, p_host->state_initial,
425 p_host->state_trace);
426 //add to routing model host list
429 xbt_dynar_foreach(p_as->link_list_d, j, p_link) {
430 create_link_wsL07(p_link->id, p_link->bandwidth,
431 p_link->bandwidth_trace, p_link->latency,
432 p_link->latency_trace, p_link->state_initial,
433 p_link->state_trace, p_link->policy);
436 xbt_dynar_foreach(p_as->route_list_d, j, p_route) {
437 surf_routing_add_route((char *) p_route->src_id,
438 (char *) p_route->dest_id, p_route->links_id);
442 sg_platf_new_AS_close();
445 surf_wsL07_add_traces();
450 * surf parse bypass application for MSG Module
452 static int surf_parse_bypass_application()
457 xbt_dynar_foreach(as_list_d, i, p_as)
459 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
460 if (p_host->function)
461 MSG_set_function(p_host->id, p_host->function, p_host->args_list);
470 int console_add_host(lua_State *L)
474 p_AS_attr p_as,current_as = NULL;
477 const char *power_trace;
478 const char *state_trace;
479 double power, power_scale;
480 int state_initial,core;
481 //get values from the table passed as argument
482 if (lua_istable(L, -1)) {
484 lua_pushstring(L, "AS");
486 AS_id = lua_tostring(L, -1);
490 lua_pushstring(L, "id");
492 id = lua_tostring(L, -1);
496 lua_pushstring(L, "power");
498 power = lua_tonumber(L, -1);
502 lua_pushstring(L, "power_scale");
504 power_scale = lua_tonumber(L, -1);
508 lua_pushstring(L, "power_trace");
510 power_trace = lua_tostring(L, -1);
513 lua_pushstring(L, "core");
515 core = lua_tonumber(L, -1);
519 lua_pushstring(L, "state_initial");
521 state_initial = lua_tonumber(L, -1);
525 lua_pushstring(L, "state_trace");
527 state_trace = lua_tostring(L, -1);
532 ("Bad Arguments to create host, Should be a table with named arguments");
535 xbt_dynar_foreach(as_list_d, i, p_as){
536 if (p_as->id == AS_id){
544 XBT_ERROR("No AS_id :%s found",AS_id);
548 host = malloc(sizeof(host_attr));
550 host->power_peak = power;
551 host->power_scale = power_scale;
552 host->power_trace = power_trace;
554 host->state_initial = state_initial;
555 host->state_trace = state_trace;
556 host->function = NULL;
557 host->properties = xbt_dict_new();
558 xbt_dynar_push(current_as->host_list_d, &host);
563 int console_add_link(lua_State *L) {
566 p_AS_attr p_as,current_as = NULL;
568 double bandwidth, latency;
569 const char *bandwidth_trace;
570 const char *latency_trace;
571 const char *state_trace;
572 int state_initial, policy;
574 //get values from the table passed as argument
575 if (lua_istable(L, -1)) {
578 lua_pushstring(L, "AS");
580 AS_id = lua_tostring(L, -1);
584 lua_pushstring(L, "id");
586 id = lua_tostring(L, -1);
589 // get bandwidth value
590 lua_pushstring(L, "bandwidth");
592 bandwidth = lua_tonumber(L, -1);
596 lua_pushstring(L, "latency");
598 latency = lua_tonumber(L, -1);
601 /*Optional Arguments */
603 //get bandwidth_trace value
604 lua_pushstring(L, "bandwidth_trace");
606 bandwidth_trace = lua_tostring(L, -1);
609 //get latency_trace value
610 lua_pushstring(L, "latency_trace");
612 latency_trace = lua_tostring(L, -1);
615 //get state_trace value
616 lua_pushstring(L, "state_trace");
618 state_trace = lua_tostring(L, -1);
621 //get state_initial value
622 lua_pushstring(L, "state_initial");
624 state_initial = lua_tonumber(L, -1);
628 lua_pushstring(L, "policy");
630 policy = lua_tonumber(L, -1);
635 ("Bad Arguments to create link, Should be a table with named arguments");
638 xbt_dynar_foreach(as_list_d, i, p_as){
639 if (p_as->id == AS_id){
647 XBT_ERROR("No AS_id :%s found",AS_id);
650 p_link_attr link = malloc(sizeof(link_attr));
652 link->bandwidth = bandwidth;
653 link->latency = latency;
654 link->bandwidth_trace = bandwidth_trace;
655 link->latency_trace = latency_trace;
656 link->state_trace = state_trace;
657 link->state_initial = state_initial;
658 link->policy = policy;
659 xbt_dynar_push(current_as->link_list_d, &link);
664 * add Router to AS components
666 int console_add_router(lua_State* L) {
667 p_router_attr router;
670 p_AS_attr p_as,current_as = NULL;
672 if (lua_istable(L, -1)) {
674 lua_pushstring(L, "AS");
676 AS_id = lua_tostring(L, -1);
679 lua_pushstring(L, "id");
681 id = lua_tostring(L, -1);
684 xbt_dynar_foreach(as_list_d, i, p_as){
685 if (p_as->id == AS_id){
693 XBT_ERROR("No AS_id '%s' found",AS_id);
696 router = malloc(sizeof(router_attr));
698 xbt_dynar_push(current_as->router_list_d, &router);
704 int console_add_route(lua_State *L)
708 p_AS_attr p_as,current_as = NULL;
711 p_route_attr route = malloc(sizeof(route_attr));
714 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)
717 lua_pushstring(L, "AS");
719 AS_id = lua_tostring(L, -1);
722 xbt_dynar_foreach(as_list_d, i, p_as){
723 if (p_as->id == AS_id){
731 XBT_ERROR("addRoute: No AS_id :%s found",AS_id);
735 lua_pushstring(L, "src");
737 route->src_id = lua_tostring(L, -1);
740 // get Destination Value
741 lua_pushstring(L, "dest");
743 route->dest_id = lua_tostring(L, -1);
746 // get Links Table (char* to be splited later)
747 lua_pushstring(L, "links");
749 links = lua_tostring(L, -1);
752 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
754 char *tmp_links = xbt_strdup(links);
755 link_id = strtok(tmp_links,","); //tmp_link = strtok((char*)links,",");
756 while(link_id != NULL)
758 xbt_dynar_push(route->links_id, &link_id);
759 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.
761 xbt_dynar_push(current_as->route_list_d, &route);
764 else { // Route.new is declared as a function
765 AS_id = luaL_checkstring(L, 1);
766 xbt_dynar_foreach(as_list_d, i, p_as){
767 if (p_as->id == AS_id){
775 XBT_ERROR("addRoute: No AS_id :%s found",AS_id);
778 route->src_id = luaL_checkstring(L, 2);
779 route->dest_id = luaL_checkstring(L, 3);
780 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
782 while (lua_next(L, 4) != 0)
784 link_id = lua_tostring(L, -1);
785 xbt_dynar_push(route->links_id, &link_id);
786 XBT_DEBUG("index = %f , Link_id = %s \n", lua_tonumber(L, -2),
787 lua_tostring(L, -1));
791 //add route to platform's route list
792 xbt_dynar_push(current_as->route_list_d, &route);
799 int console_add_AS(lua_State *L)
804 int console_set_function(lua_State *L)
806 return Host_set_function(L);
809 int console_host_set_property(lua_State *L)
811 return Host_set_property(L);
814 int console_parse_platform()
816 return surf_parse_bypass_platform();
819 int console_parse_application()
821 return surf_parse_bypass_application();
824 int console_parse_platform_wsL07()
826 return surf_wsL07_parse_bypass_platform();