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("");
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);
63 current_property_set = NULL;
68 * create link resource via network model
70 static void create_link(const char *name,
71 double bw_initial, const char *trace,
72 double lat_initial, const char *latency_trace,
73 int state_init, const char *state_trace,
76 tmgr_trace_t bw_trace;
77 tmgr_trace_t lat_trace;
78 e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
79 e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
80 tmgr_trace_t st_trace;
82 bw_trace = tmgr_trace_new(trace);
84 bw_trace = tmgr_trace_new("");
87 lat_trace = tmgr_trace_new(latency_trace);
89 lat_trace = tmgr_trace_new("");
92 st_trace = tmgr_trace_new(state_trace);
94 st_trace = tmgr_trace_new("");
97 state_initial_link = SURF_RESOURCE_OFF;
99 policy_initial_link = SURF_LINK_FATPIPE;
101 surf_link_create_resource(xbt_strdup(name), bw_initial, bw_trace,
102 lat_initial, lat_trace, state_initial_link,
103 st_trace, policy_initial_link, xbt_dict_new());
108 *create host resource via workstation_ptask_L07 model [for SimDag]
110 static void create_host_wsL07(const char *id, double power_peak,
111 double power_sc, const char *power_tr,
112 int state_init, const char *state_tr)
114 double power_scale = 1.0;
115 tmgr_trace_t power_trace = NULL;
116 e_surf_resource_state_t state_initial;
117 tmgr_trace_t state_trace;
119 power_scale = power_sc;
120 if (state_init == -1)
121 state_initial = SURF_RESOURCE_OFF;
123 state_initial = SURF_RESOURCE_ON;
125 power_trace = tmgr_trace_new(power_tr);
127 power_trace = tmgr_trace_new("");
129 state_trace = tmgr_trace_new(state_tr);
131 state_trace = tmgr_trace_new("");
133 surf_wsL07_host_create_resource(xbt_strdup(id), power_peak, power_scale,
134 power_trace, state_initial, state_trace,
135 current_property_set);
136 current_property_set = NULL;
140 * create link resource via workstation_ptask_L07 model [for SimDag]
143 static void create_link_wsL07(const char *name,
144 double bw_initial, const char *trace,
146 const char *latency_trace, int state_init,
147 const char *state_trace, int policy)
149 tmgr_trace_t bw_trace;
150 tmgr_trace_t lat_trace;
151 e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
152 e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
153 tmgr_trace_t st_trace;
155 bw_trace = tmgr_trace_new(trace);
157 bw_trace = tmgr_trace_new("");
160 lat_trace = tmgr_trace_new(latency_trace);
162 lat_trace = tmgr_trace_new("");
165 st_trace = tmgr_trace_new(state_trace);
167 st_trace = tmgr_trace_new("");
169 if (state_init == -1)
170 state_initial_link = SURF_RESOURCE_OFF;
172 policy_initial_link = SURF_LINK_FATPIPE;
174 surf_wsL07_link_create_resource(xbt_strdup(name), bw_initial, bw_trace,
175 lat_initial, lat_trace,
176 state_initial_link, st_trace,
177 policy_initial_link, xbt_dict_new());
185 static int AS_new(lua_State * L)
188 if (xbt_dynar_is_empty(as_list_d))
189 as_list_d = xbt_dynar_new(sizeof(p_AS_attr), &xbt_free_ref);
194 if (lua_istable(L, 1)) {
195 lua_pushstring(L, "id");
197 id = lua_tostring(L, -1);
200 lua_pushstring(L, "mode");
202 mode = lua_tostring(L, -1);
206 ("Bad Arguments to AS.new, Should be a table with named arguments");
209 AS = malloc(sizeof(AS_attr));
212 AS->host_list_d = xbt_dynar_new(sizeof(p_host_attr),&xbt_free_ref);
213 AS->link_list_d = xbt_dynar_new(sizeof(p_link_attr),&xbt_free_ref);
214 AS->route_list_d = xbt_dynar_new(sizeof(p_route_attr),&xbt_free_ref);
215 xbt_dynar_push(as_list_d, &AS);
221 * add new host to platform hosts list
223 static int Host_new(lua_State * L)
227 p_AS_attr p_as,current_as = NULL;
230 const char *power_trace;
231 const char *state_trace;
232 double power, power_scale;
233 int state_initial,core;
234 //get values from the table passed as argument
235 if (lua_istable(L, -1)) {
237 lua_pushstring(L, "AS");
239 AS_id = lua_tostring(L, -1);
243 lua_pushstring(L, "id");
245 id = lua_tostring(L, -1);
249 lua_pushstring(L, "power");
251 power = lua_tonumber(L, -1);
255 lua_pushstring(L, "power_scale");
257 power_scale = lua_tonumber(L, -1);
261 lua_pushstring(L, "power_trace");
263 power_trace = lua_tostring(L, -1);
266 lua_pushstring(L, "core");
268 core = lua_tonumber(L, -1);
272 lua_pushstring(L, "state_initial");
274 state_initial = lua_tonumber(L, -1);
278 lua_pushstring(L, "state_trace");
280 state_trace = lua_tostring(L, -1);
285 ("Bad Arguments to create host, Should be a table with named arguments");
288 xbt_dynar_foreach(as_list_d, i, p_as){
289 if (p_as->id == AS_id){
297 XBT_ERROR("No AS_id :%s found",AS_id);
301 host = malloc(sizeof(host_attr));
303 host->power_peak = power;
304 host->power_scale = power_scale;
305 host->power_trace = power_trace;
307 host->state_initial = state_initial;
308 host->state_trace = state_trace;
309 host->function = NULL;
310 xbt_dynar_push(current_as->host_list_d, &host);
316 * add link to platform links list
318 static int Link_new(lua_State * L) // (id,bandwidth,latency)
322 p_AS_attr p_as,current_as = NULL;
324 double bandwidth, latency;
325 const char *bandwidth_trace;
326 const char *latency_trace;
327 const char *state_trace;
328 int state_initial, policy;
330 //get values from the table passed as argument
331 if (lua_istable(L, -1)) {
334 lua_pushstring(L, "AS");
336 AS_id = lua_tostring(L, -1);
340 lua_pushstring(L, "id");
342 id = lua_tostring(L, -1);
345 // get bandwidth value
346 lua_pushstring(L, "bandwidth");
348 bandwidth = lua_tonumber(L, -1);
352 lua_pushstring(L, "latency");
354 latency = lua_tonumber(L, -1);
357 /*Optional Arguments */
359 //get bandwidth_trace value
360 lua_pushstring(L, "bandwidth_trace");
362 bandwidth_trace = lua_tostring(L, -1);
365 //get latency_trace value
366 lua_pushstring(L, "latency_trace");
368 latency_trace = lua_tostring(L, -1);
371 //get state_trace value
372 lua_pushstring(L, "state_trace");
374 state_trace = lua_tostring(L, -1);
377 //get state_initial value
378 lua_pushstring(L, "state_initial");
380 state_initial = lua_tonumber(L, -1);
384 lua_pushstring(L, "policy");
386 policy = lua_tonumber(L, -1);
391 ("Bad Arguments to create link, Should be a table with named arguments");
394 xbt_dynar_foreach(as_list_d, i, p_as){
395 if (p_as->id == AS_id){
403 XBT_ERROR("No AS_id :%s found",AS_id);
406 p_link_attr link = malloc(sizeof(link_attr));
408 link->bandwidth = bandwidth;
409 link->latency = latency;
410 link->bandwidth_trace = bandwidth_trace;
411 link->latency_trace = latency_trace;
412 link->state_trace = state_trace;
413 link->state_initial = state_initial;
414 link->policy = policy;
415 xbt_dynar_push(current_as->link_list_d, &link);
421 * add route to platform routes list
423 static int Route_new(lua_State * L) // (src_id,dest_id,links_number,link_table)
427 p_AS_attr p_as,current_as = NULL;
430 p_route_attr route = malloc(sizeof(route_attr));
433 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)
436 lua_pushstring(L, "AS");
438 AS_id = lua_tostring(L, -1);
441 xbt_dynar_foreach(as_list_d, i, p_as){
442 if (p_as->id == AS_id){
450 XBT_ERROR("addRoute: No AS_id :%s found",AS_id);
454 lua_pushstring(L, "src");
456 route->src_id = lua_tostring(L, -1);
459 // get Destination Value
460 lua_pushstring(L, "dest");
462 route->dest_id = lua_tostring(L, -1);
465 // get Links Table (char* to be splited later)
466 lua_pushstring(L, "links");
468 links = lua_tostring(L, -1);
471 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
473 char *tmp_links = xbt_strdup(links);
474 link_id = strtok(tmp_links,","); //tmp_link = strtok((char*)links,",");
475 while(link_id != NULL)
477 xbt_dynar_push(route->links_id, &link_id);
478 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.
480 xbt_dynar_push(current_as->route_list_d, &route);
483 else { // Route.new is declared as a function
484 AS_id = luaL_checkstring(L, 1);
485 xbt_dynar_foreach(as_list_d, i, p_as){
486 if (p_as->id == AS_id){
494 XBT_ERROR("addRoute: No AS_id :%s found",AS_id);
497 route->src_id = luaL_checkstring(L, 2);
498 route->dest_id = luaL_checkstring(L, 3);
499 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
501 while (lua_next(L, 4) != 0)
503 link_id = lua_tostring(L, -1);
504 xbt_dynar_push(route->links_id, &link_id);
505 XBT_DEBUG("index = %f , Link_id = %s \n", lua_tonumber(L, -2),
506 lua_tostring(L, -1));
510 //add route to platform's route list
511 xbt_dynar_push(current_as->route_list_d, &route);
518 * set function to process
520 static int Host_set_function(lua_State * L) //(host,function,nb_args,list_args)
525 const char *function;
530 if (lua_istable(L, -1)) {
532 lua_pushstring(L, "host");
534 host = lua_tostring(L, -1);
537 lua_pushstring(L, "fct");
539 function = lua_tostring(L, -1);
542 lua_pushstring(L,"args");
544 args = lua_tostring(L,-1);
548 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
552 // look for the index of host in host_list for each AS
553 xbt_dynar_foreach(as_list_d, i, p_as)
555 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
556 if (p_host->id == host) {
557 p_host->function = function;
558 p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
559 // split & fill the args list
560 tmp_arg = strtok((char*)args,",");
561 while (tmp_arg != NULL) {
562 xbt_dynar_push(p_host->args_list, &tmp_arg);
563 tmp_arg = strtok(NULL,",");
569 XBT_ERROR("Host : %s Not Found !!", host);
574 * surf parse bypass platform
575 * through CPU/network Models
578 static int surf_parse_bypass_platform()
584 p_route_attr p_route;
588 xbt_dynar_foreach(as_list_d, i,p_as)
590 create_AS(p_as->id, p_as->mode);
591 // add associated Hosts
592 xbt_dynar_foreach(p_as->host_list_d, j, p_host){
593 create_host(p_host->id, p_host->power_peak, p_host->power_scale,
594 p_host->power_trace, p_host->core, p_host->state_initial,
595 p_host->state_trace);
596 //add to routing model host list
597 surf_route_add_host((char *) p_host->id);
599 // add associated Links
600 xbt_dynar_foreach(p_as->link_list_d, j, p_link){
601 create_link(p_link->id, p_link->bandwidth, p_link->bandwidth_trace,
602 p_link->latency, p_link->latency_trace,
603 p_link->state_initial, p_link->state_trace,
606 // add associated Routes
607 xbt_dynar_foreach(p_as->route_list_d, j, p_route){
608 surf_routing_add_route((char *) p_route->src_id,
609 (char *) p_route->dest_id, p_route->links_id);
613 surf_AS_finalize(p_as->id);
617 surf_add_host_traces();
618 surf_add_link_traces();
620 return 0; // must return 0 ?!!
626 * surf parse bypass platform
627 * through workstation_ptask_L07 Model
630 static int surf_wsL07_parse_bypass_platform()
637 p_route_attr p_route;
639 xbt_dynar_foreach(as_list_d, i, p_as)
642 create_AS(p_as->id, p_as->mode);
644 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
645 create_host_wsL07(p_host->id, p_host->power_peak, p_host->power_scale,
646 p_host->power_trace, p_host->state_initial,
647 p_host->state_trace);
648 //add to routing model host list
649 surf_route_add_host((char *) p_host->id);
652 xbt_dynar_foreach(p_as->link_list_d, j, p_link) {
653 create_link_wsL07(p_link->id, p_link->bandwidth,
654 p_link->bandwidth_trace, p_link->latency,
655 p_link->latency_trace, p_link->state_initial,
656 p_link->state_trace, p_link->policy);
659 xbt_dynar_foreach(p_as->route_list_d, j, p_route) {
660 surf_routing_add_route((char *) p_route->src_id,
661 (char *) p_route->dest_id, p_route->links_id);
665 surf_AS_finalize(p_as->id);
668 surf_wsL07_add_traces();
673 * surf parse bypass application for MSG Module
675 static int surf_parse_bypass_application()
680 xbt_dynar_foreach(as_list_d, i, p_as)
682 xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
683 if (p_host->function)
684 MSG_set_function(p_host->id, p_host->function, p_host->args_list);
693 int console_add_host(lua_State *L)
698 int console_add_link(lua_State *L)
703 int console_add_route(lua_State *L)
708 int console_add_AS(lua_State *L)
713 int console_set_function(lua_State *L)
715 return Host_set_function(L);
718 int console_parse_platform()
720 return surf_parse_bypass_platform();
723 int console_parse_application()
725 return surf_parse_bypass_application();
728 int console_parse_platform_wsL07()
730 return surf_wsL07_parse_bypass_platform();