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,
37 double power_scale = 1.0;
38 int core_nb = 1; //default value
39 tmgr_trace_t power_trace = NULL;
40 e_surf_resource_state_t state_initial;
41 tmgr_trace_t state_trace;
43 power_scale = power_sc;
45 core_nb = core; //default value
47 state_initial = SURF_RESOURCE_OFF;
49 state_initial = SURF_RESOURCE_ON;
51 power_trace = tmgr_trace_new(power_tr);
53 power_trace = tmgr_trace_new("");
55 state_trace = tmgr_trace_new(state_tr);
57 state_trace = tmgr_trace_new("");
58 current_property_set = xbt_dict_new();
59 surf_host_create_resource(xbt_strdup(id), power_peak, power_scale,
60 power_trace, core_nb, state_initial, state_trace,
61 current_property_set);
66 * create link resource via network model
68 static void create_link(const char *name,
69 double bw_initial, const char *trace,
70 double lat_initial, const char *latency_trace,
71 int state_init, const char *state_trace,
74 tmgr_trace_t bw_trace;
75 tmgr_trace_t lat_trace;
76 e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
77 e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
78 tmgr_trace_t st_trace;
80 bw_trace = tmgr_trace_new(trace);
82 bw_trace = tmgr_trace_new("");
85 lat_trace = tmgr_trace_new(latency_trace);
87 lat_trace = tmgr_trace_new("");
90 st_trace = tmgr_trace_new(state_trace);
92 st_trace = tmgr_trace_new("");
95 state_initial_link = SURF_RESOURCE_OFF;
97 policy_initial_link = SURF_LINK_FATPIPE;
99 surf_link_create_resource(xbt_strdup(name), bw_initial, bw_trace,
100 lat_initial, lat_trace, state_initial_link,
101 st_trace, policy_initial_link, xbt_dict_new());
106 *create host resource via workstation_ptask_L07 model [for SimDag]
108 static void create_host_wsL07(const char *id, double power_peak,
109 double power_sc, const char *power_tr,
110 int state_init, const char *state_tr)
112 double power_scale = 1.0;
113 tmgr_trace_t power_trace = NULL;
114 e_surf_resource_state_t state_initial;
115 tmgr_trace_t state_trace;
117 power_scale = power_sc;
118 if (state_init == -1)
119 state_initial = SURF_RESOURCE_OFF;
121 state_initial = SURF_RESOURCE_ON;
123 power_trace = tmgr_trace_new(power_tr);
125 power_trace = tmgr_trace_new("");
127 state_trace = tmgr_trace_new(state_tr);
129 state_trace = tmgr_trace_new("");
130 current_property_set = xbt_dict_new();
131 surf_wsL07_host_create_resource(xbt_strdup(id), power_peak, power_scale,
132 power_trace, state_initial, state_trace,
133 current_property_set);
138 * create link resource via workstation_ptask_L07 model [for SimDag]
141 static void create_link_wsL07(const char *name,
142 double bw_initial, const char *trace,
144 const char *latency_trace, int state_init,
145 const char *state_trace, int policy)
147 tmgr_trace_t bw_trace;
148 tmgr_trace_t lat_trace;
149 e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
150 e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
151 tmgr_trace_t st_trace;
153 bw_trace = tmgr_trace_new(trace);
155 bw_trace = tmgr_trace_new("");
158 lat_trace = tmgr_trace_new(latency_trace);
160 lat_trace = tmgr_trace_new("");
163 st_trace = tmgr_trace_new(state_trace);
165 st_trace = tmgr_trace_new("");
167 if (state_init == -1)
168 state_initial_link = SURF_RESOURCE_OFF;
170 policy_initial_link = SURF_LINK_FATPIPE;
172 surf_wsL07_link_create_resource(xbt_strdup(name), bw_initial, bw_trace,
173 lat_initial, lat_trace,
174 state_initial_link, st_trace,
175 policy_initial_link, xbt_dict_new());
183 static int AS_new(lua_State * L)
186 if (xbt_dynar_is_empty(as_list_d))
187 as_list_d = xbt_dynar_new(sizeof(p_AS_attr), &xbt_free_ref);
192 if (lua_istable(L, 1)) {
193 lua_pushstring(L, "id");
195 id = lua_tostring(L, -1);
198 lua_pushstring(L, "mode");
200 mode = lua_tostring(L, -1);
204 ("Bad Arguments to AS.new, Should be a table with named arguments");
207 AS = malloc(sizeof(AS_attr));
210 AS->host_list_d = xbt_dynar_new(sizeof(p_host_attr),&xbt_free_ref);
211 AS->link_list_d = xbt_dynar_new(sizeof(p_link_attr),&xbt_free_ref);
212 AS->route_list_d = xbt_dynar_new(sizeof(p_route_attr),&xbt_free_ref);
213 xbt_dynar_push(as_list_d, &AS);
219 * add new host to platform hosts list
221 static int Host_new(lua_State * L)
225 p_AS_attr p_as,current_as = NULL;
228 const char *power_trace;
229 const char *state_trace;
230 double power, power_scale;
231 int state_initial,core;
232 //get values from the table passed as argument
233 if (lua_istable(L, -1)) {
235 lua_pushstring(L, "AS");
237 AS_id = lua_tostring(L, -1);
241 lua_pushstring(L, "id");
243 id = lua_tostring(L, -1);
247 lua_pushstring(L, "power");
249 power = lua_tonumber(L, -1);
253 lua_pushstring(L, "power_scale");
255 power_scale = lua_tonumber(L, -1);
259 lua_pushstring(L, "power_trace");
261 power_trace = lua_tostring(L, -1);
264 lua_pushstring(L, "core");
266 core = lua_tonumber(L, -1);
270 lua_pushstring(L, "state_initial");
272 state_initial = lua_tonumber(L, -1);
276 lua_pushstring(L, "state_trace");
278 state_trace = lua_tostring(L, -1);
283 ("Bad Arguments to create host, Should be a table with named arguments");
286 xbt_dynar_foreach(as_list_d, i, p_as){
287 if (p_as->id == AS_id){
295 XBT_ERROR("No AS_id :%s found",AS_id);
299 host = malloc(sizeof(host_attr));
301 host->power_peak = power;
302 host->power_scale = power_scale;
303 host->power_trace = power_trace;
305 host->state_initial = state_initial;
306 host->state_trace = state_trace;
307 host->function = NULL;
308 xbt_dynar_push(current_as->host_list_d, &host);
314 * add link to platform links list
316 static int Link_new(lua_State * L) // (id,bandwidth,latency)
320 p_AS_attr p_as,current_as = NULL;
322 double bandwidth, latency;
323 const char *bandwidth_trace;
324 const char *latency_trace;
325 const char *state_trace;
326 int state_initial, policy;
328 //get values from the table passed as argument
329 if (lua_istable(L, -1)) {
332 lua_pushstring(L, "AS");
334 AS_id = lua_tostring(L, -1);
338 lua_pushstring(L, "id");
340 id = lua_tostring(L, -1);
343 // get bandwidth value
344 lua_pushstring(L, "bandwidth");
346 bandwidth = lua_tonumber(L, -1);
350 lua_pushstring(L, "latency");
352 latency = lua_tonumber(L, -1);
355 /*Optional Arguments */
357 //get bandwidth_trace value
358 lua_pushstring(L, "bandwidth_trace");
360 bandwidth_trace = lua_tostring(L, -1);
363 //get latency_trace value
364 lua_pushstring(L, "latency_trace");
366 latency_trace = lua_tostring(L, -1);
369 //get state_trace value
370 lua_pushstring(L, "state_trace");
372 state_trace = lua_tostring(L, -1);
375 //get state_initial value
376 lua_pushstring(L, "state_initial");
378 state_initial = lua_tonumber(L, -1);
382 lua_pushstring(L, "policy");
384 policy = lua_tonumber(L, -1);
389 ("Bad Arguments to create link, Should be a table with named arguments");
392 xbt_dynar_foreach(as_list_d, i, p_as){
393 if (p_as->id == AS_id){
401 XBT_ERROR("No AS_id :%s found",AS_id);
404 p_link_attr link = malloc(sizeof(link_attr));
406 link->bandwidth = bandwidth;
407 link->latency = latency;
408 link->bandwidth_trace = bandwidth_trace;
409 link->latency_trace = latency_trace;
410 link->state_trace = state_trace;
411 link->state_initial = state_initial;
412 link->policy = policy;
413 xbt_dynar_push(current_as->link_list_d, &link);
419 * add route to platform routes list
421 static int Route_new(lua_State * L) // (src_id,dest_id,links_number,link_table)
425 p_AS_attr p_as,current_as = NULL;
428 p_route_attr route = malloc(sizeof(route_attr));
431 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)
434 lua_pushstring(L, "AS");
436 AS_id = lua_tostring(L, -1);
439 xbt_dynar_foreach(as_list_d, i, p_as){
440 if (p_as->id == AS_id){
448 XBT_ERROR("addRoute: No AS_id :%s found",AS_id);
452 lua_pushstring(L, "src");
454 route->src_id = lua_tostring(L, -1);
457 // get Destination Value
458 lua_pushstring(L, "dest");
460 route->dest_id = lua_tostring(L, -1);
463 // get Links Table (char* to be splited later)
464 lua_pushstring(L, "links");
466 links = lua_tostring(L, -1);
469 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
471 char *tmp_links = xbt_strdup(links);
472 link_id = strtok(tmp_links,","); //tmp_link = strtok((char*)links,",");
473 while(link_id != NULL)
475 xbt_dynar_push(route->links_id, &link_id);
476 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.
478 xbt_dynar_push(current_as->route_list_d, &route);
481 else { // Route.new is declared as a function
482 AS_id = luaL_checkstring(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);
495 route->src_id = luaL_checkstring(L, 2);
496 route->dest_id = luaL_checkstring(L, 3);
497 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
499 while (lua_next(L, 4) != 0)
501 link_id = lua_tostring(L, -1);
502 xbt_dynar_push(route->links_id, &link_id);
503 XBT_DEBUG("index = %f , Link_id = %s \n", lua_tonumber(L, -2),
504 lua_tostring(L, -1));
508 //add route to platform's route list
509 xbt_dynar_push(current_as->route_list_d, &route);
516 * set function to process
518 static int Host_set_function(lua_State * L) //(host,function,nb_args,list_args)
523 const char *function;
528 if (lua_istable(L, -1)) {
530 lua_pushstring(L, "host");
532 host = lua_tostring(L, -1);
535 lua_pushstring(L, "fct");
537 function = lua_tostring(L, -1);
540 lua_pushstring(L,"args");
542 args = lua_tostring(L,-1);
546 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
550 // look for the index of host in host_list for each AS
551 xbt_dynar_foreach(as_list_d, i, p_as)
553 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
554 if (p_host->id == host) {
555 p_host->function = function;
556 p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
557 // split & fill the args list
558 tmp_arg = strtok((char*)args,",");
559 while (tmp_arg != NULL) {
560 xbt_dynar_push(p_host->args_list, &tmp_arg);
561 tmp_arg = strtok(NULL,",");
567 XBT_ERROR("Host : %s Not Found !!", host);
572 * surf parse bypass platform
573 * through CPU/network Models
576 static int surf_parse_bypass_platform()
582 p_route_attr p_route;
586 xbt_dynar_foreach(as_list_d, i,p_as)
588 create_AS(p_as->id, p_as->mode);
589 // add associated Hosts
590 xbt_dynar_foreach(p_as->host_list_d, j, p_host){
591 create_host(p_host->id, p_host->power_peak, p_host->power_scale,
592 p_host->power_trace, p_host->core, p_host->state_initial,
593 p_host->state_trace);
594 //add to routing model host list
595 surf_route_add_host((char *) p_host->id);
597 // add associated Links
598 xbt_dynar_foreach(p_as->link_list_d, j, p_link){
599 create_link(p_link->id, p_link->bandwidth, p_link->bandwidth_trace,
600 p_link->latency, p_link->latency_trace,
601 p_link->state_initial, p_link->state_trace,
604 // add associated Routes
605 xbt_dynar_foreach(p_as->route_list_d, j, p_route){
606 surf_routing_add_route((char *) p_route->src_id,
607 (char *) p_route->dest_id, p_route->links_id);
611 surf_AS_finalize(p_as->id);
615 surf_add_host_traces();
616 surf_add_link_traces();
618 return 0; // must return 0 ?!!
624 * surf parse bypass platform
625 * through workstation_ptask_L07 Model
628 static int surf_wsL07_parse_bypass_platform()
635 p_route_attr p_route;
637 xbt_dynar_foreach(as_list_d, i, p_as)
640 create_AS(p_as->id, p_as->mode);
642 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
643 create_host_wsL07(p_host->id, p_host->power_peak, p_host->power_scale,
644 p_host->power_trace, p_host->state_initial,
645 p_host->state_trace);
646 //add to routing model host list
647 surf_route_add_host((char *) p_host->id);
650 xbt_dynar_foreach(p_as->link_list_d, j, p_link) {
651 create_link_wsL07(p_link->id, p_link->bandwidth,
652 p_link->bandwidth_trace, p_link->latency,
653 p_link->latency_trace, p_link->state_initial,
654 p_link->state_trace, p_link->policy);
657 xbt_dynar_foreach(p_as->route_list_d, j, p_route) {
658 surf_routing_add_route((char *) p_route->src_id,
659 (char *) p_route->dest_id, p_route->links_id);
663 surf_AS_finalize(p_as->id);
666 surf_wsL07_add_traces();
671 * surf parse bypass application for MSG Module
673 static int surf_parse_bypass_application()
678 xbt_dynar_foreach(as_list_d, i, p_as)
680 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
681 if (p_host->function)
682 MSG_set_function(p_host->id, p_host->function, p_host->args_list);
691 int console_add_host(lua_State *L)
696 int console_add_link(lua_State *L)
701 int console_add_route(lua_State *L)
706 int console_add_AS(lua_State *L)
711 int console_set_function(lua_State *L)
713 return Host_set_function(L);
716 int console_parse_platform()
718 return surf_parse_bypass_platform();
721 int console_parse_application()
723 return surf_parse_bypass_application();
726 int console_parse_platform_wsL07()
728 return surf_wsL07_parse_bypass_platform();