1 /* Copyright (c) 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 /* SimGrid Lua bindings */
9 #include "lua_private.h"
10 #include "simgrid/platf_interface.h"
11 #include "surf/surfxml_parse.h"
12 #include "surf/surf_routing.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_platf, bindings, "Lua bindings (platform module)");
19 #define PLATF_MODULE_NAME "simgrid.platf"
21 /* ********************************************************************************* */
22 /* simgrid.platf API */
23 /* ********************************************************************************* */
25 static const luaL_reg platf_functions[] = {
26 {"open", console_open},
27 {"close", console_close},
28 {"AS_open", console_AS_open},
29 {"AS_close", console_AS_close},
30 {"host_new", console_add_host},
31 {"link_new", console_add_link},
32 {"router_new", console_add_router},
33 {"route_new", console_add_route},
37 int console_open(lua_State *L) {
40 surf_parse_init_callbacks();
44 int console_close(lua_State *L) {
50 int console_add_host(lua_State *L) {
51 s_sg_platf_host_cbarg_t host;
52 memset(&host,0,sizeof(host));
55 // we get values from the table passed as argument
56 if (!lua_istable(L, -1)) {
58 ("Bad Arguments to create host, Should be a table with named arguments");
63 lua_pushstring(L, "id");
65 host.id = lua_tostring(L, -1);
69 lua_pushstring(L, "power");
71 host.power_peak = lua_tonumber(L, -1);
75 lua_pushstring(L, "power_scale");
77 host.power_scale = lua_tonumber(L, -1);
81 lua_pushstring(L, "power_trace");
83 host.power_trace = tmgr_trace_new(lua_tostring(L, -1));
86 lua_pushstring(L, "core");
88 host.core_amount = lua_tonumber(L, -1);
89 if (host.core_amount == 0)
94 lua_pushstring(L, "state_initial");
96 state = lua_tonumber(L, -1);
99 host.initial_state = SURF_RESOURCE_ON;
101 host.initial_state = SURF_RESOURCE_OFF;
104 lua_pushstring(L, "state_trace");
106 host.state_trace = tmgr_trace_new(lua_tostring(L, -1));
109 sg_platf_new_host(&host);
114 int console_add_link(lua_State *L) {
115 s_sg_platf_link_cbarg_t link;
116 memset(&link,0,sizeof(link));
120 if (! lua_istable(L, -1)) {
121 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
126 lua_pushstring(L, "id");
128 link.id = lua_tostring(L, -1);
131 // get bandwidth value
132 lua_pushstring(L, "bandwidth");
134 link.bandwidth = lua_tonumber(L, -1);
138 lua_pushstring(L, "latency");
140 link.latency = lua_tonumber(L, -1);
143 /*Optional Arguments */
145 //get bandwidth_trace value
146 lua_pushstring(L, "bandwidth_trace");
148 link.bandwidth_trace = tmgr_trace_new(lua_tostring(L, -1));
151 //get latency_trace value
152 lua_pushstring(L, "latency_trace");
154 link.latency_trace = tmgr_trace_new(lua_tostring(L, -1));
157 //get state_trace value
158 lua_pushstring(L, "state_trace");
160 link.state_trace = tmgr_trace_new(lua_tostring(L, -1));
163 //get state_initial value
164 lua_pushstring(L, "state_initial");
166 if (lua_tonumber(L, -1))
167 link.state = SURF_RESOURCE_ON;
169 link.state = SURF_RESOURCE_OFF;
173 lua_pushstring(L, "policy");
175 policy = lua_tostring(L, -1);
177 if (policy && !strcmp(policy,"FULLDUPLEX")) {
178 link.policy = SURF_LINK_FULLDUPLEX;
179 } else if (policy && !strcmp(policy,"FATPIPE")) {
180 link.policy = SURF_LINK_FATPIPE;
182 link.policy = SURF_LINK_SHARED;
185 sg_platf_new_link(&link);
190 * add Router to AS components
192 int console_add_router(lua_State* L) {
193 s_sg_platf_router_cbarg_t router;
194 memset(&router,0,sizeof(router));
196 if (! lua_istable(L, -1)) {
197 XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
201 lua_pushstring(L, "id");
203 router.id = lua_tostring(L, -1);
206 lua_pushstring(L,"coord");
208 router.coord = lua_tostring(L, -1);
211 sg_platf_new_router(&router);
216 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
218 int console_add_route(lua_State *L) {
219 static int AX_ptr = 0;
220 static int surfxml_bufferstack_size = 2048;
222 /* allocating memory for the buffer, I think 2kB should be enough */
223 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
232 if (! lua_istable(L, -1)) {
233 XBT_ERROR("Bad Arguments to create a route, Should be a table with named arguments");
237 lua_pushstring(L,"src");
239 src = lua_tostring(L, -1);
242 lua_pushstring(L,"dest");
244 dst = lua_tostring(L, -1);
247 lua_pushstring(L,"links");
249 links = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
250 if (xbt_dynar_is_empty(links))
251 xbt_dynar_push_as(links,char*,xbt_strdup(lua_tostring(L, -1)));
254 lua_pushstring(L,"symmetrical");
256 is_symmetrical = lua_tointeger(L, -1);
259 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
260 * Et ouais mon pote. That's the way it goes. F34R.
262 SURFXML_BUFFER_SET(route_src, src);
263 SURFXML_BUFFER_SET(route_dst, dst);
265 A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_YES;
267 A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
268 SURFXML_START_TAG(route);
270 xbt_dynar_foreach(links,cursor,link_id) {
271 SURFXML_BUFFER_SET(link_ctn_id, link_id);
272 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
273 SURFXML_START_TAG(link_ctn);
274 SURFXML_END_TAG(link_ctn);
276 SURFXML_END_TAG(route);
278 xbt_dynar_free(&links);
279 free(surfxml_bufferstack);
284 int console_AS_open(lua_State *L) {
288 if (! lua_istable(L, 1)) {
289 XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
293 lua_pushstring(L, "id");
295 id = lua_tostring(L, -1);
298 lua_pushstring(L, "mode");
300 mode = lua_tostring(L, -1);
303 sg_platf_new_AS_begin(id,mode);
307 int console_AS_close(lua_State *L) {
308 sg_platf_new_AS_end();
312 int console_set_function(lua_State *L) {
314 const char *host_id ;
315 const char *function_id;
318 if (! lua_istable(L, 1)) {
319 XBT_ERROR("Bad Arguments to AS.new, Should be a table with named arguments");
324 lua_pushstring(L, "host");
326 host_id = lua_tostring(L, -1);
330 lua_pushstring(L, "fct");
332 function_id = lua_tostring(L, -1);
336 lua_pushstring(L,"args");
338 args = xbt_str_split_quoted( lua_tostring(L,-1) );
341 // FIXME: hackish to go under MSG that way
342 m_host_t host = xbt_lib_get_or_null(host_lib,host_id,MSG_HOST_LEVEL);
344 XBT_ERROR("no host '%s' found",host_id);
348 MSG_set_function(host_id, function_id, args);
353 int console_host_set_property(lua_State *L) {
354 const char* name ="";
355 const char* prop_id = "";
356 const char* prop_value = "";
357 if (!lua_istable(L, -1)) {
358 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
364 lua_pushstring(L, "host");
366 name = lua_tostring(L, -1);
370 lua_pushstring(L, "prop");
372 prop_id = lua_tostring(L, -1);
375 lua_pushstring(L,"value");
377 prop_value = lua_tostring(L,-1);
380 // FIXME: hackish to go under MSG that way
381 m_host_t host = xbt_lib_get_or_null(host_lib,name,MSG_HOST_LEVEL);
383 XBT_ERROR("no host '%s' found",name);
386 xbt_dict_t props = MSG_host_get_properties(host);
387 xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
393 * \brief Registers the platform functions into the table simgrid.platf.
394 * \param L a lua state
396 void sglua_register_platf_functions(lua_State* L)
398 luaL_openlib(L, PLATF_MODULE_NAME, platf_functions, 0);