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 host_list_d;
18 static xbt_dynar_t link_list_d;
19 static xbt_dynar_t route_list_d;
22 * Initialize platform model routing
25 static void create_AS(const char *id, const char *mode)
27 surf_AS_new(id, mode);
31 * create host resource via CPU model [for MSG]
34 static void create_host(const char *id, double power_peak, double power_sc,
35 const char *power_tr,int core,int state_init,
39 double power_scale = 1.0;
40 int core_nb = 1; //default value
41 tmgr_trace_t power_trace = NULL;
42 e_surf_resource_state_t state_initial;
43 tmgr_trace_t state_trace;
45 power_scale = power_sc;
47 core_nb = core; //default value
50 state_initial = SURF_RESOURCE_OFF;
52 state_initial = SURF_RESOURCE_ON;
54 power_trace = tmgr_trace_new(power_tr);
56 power_trace = tmgr_trace_new("");
58 state_trace = tmgr_trace_new(state_tr);
60 state_trace = tmgr_trace_new("");
61 current_property_set = xbt_dict_new();
62 surf_host_create_resource(xbt_strdup(id), power_peak, power_scale,
63 power_trace, core_nb, state_initial, state_trace,
64 current_property_set);
69 * create link resource via network model
71 static void create_link(const char *name,
72 double bw_initial, const char *trace,
73 double lat_initial, const char *latency_trace,
74 int state_init, const char *state_trace,
77 tmgr_trace_t bw_trace;
78 tmgr_trace_t lat_trace;
79 e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
80 e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
81 tmgr_trace_t st_trace;
83 bw_trace = tmgr_trace_new(trace);
85 bw_trace = tmgr_trace_new("");
88 lat_trace = tmgr_trace_new(latency_trace);
90 lat_trace = tmgr_trace_new("");
93 st_trace = tmgr_trace_new(state_trace);
95 st_trace = tmgr_trace_new("");
98 state_initial_link = SURF_RESOURCE_OFF;
100 policy_initial_link = SURF_LINK_FATPIPE;
102 surf_link_create_resource(xbt_strdup(name), bw_initial, bw_trace,
103 lat_initial, lat_trace, state_initial_link,
104 st_trace, policy_initial_link, xbt_dict_new());
109 *create host resource via workstation_ptask_L07 model [for SimDag]
111 static void create_host_wsL07(const char *id, double power_peak,
112 double power_sc, const char *power_tr,
113 int state_init, const char *state_tr)
115 double power_scale = 1.0;
116 tmgr_trace_t power_trace = NULL;
117 e_surf_resource_state_t state_initial;
118 tmgr_trace_t state_trace;
120 power_scale = power_sc;
121 if (state_init == -1)
122 state_initial = SURF_RESOURCE_OFF;
124 state_initial = SURF_RESOURCE_ON;
126 power_trace = tmgr_trace_new(power_tr);
128 power_trace = tmgr_trace_new("");
130 state_trace = tmgr_trace_new(state_tr);
132 state_trace = tmgr_trace_new("");
133 current_property_set = xbt_dict_new();
134 surf_wsL07_host_create_resource(xbt_strdup(id), power_peak, power_scale,
135 power_trace, state_initial, state_trace,
136 current_property_set);
141 * create link resource via workstation_ptask_L07 model [for SimDag]
144 static void create_link_wsL07(const char *name,
145 double bw_initial, const char *trace,
147 const char *latency_trace, int state_init,
148 const char *state_trace, int policy)
150 tmgr_trace_t bw_trace;
151 tmgr_trace_t lat_trace;
152 e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
153 e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
154 tmgr_trace_t st_trace;
156 bw_trace = tmgr_trace_new(trace);
158 bw_trace = tmgr_trace_new("");
161 lat_trace = tmgr_trace_new(latency_trace);
163 lat_trace = tmgr_trace_new("");
166 st_trace = tmgr_trace_new(state_trace);
168 st_trace = tmgr_trace_new("");
170 if (state_init == -1)
171 state_initial_link = SURF_RESOURCE_OFF;
173 policy_initial_link = SURF_LINK_FATPIPE;
175 surf_wsL07_link_create_resource(xbt_strdup(name), bw_initial, bw_trace,
176 lat_initial, lat_trace,
177 state_initial_link, st_trace,
178 policy_initial_link, xbt_dict_new());
186 static int AS_new(lua_State * L)
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));
213 * add new host to platform hosts list
215 static int Host_new(lua_State * L)
218 if (xbt_dynar_is_empty(host_list_d))
219 host_list_d = xbt_dynar_new(sizeof(p_host_attr), &xbt_free_ref);
223 const char *power_trace;
224 const char *state_trace;
225 double power, power_scale;
226 int state_initial,core;
227 //get values from the table passed as argument
228 if (lua_istable(L, -1)) {
231 lua_pushstring(L, "id");
233 id = lua_tostring(L, -1);
237 lua_pushstring(L, "power");
239 power = lua_tonumber(L, -1);
243 lua_pushstring(L, "power_scale");
245 power_scale = lua_tonumber(L, -1);
249 lua_pushstring(L, "power_trace");
251 power_trace = lua_tostring(L, -1);
254 lua_pushstring(L, "core");
256 core = lua_tonumber(L, -1);
260 lua_pushstring(L, "state_initial");
262 state_initial = lua_tonumber(L, -1);
266 lua_pushstring(L, "state_trace");
268 state_trace = lua_tostring(L, -1);
273 ("Bad Arguments to create host, Should be a table with named arguments");
277 host = malloc(sizeof(host_attr));
279 host->power_peak = power;
280 host->power_scale = power_scale;
281 host->power_trace = power_trace;
283 host->state_initial = state_initial;
284 host->state_trace = state_trace;
285 host->function = NULL;
286 xbt_dynar_push(host_list_d, &host);
292 * add link to platform links list
294 static int Link_new(lua_State * L) // (id,bandwidth,latency)
297 if (xbt_dynar_is_empty(link_list_d))
298 link_list_d = xbt_dynar_new(sizeof(p_link_attr), &xbt_free_ref);
302 double bandwidth, latency;
303 const char *bandwidth_trace;
304 const char *latency_trace;
305 const char *state_trace;
306 int state_initial, policy;
308 //get values from the table passed as argument
309 if (lua_istable(L, -1)) {
311 lua_pushstring(L, "id");
313 id = lua_tostring(L, -1);
316 // get bandwidth value
317 lua_pushstring(L, "bandwidth");
319 bandwidth = lua_tonumber(L, -1);
323 lua_pushstring(L, "latency");
325 latency = lua_tonumber(L, -1);
328 /*Optional Arguments */
330 //get bandwidth_trace value
331 lua_pushstring(L, "bandwidth_trace");
333 bandwidth_trace = lua_tostring(L, -1);
336 //get latency_trace value
337 lua_pushstring(L, "latency_trace");
339 latency_trace = lua_tostring(L, -1);
342 //get state_trace value
343 lua_pushstring(L, "state_trace");
345 state_trace = lua_tostring(L, -1);
348 //get state_initial value
349 lua_pushstring(L, "state_initial");
351 state_initial = lua_tonumber(L, -1);
355 lua_pushstring(L, "policy");
357 policy = lua_tonumber(L, -1);
362 ("Bad Arguments to create link, Should be a table with named arguments");
366 p_link_attr link = malloc(sizeof(link_attr));
368 link->bandwidth = bandwidth;
369 link->latency = latency;
370 link->bandwidth_trace = bandwidth_trace;
371 link->latency_trace = latency_trace;
372 link->state_trace = state_trace;
373 link->state_initial = state_initial;
374 link->policy = policy;
375 xbt_dynar_push(link_list_d, &link);
381 * add route to platform routes list
383 static int Route_new(lua_State * L) // (src_id,dest_id,links_number,link_table)
385 if (xbt_dynar_is_empty(route_list_d))
386 route_list_d = xbt_dynar_new(sizeof(p_route_attr), &xbt_free_ref);
390 p_route_attr route = malloc(sizeof(route_attr));
393 if (!lua_istable(L, 3)) { // if Route.new is declared as an indexed table (FIXME : we check the third arg if it's not a table)
395 lua_pushstring(L, "src");
397 route->src_id = lua_tostring(L, -1);
400 // get Destination Value
401 lua_pushstring(L, "dest");
403 route->dest_id = lua_tostring(L, -1);
406 // get Links Table (char* to be splited later)
407 lua_pushstring(L, "links");
409 links = lua_tostring(L, -1);
412 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
414 char *tmp_links = malloc(sizeof(char)*strlen(links)+1);//use xbt
415 strcpy(tmp_links,links);
416 link_id = strtok(tmp_links,","); //tmp_link = strtok((char*)links,",");
417 while(link_id != NULL)
419 xbt_dynar_push(route->links_id, &link_id);
420 link_id = strtok(NULL,","); //Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.
422 xbt_dynar_push(route_list_d, &route);
426 else { // Route.new is declared as a function
427 //const char* link_id;
428 route->src_id = luaL_checkstring(L, 1);
429 route->dest_id = luaL_checkstring(L, 2);
430 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
432 while (lua_next(L, 3) != 0)
434 link_id = lua_tostring(L, -1);
435 xbt_dynar_push(route->links_id, &link_id);
436 DEBUG2("index = %f , Link_id = %s \n", lua_tonumber(L, -2),
437 lua_tostring(L, -1));
441 //add route to platform's route list
442 xbt_dynar_push(route_list_d, &route);
449 * set function to process
451 static int Host_set_function(lua_State * L) //(host,function,nb_args,list_args)
455 const char *function;
460 if (lua_istable(L, -1)) {
462 lua_pushstring(L, "host");
464 host = lua_tostring(L, -1);
467 lua_pushstring(L, "fct");
469 function = lua_tostring(L, -1);
472 lua_pushstring(L,"args");
474 args = lua_tostring(L,-1);
478 ERROR0("Bad Arguments to create link, Should be a table with named arguments");
482 // look for the index of host in host_list
483 xbt_dynar_foreach(host_list_d, i, p_host) {
484 if (p_host->id == host) {
485 p_host->function = function;
486 p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
487 // split & fill the args list
488 tmp_arg = strtok((char*)args,",");
489 while (tmp_arg != NULL) {
490 xbt_dynar_push(p_host->args_list, &tmp_arg);
491 tmp_arg = strtok(NULL,",");
496 ERROR1("Host : %s Not Found !!", host);
501 * surf parse bypass platform
502 * through CPU/network Models
505 static int surf_parse_bypass_platform()
510 p_route_attr p_route;
513 create_AS(AS->id, AS->mode);
516 xbt_dynar_foreach(host_list_d, i, p_host) {
517 create_host(p_host->id, p_host->power_peak, p_host->power_scale,
518 p_host->power_trace, p_host->core, p_host->state_initial,
519 p_host->state_trace);
520 //add to routing model host list
521 surf_route_add_host((char *) p_host->id);
526 xbt_dynar_foreach(link_list_d, i, p_link) {
527 create_link(p_link->id, p_link->bandwidth, p_link->bandwidth_trace,
528 p_link->latency, p_link->latency_trace,
529 p_link->state_initial, p_link->state_trace,
533 xbt_dynar_foreach(route_list_d, i, p_route) {
534 surf_routing_add_route((char *) p_route->src_id,
535 (char *) p_route->dest_id, p_route->links_id);
540 surf_AS_finalize(AS->id);
543 surf_add_host_traces();
544 surf_add_link_traces();
546 return 0; // must return 0 ?!!
552 * surf parse bypass platform
553 * through workstation_ptask_L07 Model
556 static int surf_wsL07_parse_bypass_platform()
562 p_route_attr p_route;
565 create_AS(AS->id, AS->mode);
568 xbt_dynar_foreach(host_list_d, i, p_host) {
569 create_host_wsL07(p_host->id, p_host->power_peak, p_host->power_scale,
570 p_host->power_trace, p_host->state_initial,
571 p_host->state_trace);
572 //add to routing model host list
573 surf_route_add_host((char *) p_host->id);
577 xbt_dynar_foreach(link_list_d, i, p_link) {
578 create_link_wsL07(p_link->id, p_link->bandwidth,
579 p_link->bandwidth_trace, p_link->latency,
580 p_link->latency_trace, p_link->state_initial,
581 p_link->state_trace, p_link->policy);
584 xbt_dynar_foreach(route_list_d, i, p_route) {
585 surf_routing_add_route((char *) p_route->src_id,
586 (char *) p_route->dest_id, p_route->links_id);
591 surf_AS_finalize(AS->id);
593 surf_wsL07_add_traces();
599 * surf parse bypass application for MSG Module
601 static int surf_parse_bypass_application()
605 xbt_dynar_foreach(host_list_d, i, p_host) {
606 if (p_host->function)
607 MSG_set_function(p_host->id, p_host->function, p_host->args_list);
616 int console_add_host(lua_State *L)
621 int console_add_link(lua_State *L)
626 int console_add_route(lua_State *L)
631 int console_add_AS(lua_State *L)
636 int console_set_function(lua_State *L)
638 return Host_set_function(L);
641 int console_parse_platform()
643 return surf_parse_bypass_platform();
646 int console_parse_application()
648 return surf_parse_bypass_application();
651 int console_parse_platform_wsL07()
653 return surf_wsL07_parse_bypass_platform();