1 /* SimGrid Lua bindings */
3 /* Copyright (c) 2010, the SimGrid team. All right reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua,bindings,"Lua Bindings");
17 #define TASK_MODULE_NAME "simgrid.Task"
18 #define HOST_MODULE_NAME "simgrid.Host"
20 /* ********************************************************************************* */
21 /* helper functions */
22 /* ********************************************************************************* */
24 static void stackDump (const char *msg, lua_State *L) {
28 int top = lua_gettop(L);
31 p+=sprintf(p,"STACK(top=%d): ",top);
32 /* lua_getglobal(L,"vardump");
33 xbt_assert0(lua_isfunction(L,-1), "Vardump not found");
36 for (i = 1; i <= top; i++) { /* repeat for each level */
38 int t = lua_type(L, i);
41 case LUA_TSTRING: /* strings */
42 p+=sprintf(p,"`%s'", lua_tostring(L, i));
45 case LUA_TBOOLEAN: /* booleans */
46 p+=sprintf(p,lua_toboolean(L, i) ? "true" : "false");
49 case LUA_TNUMBER: /* numbers */
50 p+=sprintf(p,"%g", lua_tonumber(L, i));
54 p+=sprintf(p, "Table");
57 default: /* other values */
59 /* if ((ptr = luaL_checkudata(L,i,TASK_MODULE_NAME))) {
62 p+=printf(p,"%s", lua_typename(L, t));
67 p+=sprintf(p," "); /* put a separator */
69 INFO2("%s%s",msg,buff);
72 /** @brief ensures that a userdata on the stack is a task and returns the pointer inside the userdata */
73 static m_task_t checkTask (lua_State *L,int index) {
75 luaL_checktype(L,index,LUA_TTABLE);
76 lua_getfield(L,index,"__simgrid_task");
77 pi = (m_task_t*)luaL_checkudata(L,-1,TASK_MODULE_NAME);
79 luaL_typerror(L,index,TASK_MODULE_NAME);
82 luaL_error(L,"null Task");
87 /** @brief leaves a new userdata on top of the stack, sets its metatable, and sets the Task pointer inside the userdata */
88 /*static m_task_t *pushTask (lua_State *L,m_task_t tk) {
91 pi = (m_task_t*)lua_newuserdata(L,sizeof(m_task_t));
94 DEBUG1("push lua task with Name : %s \n",MSG_task_get_name(*pi));
95 luaL_getmetatable(L,TASK_MODULE_NAME);
96 lua_setmetatable(L,-2);
101 /* ********************************************************************************* */
102 /* wrapper functions */
103 /* ********************************************************************************* */
109 static int Task_new(lua_State* L) {
110 const char *name=luaL_checkstring(L,1);
111 int comp_size = luaL_checkint(L,2);
112 int msg_size = luaL_checkint(L,3);
113 INFO0("Creating task");
114 m_task_t msg_task = MSG_task_create(name,comp_size,msg_size,NULL);
115 lua_newtable (L); /* create a table, put the userdata on top of it */
116 m_task_t *lua_task = (m_task_t*)lua_newuserdata(L,sizeof(m_task_t));
117 *lua_task = msg_task;
118 luaL_getmetatable(L,TASK_MODULE_NAME);
119 lua_setmetatable(L,-2);
120 lua_setfield (L, -2, "__simgrid_task"); /* put the userdata as field of the table */
121 /* remove the args from the stack */
128 static int Task_get_name(lua_State *L) {
129 m_task_t tk = checkTask(L,-1);
130 lua_pushstring(L,MSG_task_get_name(tk));
134 static int Task_computation_duration(lua_State *L){
135 m_task_t tk = checkTask(L,-1);
136 lua_pushnumber(L,MSG_task_get_compute_duration (tk));
140 static int Task_execute(lua_State *L){
141 m_task_t tk = checkTask(L,-1);
142 int res = MSG_task_execute(tk);
143 lua_pushnumber(L,res);
147 static int Task_destroy(lua_State *L) {
148 m_task_t tk = checkTask(L,-1);
149 int res = MSG_task_destroy(tk);
150 lua_pushnumber(L,res);
153 static int Task_send(lua_State *L) {
154 stackDump("send ",L);
155 m_task_t tk = checkTask(L,-2);
156 const char *mailbox = luaL_checkstring(L,-1);
157 lua_pop(L,1); // remove the string so that the task is on top of it
158 MSG_task_set_data(tk,L); // Copy my stack into the task, so that the receiver can copy the lua task directly
159 MSG_error_t res = MSG_task_send(tk,mailbox);
160 while (MSG_task_has_data(tk)) // Don't mess up with my stack: the receiver didn't copy the data yet
161 MSG_process_sleep(0); // yield
163 if (res != MSG_OK) switch(res) {
165 ERROR0("MSG_task_send failed : Timeout");
167 case MSG_TRANSFER_FAILURE :
168 ERROR0("MSG_task_send failed : Transfer Failure");
170 case MSG_HOST_FAILURE :
171 ERROR0("MSG_task_send failed : Host Failure ");
174 ERROR0("MSG_task_send failed : Unexpected error , please report this bug");
179 static int Task_recv(lua_State *L) {
181 const char *mailbox = luaL_checkstring(L,-1);
182 MSG_error_t res = MSG_task_receive(&tk,mailbox);
184 lua_State *sender_stack = MSG_task_get_data(tk);
185 lua_xmove(sender_stack,L,1); // copy the data directly from sender's stack
186 MSG_task_set_data(tk,NULL);
188 if(res != MSG_OK) switch(res){
190 ERROR0("MSG_task_receive failed : Timeout");
192 case MSG_TRANSFER_FAILURE :
193 ERROR0("MSG_task_receive failed : Transfer Failure");
195 case MSG_HOST_FAILURE :
196 ERROR0("MSG_task_receive failed : Host Failure ");
199 ERROR0("MSG_task_receive failed : Unexpected error , please report this bug");
206 static const luaL_reg Task_methods[] = {
208 {"name", Task_get_name},
209 {"computation_duration", Task_computation_duration},
210 {"execute", Task_execute},
211 {"destroy", Task_destroy},
216 static int Task_gc(lua_State *L) {
217 m_task_t tk=checkTask(L,-1);
218 if (tk) MSG_task_destroy(tk);
222 static int Task_tostring(lua_State *L) {
223 lua_pushfstring(L, "Task :%p",lua_touserdata(L,1));
227 static const luaL_reg Task_meta[] = {
229 {"__tostring", Task_tostring},
236 static m_host_t checkHost (lua_State *L,int index) {
238 luaL_checktype(L,index,LUA_TTABLE);
239 lua_getfield(L,index,"__simgrid_host");
240 pi = (m_host_t*)luaL_checkudata(L,-1,HOST_MODULE_NAME);
242 luaL_typerror(L,index,HOST_MODULE_NAME);
245 luaL_error(L,"null Host");
251 static int Host_get_by_name(lua_State *L)
253 const char *name=luaL_checkstring(L,1);
254 DEBUG0("Getting Host from name...");
255 m_host_t msg_host = MSG_get_host_by_name(name);
258 //ERROR1("MSG_get_host_by_name(%s) failled",name);
259 luaL_error(L,"null Host : MSG_get_host_by_name failled");
262 lua_newtable (L); /* create a table, put the userdata on top of it */
263 m_host_t *lua_host = (m_host_t*)lua_newuserdata(L,sizeof(m_host_t));
264 *lua_host = msg_host;
265 luaL_getmetatable(L,HOST_MODULE_NAME);
266 lua_setmetatable(L,-2);
267 lua_setfield (L, -2, "__simgrid_host"); /* put the userdata as field of the table */
268 /* remove the args from the stack */
274 static int Host_get_name(lua_State *L) {
275 m_host_t ht = checkHost(L,-1);
276 lua_pushstring(L,MSG_host_get_name(ht));
280 static int Host_number(lua_State *L) {
281 lua_pushnumber(L,MSG_get_host_number());
286 static const luaL_reg Host_methods[] = {
287 {"getByName", Host_get_by_name},
288 {"name", Host_get_name},
289 {"number", Host_number},
293 static int Host_gc(lua_State *L)
295 m_host_t ht = checkHost(L,-1);
300 static int Host_tostring(lua_State *L)
302 lua_pushfstring(L,"Host :%p",lua_touserdata(L,1));
306 static const luaL_reg Host_meta[] = {
308 {"__tostring", Host_tostring},
313 * Environment related
315 extern lua_State *simgrid_lua_state;
317 static int run_lua_code(int argc,char **argv) {
318 DEBUG1("Run lua code %s",argv[0]);
319 // fprintf(stderr,"Run lua code %s\n", (argv ? argv[0] : "(null)"));
320 lua_State *L = lua_newthread(simgrid_lua_state);
321 int ref = luaL_ref(simgrid_lua_state, LUA_REGISTRYINDEX); // protect the thread from being garbage collected
324 /* Start the co-routine */
325 lua_getglobal(L,argv[0]);
326 xbt_assert1(lua_isfunction(L,-1),
327 "The lua function %s does not seem to exist",argv[0]);
329 // push arguments onto the stack
332 lua_pushstring(L,argv[i]);
334 // Call the function (in resume)
335 xbt_assert2(lua_pcall(L, argc-1, 1, 0) == 0,
336 "error running function `%s': %s",argv[0], lua_tostring(L, -1));
338 /* retrieve result */
339 if (lua_isnumber(L, -1)) {
340 res = lua_tonumber(L, -1);
341 lua_pop(L, 1); /* pop returned value */
345 luaL_unref(simgrid_lua_state,LUA_REGISTRYINDEX,ref );
346 DEBUG1("Execution of lua code %s is over", (argv ? argv[0] : "(null)"));
349 static int launch_application(lua_State *L) {
350 const char * file = luaL_checkstring(L,1);
351 MSG_function_register_default(run_lua_code);
352 MSG_launch_application(file);
355 #include "simix/simix.h" //FIXME: KILLME when debugging on simix internals become useless
356 static int create_environment(lua_State *L) {
357 const char *file = luaL_checkstring(L,1);
358 DEBUG1("Loading environment file %s",file);
359 MSG_create_environment(file);
360 smx_host_t *hosts = SIMIX_host_get_table();
362 for (i=0;i<SIMIX_host_get_number();i++) {
363 DEBUG1("We have an host %s", SIMIX_host_get_name(hosts[i]));
368 static int debug(lua_State *L) {
369 const char *str = luaL_checkstring(L,1);
373 static int info(lua_State *L) {
374 const char *str = luaL_checkstring(L,1);
378 static int run(lua_State *L) {
382 static int clean(lua_State *L) {
386 static const luaL_Reg simgrid_funcs[] = {
387 { "create_environment", create_environment},
388 { "launch_application", launch_application},
394 { "platform", create_environment},
395 { "application", launch_application},
399 /* ********************************************************************************* */
400 /* module management functions */
401 /* ********************************************************************************* */
403 extern const char*xbt_ctx_factory_to_use; /*Hack: let msg load directly the right factory */
405 #define LUA_MAX_ARGS_COUNT 10 /* maximum amount of arguments we can get from lua on command line */
407 int luaopen_simgrid(lua_State* L); // Fuck gcc: we don't need that prototype
408 int luaopen_simgrid(lua_State* L) {
409 //xbt_ctx_factory_to_use = "lua";
411 char **argv=malloc(sizeof(char*)*LUA_MAX_ARGS_COUNT);
413 argv[0] = (char*)"/usr/bin/lua"; /* Lie on the argv[0] so that the stack dumping facilities find the right binary. FIXME: what if lua is not in that location? */
414 /* Get the command line arguments from the lua interpreter */
415 lua_getglobal(L,"arg");
416 xbt_assert1(lua_istable(L,-1),"arg parameter is not a table but a %s",lua_typename(L,-1));
420 lua_pushinteger(L,argc-2);
422 if (lua_isnil(L,-1)) {
425 xbt_assert1(lua_isstring(L,-1),"argv[%d] got from lua is no string",argc-1);
426 xbt_assert2(argc<LUA_MAX_ARGS_COUNT,
427 "Too many arguments, please increase LUA_MAX_ARGS_COUNT in %s before recompiling SimGrid if you insist on having more than %d args on command line",
428 __FILE__,LUA_MAX_ARGS_COUNT-1);
429 argv[argc-1] = (char*)luaL_checkstring(L,-1);
431 DEBUG1("Got command line argument %s from lua",argv[argc-1]);
436 /* Initialize the MSG core */
437 MSG_global_init(&argc,argv);
438 DEBUG1("Still %d arguments on command line",argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid
440 /* register the core C functions to lua */
441 luaL_register(L, "simgrid", simgrid_funcs);
442 /* register the task methods to lua */
443 luaL_openlib(L,TASK_MODULE_NAME,Task_methods,0); //create methods table,add it to the globals
444 luaL_newmetatable(L,TASK_MODULE_NAME); //create metatable for Task,add it to the Lua registry
445 luaL_openlib(L,0,Task_meta,0);// fill metatable
446 lua_pushliteral(L,"__index");
447 lua_pushvalue(L,-3); //dup methods table
448 lua_rawset(L,-3); //matatable.__index = methods
449 lua_pushliteral(L,"__metatable");
450 lua_pushvalue(L,-3); //dup methods table
451 lua_rawset(L,-3); //hide metatable:metatable.__metatable = methods
452 lua_pop(L,1); //drop metatable
454 /* register the hosts methods to lua*/
455 luaL_openlib(L,HOST_MODULE_NAME,Host_methods,0);
456 luaL_newmetatable(L,HOST_MODULE_NAME);
457 luaL_openlib(L,0,Host_meta,0);
458 lua_pushliteral(L,"__index");
461 lua_pushliteral(L,"__metatable");
466 /* Keep the context mechanism informed of our lua world today */
467 simgrid_lua_state = L;