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"
10 #include "simgrid/platf.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_console, bindings, "Lua Bindings");
16 int console_open(lua_State *L) {
19 surf_parse_init_callbacks();
23 int console_close(lua_State *L) {
29 int console_add_host(lua_State *L) {
30 s_sg_platf_host_cbarg_t host;
31 memset(&host,0,sizeof(host));
34 // we get values from the table passed as argument
35 if (!lua_istable(L, -1)) {
37 ("Bad Arguments to create host, Should be a table with named arguments");
42 lua_pushstring(L, "id");
44 host.id = lua_tostring(L, -1);
48 lua_pushstring(L, "power");
50 host.power_peak = lua_tonumber(L, -1);
54 lua_pushstring(L, "power_scale");
56 host.power_scale = lua_tonumber(L, -1);
60 lua_pushstring(L, "power_trace");
62 host.power_trace = tmgr_trace_new(lua_tostring(L, -1));
65 lua_pushstring(L, "core");
67 host.core_amount = lua_tonumber(L, -1);
68 if (host.core_amount == 0)
73 lua_pushstring(L, "state_initial");
75 state = lua_tonumber(L, -1);
78 host.initial_state = SURF_RESOURCE_ON;
80 host.initial_state = SURF_RESOURCE_OFF;
83 lua_pushstring(L, "state_trace");
85 host.state_trace = tmgr_trace_new(lua_tostring(L, -1));
88 sg_platf_new_host(&host);
93 int console_add_link(lua_State *L) {
94 s_sg_platf_link_cbarg_t link;
95 memset(&link,0,sizeof(link));
99 if (! lua_istable(L, -1)) {
100 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
105 lua_pushstring(L, "id");
107 link.id = lua_tostring(L, -1);
110 // get bandwidth value
111 lua_pushstring(L, "bandwidth");
113 link.bandwidth = lua_tonumber(L, -1);
117 lua_pushstring(L, "latency");
119 link.latency = lua_tonumber(L, -1);
122 /*Optional Arguments */
124 //get bandwidth_trace value
125 lua_pushstring(L, "bandwidth_trace");
127 link.bandwidth_trace = tmgr_trace_new(lua_tostring(L, -1));
130 //get latency_trace value
131 lua_pushstring(L, "latency_trace");
133 link.latency_trace = tmgr_trace_new(lua_tostring(L, -1));
136 //get state_trace value
137 lua_pushstring(L, "state_trace");
139 link.state_trace = tmgr_trace_new(lua_tostring(L, -1));
142 //get state_initial value
143 lua_pushstring(L, "state_initial");
145 if (lua_tonumber(L, -1))
146 link.state = SURF_RESOURCE_ON;
148 link.state = SURF_RESOURCE_OFF;
152 lua_pushstring(L, "policy");
154 policy = lua_tostring(L, -1);
156 if (policy && !strcmp(policy,"FULLDUPLEX")) {
157 link.policy = SURF_LINK_FULLDUPLEX;
158 } else if (policy && !strcmp(policy,"FATPIPE")) {
159 link.policy = SURF_LINK_FATPIPE;
161 link.policy = SURF_LINK_SHARED;
164 sg_platf_new_link(&link);
169 * add Router to AS components
171 int console_add_router(lua_State* L) {
172 s_sg_platf_router_cbarg_t router;
173 memset(&router,0,sizeof(router));
175 if (! lua_istable(L, -1)) {
176 XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
180 lua_pushstring(L, "id");
182 router.id = lua_tostring(L, -1);
185 lua_pushstring(L,"coord");
187 router.coord = lua_tostring(L, -1);
190 sg_platf_new_router(&router);
195 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
197 int console_add_route(lua_State *L) {
198 static int AX_ptr = 0;
199 static int surfxml_bufferstack_size = 2048;
201 /* allocating memory for the buffer, I think 2kB should be enough */
202 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
211 if (! lua_istable(L, -1)) {
212 XBT_ERROR("Bad Arguments to create a route, Should be a table with named arguments");
216 lua_pushstring(L,"src");
218 src = lua_tostring(L, -1);
221 lua_pushstring(L,"dest");
223 dst = lua_tostring(L, -1);
226 lua_pushstring(L,"links");
228 links = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
229 if (xbt_dynar_is_empty(links))
230 xbt_dynar_push_as(links,char*,xbt_strdup(lua_tostring(L, -1)));
233 lua_pushstring(L,"symmetrical");
235 is_symmetrical = lua_tointeger(L, -1);
238 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
239 * Et ouais mon pote. That's the way it goes. F34R.
241 SURFXML_BUFFER_SET(route_src, src);
242 SURFXML_BUFFER_SET(route_dst, dst);
244 A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_YES;
246 A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
247 SURFXML_START_TAG(route);
249 xbt_dynar_foreach(links,cursor,link_id) {
250 SURFXML_BUFFER_SET(link_ctn_id, link_id);
251 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
252 SURFXML_START_TAG(link_ctn);
253 SURFXML_END_TAG(link_ctn);
255 SURFXML_END_TAG(route);
257 xbt_dynar_free(&links);
258 free(surfxml_bufferstack);
263 int console_AS_open(lua_State *L) {
267 if (! lua_istable(L, 1)) {
268 XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
272 lua_pushstring(L, "id");
274 id = lua_tostring(L, -1);
277 lua_pushstring(L, "mode");
279 mode = lua_tostring(L, -1);
282 sg_platf_new_AS_begin(id,mode);
286 int console_AS_close(lua_State *L) {
287 sg_platf_new_AS_end();
291 int console_set_function(lua_State *L) {
293 const char *host_id ;
294 const char *function_id;
297 if (! lua_istable(L, 1)) {
298 XBT_ERROR("Bad Arguments to AS.new, Should be a table with named arguments");
303 lua_pushstring(L, "host");
305 host_id = lua_tostring(L, -1);
309 lua_pushstring(L, "fct");
311 function_id = lua_tostring(L, -1);
315 lua_pushstring(L,"args");
317 args = xbt_str_split_quoted( lua_tostring(L,-1) );
320 // FIXME: hackish to go under MSG that way
321 m_host_t host = xbt_lib_get_or_null(host_lib,host_id,MSG_HOST_LEVEL);
323 XBT_ERROR("no host '%s' found",host_id);
327 MSG_set_function(host_id, function_id, args);
332 int console_host_set_property(lua_State *L) {
333 const char* name ="";
334 const char* prop_id = "";
335 const char* prop_value = "";
336 if (!lua_istable(L, -1)) {
337 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
343 lua_pushstring(L, "host");
345 name = lua_tostring(L, -1);
349 lua_pushstring(L, "prop");
351 prop_id = lua_tostring(L, -1);
354 lua_pushstring(L,"value");
356 prop_value = lua_tostring(L,-1);
359 // FIXME: hackish to go under MSG that way
360 m_host_t host = xbt_lib_get_or_null(host_lib,name,MSG_HOST_LEVEL);
362 XBT_ERROR("no host '%s' found",name);
365 xbt_dict_t props = MSG_host_get_properties(host);
366 xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);