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"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(luax, bindings, "Lua Bindings");
15 static xbt_dynar_t host_list_d;
16 static xbt_dynar_t link_list_d;
17 static xbt_dynar_t route_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 state_init,
37 double power_scale = 1.0;
38 tmgr_trace_t power_trace = NULL;
39 e_surf_resource_state_t state_initial;
40 tmgr_trace_t state_trace;
42 power_scale = power_sc;
44 state_initial = SURF_RESOURCE_OFF;
46 state_initial = SURF_RESOURCE_ON;
48 power_trace = tmgr_trace_new(power_tr);
50 power_trace = tmgr_trace_new("");
52 state_trace = tmgr_trace_new(state_tr);
54 state_trace = tmgr_trace_new("");
55 current_property_set = xbt_dict_new();
56 surf_host_create_resource(xbt_strdup(id), power_peak, power_scale,
57 power_trace, state_initial, state_trace,
58 current_property_set);
63 * create link resource via network model
65 static void create_link(const char *name,
66 double bw_initial, const char *trace,
67 double lat_initial, const char *latency_trace,
68 int state_init, const char *state_trace,
71 tmgr_trace_t bw_trace;
72 tmgr_trace_t lat_trace;
73 e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
74 e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
75 tmgr_trace_t st_trace;
77 bw_trace = tmgr_trace_new(trace);
79 bw_trace = tmgr_trace_new("");
82 lat_trace = tmgr_trace_new(latency_trace);
84 lat_trace = tmgr_trace_new("");
87 st_trace = tmgr_trace_new(state_trace);
89 st_trace = tmgr_trace_new("");
92 state_initial_link = SURF_RESOURCE_OFF;
94 policy_initial_link = SURF_LINK_FATPIPE;
96 surf_link_create_resource(xbt_strdup(name), bw_initial, bw_trace,
97 lat_initial, lat_trace, state_initial_link,
98 st_trace, policy_initial_link, xbt_dict_new());
103 *create host resource via workstation_ptask_L07 model [for SimDag]
105 static void create_host_wsL07(const char *id, double power_peak,
106 double power_sc, const char *power_tr,
107 int state_init, const char *state_tr)
109 double power_scale = 1.0;
110 tmgr_trace_t power_trace = NULL;
111 e_surf_resource_state_t state_initial;
112 tmgr_trace_t state_trace;
114 power_scale = power_sc;
115 if (state_init == -1)
116 state_initial = SURF_RESOURCE_OFF;
118 state_initial = SURF_RESOURCE_ON;
120 power_trace = tmgr_trace_new(power_tr);
122 power_trace = tmgr_trace_new("");
124 state_trace = tmgr_trace_new(state_tr);
126 state_trace = tmgr_trace_new("");
127 current_property_set = xbt_dict_new();
128 surf_wsL07_host_create_resource(xbt_strdup(id), power_peak, power_scale,
129 power_trace, state_initial, state_trace,
130 current_property_set);
135 * create link resource via workstation_ptask_L07 model [for SimDag]
138 static void create_link_wsL07(const char *name,
139 double bw_initial, const char *trace,
141 const char *latency_trace, int state_init,
142 const char *state_trace, int policy)
144 tmgr_trace_t bw_trace;
145 tmgr_trace_t lat_trace;
146 e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
147 e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
148 tmgr_trace_t st_trace;
150 bw_trace = tmgr_trace_new(trace);
152 bw_trace = tmgr_trace_new("");
155 lat_trace = tmgr_trace_new(latency_trace);
157 lat_trace = tmgr_trace_new("");
160 st_trace = tmgr_trace_new(state_trace);
162 st_trace = tmgr_trace_new("");
164 if (state_init == -1)
165 state_initial_link = SURF_RESOURCE_OFF;
167 policy_initial_link = SURF_LINK_FATPIPE;
169 surf_wsL07_link_create_resource(xbt_strdup(name), bw_initial, bw_trace,
170 lat_initial, lat_trace,
171 state_initial_link, st_trace,
172 policy_initial_link, xbt_dict_new());
180 static int AS_new(lua_State * L)
184 if (lua_istable(L, 1)) {
185 lua_pushstring(L, "id");
187 id = lua_tostring(L, -1);
190 lua_pushstring(L, "mode");
192 mode = lua_tostring(L, -1);
196 ("Bad Arguments to AS.new, Should be a table with named arguments");
199 AS = malloc(sizeof(AS_attr));
207 * add new host to platform hosts list
209 static int Host_new(lua_State * L)
212 if (xbt_dynar_is_empty(host_list_d))
213 host_list_d = xbt_dynar_new(sizeof(p_host_attr), &xbt_free_ref);
217 const char *power_trace;
218 const char *state_trace;
219 double power, power_scale;
221 //get values from the table passed as argument
222 if (lua_istable(L, -1)) {
225 lua_pushstring(L, "id");
227 id = lua_tostring(L, -1);
231 lua_pushstring(L, "power");
233 power = lua_tonumber(L, -1);
237 lua_pushstring(L, "power_scale");
239 power_scale = lua_tonumber(L, -1);
243 lua_pushstring(L, "power_trace");
245 power_trace = lua_tostring(L, -1);
249 lua_pushstring(L, "state_initial");
251 state_initial = lua_tonumber(L, -1);
255 lua_pushstring(L, "state_trace");
257 state_trace = lua_tostring(L, -1);
262 ("Bad Arguments to create host, Should be a table with named arguments");
266 host = malloc(sizeof(host_attr));
268 host->power_peak = power;
269 host->power_scale = power_scale;
270 host->power_trace = power_trace;
271 host->state_initial = state_initial;
272 host->state_trace = state_trace;
273 host->function = NULL;
274 xbt_dynar_push(host_list_d, &host);
280 * add link to platform links list
282 static int Link_new(lua_State * L) // (id,bandwidth,latency)
284 if (xbt_dynar_is_empty(link_list_d))
285 link_list_d = xbt_dynar_new(sizeof(p_link_attr), &xbt_free_ref);
288 double bandwidth, latency;
289 const char *bandwidth_trace;
290 const char *latency_trace;
291 const char *state_trace;
292 int state_initial, policy;
294 //get values from the table passed as argument
295 if (lua_istable(L, -1)) {
297 lua_pushstring(L, "id");
299 id = lua_tostring(L, -1);
302 // get bandwidth value
303 lua_pushstring(L, "bandwidth");
305 bandwidth = lua_tonumber(L, -1);
309 lua_pushstring(L, "latency");
311 latency = lua_tonumber(L, -1);
314 /*Optional Arguments */
316 //get bandwidth_trace value
317 lua_pushstring(L, "bandwidth_trace");
319 bandwidth_trace = lua_tostring(L, -1);
322 //get latency_trace value
323 lua_pushstring(L, "latency_trace");
325 latency_trace = lua_tostring(L, -1);
328 //get state_trace value
329 lua_pushstring(L, "state_trace");
331 state_trace = lua_tostring(L, -1);
334 //get state_initial value
335 lua_pushstring(L, "state_initial");
337 state_initial = lua_tonumber(L, -1);
342 lua_pushstring(L, "policy");
344 policy = lua_tonumber(L, -1);
349 ("Bad Arguments to create link, Should be a table with named arguments");
353 p_link_attr link = malloc(sizeof(link_attr));
355 link->bandwidth = bandwidth;
356 link->latency = latency;
357 link->bandwidth_trace = bandwidth_trace;
358 link->latency_trace = latency_trace;
359 link->state_trace = state_trace;
360 link->state_initial = state_initial;
361 link->policy = policy;
362 xbt_dynar_push(link_list_d, &link);
367 * add route to platform routes list
369 static int Route_new(lua_State * L) // (src_id,dest_id,links_number,link_table)
371 if (xbt_dynar_is_empty(route_list_d))
372 route_list_d = xbt_dynar_new(sizeof(p_route_attr), &xbt_free_ref);
374 p_route_attr route = malloc(sizeof(route_attr));
375 route->src_id = luaL_checkstring(L, 1);
376 route->dest_id = luaL_checkstring(L, 2);
377 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
379 while (lua_next(L, 3) != 0) {
380 link_id = lua_tostring(L, -1);
381 xbt_dynar_push(route->links_id, &link_id);
382 DEBUG2("index = %f , Link_id = %s \n", lua_tonumber(L, -2),
383 lua_tostring(L, -1));
388 //add route to platform's route list
389 xbt_dynar_push(route_list_d, &route);
394 * set function to process
396 static int Host_set_function(lua_State * L) //(host,function,nb_args,list_args)
398 // look for the index of host in host_list
399 const char *host_id = luaL_checkstring(L, 1);
400 const char *argument;
404 xbt_dynar_foreach(host_list_d, i, p_host) {
405 if (p_host->id == host_id) {
406 p_host->function = luaL_checkstring(L, 2);
407 if (lua_istable(L, 3)) {
408 p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
409 // fill the args list
412 while (lua_next(L, 3) != 0) {
413 argument = lua_tostring(L, -1);
414 xbt_dynar_push(p_host->args_list, &argument);
415 DEBUG2("index = %f , Arg_id = %s \n", lua_tonumber(L, -2),
416 lua_tostring(L, -1));
425 ERROR1("Host : %s Not Found !!", host_id);
430 * surf parse bypass platform
431 * through CPU/network Models
434 static int surf_parse_bypass_platform()
439 p_route_attr p_route;
442 create_AS(AS->id, AS->mode);
446 xbt_dynar_foreach(host_list_d, i, p_host) {
447 create_host(p_host->id, p_host->power_peak, p_host->power_scale,
448 p_host->power_trace, p_host->state_initial,
449 p_host->state_trace);
450 //add to routing model host list
451 surf_route_add_host((char *) p_host->id);
456 xbt_dynar_foreach(link_list_d, i, p_link) {
457 create_link(p_link->id, p_link->bandwidth, p_link->bandwidth_trace,
458 p_link->latency, p_link->latency_trace,
459 p_link->state_initial, p_link->state_trace,
463 xbt_dynar_foreach(route_list_d, i, p_route) {
464 surf_routing_add_route((char *) p_route->src_id,
465 (char *) p_route->dest_id, p_route->links_id);
470 surf_AS_finalize(AS->id);
473 surf_add_host_traces();
474 surf_add_link_traces();
476 return 0; // must return 0 ?!!
482 * surf parse bypass platform
483 * through workstation_ptask_L07 Model
486 static int surf_wsL07_parse_bypass_platform()
491 p_route_attr p_route;
494 create_AS(AS->id, AS->mode);
497 xbt_dynar_foreach(host_list_d, i, p_host) {
498 create_host_wsL07(p_host->id, p_host->power_peak, p_host->power_scale,
499 p_host->power_trace, p_host->state_initial,
500 p_host->state_trace);
501 //add to routing model host list
502 surf_route_add_host((char *) p_host->id);
506 xbt_dynar_foreach(link_list_d, i, p_link) {
507 create_link_wsL07(p_link->id, p_link->bandwidth,
508 p_link->bandwidth_trace, p_link->latency,
509 p_link->latency_trace, p_link->state_initial,
510 p_link->state_trace, p_link->policy);
513 xbt_dynar_foreach(route_list_d, i, p_route) {
514 surf_routing_add_route((char *) p_route->src_id,
515 (char *) p_route->dest_id, p_route->links_id);
520 surf_AS_finalize(AS->id);
522 surf_wsL07_add_traces();
528 * surf parse bypass application for MSG Module
530 static int surf_parse_bypass_application()
534 xbt_dynar_foreach(host_list_d, i, p_host) {
535 if (p_host->function)
536 MSG_set_function(p_host->id, p_host->function, p_host->args_list);
546 int console_add_host(lua_State *L)
551 int console_add_link(lua_State *L)
556 int console_add_route(lua_State *L)
561 int console_add_AS(lua_State *L)
566 int console_set_function(lua_State *L)
568 return Host_set_function(L);
571 int console_parse_platform()
573 return surf_parse_bypass_platform();
576 int console_parse_application()
578 return surf_parse_bypass_application();
581 int console_parse_platform_wsL07()
583 return surf_wsL07_parse_bypass_platform();