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
49 state_initial = SURF_RESOURCE_OFF;
51 state_initial = SURF_RESOURCE_ON;
53 power_trace = tmgr_trace_new(power_tr);
55 power_trace = tmgr_trace_new("");
57 state_trace = tmgr_trace_new(state_tr);
59 state_trace = tmgr_trace_new("");
60 current_property_set = xbt_dict_new();
61 surf_host_create_resource(xbt_strdup(id), power_peak, power_scale,
62 power_trace, core_nb, state_initial, state_trace,
63 current_property_set);
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("");
132 current_property_set = xbt_dict_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);
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)
189 if (lua_istable(L, 1)) {
190 lua_pushstring(L, "id");
192 id = lua_tostring(L, -1);
195 lua_pushstring(L, "mode");
197 mode = lua_tostring(L, -1);
201 ("Bad Arguments to AS.new, Should be a table with named arguments");
204 AS = malloc(sizeof(AS_attr));
212 * add new host to platform hosts list
214 static int Host_new(lua_State * L)
217 if (xbt_dynar_is_empty(host_list_d))
218 host_list_d = xbt_dynar_new(sizeof(p_host_attr), &xbt_free_ref);
222 const char *power_trace;
223 const char *state_trace;
224 double power, power_scale;
225 int state_initial,core;
226 //get values from the table passed as argument
227 if (lua_istable(L, -1)) {
230 lua_pushstring(L, "id");
232 id = lua_tostring(L, -1);
236 lua_pushstring(L, "power");
238 power = lua_tonumber(L, -1);
242 lua_pushstring(L, "power_scale");
244 power_scale = lua_tonumber(L, -1);
248 lua_pushstring(L, "power_trace");
250 power_trace = lua_tostring(L, -1);
253 lua_pushstring(L, "core");
255 core = lua_tonumber(L, -1);
259 lua_pushstring(L, "state_initial");
261 state_initial = lua_tonumber(L, -1);
265 lua_pushstring(L, "state_trace");
267 state_trace = lua_tostring(L, -1);
272 ("Bad Arguments to create host, Should be a table with named arguments");
276 host = malloc(sizeof(host_attr));
278 host->power_peak = power;
279 host->power_scale = power_scale;
280 host->power_trace = power_trace;
282 host->state_initial = state_initial;
283 host->state_trace = state_trace;
284 host->function = NULL;
285 xbt_dynar_push(host_list_d, &host);
291 * add link to platform links list
293 static int Link_new(lua_State * L) // (id,bandwidth,latency)
296 if (xbt_dynar_is_empty(link_list_d))
297 link_list_d = xbt_dynar_new(sizeof(p_link_attr), &xbt_free_ref);
301 double bandwidth, latency;
302 const char *bandwidth_trace;
303 const char *latency_trace;
304 const char *state_trace;
305 int state_initial, policy;
307 //get values from the table passed as argument
308 if (lua_istable(L, -1)) {
310 lua_pushstring(L, "id");
312 id = lua_tostring(L, -1);
315 // get bandwidth value
316 lua_pushstring(L, "bandwidth");
318 bandwidth = lua_tonumber(L, -1);
322 lua_pushstring(L, "latency");
324 latency = lua_tonumber(L, -1);
327 /*Optional Arguments */
329 //get bandwidth_trace value
330 lua_pushstring(L, "bandwidth_trace");
332 bandwidth_trace = lua_tostring(L, -1);
335 //get latency_trace value
336 lua_pushstring(L, "latency_trace");
338 latency_trace = lua_tostring(L, -1);
341 //get state_trace value
342 lua_pushstring(L, "state_trace");
344 state_trace = lua_tostring(L, -1);
347 //get state_initial value
348 lua_pushstring(L, "state_initial");
350 state_initial = lua_tonumber(L, -1);
354 lua_pushstring(L, "policy");
356 policy = lua_tonumber(L, -1);
361 ("Bad Arguments to create link, Should be a table with named arguments");
365 p_link_attr link = malloc(sizeof(link_attr));
367 link->bandwidth = bandwidth;
368 link->latency = latency;
369 link->bandwidth_trace = bandwidth_trace;
370 link->latency_trace = latency_trace;
371 link->state_trace = state_trace;
372 link->state_initial = state_initial;
373 link->policy = policy;
374 xbt_dynar_push(link_list_d, &link);
380 * add route to platform routes list
382 static int Route_new(lua_State * L) // (src_id,dest_id,links_number,link_table)
384 if (xbt_dynar_is_empty(route_list_d))
385 route_list_d = xbt_dynar_new(sizeof(p_route_attr), &xbt_free_ref);
389 p_route_attr route = malloc(sizeof(route_attr));
392 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)
394 lua_pushstring(L, "src");
396 route->src_id = lua_tostring(L, -1);
399 // get Destination Value
400 lua_pushstring(L, "dest");
402 route->dest_id = lua_tostring(L, -1);
405 // get Links Table (char* to be splited later)
406 lua_pushstring(L, "links");
408 links = lua_tostring(L, -1);
411 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
413 char *tmp_links = xbt_strdup(links);
414 link_id = strtok(tmp_links,","); //tmp_link = strtok((char*)links,",");
415 while(link_id != NULL)
417 xbt_dynar_push(route->links_id, &link_id);
418 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.
420 xbt_dynar_push(route_list_d, &route);
424 else { // Route.new is declared as a function
425 //const char* link_id;
426 route->src_id = luaL_checkstring(L, 1);
427 route->dest_id = luaL_checkstring(L, 2);
428 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
430 while (lua_next(L, 3) != 0)
432 link_id = lua_tostring(L, -1);
433 xbt_dynar_push(route->links_id, &link_id);
434 XBT_DEBUG("index = %f , Link_id = %s \n", lua_tonumber(L, -2),
435 lua_tostring(L, -1));
439 //add route to platform's route list
440 xbt_dynar_push(route_list_d, &route);
447 * set function to process
449 static int Host_set_function(lua_State * L) //(host,function,nb_args,list_args)
453 const char *function;
458 if (lua_istable(L, -1)) {
460 lua_pushstring(L, "host");
462 host = lua_tostring(L, -1);
465 lua_pushstring(L, "fct");
467 function = lua_tostring(L, -1);
470 lua_pushstring(L,"args");
472 args = lua_tostring(L,-1);
476 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
480 // look for the index of host in host_list
481 xbt_dynar_foreach(host_list_d, i, p_host) {
482 if (p_host->id == host) {
483 p_host->function = function;
484 p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
485 // split & fill the args list
486 tmp_arg = strtok((char*)args,",");
487 while (tmp_arg != NULL) {
488 xbt_dynar_push(p_host->args_list, &tmp_arg);
489 tmp_arg = strtok(NULL,",");
494 XBT_ERROR("Host : %s Not Found !!", host);
499 * surf parse bypass platform
500 * through CPU/network Models
503 static int surf_parse_bypass_platform()
508 p_route_attr p_route;
511 create_AS(AS->id, AS->mode);
514 xbt_dynar_foreach(host_list_d, i, p_host) {
515 create_host(p_host->id, p_host->power_peak, p_host->power_scale,
516 p_host->power_trace, p_host->core, p_host->state_initial,
517 p_host->state_trace);
518 //add to routing model host list
519 surf_route_add_host((char *) p_host->id);
524 xbt_dynar_foreach(link_list_d, i, p_link) {
525 create_link(p_link->id, p_link->bandwidth, p_link->bandwidth_trace,
526 p_link->latency, p_link->latency_trace,
527 p_link->state_initial, p_link->state_trace,
531 xbt_dynar_foreach(route_list_d, i, p_route) {
532 surf_routing_add_route((char *) p_route->src_id,
533 (char *) p_route->dest_id, p_route->links_id);
538 surf_AS_finalize(AS->id);
541 surf_add_host_traces();
542 surf_add_link_traces();
544 return 0; // must return 0 ?!!
550 * surf parse bypass platform
551 * through workstation_ptask_L07 Model
554 static int surf_wsL07_parse_bypass_platform()
560 p_route_attr p_route;
563 create_AS(AS->id, AS->mode);
566 xbt_dynar_foreach(host_list_d, i, p_host) {
567 create_host_wsL07(p_host->id, p_host->power_peak, p_host->power_scale,
568 p_host->power_trace, p_host->state_initial,
569 p_host->state_trace);
570 //add to routing model host list
571 surf_route_add_host((char *) p_host->id);
575 xbt_dynar_foreach(link_list_d, i, p_link) {
576 create_link_wsL07(p_link->id, p_link->bandwidth,
577 p_link->bandwidth_trace, p_link->latency,
578 p_link->latency_trace, p_link->state_initial,
579 p_link->state_trace, p_link->policy);
582 xbt_dynar_foreach(route_list_d, i, p_route) {
583 surf_routing_add_route((char *) p_route->src_id,
584 (char *) p_route->dest_id, p_route->links_id);
589 surf_AS_finalize(AS->id);
591 surf_wsL07_add_traces();
597 * surf parse bypass application for MSG Module
599 static int surf_parse_bypass_application()
603 xbt_dynar_foreach(host_list_d, i, p_host) {
604 if (p_host->function)
605 MSG_set_function(p_host->id, p_host->function, p_host->args_list);
614 int console_add_host(lua_State *L)
619 int console_add_link(lua_State *L)
624 int console_add_route(lua_State *L)
629 int console_add_AS(lua_State *L)
634 int console_set_function(lua_State *L)
636 return Host_set_function(L);
639 int console_parse_platform()
641 return surf_parse_bypass_platform();
644 int console_parse_application()
646 return surf_parse_bypass_application();
649 int console_parse_platform_wsL07()
651 return surf_wsL07_parse_bypass_platform();