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 "simgrid_lua.h"
10 #include "lua_state_cloner.h"
11 #include "lua_utils.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua, bindings, "Lua Bindings");
15 #define TASK_MODULE_NAME "simgrid.task"
16 #define COMM_MODULE_NAME "simgrid.comm"
17 #define HOST_MODULE_NAME "simgrid.host"
18 #define PROCESS_MODULE_NAME "simgrid.process"
20 #define LINK_MODULE_NAME "simgrid.link"
21 #define ROUTE_MODULE_NAME "simgrid.route"
22 #define PLATF_MODULE_NAME "simgrid.platf"
24 static lua_State* sglua_maestro_state;
26 static const char* msg_errors[] = {
34 int luaopen_simgrid(lua_State *L);
35 static void register_c_functions(lua_State *L);
36 static int run_lua_code(int argc, char **argv);
38 /* ********************************************************************************* */
39 /* simgrid.task API */
40 /* ********************************************************************************* */
43 * \brief Ensures that a value in the stack is a valid task and returns it.
44 * \param L a Lua state
45 * \param index an index in the Lua stack
46 * \return the C task corresponding to this Lua task
48 static m_task_t sglua_checktask(lua_State* L, int index)
50 sglua_stack_dump("check task: ", L);
51 luaL_checktype(L, index, LUA_TTABLE);
53 lua_getfield(L, index, "__simgrid_task");
54 /* ... task ... ctask */
55 m_task_t task = *((m_task_t*) luaL_checkudata(L, -1, TASK_MODULE_NAME));
60 luaL_error(L, "This task was sent to someone else, you cannot access it anymore");
67 * \brief Creates a new task and leaves it onto the stack.
68 * \param L a Lua state
69 * \return number of values returned to Lua
71 * - Argument 1 (string): name of the task
72 * - Argument 2 (number): computation size
73 * - Argument 3 (number): communication size
74 * - Return value (task): the task created
76 * A Lua task is a regular table with a full userdata inside, and both share
77 * the same metatable. For the regular table, the metatable allows OO-style
78 * writing such as your_task:send(someone).
79 * For the userdata, the metatable is used to check its type.
81 static int l_task_new(lua_State* L)
83 XBT_DEBUG("Task new");
84 const char* name = luaL_checkstring(L, 1);
85 int comp_size = luaL_checkint(L, 2);
86 int msg_size = luaL_checkint(L, 3);
90 m_task_t msg_task = MSG_task_create(name, comp_size, msg_size, NULL);
94 luaL_getmetatable(L, TASK_MODULE_NAME);
96 lua_setmetatable(L, -2);
98 m_task_t* lua_task = (m_task_t*) lua_newuserdata(L, sizeof(m_task_t));
100 *lua_task = msg_task;
101 luaL_getmetatable(L, TASK_MODULE_NAME);
103 lua_setmetatable(L, -2);
105 lua_setfield(L, -2, "__simgrid_task");
111 * \brief Returns the name of a task.
112 * \param L a Lua state
113 * \return number of values returned to Lua
115 * - Argument 1 (task): a task
116 * - Return value (string): name of the task
118 static int l_task_get_name(lua_State* L)
120 m_task_t task = sglua_checktask(L, 1);
121 lua_pushstring(L, MSG_task_get_name(task));
126 * \brief Returns the computation duration of a task.
127 * \param L a Lua state
128 * \return number of values returned to Lua
130 * - Argument 1 (task): a task
131 * - Return value (number): computation duration of this task
133 static int l_task_get_computation_duration(lua_State* L)
135 m_task_t task = sglua_checktask(L, 1);
136 lua_pushnumber(L, MSG_task_get_compute_duration(task));
141 * \brief Executes a task.
142 * \param L a Lua state
143 * \return number of values returned to Lua
145 * - Argument 1 (task): the task to execute
146 * - Return value (nil or string): nil if the task was successfully executed,
147 * or an error string in case of failure, which may be "task canceled" or
150 static int l_task_execute(lua_State* L)
152 m_task_t task = sglua_checktask(L, 1);
153 MSG_error_t res = MSG_task_execute(task);
159 lua_pushstring(L, msg_errors[res]);
165 * \brief Sends a task to a mailbox and waits for its completion.
166 * \param L a Lua state
167 * \return number of values returned to Lua
169 * - Argument 1 (task): the task to send
170 * - Argument 2 (string or compatible): mailbox name, as a real string or any
171 * type convertible to string (numbers always are)
172 * - Return values (nil or string): nil if the communication was successful,
173 * or an error string in case of failure, which may be "timeout",
174 * "host failure" or "transfer failure"
176 static int l_task_send(lua_State* L)
178 m_task_t task = sglua_checktask(L, 1);
179 const char* mailbox = luaL_checkstring(L, 2);
183 /* copy my stack into the task, so that the receiver can copy the lua task */
184 MSG_task_set_data(task, L);
185 MSG_error_t res = MSG_task_send(task, mailbox);
186 while (MSG_task_get_data(task) != NULL) {
187 /* don't mess up with my stack: the receiver didn't copy the data yet */
188 MSG_process_sleep(0);
192 /* the receiver is the owner of the task and may destroy it:
193 * remove the C task on my side so that I don't garbage collect it */
194 lua_getfield(L, 1, "__simgrid_task");
196 m_task_t* udata = (m_task_t*) luaL_checkudata(L, -1, TASK_MODULE_NAME);
202 lua_pushstring(L, msg_errors[res]);
207 static int l_task_isend(lua_State* L)
213 static int l_task_dsend(lua_State* L)
220 * \brief Receives a task.
221 * \param L a Lua state
222 * \return number of values returned to Lua
224 * - Argument 1 (string or compatible): mailbox name, as a real string or any
225 * type convertible to string (numbers always are)
226 * - Argument 2 (number, optional): timeout (default is no timeout)
227 * - Return values (task or nil + string): the task received, or nil plus an
228 * error message if the communication has failed
230 static int l_task_recv(lua_State* L)
232 m_task_t task = NULL;
233 const char* mailbox = luaL_checkstring(L, 1);
235 if (lua_gettop(L) >= 2) {
236 /* mailbox timeout */
237 timeout = luaL_checknumber(L, 2);
242 /* no timeout by default */
246 MSG_error_t res = MSG_task_receive_with_timeout(&task, mailbox, timeout);
249 /* copy the data directly from sender's stack */
250 lua_State* sender_stack = MSG_task_get_data(task);
251 sglua_copy_value(sender_stack, L);
253 MSG_task_set_data(task, NULL);
258 lua_pushstring(L, msg_errors[res]);
263 static int l_task_irecv(lua_State* L)
269 static const luaL_reg task_functions[] = {
271 {"get_name", l_task_get_name},
272 {"get_computation_duration", l_task_get_computation_duration},
273 {"execute", l_task_execute},
274 {"send", l_task_send},
275 {"isend", l_task_isend},
276 {"dsend", l_task_dsend},
277 {"recv", l_task_recv},
278 {"irecv", l_task_irecv},
283 * \brief Finalizes the userdata of a task.
284 * \param L a Lua state
285 * \return number of values returned to Lua
287 * - Argument 1 (userdata): a C task, possibly NULL if it was sent to another
290 static int l_task_gc(lua_State* L)
293 m_task_t task = *((m_task_t*) luaL_checkudata(L, 1, TASK_MODULE_NAME));
294 /* the task is NULL if I sent it to someone else */
296 MSG_task_destroy(task);
302 * \brief Returns a string representation of a C task.
303 * \param L a Lua state
304 * \return number of values returned to Lua
306 * - Argument 1 (userdata): a task
307 * - Return value (string): a string describing this task
309 static int l_task_tostring(lua_State* L)
311 m_task_t task = *((m_task_t*) luaL_checkudata(L, 1, TASK_MODULE_NAME));
312 lua_pushfstring(L, "Task: %p", task);
317 * \brief Metamethods of both a task table and the userdata inside it.
319 static const luaL_reg task_meta[] = {
320 {"__gc", l_task_gc}, /* will be called only for userdata */
321 {"__tostring", l_task_tostring},
325 /* ********************************************************************************* */
326 /* simgrid.comm API */
327 /* ********************************************************************************* */
330 * \brief Ensures that a value in the stack is a comm and returns it.
331 * \param L a Lua state
332 * \param index an index in the Lua stack
335 static msg_comm_t sglua_checkcomm(lua_State* L, int index)
337 msg_comm_t comm = *((msg_comm_t*) luaL_checkudata(L, index, COMM_MODULE_NAME));
343 * \brief Blocks the current process until a communication is finished.
344 * \param L a Lua state
345 * \return number of values returned to Lua
347 * This function performs a waitany operation: you can specify an
348 * individual communication or a list of communications.
349 * If you provide a list, the function returns whenever one communication of
350 * the list is finished, and this communication will be removed from you list.
351 * This means you can make a waitall operation with successive calls to this
354 * - Argument 1 (comm or table): a comm or an array of comms
355 * - Argument 2 (number, optional): timeout (supported only when there is only
357 * - Return values (comm + string): the first comm of your list that finishes,
358 * plus an error string if this comm was unsuccessful.
360 static int l_comm_wait(lua_State* L) {
362 if (lua_istable(L, 1)) {
363 // TODO implement waitany
368 msg_comm_t comm = sglua_checkcomm(L, 1);
370 if (lua_gettop(L) >= 2) {
371 timeout = luaL_checknumber(L, 2);
374 MSG_error_t res = MSG_comm_wait(comm, timeout);
381 lua_pushstring(L, msg_errors[res]);
390 * @brief Returns whether a communication is finished.
392 * This function always returns immediately.
393 * It performs a testany operation: you can provide an individual
394 * communication or a list of communications.
396 * - Argument 1 (comm or table): a comm or an array of comms
397 * - Return values (nil or comm + string): if no comm from your list is finished
398 * yet, returns nil. If a comm is finished, removes it from your list and
399 * returns it, plus an error string if it was unsuccessful.
401 static int l_comm_test(lua_State* L) {
403 if (lua_istable(L, 1)) {
404 /* TODO implement testany */
409 msg_comm_t comm = sglua_checkcomm(L, 1);
411 if (!MSG_comm_test(comm)) {
417 MSG_error_t res = MSG_comm_get_status(comm);
422 lua_pushstring(L, msg_errors[res]);
430 static const luaL_reg comm_functions[] = {
431 {"wait", l_comm_wait},
432 {"test", l_comm_test},
437 * \brief Finalizes a comm userdata.
438 * \param L a Lua state
439 * \return number of values returned to Lua
441 * - Argument 1 (userdata): a comm
443 static int l_comm_gc(lua_State* L)
446 msg_comm_t comm = *((msg_comm_t*) luaL_checkudata(L, 1, COMM_MODULE_NAME));
447 MSG_comm_destroy(comm);
452 * \brief Metamethods of the comm userdata.
454 static const luaL_reg comm_meta[] = {
459 /* ********************************************************************************* */
460 /* simgrid.host API */
461 /* ********************************************************************************* */
464 * \brief Ensures that a value in the stack is a host and returns it.
465 * \param L a Lua state
466 * \param index an index in the Lua stack
467 * \return the C host corresponding to this Lua host
469 static m_host_t sglua_checkhost(lua_State * L, int index)
472 luaL_checktype(L, index, LUA_TTABLE);
473 lua_getfield(L, index, "__simgrid_host");
474 pi = (m_host_t *) luaL_checkudata(L, lua_gettop(L), HOST_MODULE_NAME);
476 luaL_typerror(L, index, HOST_MODULE_NAME);
479 luaL_error(L, "null Host");
485 * \brief Returns a host given its name.
486 * \param L a Lua state
487 * \return number of values returned to Lua
489 * - Argument 1 (string): name of a host
490 * - Return value (host): the corresponding host
492 static int l_host_get_by_name(lua_State * L)
494 const char *name = luaL_checkstring(L, 1);
495 XBT_DEBUG("Getting Host from name...");
496 m_host_t msg_host = MSG_get_host_by_name(name);
498 luaL_error(L, "null Host : MSG_get_host_by_name failed");
500 lua_newtable(L); /* create a table, put the userdata on top of it */
501 m_host_t *lua_host = (m_host_t *) lua_newuserdata(L, sizeof(m_host_t));
502 *lua_host = msg_host;
503 luaL_getmetatable(L, HOST_MODULE_NAME);
504 lua_setmetatable(L, -2);
505 lua_setfield(L, -2, "__simgrid_host"); /* put the userdata as field of the table */
506 /* remove the args from the stack */
512 * \brief Returns the name of a host.
513 * \param L a Lua state
514 * \return number of values returned to Lua
516 * - Argument 1 (host): a host
517 * - Return value (string): name of this host
519 static int l_host_get_name(lua_State * L)
521 m_host_t ht = sglua_checkhost(L, 1);
522 lua_pushstring(L, MSG_host_get_name(ht));
527 * \brief Returns the number of existing hosts.
528 * \param L a Lua state
529 * \return number of values returned to Lua
531 * - Return value (number): number of hosts
533 static int l_host_number(lua_State * L)
535 lua_pushnumber(L, MSG_get_host_number());
540 * \brief Returns the host given its index.
541 * \param L a Lua state
542 * \return number of values returned to Lua
544 * - Argument 1 (number): an index (1 is the first)
545 * - Return value (host): the host at this index
547 static int l_host_at(lua_State * L)
549 int index = luaL_checkinteger(L, 1);
550 m_host_t host = MSG_get_host_table()[index - 1]; // lua indexing start by 1 (lua[1] <=> C[0])
551 lua_newtable(L); /* create a table, put the userdata on top of it */
552 m_host_t *lua_host = (m_host_t *) lua_newuserdata(L, sizeof(m_host_t));
554 luaL_getmetatable(L, HOST_MODULE_NAME);
555 lua_setmetatable(L, -2);
556 lua_setfield(L, -2, "__simgrid_host"); /* put the userdata as field of the table */
561 * \brief Returns the host where the current process is located.
562 * \param L a Lua state
563 * \return number of values returned to Lua
565 * - Return value (host): the current host
567 static int l_host_self(lua_State * L)
570 m_host_t host = MSG_host_self();
573 m_host_t* lua_host = (m_host_t*) lua_newuserdata(L, sizeof(m_host_t));
576 luaL_getmetatable(L, HOST_MODULE_NAME);
578 lua_setmetatable(L, -2);
580 lua_setfield(L, -2, "__simgrid_host");
586 * \brief Returns the value of a host property.
587 * \param L a Lua state
588 * \return number of values returned to Lua
590 * - Argument 1 (host): a host
591 * - Argument 2 (string): name of the property to get
592 * - Return value (string): the value of this property
594 static int l_host_get_property_value(lua_State * L)
596 m_host_t ht = sglua_checkhost(L, 1);
597 const char *prop = luaL_checkstring(L, 2);
598 lua_pushstring(L,MSG_host_get_property_value(ht,prop));
603 * \brief Makes the current process sleep for a while.
604 * \param L a Lua state
605 * \return number of values returned to Lua
607 * - Argument 1 (number): duration of the sleep
609 static int l_host_sleep(lua_State *L)
611 int time = luaL_checknumber(L, 1);
612 MSG_process_sleep(time);
617 * \brief Destroys a host.
618 * \param L a Lua state
619 * \return number of values returned to Lua
621 * - Argument 1 (host): the host to destroy
623 static int l_host_destroy(lua_State *L)
625 m_host_t ht = sglua_checkhost(L, 1);
626 __MSG_host_destroy(ht);
630 static const luaL_reg host_functions[] = {
631 {"get_by_name", l_host_get_by_name},
632 {"name", l_host_get_name},
633 {"number", l_host_number},
635 {"self", l_host_self},
636 {"get_prop_value", l_host_get_property_value},
637 {"sleep", l_host_sleep},
638 {"destroy", l_host_destroy},
639 // Bypass XML Methods
640 {"set_function", console_set_function},
641 {"set_property", console_host_set_property},
646 * \brief Returns a string representation of a host.
647 * \param L a Lua state
648 * \return number of values returned to Lua
650 * - Argument 1 (userdata): a host
651 * - Return value (string): a string describing this host
653 static int l_host_tostring(lua_State * L)
655 lua_pushfstring(L, "Host :%p", lua_touserdata(L, 1));
659 static const luaL_reg host_meta[] = {
660 {"__tostring", l_host_tostring},
664 /* ********************************************************************************* */
665 /* simgrid.process API */
666 /* ********************************************************************************* */
669 * \brief Makes the current process sleep for a while.
670 * \param L a Lua state
671 * \return number of values returned to Lua
673 * - Argument 1 (number): duration of the sleep
674 * - Return value (nil or string): nil in everything went ok, or a string error
675 * if case of failure ("host failure")
677 static int l_process_sleep(lua_State* L)
679 double duration = luaL_checknumber(L, 1);
680 MSG_error_t res = MSG_process_sleep(duration);
687 case MSG_HOST_FAILURE:
688 lua_pushliteral(L, "host failure");
692 xbt_die("Unexpected result of MSG_process_sleep: %d, please report this bug", res);
696 static const luaL_reg process_functions[] = {
697 {"sleep", l_process_sleep},
698 /* TODO: self, create, kill, suspend, is_suspended, resume, get_name,
699 * get_pid, get_ppid, migrate
704 /* ********************************************************************************* */
705 /* lua_stub_generator functions */
706 /* ********************************************************************************* */
708 xbt_dict_t process_function_set;
709 xbt_dynar_t process_list;
710 xbt_dict_t machine_set;
711 static s_process_t process;
713 void s_process_free(void *process)
715 s_process_t *p = (s_process_t *) process;
717 for (i = 0; i < p->argc; i++)
723 static int gras_add_process_function(lua_State * L)
726 const char *process_host = luaL_checkstring(L, 1);
727 const char *process_function = luaL_checkstring(L, 2);
729 if (xbt_dict_is_empty(machine_set)
730 || xbt_dict_is_empty(process_function_set)
731 || xbt_dynar_is_empty(process_list)) {
732 process_function_set = xbt_dict_new();
733 process_list = xbt_dynar_new(sizeof(s_process_t), s_process_free);
734 machine_set = xbt_dict_new();
737 xbt_dict_set(machine_set, process_host, NULL, NULL);
738 xbt_dict_set(process_function_set, process_function, NULL, NULL);
741 process.argv = xbt_new(char *, 1);
742 process.argv[0] = xbt_strdup(process_function);
743 process.host = strdup(process_host);
746 while (lua_next(L, 3) != 0) {
747 arg = lua_tostring(L, -1);
750 xbt_realloc(process.argv, (process.argc) * sizeof(char *));
751 process.argv[(process.argc) - 1] = xbt_strdup(arg);
753 XBT_DEBUG("index = %f , arg = %s \n", lua_tonumber(L, -2),
754 lua_tostring(L, -1));
758 //add to the process list
759 xbt_dynar_push(process_list, &process);
763 static int gras_generate(lua_State * L)
765 const char *project_name = luaL_checkstring(L, 1);
766 generate_sim(project_name);
767 generate_rl(project_name);
768 generate_makefile_local(project_name);
772 /* ********************************************************************************* */
773 /* simgrid.platf API */
774 /* ********************************************************************************* */
776 static const luaL_reg platf_functions[] = {
777 {"open", console_open},
778 {"close", console_close},
779 {"AS_open", console_AS_open},
780 {"AS_close", console_AS_close},
781 {"host_new", console_add_host},
782 {"link_new", console_add_link},
783 {"router_new", console_add_router},
784 {"route_new", console_add_route},
788 /* ********************************************************************************* */
790 /* ********************************************************************************* */
793 * \brief Deploys your application.
794 * \param L a Lua state
795 * \return number of values returned to Lua
797 * - Argument 1 (string): name of the deployment file to load
799 static int launch_application(lua_State* L) {
801 const char* file = luaL_checkstring(L, 1);
802 MSG_function_register_default(run_lua_code);
803 MSG_launch_application(file);
808 * \brief Creates the platform.
809 * \param L a Lua state
810 * \return number of values returned to Lua
812 * - Argument 1 (string): name of the platform file to load
814 static int create_environment(lua_State* L) {
816 const char* file = luaL_checkstring(L, 1);
817 XBT_DEBUG("Loading environment file %s", file);
818 MSG_create_environment(file);
823 * \brief Prints a log string with debug level.
824 * \param L a Lua state
825 * \return number of values returned to Lua
827 * - Argument 1 (string): the text to print
829 static int debug(lua_State* L) {
831 const char* str = luaL_checkstring(L, 1);
832 XBT_DEBUG("%s", str);
837 * \brief Prints a log string with info level.
838 * \param L a Lua state
839 * \return number of values returned to Lua
841 * - Argument 1 (string): the text to print
843 static int info(lua_State* L) {
845 const char* str = luaL_checkstring(L, 1);
851 * \brief Runs your application.
852 * \param L a Lua state
853 * \return number of values returned to Lua
855 static int run(lua_State* L) {
862 * \brief Returns the current simulated time.
863 * \param L a Lua state
864 * \return number of values returned to Lua
866 * - Return value (number): the simulated time
868 static int get_clock(lua_State* L) {
870 lua_pushnumber(L, MSG_get_clock());
875 * \brief Cleans the simulation.
876 * \param L a Lua state
877 * \return number of values returned to Lua
879 static int simgrid_gc(lua_State * L)
886 * Register platform for MSG
888 static int msg_register_platform(lua_State * L)
890 /* Tell Simgrid we dont wanna use its parser */
891 //surf_parse = console_parse_platform;
892 surf_parse_reset_callbacks();
893 MSG_create_environment(NULL);
898 * Register platform for Simdag
901 static int sd_register_platform(lua_State * L)
903 //surf_parse = console_parse_platform_wsL07;
904 surf_parse_reset_callbacks();
905 SD_create_environment(NULL);
910 * Register platform for gras
912 static int gras_register_platform(lua_State * L)
914 //surf_parse = console_parse_platform;
915 surf_parse_reset_callbacks();
916 gras_create_environment(NULL);
921 * Register applicaiton for MSG
923 static int msg_register_application(lua_State * L)
925 MSG_function_register_default(run_lua_code);
926 //surf_parse = console_parse_application;
927 MSG_launch_application(NULL);
932 * Register application for gras
934 static int gras_register_application(lua_State * L)
936 gras_function_register_default(run_lua_code);
937 //surf_parse = console_parse_application;
938 gras_launch_application(NULL);
942 static const luaL_Reg simgrid_functions[] = {
943 {"create_environment", create_environment},
944 {"launch_application", launch_application},
948 {"get_clock", get_clock},
950 {"platform", create_environment},
951 {"application", launch_application},
952 /* methods to bypass XML parser */
953 {"msg_register_platform", msg_register_platform},
954 {"sd_register_platform", sd_register_platform},
955 {"msg_register_application", msg_register_application},
956 {"gras_register_platform", gras_register_platform},
957 {"gras_register_application", gras_register_application},
958 /* gras sub generator method */
959 {"gras_set_process_function", gras_add_process_function},
960 {"gras_generate", gras_generate},
964 /* ********************************************************************************* */
965 /* module management functions */
966 /* ********************************************************************************* */
968 #define LUA_MAX_ARGS_COUNT 10 /* maximum amount of arguments we can get from lua on command line */
971 * \brief Opens the simgrid Lua module.
973 * This function is called automatically by the Lua interpreter when some
974 * Lua code requires the "simgrid" module.
976 * \param L the Lua state
978 int luaopen_simgrid(lua_State *L)
980 XBT_DEBUG("luaopen_simgrid *****");
982 /* Get the command line arguments from the lua interpreter */
983 char **argv = malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT);
985 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? */
987 lua_getglobal(L, "arg");
988 /* if arg is a null value, it means we use lua only as a script to init platform
989 * else it should be a table and then take arg in consideration
991 if (lua_istable(L, -1)) {
995 lua_pushinteger(L, argc - 2);
997 if (lua_isnil(L, -1)) {
1000 xbt_assert(lua_isstring(L, -1),
1001 "argv[%d] got from lua is no string", argc - 1);
1002 xbt_assert(argc < LUA_MAX_ARGS_COUNT,
1003 "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",
1004 __FILE__, LUA_MAX_ARGS_COUNT - 1);
1005 argv[argc - 1] = (char *) luaL_checkstring(L, -1);
1007 XBT_DEBUG("Got command line argument %s from lua", argv[argc - 1]);
1010 argv[argc--] = NULL;
1012 /* Initialize the MSG core */
1013 MSG_global_init(&argc, argv);
1014 XBT_DEBUG("Still %d arguments on command line", argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid
1017 /* Keep the context mechanism informed of our lua world today */
1018 sglua_maestro_state = L;
1020 /* initialize access to my tables by children Lua states */
1022 lua_setfield(L, LUA_REGISTRYINDEX, "simgrid.maestro_tables");
1024 register_c_functions(L);
1030 * \brief Returns whether a Lua state is the maestro state.
1031 * \param L a Lua state
1032 * \return true if this is maestro
1034 int sglua_is_maestro(lua_State* L) {
1035 return L == sglua_maestro_state;
1039 * \brief Returns the maestro state.
1040 * \return the maestro Lua state
1042 lua_State* sglua_get_maestro(void) {
1043 return sglua_maestro_state;
1047 * \brief Registers the task functions into the table simgrid.task.
1049 * Also initialize the metatable of the task userdata type.
1051 * \param L a lua state
1053 static void register_task_functions(lua_State* L)
1055 /* create a table simgrid.task and fill it with task functions */
1056 luaL_openlib(L, TASK_MODULE_NAME, task_functions, 0);
1059 /* create the metatable for tasks, add it to the Lua registry */
1060 luaL_newmetatable(L, TASK_MODULE_NAME);
1061 /* simgrid.task mt */
1062 /* fill the metatable */
1063 luaL_openlib(L, NULL, task_meta, 0);
1064 /* simgrid.task mt */
1065 lua_pushvalue(L, -2);
1066 /* simgrid.task mt simgrid.task */
1067 /* metatable.__index = simgrid.task
1068 * we put the task functions inside the task itself:
1069 * this allows to write my_task:method(args) for
1070 * simgrid.task.method(my_task, args) */
1071 lua_setfield(L, -2, "__index");
1072 /* simgrid.task mt */
1078 * \brief Registers the comm functions into the table simgrid.comm.
1080 * Also initialize the metatable of the comm userdata type.
1082 * \param L a lua state
1084 static void register_comm_functions(lua_State* L)
1086 /* create a table simgrid.com and fill it with com functions */
1087 luaL_openlib(L, COMM_MODULE_NAME, comm_functions, 0);
1090 /* create the metatable for comms, add it to the Lua registry */
1091 luaL_newmetatable(L, COMM_MODULE_NAME);
1092 /* simgrid.comm mt */
1093 /* fill the metatable */
1094 luaL_openlib(L, NULL, comm_meta, 0);
1095 /* simgrid.comm mt */
1096 lua_pushvalue(L, -2);
1097 /* simgrid.comm mt simgrid.comm */
1098 /* metatable.__index = simgrid.comm
1099 * we put the comm functions inside the comm itself:
1100 * this allows to write my_comm:method(args) for
1101 * simgrid.comm.method(my_comm, args) */
1102 lua_setfield(L, -2, "__index");
1103 /* simgrid.comm mt */
1109 * \brief Registers the host functions into the table simgrid.host.
1111 * Also initialize the metatable of the host userdata type.
1113 * \param L a lua state
1115 static void register_host_functions(lua_State* L)
1117 /* create a table simgrid.host and fill it with host functions */
1118 luaL_openlib(L, HOST_MODULE_NAME, host_functions, 0);
1121 /* create the metatable for host, add it to the Lua registry */
1122 luaL_newmetatable(L, HOST_MODULE_NAME);
1123 /* simgrid.host mt */
1124 /* fill the metatable */
1125 luaL_openlib(L, NULL, host_meta, 0);
1126 /* simgrid.host mt */
1127 lua_pushvalue(L, -2);
1128 /* simgrid.host mt simgrid.host */
1129 /* metatable.__index = simgrid.host
1130 * we put the host functions inside the host userdata itself:
1131 * this allows to write my_host:method(args) for
1132 * simgrid.host.method(my_host, args) */
1133 lua_setfield(L, -2, "__index");
1134 /* simgrid.host mt */
1140 * \brief Registers the process functions into the table simgrid.process.
1141 * \param L a lua state
1143 static void register_process_functions(lua_State* L)
1145 luaL_openlib(L, PROCESS_MODULE_NAME, process_functions, 0);
1146 /* simgrid.process */
1151 * \brief Registers the platform functions into the table simgrid.platf.
1152 * \param L a lua state
1154 static void register_platf_functions(lua_State* L)
1156 luaL_openlib(L, PLATF_MODULE_NAME, platf_functions, 0);
1162 * \brief Makes the core functions available to the Lua world.
1163 * \param L a Lua world
1165 static void register_core_functions(lua_State *L)
1167 /* register the core C functions to lua */
1168 luaL_register(L, "simgrid", simgrid_functions);
1171 /* set a finalizer that cleans simgrid, by adding to the simgrid module a
1172 * dummy userdata whose __gc metamethod calls MSG_clean() */
1173 lua_newuserdata(L, sizeof(void*));
1176 /* simgrid udata mt */
1177 lua_pushcfunction(L, simgrid_gc);
1178 /* simgrid udata mt simgrid_gc */
1179 lua_setfield(L, -2, "__gc");
1180 /* simgrid udata mt */
1181 lua_setmetatable(L, -2);
1183 lua_setfield(L, -2, "__simgrid_loaded");
1190 * \brief Creates the simgrid module and make it available to Lua.
1191 * \param L a Lua world
1193 static void register_c_functions(lua_State *L)
1195 register_core_functions(L);
1196 register_task_functions(L);
1197 register_comm_functions(L);
1198 register_host_functions(L);
1199 register_process_functions(L);
1200 register_platf_functions(L);
1204 * \brief Runs a Lua function as a new simulated process.
1205 * \param argc number of arguments of the function
1206 * \param argv name of the Lua function and array of its arguments
1207 * \return result of the function
1209 static int run_lua_code(int argc, char **argv)
1211 XBT_DEBUG("Run lua code %s", argv[0]);
1213 /* create a new state, getting globals from maestro */
1214 lua_State *L = sglua_clone_maestro();
1217 /* start the function */
1218 lua_getglobal(L, argv[0]);
1219 xbt_assert(lua_isfunction(L, -1),
1220 "There is no Lua function with name `%s'", argv[0]);
1222 /* push arguments onto the stack */
1224 for (i = 1; i < argc; i++)
1225 lua_pushstring(L, argv[i]);
1227 /* call the function */
1228 _XBT_GNUC_UNUSED int err;
1229 err = lua_pcall(L, argc - 1, 1, 0);
1230 xbt_assert(err == 0, "Error running function `%s': %s", argv[0],
1231 lua_tostring(L, -1));
1233 /* retrieve result */
1234 if (lua_isnumber(L, -1)) {
1235 res = lua_tonumber(L, -1);
1236 lua_pop(L, 1); /* pop returned value */
1239 XBT_DEBUG("Execution of Lua code %s is over", (argv ? argv[0] : "(null)"));