1 /* SimGrid Lua bindings */
3 /* Copyright (c) 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * 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"
19 // Surf ( bypass XML )
20 #define LINK_MODULE_NAME "simgrid.Link"
21 #define ROUTE_MODULE_NAME "simgrid.Route"
23 /* ********************************************************************************* */
24 /* helper functions */
25 /* ********************************************************************************* */
26 static void stackDump (const char *msg, lua_State *L) {
30 int top = lua_gettop(L);
33 p+=sprintf(p,"STACK(top=%d): ",top);
35 for (i = 1; i <= top; i++) { /* repeat for each level */
36 int t = lua_type(L, i);
39 case LUA_TSTRING: /* strings */
40 p+=sprintf(p,"`%s'", lua_tostring(L, i));
43 case LUA_TBOOLEAN: /* booleans */
44 p+=sprintf(p,lua_toboolean(L, i) ? "true" : "false");
47 case LUA_TNUMBER: /* numbers */
48 p+=sprintf(p,"%g", lua_tonumber(L, i));
52 p+=sprintf(p, "Table");
55 default: /* other values */
57 /* if ((ptr = luaL_checkudata(L,i,TASK_MODULE_NAME))) {
60 p+=printf(p,"%s", lua_typename(L, t));
65 p+=sprintf(p," "); /* put a separator */
67 INFO2("%s%s",msg,buff);
70 /** @brief ensures that a userdata on the stack is a task and returns the pointer inside the userdata */
71 static m_task_t checkTask (lua_State *L,int index) {
73 luaL_checktype(L,index,LUA_TTABLE);
74 lua_getfield(L,index,"__simgrid_task");
75 pi = (m_task_t*)luaL_checkudata(L,-1,TASK_MODULE_NAME);
77 luaL_typerror(L,index,TASK_MODULE_NAME);
80 luaL_error(L,"null Task");
84 /* ********************************************************************************* */
85 /* wrapper functions */
86 /* ********************************************************************************* */
89 * A task is either something to compute somewhere, or something to exchange between two hosts (or both).
90 * It is defined by a computing amount and a message size.
98 * Construct an new task with the specified processing amount and amount
101 * @param name Task's name
103 * @param computeDuration A value of the processing amount (in flop) needed to process the task.
104 * If 0, then it cannot be executed with the execute() method.
105 * This value has to be >= 0.
107 * @param messageSize A value of amount of data (in bytes) needed to transfert this task.
108 * If 0, then it cannot be transfered with the get() and put() methods.
109 * This value has to be >= 0.
111 static int Task_new(lua_State* L) {
112 const char *name=luaL_checkstring(L,1);
113 int comp_size = luaL_checkint(L,2);
114 int msg_size = luaL_checkint(L,3);
115 m_task_t msg_task = MSG_task_create(name,comp_size,msg_size,NULL);
116 lua_newtable (L); /* create a table, put the userdata on top of it */
117 m_task_t *lua_task = (m_task_t*)lua_newuserdata(L,sizeof(m_task_t));
118 *lua_task = msg_task;
119 luaL_getmetatable(L,TASK_MODULE_NAME);
120 lua_setmetatable(L,-2);
121 lua_setfield (L, -2, "__simgrid_task"); /* put the userdata as field of the table */
122 /* remove the args from the stack */
129 static int Task_get_name(lua_State *L) {
130 m_task_t tk = checkTask(L,-1);
131 lua_pushstring(L,MSG_task_get_name(tk));
135 static int Task_computation_duration(lua_State *L){
136 m_task_t tk = checkTask(L,-1);
137 lua_pushnumber(L,MSG_task_get_compute_duration (tk));
141 static int Task_execute(lua_State *L){
142 m_task_t tk = checkTask(L,-1);
143 int res = MSG_task_execute(tk);
144 lua_pushnumber(L,res);
148 static int Task_destroy(lua_State *L) {
149 m_task_t tk = checkTask(L,-1);
150 int res = MSG_task_destroy(tk);
151 lua_pushnumber(L,res);
155 static int Task_send(lua_State *L) {
156 //stackDump("send ",L);
157 m_task_t tk = checkTask(L,-2);
158 const char *mailbox = luaL_checkstring(L,-1);
159 lua_pop(L,1); // remove the string so that the task is on top of it
160 MSG_task_set_data(tk,L); // Copy my stack into the task, so that the receiver can copy the lua task directly
161 MSG_error_t res = MSG_task_send(tk,mailbox);
162 while (MSG_task_get_data(tk)!=NULL) // Don't mess up with my stack: the receiver didn't copy the data yet
163 MSG_process_sleep(0); // yield
165 if (res != MSG_OK) switch(res) {
167 ERROR0("MSG_task_send failed : Timeout");
169 case MSG_TRANSFER_FAILURE :
170 ERROR0("MSG_task_send failed : Transfer Failure");
172 case MSG_HOST_FAILURE :
173 ERROR0("MSG_task_send failed : Host Failure ");
176 ERROR0("MSG_task_send failed : Unexpected error , please report this bug");
182 static int Task_recv(lua_State *L) {
184 const char *mailbox = luaL_checkstring(L,-1);
185 MSG_error_t res = MSG_task_receive(&tk,mailbox);
187 lua_State *sender_stack = MSG_task_get_data(tk);
188 lua_xmove(sender_stack,L,1); // copy the data directly from sender's stack
189 MSG_task_set_data(tk,NULL);
191 if(res != MSG_OK) switch(res){
193 ERROR0("MSG_task_receive failed : Timeout");
195 case MSG_TRANSFER_FAILURE :
196 ERROR0("MSG_task_receive failed : Transfer Failure");
198 case MSG_HOST_FAILURE :
199 ERROR0("MSG_task_receive failed : Host Failure ");
202 ERROR0("MSG_task_receive failed : Unexpected error , please report this bug");
209 static const luaL_reg Task_methods[] = {
211 {"name", Task_get_name},
212 {"computation_duration", Task_computation_duration},
213 {"execute", Task_execute},
214 {"destroy", Task_destroy},
219 static int Task_gc(lua_State *L) {
220 m_task_t tk=checkTask(L,-1);
221 if (tk) MSG_task_destroy(tk);
225 static int Task_tostring(lua_State *L) {
226 lua_pushfstring(L, "Task :%p",lua_touserdata(L,1));
230 static const luaL_reg Task_meta[] = {
232 {"__tostring", Task_tostring},
239 static m_host_t checkHost (lua_State *L,int index) {
241 luaL_checktype(L,index,LUA_TTABLE);
242 lua_getfield(L,index,"__simgrid_host");
243 pi = (m_host_t*)luaL_checkudata(L,-1,HOST_MODULE_NAME);
245 luaL_typerror(L,index,HOST_MODULE_NAME);
248 luaL_error(L,"null Host");
253 static int Host_get_by_name(lua_State *L)
255 const char *name=luaL_checkstring(L,1);
256 DEBUG0("Getting Host from name...");
257 m_host_t msg_host = MSG_get_host_by_name(name);
260 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());
285 static int Host_at(lua_State *L)
287 int index = luaL_checkinteger(L,1);
288 m_host_t host = MSG_get_host_table()[index-1]; // lua indexing start by 1 (lua[1] <=> C[0])
289 lua_newtable (L); /* create a table, put the userdata on top of it */
290 m_host_t *lua_host = (m_host_t*)lua_newuserdata(L,sizeof(m_host_t));
292 luaL_getmetatable(L,HOST_MODULE_NAME);
293 lua_setmetatable(L,-2);
294 lua_setfield (L, -2, "__simgrid_host"); /* put the userdata as field of the table */
299 /*****************************************************************************************
300 * BYPASS XML SURF Methods *
301 ***************************
302 ***************************
303 ******************************************************************************************/
304 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
305 #include "surf/surf_private.h"
306 typedef struct t_host_attr
309 // Mandatory attributes
312 // Optional attributes
314 const char *power_trace;
316 const char *state_trace;
317 //deployment attribute
318 const char* function;
319 xbt_dynar_t args_list;
320 }host_attr,*p_host_attr;
322 typedef struct t_link_attr
327 }link_attr,*p_link_attr;
329 typedef struct t_route_attr
333 xbt_dynar_t links_id;
335 }route_attr,*p_route_attr;
337 //using xbt_dynar_t :
338 static xbt_dynar_t host_list_d ;
339 static xbt_dynar_t link_list_d ;
340 static xbt_dynar_t route_list_d ;
345 static void create_host(const char* id,double power_peak,double power_sc,
346 const char* power_tr,int state_init,
347 const char* state_tr)
350 double power_scale = 1.0;
351 tmgr_trace_t power_trace = NULL;
352 e_surf_resource_state_t state_initial;
353 tmgr_trace_t state_trace;
355 power_scale = power_sc;
356 if (state_init == -1)
357 state_initial = SURF_RESOURCE_OFF;
359 state_initial = SURF_RESOURCE_ON;
361 power_trace = tmgr_trace_new(power_tr);
363 power_trace = tmgr_trace_new("");
365 state_trace = tmgr_trace_new(state_tr);
367 state_trace = tmgr_trace_new("");
368 current_property_set = xbt_dict_new();
369 surf_host_create_resource(xbt_strdup(id), power_peak, power_scale,
370 power_trace, state_initial, state_trace, current_property_set);
374 static int Host_new(lua_State *L)
377 if(xbt_dynar_is_empty(host_list_d))
378 host_list_d = xbt_dynar_new(sizeof(p_host_attr), &xbt_free_ref);
382 const char *power_trace;
383 const char *state_trace;
384 double power,power_scale;
386 //get values from the table passed as argument
387 if (lua_istable(L,-1)) {
390 lua_pushstring(L,"id");
391 lua_gettable(L, -2 );
392 id = lua_tostring(L,-1);
396 lua_pushstring(L,"power");
397 lua_gettable(L, -2 );
398 power = lua_tonumber(L,-1);
402 lua_pushstring(L,"power_scale");
403 lua_gettable(L, -2 );
404 power_scale = lua_tonumber(L,-1);
408 lua_pushstring(L,"power_trace");
409 lua_gettable(L, -2 );
410 power_trace = lua_tostring(L,-1);
414 lua_pushstring(L,"state_initial");
415 lua_gettable(L, -2 );
416 state_initial = lua_tonumber(L,-1);
420 lua_pushstring(L,"state_trace");
421 lua_gettable(L, -2 );
422 state_trace = lua_tostring(L,-1);
426 ERROR0("Bad Arguments to create host, Should be a table with named arguments");
430 host = malloc(sizeof(host_attr));
432 host->power_peak = power;
433 host->power_scale = power_scale;
434 host->power_trace = power_trace;
435 host->state_initial = state_initial;
436 host->state_trace = state_trace;
437 host->function = NULL;
438 xbt_dynar_push(host_list_d, &host);
443 static int Link_new(lua_State *L) // (id,bandwidth,latency)
445 if(xbt_dynar_is_empty(link_list_d))
446 link_list_d = xbt_dynar_new(sizeof(p_link_attr), &xbt_free_ref);
449 double bandwidth,latency;
450 //get values from the table passed as argument
451 if (lua_istable(L,-1)) {
453 lua_pushstring(L,"id");
454 lua_gettable(L, -2 );
455 id = lua_tostring(L,-1);
458 // get bandwidth value
459 lua_pushstring(L,"bandwidth");
460 lua_gettable(L, -2 );
461 bandwidth = lua_tonumber(L,-1);
465 lua_pushstring(L,"latency");
466 lua_gettable(L, -2 );
467 latency = lua_tonumber(L,-1);
471 ERROR0("Bad Arguments to create link, Should be a table with named arguments");
475 p_link_attr link = malloc(sizeof(link_attr));
477 link->bandwidth = bandwidth;
478 link->latency = latency;
479 xbt_dynar_push(link_list_d,&link);
483 static int Route_new(lua_State *L) // (src_id,dest_id,links_number,link_table)
485 if(xbt_dynar_is_empty(route_list_d))
486 route_list_d = xbt_dynar_new(sizeof(p_route_attr), &xbt_free_ref);
487 const char * link_id;
488 p_route_attr route = malloc(sizeof(route_attr));
489 route->src_id = luaL_checkstring(L,1);
490 route->dest_id = luaL_checkstring(L,2);
491 route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
493 while (lua_next(L,3) != 0) {
494 link_id = lua_tostring(L, -1);
495 xbt_dynar_push(route->links_id, &link_id);
496 DEBUG2("index = %f , Link_id = %s \n",lua_tonumber(L, -2),lua_tostring(L, -1));
501 //add route to platform's route list
502 xbt_dynar_push(route_list_d,&route);
506 static int Host_set_function(lua_State *L) //(host,function,nb_args,list_args)
508 // look for the index of host in host_list
509 const char *host_id = luaL_checkstring(L,1);
510 const char* argument;
514 xbt_dynar_foreach(host_list_d,i,p_host)
516 if(p_host->id == host_id)
518 p_host->function = luaL_checkstring(L,2);
519 p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
520 // fill the args list
523 while (lua_next(L,3) != 0) {
524 argument = lua_tostring(L, -1);
525 xbt_dynar_push(p_host->args_list, &argument);
526 DEBUG2("index = %f , Arg_id = %s \n",lua_tonumber(L, -2),lua_tostring(L, -1));
534 ERROR1("Host : %s Not Fount !!",host_id);
539 * surf parse bypass platform
541 static int surf_parse_bypass_platform()
546 p_route_attr p_route;
549 xbt_dynar_foreach(host_list_d,i,p_host)
551 create_host(p_host->id,p_host->power_peak,p_host->power_scale,p_host->power_trace,
552 p_host->state_initial,p_host->state_trace);
553 //add to routing model host list
554 surf_route_add_host((char*)p_host->id);
558 xbt_dynar_foreach(link_list_d,i,p_link)
560 surf_link_create_resouce((char*)p_link->id,p_link->bandwidth,p_link->latency);
563 xbt_dynar_foreach(route_list_d,i,p_route)
565 surf_route_set_resource((char*)p_route->src_id,(char*)p_route->dest_id,p_route->links_id,0);
569 surf_add_host_traces();
571 surf_add_link_traces();
573 return 0; // must return 0 ?!!
577 * surf parse bypass application
579 static int surf_parse_bypass_application()
583 xbt_dynar_foreach(host_list_d,i,p_host)
586 MSG_set_function(p_host->id,p_host->function,p_host->args_list);
591 //***********Register Methods *******************************************//
595 static const luaL_reg Host_methods[] = {
596 {"getByName", Host_get_by_name},
597 {"name", Host_get_name},
598 {"number", Host_number},
600 // Bypass XML Methods
602 {"setFunction", Host_set_function},
606 static int Host_gc(lua_State *L)
608 m_host_t ht = checkHost(L,-1);
613 static int Host_tostring(lua_State *L)
615 lua_pushfstring(L,"Host :%p",lua_touserdata(L,1));
619 static const luaL_reg Host_meta[] = {
621 {"__tostring", Host_tostring},
628 static const luaL_reg Link_methods[] = {
635 static const luaL_reg Route_methods[] ={
641 * Environment related
644 extern lua_State *simgrid_lua_state;
646 static int run_lua_code(int argc,char **argv) {
647 DEBUG1("Run lua code %s",argv[0]);
648 lua_State *L = lua_newthread(simgrid_lua_state);
649 int ref = luaL_ref(simgrid_lua_state, LUA_REGISTRYINDEX); // protect the thread from being garbage collected
652 /* Start the co-routine */
653 lua_getglobal(L,argv[0]);
654 xbt_assert1(lua_isfunction(L,-1),
655 "The lua function %s does not seem to exist",argv[0]);
657 // push arguments onto the stack
660 lua_pushstring(L,argv[i]);
662 // Call the function (in resume)
663 xbt_assert2(lua_pcall(L, argc-1, 1, 0) == 0,
664 "error running function `%s': %s",argv[0], lua_tostring(L, -1));
666 /* retrieve result */
667 if (lua_isnumber(L, -1)) {
668 res = lua_tonumber(L, -1);
669 lua_pop(L, 1); /* pop returned value */
672 luaL_unref(simgrid_lua_state,LUA_REGISTRYINDEX,ref );
673 DEBUG1("Execution of lua code %s is over", (argv ? argv[0] : "(null)"));
676 static int launch_application(lua_State *L) {
677 const char * file = luaL_checkstring(L,1);
678 MSG_function_register_default(run_lua_code);
679 MSG_launch_application(file);
682 #include "simix/simix.h" //FIXME: KILLME when debugging on simix internals become useless
683 static int create_environment(lua_State *L) {
684 const char *file = luaL_checkstring(L,1);
685 DEBUG1("Loading environment file %s",file);
686 MSG_create_environment(file);
687 smx_host_t *hosts = SIMIX_host_get_table();
689 for (i=0;i<SIMIX_host_get_number();i++) {
690 DEBUG1("We have an host %s", SIMIX_host_get_name(hosts[i]));
696 static int debug(lua_State *L) {
697 const char *str = luaL_checkstring(L,1);
701 static int info(lua_State *L) {
702 const char *str = luaL_checkstring(L,1);
706 static int run(lua_State *L) {
710 static int clean(lua_State *L) {
718 static int register_platform(lua_State *L)
720 /* Tell Simgrid we dont wanna use its parser*/
721 surf_parse = surf_parse_bypass_platform;
722 MSG_create_environment(NULL);
726 static int register_application(lua_State *L)
728 MSG_function_register_default(run_lua_code);
729 surf_parse = surf_parse_bypass_application;
730 MSG_launch_application(NULL);
734 static const luaL_Reg simgrid_funcs[] = {
735 { "create_environment", create_environment},
736 { "launch_application", launch_application},
742 { "platform", create_environment},
743 { "application", launch_application},
744 /* methods to bypass XML parser*/
745 { "register_platform",register_platform},
746 { "register_application",register_application},
750 /* ********************************************************************************* */
751 /* module management functions */
752 /* ********************************************************************************* */
754 extern const char*xbt_ctx_factory_to_use; /*Hack: let msg load directly the right factory */
756 #define LUA_MAX_ARGS_COUNT 10 /* maximum amount of arguments we can get from lua on command line */
758 int luaopen_simgrid(lua_State* L); // Fuck gcc: we don't need that prototype
759 int luaopen_simgrid(lua_State* L) {
761 //xbt_ctx_factory_to_use = "lua";
762 char **argv=malloc(sizeof(char*)*LUA_MAX_ARGS_COUNT);
764 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? */
765 /* Get the command line arguments from the lua interpreter */
766 lua_getglobal(L,"arg");
767 /* if arg is a null value, it means we use lua only as a script to init platform
768 * else it should be a table and then take arg in consideration
770 if(lua_istable(L,-1))
775 lua_pushinteger(L,argc-2);
777 if (lua_isnil(L,-1)) {
780 xbt_assert1(lua_isstring(L,-1),"argv[%d] got from lua is no string",argc-1);
781 xbt_assert2(argc<LUA_MAX_ARGS_COUNT,
782 "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",
783 __FILE__,LUA_MAX_ARGS_COUNT-1);
784 argv[argc-1] = (char*)luaL_checkstring(L,-1);
786 DEBUG1("Got command line argument %s from lua",argv[argc-1]);
791 /* Initialize the MSG core */
792 MSG_global_init(&argc,argv);
793 DEBUG1("Still %d arguments on command line",argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid
795 /* register the core C functions to lua */
796 luaL_register(L, "simgrid", simgrid_funcs);
797 /* register the task methods to lua */
798 luaL_openlib(L,TASK_MODULE_NAME,Task_methods,0); //create methods table,add it to the globals
799 luaL_newmetatable(L,TASK_MODULE_NAME); //create metatable for Task,add it to the Lua registry
800 luaL_openlib(L,0,Task_meta,0);// fill metatable
801 lua_pushliteral(L,"__index");
802 lua_pushvalue(L,-3); //dup methods table
803 lua_rawset(L,-3); //matatable.__index = methods
804 lua_pushliteral(L,"__metatable");
805 lua_pushvalue(L,-3); //dup methods table
806 lua_rawset(L,-3); //hide metatable:metatable.__metatable = methods
807 lua_pop(L,1); //drop metatable
809 /* register the hosts methods to lua*/
810 luaL_openlib(L,HOST_MODULE_NAME,Host_methods,0);
811 luaL_newmetatable(L,HOST_MODULE_NAME);
812 luaL_openlib(L,0,Host_meta,0);
813 lua_pushliteral(L,"__index");
816 lua_pushliteral(L,"__metatable");
821 /* register the links methods to lua*/
822 luaL_openlib(L,LINK_MODULE_NAME,Link_methods,0);
823 luaL_newmetatable(L,LINK_MODULE_NAME);
826 /*register the routes methods to lua*/
827 luaL_openlib(L,ROUTE_MODULE_NAME,Route_methods,0);
828 luaL_newmetatable(L,LINK_MODULE_NAME);
831 /* Keep the context mechanism informed of our lua world today */
832 simgrid_lua_state = L;