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"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_console, bindings, "Lua Bindings");
17 static xbt_dynar_t as_list_d;
20 * Initialize platform model routing
23 static void create_AS(const char *id, const char *mode)
25 surf_AS_new(id, mode);
29 * create host resource via CPU model [for MSG]
32 static void create_host(const char *id, double power_peak, double power_sc,
33 const char *power_tr,int core,int state_init,
36 double power_scale = 1.0;
37 int core_nb = 1; //default value
38 tmgr_trace_t power_trace = NULL;
39 e_surf_resource_state_t state_initial;
40 tmgr_trace_t state_trace;
42 power_scale = power_sc;
44 core_nb = core; //default value
46 state_initial = SURF_RESOURCE_OFF;
48 state_initial = SURF_RESOURCE_ON;
50 power_trace = tmgr_trace_new(power_tr);
52 power_trace = tmgr_trace_new("");
54 state_trace = tmgr_trace_new(state_tr);
56 state_trace = tmgr_trace_new("");
58 surf_host_create_resource(xbt_strdup(id), power_peak, power_scale,
59 power_trace, core_nb, state_initial, state_trace,
60 current_property_set);
61 current_property_set = NULL;
65 * create link resource via network model
67 static void create_link(const char *name,
68 double bw_initial, const char *trace,
69 double lat_initial, const char *latency_trace,
70 int state_init, const char *state_trace,
73 tmgr_trace_t bw_trace;
74 tmgr_trace_t lat_trace;
75 e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
76 e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
77 tmgr_trace_t st_trace;
79 bw_trace = tmgr_trace_new(trace);
81 bw_trace = tmgr_trace_new("");
84 lat_trace = tmgr_trace_new(latency_trace);
86 lat_trace = tmgr_trace_new("");
89 st_trace = tmgr_trace_new(state_trace);
91 st_trace = tmgr_trace_new("");
94 state_initial_link = SURF_RESOURCE_OFF;
96 policy_initial_link = SURF_LINK_FATPIPE;
98 surf_link_create_resource(xbt_strdup(name), bw_initial, bw_trace,
99 lat_initial, lat_trace, state_initial_link,
100 st_trace, policy_initial_link, xbt_dict_new());
104 *create host resource via workstation_ptask_L07 model [for SimDag]
106 static void create_host_wsL07(const char *id, double power_peak,
107 double power_sc, const char *power_tr,
108 int state_init, const char *state_tr)
110 double power_scale = 1.0;
111 tmgr_trace_t power_trace = NULL;
112 e_surf_resource_state_t state_initial;
113 tmgr_trace_t state_trace;
115 power_scale = power_sc;
116 if (state_init == -1)
117 state_initial = SURF_RESOURCE_OFF;
119 state_initial = SURF_RESOURCE_ON;
121 power_trace = tmgr_trace_new(power_tr);
123 power_trace = tmgr_trace_new("");
125 state_trace = tmgr_trace_new(state_tr);
127 state_trace = tmgr_trace_new("");
129 surf_wsL07_host_create_resource(xbt_strdup(id), power_peak, power_scale,
130 power_trace, state_initial, state_trace,
131 current_property_set);
132 current_property_set = NULL;
136 * create link resource via workstation_ptask_L07 model [for SimDag]
139 static void create_link_wsL07(const char *name,
140 double bw_initial, const char *trace,
142 const char *latency_trace, int state_init,
143 const char *state_trace, int policy)
145 tmgr_trace_t bw_trace;
146 tmgr_trace_t lat_trace;
147 e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
148 e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
149 tmgr_trace_t st_trace;
151 bw_trace = tmgr_trace_new(trace);
153 bw_trace = tmgr_trace_new("");
156 lat_trace = tmgr_trace_new(latency_trace);
158 lat_trace = tmgr_trace_new("");
161 st_trace = tmgr_trace_new(state_trace);
163 st_trace = tmgr_trace_new("");
165 if (state_init == -1)
166 state_initial_link = SURF_RESOURCE_OFF;
168 policy_initial_link = SURF_LINK_FATPIPE;
170 surf_wsL07_link_create_resource(xbt_strdup(name), bw_initial, bw_trace,
171 lat_initial, lat_trace,
172 state_initial_link, st_trace,
173 policy_initial_link, xbt_dict_new());
178 * Append new AS to the Platform
181 static int AS_new(lua_State * L)
184 if (xbt_dynar_is_empty(as_list_d))
185 as_list_d = xbt_dynar_new(sizeof(p_AS_attr), &xbt_free_ref);
190 if (lua_istable(L, 1)) {
191 lua_pushstring(L, "id");
193 id = lua_tostring(L, -1);
196 lua_pushstring(L, "mode");
198 mode = lua_tostring(L, -1);
202 ("Bad Arguments to AS.new, Should be a table with named arguments");
205 AS = malloc(sizeof(AS_attr));
208 AS->host_list_d = xbt_dynar_new(sizeof(p_host_attr),&xbt_free_ref);
209 AS->link_list_d = xbt_dynar_new(sizeof(p_link_attr),&xbt_free_ref);
210 AS->route_list_d = xbt_dynar_new(sizeof(p_route_attr),&xbt_free_ref);
211 AS->router_list_d = xbt_dynar_new(sizeof(p_router_attr),&xbt_free_ref);
212 AS->sub_as_list_id = xbt_dynar_new(sizeof(p_AS_attr),&xbt_free_ref);
213 xbt_dynar_push(as_list_d, &AS);
219 * add sub AS to the Parent AS
221 static int AS_add(lua_State *L)
225 p_AS_attr super_as,p_as;
226 const char *super_AS_id;
227 const char *sub_AS_id = NULL;
228 const char *sub_AS_routing= NULL;
229 if(lua_istable(L, -1))
231 lua_pushstring(L, "AS");
233 super_AS_id = lua_tostring(L, -1);
236 lua_pushstring(L, "id");
238 sub_AS_id = lua_tostring(L, -1);
243 xbt_dynar_foreach(as_list_d, i, p_as){
244 if (p_as->id == super_AS_id){
249 AS = malloc(sizeof(AS_attr));
251 AS->mode = sub_AS_routing;
252 AS->host_list_d = xbt_dynar_new(sizeof(p_host_attr),&xbt_free_ref);
253 AS->link_list_d = xbt_dynar_new(sizeof(p_link_attr),&xbt_free_ref);
254 AS->route_list_d = xbt_dynar_new(sizeof(p_route_attr),&xbt_free_ref);
255 AS->sub_as_list_id = xbt_dynar_new(sizeof(p_AS_attr),&xbt_free_ref);
256 xbt_dynar_push(super_as->sub_as_list_id, &AS);
263 * add new host to platform hosts list
265 static int Host_new(lua_State * L)
269 p_AS_attr p_as,current_as = NULL;
272 const char *power_trace;
273 const char *state_trace;
274 double power, power_scale;
275 int state_initial,core;
276 //get values from the table passed as argument
277 if (lua_istable(L, -1)) {
279 lua_pushstring(L, "AS");
281 AS_id = lua_tostring(L, -1);
285 lua_pushstring(L, "id");
287 id = lua_tostring(L, -1);
291 lua_pushstring(L, "power");
293 power = lua_tonumber(L, -1);
297 lua_pushstring(L, "power_scale");
299 power_scale = lua_tonumber(L, -1);
303 lua_pushstring(L, "power_trace");
305 power_trace = lua_tostring(L, -1);
308 lua_pushstring(L, "core");
310 core = lua_tonumber(L, -1);
314 lua_pushstring(L, "state_initial");
316 state_initial = lua_tonumber(L, -1);
320 lua_pushstring(L, "state_trace");
322 state_trace = lua_tostring(L, -1);
327 ("Bad Arguments to create host, Should be a table with named arguments");
330 xbt_dynar_foreach(as_list_d, i, p_as){
331 if (p_as->id == AS_id){
339 XBT_ERROR("No AS_id :%s found",AS_id);
343 host = malloc(sizeof(host_attr));
345 host->power_peak = power;
346 host->power_scale = power_scale;
347 host->power_trace = power_trace;
349 host->state_initial = state_initial;
350 host->state_trace = state_trace;
351 host->function = NULL;
352 xbt_dynar_push(current_as->host_list_d, &host);
358 * add link to AS links list
360 static int Link_new(lua_State * L) // (id,bandwidth,latency)
364 p_AS_attr p_as,current_as = NULL;
366 double bandwidth, latency;
367 const char *bandwidth_trace;
368 const char *latency_trace;
369 const char *state_trace;
370 int state_initial, policy;
372 //get values from the table passed as argument
373 if (lua_istable(L, -1)) {
376 lua_pushstring(L, "AS");
378 AS_id = lua_tostring(L, -1);
382 lua_pushstring(L, "id");
384 id = lua_tostring(L, -1);
387 // get bandwidth value
388 lua_pushstring(L, "bandwidth");
390 bandwidth = lua_tonumber(L, -1);
394 lua_pushstring(L, "latency");
396 latency = lua_tonumber(L, -1);
399 /*Optional Arguments */
401 //get bandwidth_trace value
402 lua_pushstring(L, "bandwidth_trace");
404 bandwidth_trace = lua_tostring(L, -1);
407 //get latency_trace value
408 lua_pushstring(L, "latency_trace");
410 latency_trace = lua_tostring(L, -1);
413 //get state_trace value
414 lua_pushstring(L, "state_trace");
416 state_trace = lua_tostring(L, -1);
419 //get state_initial value
420 lua_pushstring(L, "state_initial");
422 state_initial = lua_tonumber(L, -1);
426 lua_pushstring(L, "policy");
428 policy = lua_tonumber(L, -1);
433 ("Bad Arguments to create link, Should be a table with named arguments");
436 xbt_dynar_foreach(as_list_d, i, p_as){
437 if (p_as->id == AS_id){
445 XBT_ERROR("No AS_id :%s found",AS_id);
448 p_link_attr link = malloc(sizeof(link_attr));
450 link->bandwidth = bandwidth;
451 link->latency = latency;
452 link->bandwidth_trace = bandwidth_trace;
453 link->latency_trace = latency_trace;
454 link->state_trace = state_trace;
455 link->state_initial = state_initial;
456 link->policy = policy;
457 xbt_dynar_push(current_as->link_list_d, &link);
463 * add route to AS routes list
465 static int Route_new(lua_State * L) // (src_id,dest_id,links_number,link_table)
469 p_AS_attr p_as,current_as = NULL;
472 p_route_attr route = malloc(sizeof(route_attr));
475 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)
478 lua_pushstring(L, "AS");
480 AS_id = lua_tostring(L, -1);
483 xbt_dynar_foreach(as_list_d, i, p_as){
484 if (p_as->id == AS_id){
492 XBT_ERROR("addRoute: No AS_id :%s found",AS_id);
496 lua_pushstring(L, "src");
498 route->src_id = lua_tostring(L, -1);
501 // get Destination Value
502 lua_pushstring(L, "dest");
504 route->dest_id = lua_tostring(L, -1);
507 // get Links Table (char* to be splited later)
508 lua_pushstring(L, "links");
510 links = lua_tostring(L, -1);
513 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
515 char *tmp_links = xbt_strdup(links);
516 link_id = strtok(tmp_links,","); //tmp_link = strtok((char*)links,",");
517 while(link_id != NULL)
519 xbt_dynar_push(route->links_id, &link_id);
520 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.
522 xbt_dynar_push(current_as->route_list_d, &route);
525 else { // Route.new is declared as a function
526 AS_id = luaL_checkstring(L, 1);
527 xbt_dynar_foreach(as_list_d, i, p_as){
528 if (p_as->id == AS_id){
536 XBT_ERROR("addRoute: No AS_id :%s found",AS_id);
539 route->src_id = luaL_checkstring(L, 2);
540 route->dest_id = luaL_checkstring(L, 3);
541 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
543 while (lua_next(L, 4) != 0)
545 link_id = lua_tostring(L, -1);
546 xbt_dynar_push(route->links_id, &link_id);
547 XBT_DEBUG("index = %f , Link_id = %s \n", lua_tonumber(L, -2),
548 lua_tostring(L, -1));
552 //add route to platform's route list
553 xbt_dynar_push(current_as->route_list_d, &route);
560 * add Router to AS components
562 static int Router_new(lua_State* L)
564 p_router_attr router;
567 p_AS_attr p_as,current_as = NULL;
569 if (lua_istable(L, -1)) {
571 lua_pushstring(L, "AS");
573 AS_id = lua_tostring(L, -1);
576 lua_pushstring(L, "id");
578 id = lua_tostring(L, -1);
581 xbt_dynar_foreach(as_list_d, i, p_as){
582 if (p_as->id == AS_id){
590 XBT_ERROR("No AS_id :%s found",AS_id);
593 router = malloc(sizeof(router_attr));
595 xbt_dynar_push(current_as->router_list_d, &router);
601 * set function to process
603 static int Host_set_function(lua_State * L) //(host,function,nb_args,list_args)
608 const char *function;
613 if (lua_istable(L, -1)) {
615 lua_pushstring(L, "host");
617 host = lua_tostring(L, -1);
620 lua_pushstring(L, "fct");
622 function = lua_tostring(L, -1);
625 lua_pushstring(L,"args");
627 args = lua_tostring(L,-1);
631 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
635 // look for the index of host in host_list for each AS
636 xbt_dynar_foreach(as_list_d, i, p_as)
638 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
639 if (p_host->id == host) {
640 p_host->function = function;
641 p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
642 // split & fill the args list
643 tmp_arg = strtok((char*)args,",");
644 while (tmp_arg != NULL) {
645 xbt_dynar_push(p_host->args_list, &tmp_arg);
646 tmp_arg = strtok(NULL,",");
652 XBT_ERROR("Host : %s Not Found !!", host);
657 * surf parse bypass platform
658 * through CPU/network Models
661 static int surf_parse_bypass_platform()
667 p_route_attr p_route;
671 xbt_dynar_foreach(as_list_d, i,p_as)
673 create_AS(p_as->id, p_as->mode);
674 // add associated Hosts
675 xbt_dynar_foreach(p_as->host_list_d, j, p_host){
676 create_host(p_host->id, p_host->power_peak, p_host->power_scale,
677 p_host->power_trace, p_host->core, p_host->state_initial,
678 p_host->state_trace);
679 //add to routing model host list
680 surf_route_add_host((char *) p_host->id);
682 // add associated Links
683 xbt_dynar_foreach(p_as->link_list_d, j, p_link){
684 create_link(p_link->id, p_link->bandwidth, p_link->bandwidth_trace,
685 p_link->latency, p_link->latency_trace,
686 p_link->state_initial, p_link->state_trace,
689 // add associated Routes
690 xbt_dynar_foreach(p_as->route_list_d, j, p_route){
691 surf_routing_add_route((char *) p_route->src_id,
692 (char *) p_route->dest_id, p_route->links_id);
696 surf_AS_finalize(p_as->id);
700 surf_add_host_traces();
701 surf_add_link_traces();
703 return 0; // must return 0 ?!!
709 * surf parse bypass platform
710 * through workstation_ptask_L07 Model
713 static int surf_wsL07_parse_bypass_platform()
716 p_AS_attr p_as, p_sub_as;
719 p_route_attr p_route;
721 xbt_dynar_foreach(as_list_d, i, p_as)
724 create_AS(p_as->id, p_as->mode);
729 xbt_dynar_foreach(p_as->sub_as_list_id, j, p_sub_as) {
732 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
733 create_host_wsL07(p_host->id, p_host->power_peak, p_host->power_scale,
734 p_host->power_trace, p_host->state_initial,
735 p_host->state_trace);
736 //add to routing model host list
737 surf_route_add_host((char *) p_host->id);
740 xbt_dynar_foreach(p_as->link_list_d, j, p_link) {
741 create_link_wsL07(p_link->id, p_link->bandwidth,
742 p_link->bandwidth_trace, p_link->latency,
743 p_link->latency_trace, p_link->state_initial,
744 p_link->state_trace, p_link->policy);
747 xbt_dynar_foreach(p_as->route_list_d, j, p_route) {
748 surf_routing_add_route((char *) p_route->src_id,
749 (char *) p_route->dest_id, p_route->links_id);
753 surf_AS_finalize(p_as->id);
756 surf_wsL07_add_traces();
761 * surf parse bypass application for MSG Module
763 static int surf_parse_bypass_application()
768 xbt_dynar_foreach(as_list_d, i, p_as)
770 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
771 if (p_host->function)
772 MSG_set_function(p_host->id, p_host->function, p_host->args_list);
781 int console_add_host(lua_State *L)
786 int console_add_link(lua_State *L)
791 int console_add_route(lua_State *L)
796 int console_add_AS(lua_State *L)
801 int console_set_function(lua_State *L)
803 return Host_set_function(L);
806 int console_parse_platform()
808 return surf_parse_bypass_platform();
811 int console_parse_application()
813 return surf_parse_bypass_application();
816 int console_parse_platform_wsL07()
818 return surf_wsL07_parse_bypass_platform();