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, "power_scale");
94 host.power_scale = lua_tonumber(L, -1);
98 lua_pushstring(L, "power_trace");
100 host.power_trace = tmgr_trace_new(lua_tostring(L, -1));
103 lua_pushstring(L, "core");
105 host.core_amount = lua_tonumber(L, -1);
106 if (host.core_amount == 0)
107 host.core_amount = 1;
111 lua_pushstring(L, "state_initial");
113 state = lua_tonumber(L, -1);
116 host.initial_state = SURF_RESOURCE_ON;
118 host.initial_state = SURF_RESOURCE_OFF;
121 lua_pushstring(L, "state_trace");
123 host.state_trace = tmgr_trace_new(lua_tostring(L, -1));
126 sg_platf_new_host(&host);
131 int console_add_link(lua_State *L) {
132 s_sg_platf_link_cbarg_t link;
133 memset(&link,0,sizeof(link));
137 if (! lua_istable(L, -1)) {
138 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
143 lua_pushstring(L, "id");
145 link.id = lua_tostring(L, -1);
148 // get bandwidth value
149 lua_pushstring(L, "bandwidth");
151 link.bandwidth = lua_tonumber(L, -1);
155 lua_pushstring(L, "latency");
157 link.latency = lua_tonumber(L, -1);
160 /*Optional Arguments */
162 //get bandwidth_trace value
163 lua_pushstring(L, "bandwidth_trace");
165 link.bandwidth_trace = tmgr_trace_new(lua_tostring(L, -1));
168 //get latency_trace value
169 lua_pushstring(L, "latency_trace");
171 link.latency_trace = tmgr_trace_new(lua_tostring(L, -1));
174 //get state_trace value
175 lua_pushstring(L, "state_trace");
177 link.state_trace = tmgr_trace_new(lua_tostring(L, -1));
180 //get state_initial value
181 lua_pushstring(L, "state_initial");
183 if (lua_tonumber(L, -1))
184 link.state = SURF_RESOURCE_ON;
186 link.state = SURF_RESOURCE_OFF;
190 lua_pushstring(L, "policy");
192 policy = lua_tostring(L, -1);
194 if (policy && !strcmp(policy,"FULLDUPLEX")) {
195 link.policy = SURF_LINK_FULLDUPLEX;
196 } else if (policy && !strcmp(policy,"FATPIPE")) {
197 link.policy = SURF_LINK_FATPIPE;
199 link.policy = SURF_LINK_SHARED;
202 sg_platf_new_link(&link);
207 * add Router to AS components
209 int console_add_router(lua_State* L) {
210 s_sg_platf_router_cbarg_t router;
211 memset(&router,0,sizeof(router));
213 if (! lua_istable(L, -1)) {
214 XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
218 lua_pushstring(L, "id");
220 router.id = lua_tostring(L, -1);
223 lua_pushstring(L,"coord");
225 router.coord = lua_tostring(L, -1);
228 sg_platf_new_router(&router);
233 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
235 int console_add_route(lua_State *L) {
236 static int AX_ptr = 0;
237 static int surfxml_bufferstack_size = 2048;
239 /* allocating memory for the buffer, I think 2kB should be enough */
240 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
249 if (! lua_istable(L, -1)) {
250 XBT_ERROR("Bad Arguments to create a route, Should be a table with named arguments");
254 lua_pushstring(L,"src");
256 src = lua_tostring(L, -1);
259 lua_pushstring(L,"dest");
261 dst = lua_tostring(L, -1);
264 lua_pushstring(L,"links");
266 links = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
267 if (xbt_dynar_is_empty(links))
268 xbt_dynar_push_as(links,char*,xbt_strdup(lua_tostring(L, -1)));
271 lua_pushstring(L,"symmetrical");
273 is_symmetrical = lua_tointeger(L, -1);
276 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
277 * Et ouais mon pote. That's the way it goes. F34R.
279 SURFXML_BUFFER_SET(route_src, src);
280 SURFXML_BUFFER_SET(route_dst, dst);
282 A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_YES;
284 A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
285 SURFXML_START_TAG(route);
287 xbt_dynar_foreach(links,cursor,link_id) {
288 SURFXML_BUFFER_SET(link_ctn_id, link_id);
289 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
290 SURFXML_START_TAG(link_ctn);
291 SURFXML_END_TAG(link_ctn);
293 SURFXML_END_TAG(route);
295 xbt_dynar_free(&links);
296 free(surfxml_bufferstack);
301 int console_AS_open(lua_State *L) {
305 if (! lua_istable(L, 1)) {
306 XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
310 lua_pushstring(L, "id");
312 id = lua_tostring(L, -1);
315 lua_pushstring(L, "mode");
317 mode = lua_tostring(L, -1);
320 sg_platf_new_AS_begin(id,mode);
324 int console_AS_close(lua_State *L) {
325 sg_platf_new_AS_end();
329 int console_set_function(lua_State *L) {
331 const char *host_id ;
332 const char *function_id;
335 if (! lua_istable(L, 1)) {
336 XBT_ERROR("Bad Arguments to AS.new, Should be a table with named arguments");
341 lua_pushstring(L, "host");
343 host_id = lua_tostring(L, -1);
347 lua_pushstring(L, "fct");
349 function_id = lua_tostring(L, -1);
353 lua_pushstring(L,"args");
355 args = xbt_str_split_quoted( lua_tostring(L,-1) );
358 // FIXME: hackish to go under MSG that way
359 m_host_t host = xbt_lib_get_or_null(host_lib,host_id,MSG_HOST_LEVEL);
361 XBT_ERROR("no host '%s' found",host_id);
365 MSG_set_function(host_id, function_id, args);
370 int console_host_set_property(lua_State *L) {
371 const char* name ="";
372 const char* prop_id = "";
373 const char* prop_value = "";
374 if (!lua_istable(L, -1)) {
375 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
381 lua_pushstring(L, "host");
383 name = lua_tostring(L, -1);
387 lua_pushstring(L, "prop");
389 prop_id = lua_tostring(L, -1);
392 lua_pushstring(L,"value");
394 prop_value = lua_tostring(L,-1);
397 // FIXME: hackish to go under MSG that way
398 m_host_t host = xbt_lib_get_or_null(host_lib,name,MSG_HOST_LEVEL);
400 XBT_ERROR("no host '%s' found",name);
403 xbt_dict_t props = MSG_host_get_properties(host);
404 xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
410 * \brief Registers the platform functions into the table simgrid.platf.
411 * \param L a lua state
413 void sglua_register_platf_functions(lua_State* L)
415 luaL_openlib(L, PLATF_MODULE_NAME, platf_functions, 0);