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();
41 routing_register_callbacks();
46 int console_close(lua_State *L) {
50 xbt_lib_cursor_t cursor;
54 /* Initialize MSG and WKS hosts */
55 XBT_DEBUG("Initialize MSG and WKS hosts");
56 xbt_lib_foreach(host_lib, cursor, name, data) {
57 if(data[SURF_WKS_LEVEL]){
58 XBT_DEBUG("\tSee surf host %s",name);
59 SIMIX_host_create(name, data[SURF_WKS_LEVEL], NULL);
60 __MSG_host_create((smx_host_t)data[SIMIX_HOST_LEVEL]);
67 int console_add_host(lua_State *L) {
68 s_sg_platf_host_cbarg_t host;
69 memset(&host,0,sizeof(host));
72 // we get values from the table passed as argument
73 if (!lua_istable(L, -1)) {
75 ("Bad Arguments to create host, Should be a table with named arguments");
80 lua_pushstring(L, "id");
82 host.id = lua_tostring(L, -1);
86 lua_pushstring(L, "power");
88 host.power_peak = lua_tonumber(L, -1);
92 lua_pushstring(L, "core");
94 if(!lua_isnumber(L,-1)) host.core_amount = 1;// Default value
95 else host.core_amount = lua_tonumber(L, -1);
96 if (host.core_amount == 0)
101 lua_pushstring(L, "power_scale");
103 host.power_scale = lua_tonumber(L, -1);
107 lua_pushstring(L, "power_trace");
109 host.power_trace = tmgr_trace_new(lua_tostring(L, -1));
113 lua_pushstring(L, "state_initial");
115 if(!lua_isnumber(L,-1)) state = 1;// Default value
116 else state = lua_tonumber(L, -1);
120 host.initial_state = SURF_RESOURCE_ON;
122 host.initial_state = SURF_RESOURCE_OFF;
125 lua_pushstring(L, "state_trace");
127 host.state_trace = tmgr_trace_new(lua_tostring(L, -1));
130 sg_platf_new_host(&host);
135 int console_add_link(lua_State *L) {
136 s_sg_platf_link_cbarg_t link;
137 memset(&link,0,sizeof(link));
141 if (! lua_istable(L, -1)) {
142 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
147 lua_pushstring(L, "id");
149 link.id = lua_tostring(L, -1);
152 // get bandwidth value
153 lua_pushstring(L, "bandwidth");
155 link.bandwidth = lua_tonumber(L, -1);
159 lua_pushstring(L, "latency");
161 link.latency = lua_tonumber(L, -1);
164 /*Optional Arguments */
166 //get bandwidth_trace value
167 lua_pushstring(L, "bandwidth_trace");
169 link.bandwidth_trace = tmgr_trace_new(lua_tostring(L, -1));
172 //get latency_trace value
173 lua_pushstring(L, "latency_trace");
175 link.latency_trace = tmgr_trace_new(lua_tostring(L, -1));
178 //get state_trace value
179 lua_pushstring(L, "state_trace");
181 link.state_trace = tmgr_trace_new(lua_tostring(L, -1));
184 //get state_initial value
185 lua_pushstring(L, "state_initial");
187 if (lua_tonumber(L, -1))
188 link.state = SURF_RESOURCE_ON;
190 link.state = SURF_RESOURCE_OFF;
194 lua_pushstring(L, "policy");
196 policy = lua_tostring(L, -1);
198 if (policy && !strcmp(policy,"FULLDUPLEX")) {
199 link.policy = SURF_LINK_FULLDUPLEX;
200 } else if (policy && !strcmp(policy,"FATPIPE")) {
201 link.policy = SURF_LINK_FATPIPE;
203 link.policy = SURF_LINK_SHARED;
206 sg_platf_new_link(&link);
211 * add Router to AS components
213 int console_add_router(lua_State* L) {
214 s_sg_platf_router_cbarg_t router;
215 memset(&router,0,sizeof(router));
217 if (! lua_istable(L, -1)) {
218 XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
222 lua_pushstring(L, "id");
224 router.id = lua_tostring(L, -1);
227 lua_pushstring(L,"coord");
229 router.coord = lua_tostring(L, -1);
232 sg_platf_new_router(&router);
237 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
239 int console_add_route(lua_State *L) {
240 static int AX_ptr = 0;
241 static int surfxml_bufferstack_size = 2048;
243 /* allocating memory for the buffer, I think 2kB should be enough */
244 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
253 if (! lua_istable(L, -1)) {
254 XBT_ERROR("Bad Arguments to create a route, Should be a table with named arguments");
258 lua_pushstring(L,"src");
260 src = lua_tostring(L, -1);
263 lua_pushstring(L,"dest");
265 dst = lua_tostring(L, -1);
268 lua_pushstring(L,"links");
270 links = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
271 if (xbt_dynar_is_empty(links))
272 xbt_dynar_push_as(links,char*,xbt_strdup(lua_tostring(L, -1)));
275 lua_pushstring(L,"symmetrical");
277 is_symmetrical = lua_tointeger(L, -1);
280 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
281 * Et ouais mon pote. That's the way it goes. F34R.
283 SURFXML_BUFFER_SET(route_src, src);
284 SURFXML_BUFFER_SET(route_dst, dst);
286 A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_YES;
288 A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
289 SURFXML_START_TAG(route);
291 xbt_dynar_foreach(links,cursor,link_id) {
292 SURFXML_BUFFER_SET(link_ctn_id, link_id);
293 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
294 SURFXML_START_TAG(link_ctn);
295 SURFXML_END_TAG(link_ctn);
297 SURFXML_END_TAG(route);
299 xbt_dynar_free(&links);
300 free(surfxml_bufferstack);
305 int console_AS_open(lua_State *L) {
309 if (! lua_istable(L, 1)) {
310 XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
314 lua_pushstring(L, "id");
316 id = lua_tostring(L, -1);
319 lua_pushstring(L, "mode");
321 mode = lua_tostring(L, -1);
324 sg_platf_new_AS_begin(id,mode);
328 int console_AS_close(lua_State *L) {
329 sg_platf_new_AS_end();
333 int console_set_function(lua_State *L) {
335 const char *host_id ;
336 const char *function_id;
339 if (! lua_istable(L, 1)) {
340 XBT_ERROR("Bad Arguments to AS.new, Should be a table with named arguments");
345 lua_pushstring(L, "host");
347 host_id = lua_tostring(L, -1);
351 lua_pushstring(L, "fct");
353 function_id = lua_tostring(L, -1);
357 lua_pushstring(L,"args");
359 args = xbt_str_split_quoted( lua_tostring(L,-1) );
362 // FIXME: hackish to go under MSG that way
363 m_host_t host = xbt_lib_get_or_null(host_lib,host_id,MSG_HOST_LEVEL);
365 XBT_ERROR("no host '%s' found",host_id);
369 MSG_set_function(host_id, function_id, args);
374 int console_host_set_property(lua_State *L) {
375 const char* name ="";
376 const char* prop_id = "";
377 const char* prop_value = "";
378 if (!lua_istable(L, -1)) {
379 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
385 lua_pushstring(L, "host");
387 name = lua_tostring(L, -1);
391 lua_pushstring(L, "prop");
393 prop_id = lua_tostring(L, -1);
396 lua_pushstring(L,"value");
398 prop_value = lua_tostring(L,-1);
401 // FIXME: hackish to go under MSG that way
402 m_host_t host = xbt_lib_get_or_null(host_lib,name,MSG_HOST_LEVEL);
404 XBT_ERROR("no host '%s' found",name);
407 xbt_dict_t props = MSG_host_get_properties(host);
408 xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
414 * \brief Registers the platform functions into the table simgrid.platf.
415 * \param L a lua state
417 void sglua_register_platf_functions(lua_State* L)
419 luaL_openlib(L, PLATF_MODULE_NAME, platf_functions, 0);