1 /* Copyright (c) 2010, 2012-2014. 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 #include <msg/msg_private.h>
18 #include <simix/smx_host_private.h>
19 #include <surf/surf_private.h>
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_platf, bindings, "Lua bindings (platform module)");
23 #define PLATF_MODULE_NAME "simgrid.platf"
25 /* ********************************************************************************* */
26 /* simgrid.platf API */
27 /* ********************************************************************************* */
29 static const luaL_reg platf_functions[] = {
30 {"open", console_open},
31 {"close", console_close},
32 {"AS_open", console_AS_open},
33 {"AS_close", console_AS_close},
34 {"host_new", console_add_host},
35 {"link_new", console_add_link},
36 {"router_new", console_add_router},
37 {"route_new", console_add_route},
41 int console_open(lua_State *L) {
45 storage_register_callbacks();
46 routing_register_callbacks();
48 gpu_register_callbacks();
53 int console_close(lua_State *L) {
57 xbt_lib_cursor_t cursor;
61 /* Initialize MSG and WKS hosts */
62 XBT_DEBUG("Initialize MSG and WKS hosts");
63 xbt_lib_foreach(host_lib, cursor, name, data) {
64 if(data[SURF_WKS_LEVEL]){
65 XBT_DEBUG("\tSee surf host %s",name);
66 SIMIX_host_create(name, data[SURF_WKS_LEVEL], NULL);
67 __MSG_host_create((smx_host_t)data[SIMIX_HOST_LEVEL]);
74 int console_add_host(lua_State *L) {
75 s_sg_platf_host_cbarg_t host;
76 memset(&host,0,sizeof(host));
79 // we get values from the table passed as argument
80 if (!lua_istable(L, -1)) {
82 ("Bad Arguments to create host, Should be a table with named arguments");
87 lua_pushstring(L, "id");
89 host.id = lua_tostring(L, -1);
93 lua_pushstring(L, "power");
95 host.power_peak = xbt_dynar_new(sizeof(double), NULL);
96 xbt_dynar_push_as(host.power_peak, double, lua_tonumber(L, -1));
100 lua_pushstring(L, "core");
102 if(!lua_isnumber(L,-1)) host.core_amount = 1;// Default value
103 else host.core_amount = lua_tonumber(L, -1);
104 if (host.core_amount == 0)
105 host.core_amount = 1;
109 lua_pushstring(L, "power_scale");
111 if(!lua_isnumber(L,-1)) host.power_scale = 1;// Default value
112 else host.power_scale = lua_tonumber(L, -1);
116 lua_pushstring(L, "power_trace");
118 host.power_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
122 lua_pushstring(L, "state_initial");
124 if(!lua_isnumber(L,-1)) state = 1;// Default value
125 else state = lua_tonumber(L, -1);
129 host.initial_state = SURF_RESOURCE_ON;
131 host.initial_state = SURF_RESOURCE_OFF;
134 lua_pushstring(L, "state_trace");
136 host.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
139 sg_platf_new_host(&host);
144 int console_add_link(lua_State *L) {
145 s_sg_platf_link_cbarg_t link;
146 memset(&link,0,sizeof(link));
150 if (! lua_istable(L, -1)) {
151 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
156 lua_pushstring(L, "id");
158 link.id = lua_tostring(L, -1);
161 // get bandwidth value
162 lua_pushstring(L, "bandwidth");
164 link.bandwidth = lua_tonumber(L, -1);
168 lua_pushstring(L, "latency");
170 link.latency = lua_tonumber(L, -1);
173 /*Optional Arguments */
175 //get bandwidth_trace value
176 lua_pushstring(L, "bandwidth_trace");
178 link.bandwidth_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
181 //get latency_trace value
182 lua_pushstring(L, "latency_trace");
184 link.latency_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
187 //get state_trace value
188 lua_pushstring(L, "state_trace");
190 link.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
193 //get state_initial value
194 lua_pushstring(L, "state_initial");
196 if (!lua_isnumber(L,-1) || lua_tonumber(L, -1))
197 link.state = SURF_RESOURCE_ON;
199 link.state = SURF_RESOURCE_OFF;
203 lua_pushstring(L, "policy");
205 policy = lua_tostring(L, -1);
207 if (policy && !strcmp(policy,"FULLDUPLEX")) {
208 link.policy = SURF_LINK_FULLDUPLEX;
209 } else if (policy && !strcmp(policy,"FATPIPE")) {
210 link.policy = SURF_LINK_FATPIPE;
212 link.policy = SURF_LINK_SHARED;
215 sg_platf_new_link(&link);
220 * add Router to AS components
222 int console_add_router(lua_State* L) {
223 s_sg_platf_router_cbarg_t router;
224 memset(&router,0,sizeof(router));
226 if (! lua_istable(L, -1)) {
227 XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
231 lua_pushstring(L, "id");
233 router.id = lua_tostring(L, -1);
236 lua_pushstring(L,"coord");
238 router.coord = lua_tostring(L, -1);
241 sg_platf_new_router(&router);
246 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
248 int console_add_route(lua_State *L) {
249 s_sg_platf_route_cbarg_t route;
250 memset(&route,0,sizeof(route));
252 /* allocating memory for the buffer, I think 2kB should be enough */
253 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
257 if (! lua_istable(L, -1)) {
258 XBT_ERROR("Bad Arguments to create a route, Should be a table with named arguments");
262 lua_pushstring(L,"src");
264 route.src = lua_tostring(L, -1);
267 lua_pushstring(L,"dest");
269 route.dst = lua_tostring(L, -1);
272 lua_pushstring(L,"links");
274 route.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
275 if (xbt_dynar_is_empty(route.link_list))
276 xbt_dynar_push_as(route.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
279 lua_pushstring(L,"symmetrical");
281 is_symmetrical = lua_tointeger(L, -1);
287 /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
288 * Et ouais mon pote. That's the way it goes. F34R.
291 route.symmetrical = TRUE;
293 route.symmetrical = FALSE;
295 sg_platf_new_route(&route);
300 int console_AS_open(lua_State *L) {
304 if (! lua_istable(L, 1)) {
305 XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
309 lua_pushstring(L, "id");
311 id = lua_tostring(L, -1);
314 lua_pushstring(L, "mode");
316 mode = lua_tostring(L, -1);
319 int mode_int = A_surfxml_AS_routing_None;
320 if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
321 else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
322 else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
323 else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
324 else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
325 else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
326 else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
327 else xbt_die("Don't have the model name '%s'",mode);
329 s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
331 AS.routing = mode_int;
332 sg_platf_new_AS_begin(&AS);
336 int console_AS_close(lua_State *L) {
337 sg_platf_new_AS_end();
341 int console_set_function(lua_State *L) {
343 const char *host_id ;
344 const char *function_id;
347 if (! lua_istable(L, 1)) {
348 XBT_ERROR("Bad Arguments to AS.new, Should be a table with named arguments");
353 lua_pushstring(L, "host");
355 host_id = lua_tostring(L, -1);
359 lua_pushstring(L, "fct");
361 function_id = lua_tostring(L, -1);
365 lua_pushstring(L,"args");
367 args = xbt_str_split_str( lua_tostring(L,-1) , ",");
370 // FIXME: hackish to go under MSG that way
371 msg_host_t host = xbt_lib_get_or_null(host_lib,host_id,MSG_HOST_LEVEL);
373 XBT_ERROR("no host '%s' found",host_id);
377 // FIXME: use sg_platf_new_process directly (warning: find a way to check hostname)
378 MSG_set_function(host_id, function_id, args);
383 int console_host_set_property(lua_State *L) {
384 const char* name ="";
385 const char* prop_id = "";
386 const char* prop_value = "";
387 if (!lua_istable(L, -1)) {
388 XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
394 lua_pushstring(L, "host");
396 name = lua_tostring(L, -1);
400 lua_pushstring(L, "prop");
402 prop_id = lua_tostring(L, -1);
405 lua_pushstring(L,"value");
407 prop_value = lua_tostring(L,-1);
410 // FIXME: hackish to go under MSG that way
411 msg_host_t host = xbt_lib_get_or_null(host_lib,name,MSG_HOST_LEVEL);
413 XBT_ERROR("no host '%s' found",name);
416 xbt_dict_t props = MSG_host_get_properties(host);
417 xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
423 * \brief Registers the platform functions into the table simgrid.platf.
424 * \param L a lua state
426 void sglua_register_platf_functions(lua_State* L)
428 luaL_openlib(L, PLATF_MODULE_NAME, platf_functions, 0);