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 state_init,
39 double power_scale = 1.0;
40 tmgr_trace_t power_trace = NULL;
41 e_surf_resource_state_t state_initial;
42 tmgr_trace_t state_trace;
44 power_scale = power_sc;
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("");
57 current_property_set = xbt_dict_new();
58 surf_host_create_resource(xbt_strdup(id), power_peak, power_scale,
59 power_trace, state_initial, state_trace,
60 current_property_set);
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());
105 *create host resource via workstation_ptask_L07 model [for SimDag]
107 static void create_host_wsL07(const char *id, double power_peak,
108 double power_sc, const char *power_tr,
109 int state_init, const char *state_tr)
111 double power_scale = 1.0;
112 tmgr_trace_t power_trace = NULL;
113 e_surf_resource_state_t state_initial;
114 tmgr_trace_t state_trace;
116 power_scale = power_sc;
117 if (state_init == -1)
118 state_initial = SURF_RESOURCE_OFF;
120 state_initial = SURF_RESOURCE_ON;
122 power_trace = tmgr_trace_new(power_tr);
124 power_trace = tmgr_trace_new("");
126 state_trace = tmgr_trace_new(state_tr);
128 state_trace = tmgr_trace_new("");
129 current_property_set = xbt_dict_new();
130 surf_wsL07_host_create_resource(xbt_strdup(id), power_peak, power_scale,
131 power_trace, state_initial, state_trace,
132 current_property_set);
137 * create link resource via workstation_ptask_L07 model [for SimDag]
140 static void create_link_wsL07(const char *name,
141 double bw_initial, const char *trace,
143 const char *latency_trace, int state_init,
144 const char *state_trace, int policy)
146 tmgr_trace_t bw_trace;
147 tmgr_trace_t lat_trace;
148 e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
149 e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
150 tmgr_trace_t st_trace;
152 bw_trace = tmgr_trace_new(trace);
154 bw_trace = tmgr_trace_new("");
157 lat_trace = tmgr_trace_new(latency_trace);
159 lat_trace = tmgr_trace_new("");
162 st_trace = tmgr_trace_new(state_trace);
164 st_trace = tmgr_trace_new("");
166 if (state_init == -1)
167 state_initial_link = SURF_RESOURCE_OFF;
169 policy_initial_link = SURF_LINK_FATPIPE;
171 surf_wsL07_link_create_resource(xbt_strdup(name), bw_initial, bw_trace,
172 lat_initial, lat_trace,
173 state_initial_link, st_trace,
174 policy_initial_link, xbt_dict_new());
182 static int AS_new(lua_State * L)
186 if (lua_istable(L, 1)) {
187 lua_pushstring(L, "id");
189 id = lua_tostring(L, -1);
192 lua_pushstring(L, "mode");
194 mode = lua_tostring(L, -1);
198 ("Bad Arguments to AS.new, Should be a table with named arguments");
201 AS = malloc(sizeof(AS_attr));
209 * add new host to platform hosts list
211 static int Host_new(lua_State * L)
214 if (xbt_dynar_is_empty(host_list_d))
215 host_list_d = xbt_dynar_new(sizeof(p_host_attr), &xbt_free_ref);
219 const char *power_trace;
220 const char *state_trace;
221 double power, power_scale;
223 //get values from the table passed as argument
224 if (lua_istable(L, -1)) {
227 lua_pushstring(L, "id");
229 id = lua_tostring(L, -1);
233 lua_pushstring(L, "power");
235 power = lua_tonumber(L, -1);
239 lua_pushstring(L, "power_scale");
241 power_scale = lua_tonumber(L, -1);
245 lua_pushstring(L, "power_trace");
247 power_trace = lua_tostring(L, -1);
251 lua_pushstring(L, "state_initial");
253 state_initial = lua_tonumber(L, -1);
257 lua_pushstring(L, "state_trace");
259 state_trace = lua_tostring(L, -1);
264 ("Bad Arguments to create host, Should be a table with named arguments");
268 host = malloc(sizeof(host_attr));
270 host->power_peak = power;
271 host->power_scale = power_scale;
272 host->power_trace = power_trace;
273 host->state_initial = state_initial;
274 host->state_trace = state_trace;
275 host->function = NULL;
276 xbt_dynar_push(host_list_d, &host);
282 * add link to platform links list
284 static int Link_new(lua_State * L) // (id,bandwidth,latency)
287 if (xbt_dynar_is_empty(link_list_d))
288 link_list_d = xbt_dynar_new(sizeof(p_link_attr), &xbt_free_ref);
292 double bandwidth, latency;
293 const char *bandwidth_trace;
294 const char *latency_trace;
295 const char *state_trace;
296 int state_initial, policy;
298 //get values from the table passed as argument
299 if (lua_istable(L, -1)) {
301 lua_pushstring(L, "id");
303 id = lua_tostring(L, -1);
306 // get bandwidth value
307 lua_pushstring(L, "bandwidth");
309 bandwidth = lua_tonumber(L, -1);
313 lua_pushstring(L, "latency");
315 latency = lua_tonumber(L, -1);
318 /*Optional Arguments */
320 //get bandwidth_trace value
321 lua_pushstring(L, "bandwidth_trace");
323 bandwidth_trace = lua_tostring(L, -1);
326 //get latency_trace value
327 lua_pushstring(L, "latency_trace");
329 latency_trace = lua_tostring(L, -1);
332 //get state_trace value
333 lua_pushstring(L, "state_trace");
335 state_trace = lua_tostring(L, -1);
338 //get state_initial value
339 lua_pushstring(L, "state_initial");
341 state_initial = lua_tonumber(L, -1);
345 lua_pushstring(L, "policy");
347 policy = lua_tonumber(L, -1);
352 ("Bad Arguments to create link, Should be a table with named arguments");
356 p_link_attr link = malloc(sizeof(link_attr));
358 link->bandwidth = bandwidth;
359 link->latency = latency;
360 link->bandwidth_trace = bandwidth_trace;
361 link->latency_trace = latency_trace;
362 link->state_trace = state_trace;
363 link->state_initial = state_initial;
364 link->policy = policy;
365 xbt_dynar_push(link_list_d, &link);
371 * add route to platform routes list
373 static int Route_new(lua_State * L) // (src_id,dest_id,links_number,link_table)
375 if (xbt_dynar_is_empty(route_list_d))
376 route_list_d = xbt_dynar_new(sizeof(p_route_attr), &xbt_free_ref);
380 p_route_attr route = malloc(sizeof(route_attr));
383 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)
385 lua_pushstring(L, "src");
387 route->src_id = lua_tostring(L, -1);
390 // get Destination Value
391 lua_pushstring(L, "dest");
393 route->dest_id = lua_tostring(L, -1);
396 // get Links Table (char* to be splited later)
397 lua_pushstring(L, "links");
399 links = lua_tostring(L, -1);
402 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
404 char *tmp_links = malloc(sizeof(char)*strlen(links)+1);//use xbt
405 strcpy(tmp_links,links);
406 link_id = strtok(tmp_links,","); //tmp_link = strtok((char*)links,",");
407 while(link_id != NULL)
409 xbt_dynar_push(route->links_id, &link_id);
410 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.
412 xbt_dynar_push(route_list_d, &route);
416 else { // Route.new is declared as a function
417 //const char* link_id;
418 route->src_id = luaL_checkstring(L, 1);
419 route->dest_id = luaL_checkstring(L, 2);
420 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
422 while (lua_next(L, 3) != 0)
424 link_id = lua_tostring(L, -1);
425 xbt_dynar_push(route->links_id, &link_id);
426 DEBUG2("index = %f , Link_id = %s \n", lua_tonumber(L, -2),
427 lua_tostring(L, -1));
431 //add route to platform's route list
432 xbt_dynar_push(route_list_d, &route);
439 * set function to process
441 static int Host_set_function(lua_State * L) //(host,function,nb_args,list_args)
445 const char *function;
450 if (lua_istable(L, -1)) {
452 lua_pushstring(L, "host");
454 host = lua_tostring(L, -1);
457 lua_pushstring(L, "fct");
459 function = lua_tostring(L, -1);
462 lua_pushstring(L,"args");
464 args = lua_tostring(L,-1);
468 ERROR0("Bad Arguments to create link, Should be a table with named arguments");
472 // look for the index of host in host_list
473 xbt_dynar_foreach(host_list_d, i, p_host) {
474 if (p_host->id == host) {
475 p_host->function = function;
476 p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
477 // split & fill the args list
478 tmp_arg = strtok((char*)args,",");
479 while (tmp_arg != NULL) {
480 xbt_dynar_push(p_host->args_list, &tmp_arg);
481 tmp_arg = strtok(NULL,",");
486 ERROR1("Host : %s Not Found !!", host);
491 * surf parse bypass platform
492 * through CPU/network Models
495 static int surf_parse_bypass_platform()
500 p_route_attr p_route;
503 create_AS(AS->id, AS->mode);
506 xbt_dynar_foreach(host_list_d, i, p_host) {
507 create_host(p_host->id, p_host->power_peak, p_host->power_scale,
508 p_host->power_trace, p_host->state_initial,
509 p_host->state_trace);
510 //add to routing model host list
511 surf_route_add_host((char *) p_host->id);
516 xbt_dynar_foreach(link_list_d, i, p_link) {
517 create_link(p_link->id, p_link->bandwidth, p_link->bandwidth_trace,
518 p_link->latency, p_link->latency_trace,
519 p_link->state_initial, p_link->state_trace,
523 xbt_dynar_foreach(route_list_d, i, p_route) {
524 surf_routing_add_route((char *) p_route->src_id,
525 (char *) p_route->dest_id, p_route->links_id);
530 surf_AS_finalize(AS->id);
533 surf_add_host_traces();
534 surf_add_link_traces();
536 return 0; // must return 0 ?!!
542 * surf parse bypass platform
543 * through workstation_ptask_L07 Model
546 static int surf_wsL07_parse_bypass_platform()
552 p_route_attr p_route;
555 create_AS(AS->id, AS->mode);
558 xbt_dynar_foreach(host_list_d, i, p_host) {
559 create_host_wsL07(p_host->id, p_host->power_peak, p_host->power_scale,
560 p_host->power_trace, p_host->state_initial,
561 p_host->state_trace);
562 //add to routing model host list
563 surf_route_add_host((char *) p_host->id);
567 xbt_dynar_foreach(link_list_d, i, p_link) {
568 create_link_wsL07(p_link->id, p_link->bandwidth,
569 p_link->bandwidth_trace, p_link->latency,
570 p_link->latency_trace, p_link->state_initial,
571 p_link->state_trace, p_link->policy);
574 xbt_dynar_foreach(route_list_d, i, p_route) {
575 surf_routing_add_route((char *) p_route->src_id,
576 (char *) p_route->dest_id, p_route->links_id);
581 surf_AS_finalize(AS->id);
583 surf_wsL07_add_traces();
589 * surf parse bypass application for MSG Module
591 static int surf_parse_bypass_application()
595 xbt_dynar_foreach(host_list_d, i, p_host) {
596 if (p_host->function)
597 MSG_set_function(p_host->id, p_host->function, p_host->args_list);
606 int console_add_host(lua_State *L)
611 int console_add_link(lua_State *L)
616 int console_add_route(lua_State *L)
621 int console_add_AS(lua_State *L)
626 int console_set_function(lua_State *L)
628 return Host_set_function(L);
631 int console_parse_platform()
633 return surf_parse_bypass_platform();
636 int console_parse_application()
638 return surf_parse_bypass_application();
641 int console_parse_platform_wsL07()
643 return surf_wsL07_parse_bypass_platform();