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. */
8 #include "simgrid_lua.h"
9 #include <string.h> // memcpy
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua, bindings, "Lua Bindings");
13 static lua_State *lua_maestro_state;
15 #define TASK_MODULE_NAME "simgrid.Task"
16 #define HOST_MODULE_NAME "simgrid.Host"
18 #define LINK_MODULE_NAME "simgrid.Link"
19 #define ROUTE_MODULE_NAME "simgrid.Route"
20 #define AS_MODULE_NAME "simgrid.AS"
21 #define TRACE_MODULE_NAME "simgrid.Trace"
24 * @brief A chunk of memory.
26 * TODO replace this by a dynar
28 typedef struct s_buffer {
32 } s_buffer_t, *buffer_t;
34 static const char* value_tostring(lua_State* L, int index);
35 static const char* keyvalue_tostring(lua_State* L, int key_index, int value_index);
36 static void stack_dump(const char *msg, lua_State* L);
37 static int writer(lua_State* L, const void* source, size_t size, void* userdata);
38 static void move_value(lua_State* src, lua_State* dst);
39 static void move_value_impl(lua_State* src, lua_State* dst, const char* name);
40 static int l_get_from_father(lua_State* L);
41 static lua_State *clone_lua_state(lua_State* L);
42 static m_task_t check_task(lua_State *L, int index);
43 static void register_c_functions(lua_State *L);
45 /* ********************************************************************************* */
46 /* helper functions */
47 /* ********************************************************************************* */
50 * @brief Returns a string representation of a value in the Lua stack.
52 * This function is for debugging purposes.
53 * It always returns the same pointer.
55 * @param L the Lua state
56 * @param index index in the stack
57 * @return a string representation of the value at this index
59 static const char* value_tostring(lua_State* L, int index) {
63 switch (lua_type(L, index)) {
70 sprintf(buff, "%.3f", lua_tonumber(L, index));
74 sprintf(buff, "%s", lua_toboolean(L, index) ? "true" : "false");
78 snprintf(buff, 63, "'%s'", lua_tostring(L, index));
82 if (lua_iscfunction(L, index)) {
83 sprintf(buff, "C-function");
86 sprintf(buff, "function");
91 sprintf(buff, "table(%zu)", lua_objlen(L, index));
94 case LUA_TLIGHTUSERDATA:
96 sprintf(buff, "userdata(%p)", lua_touserdata(L, index));
100 sprintf(buff, "thread");
107 * @brief Returns a string representation of a key-value pair.
109 * This function is for debugging purposes.
110 * It always returns the same pointer.
112 * @param L the Lua state
113 * @param key_index index of the key
114 * @param value_index index of the value
115 * @return a string representation of the key-value pair
117 static const char* keyvalue_tostring(lua_State* L, int key_index, int value_index) {
119 static char buff[64];
120 /* value_tostring also always returns the same pointer */
121 int len = snprintf(buff, 63, "[%s] -> ", value_tostring(L, key_index));
122 snprintf(buff + len, 63 - len, "%s", value_tostring(L, value_index));
127 * @brief Returns a string composed of the specified number of spaces.
129 * This function is for debugging purposes.
130 * It always returns the same pointer.
132 * @param length length of the string
133 * @return a string of this length with only spaces
135 static const char* get_spaces(int length) {
137 static char spaces[128];
139 xbt_assert(length < 128);
140 memset(spaces, ' ', length);
141 spaces[length] = '\0';
146 * @brief Dumps the Lua stack if debug logs are enabled.
147 * @param msg a message to print
148 * @param L a Lua state
150 static void stack_dump(const char* msg, lua_State* L)
152 if (XBT_LOG_ISENABLED(lua, xbt_log_priority_debug)) {
156 int top = lua_gettop(L);
163 for (i = 1; i <= top; i++) { /* repeat for each level */
165 p += sprintf(p, "%s", value_tostring(L, i));
166 p += sprintf(p, " "); /* put a separator */
168 XBT_DEBUG("%s%s", msg, buff);
173 * @brief Writes the specified data into a memory buffer.
175 * This function is a valid lua_Writer that writes into a memory buffer passed
178 * @param L a lua state
179 * @param source some data
180 * @param sz number of bytes of data
181 * @param user_data the memory buffer to write
183 static int writer(lua_State* L, const void* source, size_t size, void* userdata) {
185 buffer_t buffer = (buffer_t) userdata;
186 while (buffer->capacity < buffer->size + size) {
187 buffer->capacity *= 2;
188 buffer->data = xbt_realloc(buffer->data, buffer->capacity);
190 memcpy(buffer->data + buffer->size, source, size);
191 buffer->size += size;
197 * @brief Pops a value from a state and pushes it onto the stack of another
200 * @param src the source state
201 * @param dst the destination state
203 static void move_value(lua_State* src, lua_State* dst) {
207 /* create a list of visited tables at index 1 of dst */
211 /* dst: ... visited */
213 /* dst: visited ... */
215 move_value_impl(src, dst, value_tostring(src, -1));
217 dst: visited ... value */
220 stack_dump("src after xmove: ", src);
221 stack_dump("dst after xmove: ", dst);
226 * @brief Pops a value from the stack of a source state and pushes it on the
227 * stack of another state.
229 * If the value is a table, its content is copied recursively. To avoid cycles,
230 * a table of previously visited tables must be present at index 1 of dst.
231 * Its keys are pointers to visited tables in src and its values are the tables
234 * TODO: add support of closures
236 * @param src the source state
237 * @param dst the destination state, with a list of visited tables at index 1
238 * @param name a name describing the value
240 static void move_value_impl(lua_State* src, lua_State *dst, const char* name) {
242 luaL_checkany(src, -1); /* check the value to copy */
243 luaL_checktype(dst, 1, LUA_TTABLE); /* check the presence of a table of
244 previously visited tables */
246 int indent = (lua_gettop(dst) - 1) * 6;
247 XBT_DEBUG("%sCopying data %s", get_spaces(indent), name);
250 stack_dump("src before copying a value (should be ... value): ", src);
251 stack_dump("dst before copying a value (should be visited ...): ", dst);
253 switch (lua_type(src, -1)) {
260 lua_pushnumber(dst, lua_tonumber(src, -1));
264 lua_pushboolean(dst, lua_toboolean(src, -1));
268 /* no worries about memory: lua_pushstring makes a copy */
269 lua_pushstring(dst, lua_tostring(src, -1));
273 /* it's a function that does not exist yet in L2 */
275 if (lua_iscfunction(src, -1)) {
276 /* it's a C function: just copy the pointer */
277 lua_CFunction f = lua_tocfunction(src, -1);
278 lua_pushcfunction(dst, f);
281 /* it's a Lua function: dump it from src */
282 XBT_DEBUG("%sDumping Lua function '%s'", get_spaces(indent), name);
285 buffer.capacity = 64;
287 buffer.data = xbt_new(char, buffer.capacity);
289 /* copy the binary chunk from src into a buffer */
290 int error = lua_dump(src, writer, &buffer);
291 xbt_assert(!error, "Failed to dump function '%s' from the source state: error %d",
294 /* load the chunk into dst */
295 error = luaL_loadbuffer(dst, buffer.data, buffer.size, name);
296 xbt_assert(!error, "Failed to load function '%s' into the destination state: %s",
297 name, lua_tostring(dst, -1));
298 XBT_DEBUG("%sFunction '%s' successfully loaded", get_spaces(indent), name);
304 /* see if this table was already visited */
305 lua_pushlightuserdata(dst, (void*) lua_topointer(src, -1));
306 /* dst: visited ... psrctable */
307 lua_gettable(dst, 1);
308 /* dst: visited ... table/nil */
309 if (lua_istable(dst, -1)) {
310 XBT_DEBUG("%sNothing to do: table already visited", get_spaces(indent));
311 /* dst: visited ... table */
314 XBT_DEBUG("%sFirst visit of this table", get_spaces(indent));
315 /* dst: visited ... nil */
317 /* dst: visited ... */
319 /* first visit: create the new table in dst */
321 /* dst: visited ... table */
323 /* mark the table as visited to avoid infinite recursion */
324 lua_pushlightuserdata(dst, (void*) lua_topointer(src, -1));
325 /* dst: visited ... table psrctable */
326 lua_pushvalue(dst, -2);
327 /* dst: visited ... table psrctable table */
328 lua_settable(dst, 1);
329 /* dst: visited ... table */
330 XBT_DEBUG("%sTable marked as visited", get_spaces(indent));
332 stack_dump("dst after marking the table as visited (should be visited ... table): ", dst);
334 /* copy the metatable if any */
335 int has_meta_table = lua_getmetatable(src, -1);
336 /* src: ... table mt? */
337 if (has_meta_table) {
338 XBT_DEBUG("%sCopying metatable", get_spaces(indent));
339 /* src: ... table mt */
340 move_value_impl(src, dst, "metatable");
342 dst: visited ... table mt */
343 lua_setmetatable(dst, -2);
344 /* dst: visited ... table */
347 XBT_DEBUG("%sNo metatable", get_spaces(indent));
350 stack_dump("src before traversing the table (should be ... table): ", src);
351 stack_dump("dst before traversing the table (should be visited ... table): ", dst);
353 /* traverse the table of src and copy each element */
355 /* src: ... table nil */
356 while (lua_next(src, -2) != 0) {
357 /* src: ... table key value */
359 XBT_DEBUG("%sCopying table element %s", get_spaces(indent), keyvalue_tostring(src, -2, -1));
361 stack_dump("src before copying table element (should be ... table key value): ", src);
362 stack_dump("dst before copying table element (should be visited ... table): ", dst);
365 lua_pushvalue(src, -2);
366 /* src: ... table key value key */
368 XBT_DEBUG("%sCopying the key part of the table element", get_spaces(indent));
369 move_value_impl(src, dst, value_tostring(src, -1));
370 /* src: ... table key value
371 dst: visited ... table key */
372 XBT_DEBUG("%sCopied the key part of the table element", get_spaces(indent));
375 XBT_DEBUG("%sCopying the value part of the table element", get_spaces(indent));
376 move_value_impl(src, dst, value_tostring(src, -1));
377 /* src: ... table key
378 dst: visited ... table key value */
379 XBT_DEBUG("%sCopied the value part of the table element", get_spaces(indent));
382 /* set the table element */
383 lua_settable(dst, -3);
384 /* dst: visited ... table */
386 /* the key stays on top of src for next iteration */
387 stack_dump("src before next iteration (should be ... table key): ", src);
388 stack_dump("dst before next iteration (should be visited ... table): ", dst);
390 XBT_DEBUG("%sTable element copied", get_spaces(indent));
392 XBT_DEBUG("%sFinished traversing the table", get_spaces(indent));
396 case LUA_TLIGHTUSERDATA:
397 lua_pushlightuserdata(dst, lua_touserdata(src, -1));
403 size_t size = lua_objlen(src, -1);
404 void* src_block = lua_touserdata(src, -1);
405 void* dst_block = lua_newuserdata(dst, size);
406 memcpy(dst_block, src_block, size);
408 /* copy the metatable if any */
409 int has_meta_table = lua_getmetatable(src, -1);
410 /* src: ... udata mt? */
411 if (has_meta_table) {
412 XBT_DEBUG("%sCopying metatable of userdata", get_spaces(indent));
413 /* src: ... udata mt */
414 move_value_impl(src, dst, "metatable");
416 dst: visited ... udata mt */
417 lua_setmetatable(dst, -2);
418 /* dst: visited ... udata */
420 XBT_DEBUG("%sMetatable of userdata copied", get_spaces(indent));
423 XBT_DEBUG("%sNo metatable for this userdata", get_spaces(indent));
430 XBT_WARN("Cannot copy a thread from the source state.");
435 /* pop the value from src */
439 XBT_DEBUG("%sData copied", get_spaces(indent));
441 stack_dump("src after copying a value (should be ...): ", src);
442 stack_dump("dst after copying a value (should be visited ... value): ", dst);
446 * @brief Copies a global value or a registry value from the father state.
448 * The state L must have a father, i.e. it should have been created by
450 * This function is meant to be an __index metamethod.
451 * Consequently, it assumes that the stack has two elements:
452 * a table (either the environment or the registry of L) and the string key of
453 * a value that does not exist yet in this table. It copies the corresponding
454 * value from the father state and pushes it on the stack of L.
455 * If the value does not exist in the father state either, nil is pushed.
457 * TODO: make this function thread safe. If the simulation runs in parallel,
458 * several simulated processes may trigger this __index metamethod at the same
459 * time and get globals from maestro.
461 * @param L the current state
462 * @return number of return values pushed (always 1)
464 static int l_get_from_father(lua_State *L) {
466 /* check the arguments */
467 luaL_checktype(L, 1, LUA_TTABLE);
468 const char* key = luaL_checkstring(L, 2);
470 XBT_DEBUG("__index of '%s' begins", key);
472 /* want a global or a registry value? */
474 if (lua_equal(L, 1, LUA_REGISTRYINDEX)) {
476 pseudo_index = LUA_REGISTRYINDEX;
477 XBT_DEBUG("Will get the value from the registry of the father");
481 pseudo_index = LUA_GLOBALSINDEX;
482 XBT_DEBUG("Will get the value from the globals of the father");
486 lua_pushstring(L, "simgrid.father_state");
487 /* L table key "simgrid.father_state" */
488 lua_rawget(L, LUA_REGISTRYINDEX);
489 /* L table key father */
491 lua_State* father = lua_touserdata(L, -1);
493 if (father == NULL) {
494 XBT_WARN("This state has no father");
503 /* get the list of visited tables */
504 lua_pushstring(L, "simgrid.father_visited_tables");
505 /* L: table key "simgrid.father_visited_tables" */
506 lua_rawget(L, LUA_REGISTRYINDEX);
507 /* L: table key visited */
509 /* L: visited table key */
511 /* get the value from the father */
512 lua_getfield(father, pseudo_index, key);
513 /* father: ... value */
515 /* push the value onto the stack of L */
516 move_value_impl(father, L, key);
518 L: visited table key value */
520 /* L: table key value */
522 /* prepare the return value of __index */
523 lua_pushvalue(L, -1);
524 /* L: table key value value */
526 /* L: value table key value */
528 /* save the copied value in the table for subsequent accesses */
534 XBT_DEBUG("__index of '%s' returns %s", key, value_tostring(L, -1));
540 * @brief Creates a new Lua state and get its environment from an existing state.
542 * The state created is independent from the existing one and has its own
543 * copies of global variables and functions.
544 * However, the global variables and functions are not copied right now from
545 * the original state; they are copied only the first time they are accessed.
546 * This behavior saves time and memory, and is okay for Simgrid's needs.
548 * @param father an existing state
549 * @return the state created
551 static lua_State* clone_lua_state(lua_State *father) {
553 /* create the new state */
554 lua_State *L = luaL_newstate();
556 /* set its environment and its registry:
557 * - create a table newenv
558 * - create a metatable mt
559 * - set mt.__index = a function that copies the global from the father state
560 * - set mt as the metatable of the registry
561 * - set mt as the metatable of newenv
562 * - set newenv as the environment of the new state
564 lua_pushthread(L); /* thread */
565 lua_newtable(L); /* thread newenv */
566 lua_newtable(L); /* thread newenv mt */
567 lua_pushvalue(L, LUA_REGISTRYINDEX); /* thread newenv mt reg */
568 lua_pushcfunction(L, l_get_from_father); /* thread newenv mt reg f */
569 lua_setfield(L, -3, "__index"); /* thread newenv mt reg */
570 lua_pushvalue(L, -2); /* thread newenv mt reg mt */
571 lua_setmetatable(L, -2); /* thread newenv mt reg */
572 lua_pop(L, 1); /* thread newenv mt */
573 lua_setmetatable(L, -2); /* thread newenv */
574 lua_setfenv(L, -2); /* thread */
575 lua_pop(L, 1); /* -- */
577 /* set a pointer to the father */
578 lua_pushlightuserdata(L, father); /* father */
579 lua_setfield(L, LUA_REGISTRYINDEX, "simgrid.father_state");
582 lua_getfield(L, LUA_REGISTRYINDEX, "simgrid.father_state"); /* father */
584 /* create the table of visited tables from the father */
585 lua_newtable(L); /* visited */
586 lua_setfield(L, LUA_REGISTRYINDEX, "simgrid.father_visited_tables");
589 /* open the standard libs (theoretically, this is not necessary as they can
590 * be inherited like any global values, but without a proper support of
591 * closures, iterators like ipairs don't work). */
592 XBT_DEBUG("Metatable of globals and registry set, opening standard libraries now");
595 XBT_DEBUG("New state created");
601 * @brief Ensures that a userdata on the stack is a task
602 * and returns the pointer inside the userdata.
603 * @param L a Lua state
604 * @param index an index in the Lua stack
605 * @return the task at this index
607 static m_task_t checkTask(lua_State * L, int index)
610 luaL_checktype(L, index, LUA_TTABLE);
611 lua_getfield(L, index, "__simgrid_task");
613 pi = (m_task_t *) luaL_checkudata(L, -1, TASK_MODULE_NAME);
616 luaL_typerror(L, index, TASK_MODULE_NAME);
619 luaL_error(L, "null Task");
624 /* ********************************************************************************* */
625 /* wrapper functions */
626 /* ********************************************************************************* */
629 * A task is either something to compute somewhere, or something to exchange between two hosts (or both).
630 * It is defined by a computing amount and a message size.
639 * @brief Constructs a new task with the specified processing amount and amount
642 * @param name Task's name
644 * @param computeDuration A value of the processing amount (in flop) needed to process the task.
645 * If 0, then it cannot be executed with the execute() method.
646 * This value has to be >= 0.
648 * @param messageSize A value of amount of data (in bytes) needed to transfert this task.
649 * If 0, then it cannot be transfered with the get() and put() methods.
650 * This value has to be >= 0.
652 static int Task_new(lua_State * L)
654 XBT_DEBUG("Task new...");
655 const char *name = luaL_checkstring(L, 1);
656 int comp_size = luaL_checkint(L, 2);
657 int msg_size = luaL_checkint(L, 3);
658 m_task_t msg_task = MSG_task_create(name, comp_size, msg_size, NULL);
659 lua_newtable(L); /* create a table, put the userdata on top of it */
660 m_task_t *lua_task = (m_task_t *) lua_newuserdata(L, sizeof(m_task_t));
661 *lua_task = msg_task;
662 luaL_getmetatable(L, TASK_MODULE_NAME);
663 lua_setmetatable(L, -2);
664 lua_setfield(L, -2, "__simgrid_task"); /* put the userdata as field of the table */
665 /* remove the args from the stack */
672 static int Task_get_name(lua_State * L)
674 m_task_t tk = checkTask(L, -1);
675 lua_pushstring(L, MSG_task_get_name(tk));
679 static int Task_computation_duration(lua_State * L)
681 m_task_t tk = checkTask(L, -1);
682 lua_pushnumber(L, MSG_task_get_compute_duration(tk));
686 static int Task_execute(lua_State * L)
688 m_task_t tk = checkTask(L, -1);
689 int res = MSG_task_execute(tk);
690 lua_pushnumber(L, res);
694 static int Task_destroy(lua_State * L)
696 m_task_t tk = checkTask(L, -1);
697 int res = MSG_task_destroy(tk);
698 lua_pushnumber(L, res);
702 static int Task_send(lua_State * L)
704 //stack_dump("send ", L);
705 m_task_t tk = checkTask(L, 1);
706 const char *mailbox = luaL_checkstring(L, 2);
707 lua_pop(L, 1); // remove the string so that the task is on top of it
708 MSG_task_set_data(tk, L); // Copy my stack into the task, so that the receiver can copy the lua task directly
709 MSG_error_t res = MSG_task_send(tk, mailbox);
710 while (MSG_task_get_data(tk) != NULL) // Don't mess up with my stack: the receiver didn't copy the data yet
711 MSG_process_sleep(0); // yield
716 XBT_DEBUG("MSG_task_send failed : Timeout");
718 case MSG_TRANSFER_FAILURE:
719 XBT_DEBUG("MSG_task_send failed : Transfer Failure");
721 case MSG_HOST_FAILURE:
722 XBT_DEBUG("MSG_task_send failed : Host Failure ");
726 ("MSG_task_send failed : Unexpected error , please report this bug");
732 static int Task_recv_with_timeout(lua_State *L)
735 const char *mailbox = luaL_checkstring(L, -2);
736 int timeout = luaL_checknumber(L, -1);
737 MSG_error_t res = MSG_task_receive_with_timeout(&tk, mailbox, timeout);
740 lua_State *sender_stack = MSG_task_get_data(tk);
742 stack_dump("sender before moving data: ", sender_stack);
743 stack_dump("receiver before moving data: ", L);
745 move_value(sender_stack, L); // copy the data directly from sender's stack
747 stack_dump("sender after moving data: ", sender_stack);
748 stack_dump("receiver after moving data: ", L);
750 MSG_task_set_data(tk, NULL);
755 XBT_DEBUG("MSG_task_receive failed : Timeout");
757 case MSG_TRANSFER_FAILURE:
758 XBT_DEBUG("MSG_task_receive failed : Transfer Failure");
760 case MSG_HOST_FAILURE:
761 XBT_DEBUG("MSG_task_receive failed : Host Failure ");
764 XBT_ERROR("MSG_task_receive failed : Unexpected error , please report this bug");
772 static int Task_recv(lua_State * L)
774 lua_pushnumber(L, -1.0);
775 return Task_recv_with_timeout(L);
778 static const luaL_reg Task_methods[] = {
780 {"name", Task_get_name},
781 {"computation_duration", Task_computation_duration},
782 {"execute", Task_execute},
783 {"destroy", Task_destroy},
786 {"recv_timeout", Task_recv_with_timeout},
790 static int Task_gc(lua_State * L)
792 m_task_t tk = checkTask(L, -1);
794 MSG_task_destroy(tk);
798 static int Task_tostring(lua_State * L)
800 lua_pushfstring(L, "Task :%p", lua_touserdata(L, 1));
804 static const luaL_reg Task_meta[] = {
806 {"__tostring", Task_tostring},
813 static m_host_t checkHost(lua_State * L, int index)
816 luaL_checktype(L, index, LUA_TTABLE);
817 lua_getfield(L, index, "__simgrid_host");
818 pi = (m_host_t *) luaL_checkudata(L, -1, HOST_MODULE_NAME);
820 luaL_typerror(L, index, HOST_MODULE_NAME);
823 luaL_error(L, "null Host");
828 static int Host_get_by_name(lua_State * L)
830 const char *name = luaL_checkstring(L, 1);
831 XBT_DEBUG("Getting Host from name...");
832 m_host_t msg_host = MSG_get_host_by_name(name);
834 luaL_error(L, "null Host : MSG_get_host_by_name failed");
836 lua_newtable(L); /* create a table, put the userdata on top of it */
837 m_host_t *lua_host = (m_host_t *) lua_newuserdata(L, sizeof(m_host_t));
838 *lua_host = msg_host;
839 luaL_getmetatable(L, HOST_MODULE_NAME);
840 lua_setmetatable(L, -2);
841 lua_setfield(L, -2, "__simgrid_host"); /* put the userdata as field of the table */
842 /* remove the args from the stack */
847 static int Host_get_name(lua_State * L)
849 m_host_t ht = checkHost(L, -1);
850 lua_pushstring(L, MSG_host_get_name(ht));
854 static int Host_number(lua_State * L)
856 lua_pushnumber(L, MSG_get_host_number());
860 static int Host_at(lua_State * L)
862 int index = luaL_checkinteger(L, 1);
863 m_host_t host = MSG_get_host_table()[index - 1]; // lua indexing start by 1 (lua[1] <=> C[0])
864 lua_newtable(L); /* create a table, put the userdata on top of it */
865 m_host_t *lua_host = (m_host_t *) lua_newuserdata(L, sizeof(m_host_t));
867 luaL_getmetatable(L, HOST_MODULE_NAME);
868 lua_setmetatable(L, -2);
869 lua_setfield(L, -2, "__simgrid_host"); /* put the userdata as field of the table */
874 static int Host_self(lua_State * L)
876 m_host_t host = MSG_host_self();
878 m_host_t *lua_host =(m_host_t *)lua_newuserdata(L,sizeof(m_host_t));
880 luaL_getmetatable(L, HOST_MODULE_NAME);
881 lua_setmetatable(L, -2);
882 lua_setfield(L, -2, "__simgrid_host");
886 static int Host_get_property_value(lua_State * L)
888 m_host_t ht = checkHost(L, -2);
889 const char *prop = luaL_checkstring(L, -1);
890 lua_pushstring(L,MSG_host_get_property_value(ht,prop));
894 static int Host_sleep(lua_State *L)
896 int time = luaL_checknumber(L, -1);
897 MSG_process_sleep(time);
901 static int Host_destroy(lua_State *L)
903 m_host_t ht = checkHost(L, -1);
904 __MSG_host_destroy(ht);
908 /* ********************************************************************************* */
909 /* lua_stub_generator functions */
910 /* ********************************************************************************* */
912 xbt_dict_t process_function_set;
913 xbt_dynar_t process_list;
914 xbt_dict_t machine_set;
915 static s_process_t process;
917 void s_process_free(void *process)
919 s_process_t *p = (s_process_t *) process;
921 for (i = 0; i < p->argc; i++)
927 static int gras_add_process_function(lua_State * L)
930 const char *process_host = luaL_checkstring(L, 1);
931 const char *process_function = luaL_checkstring(L, 2);
933 if (xbt_dict_is_empty(machine_set)
934 || xbt_dict_is_empty(process_function_set)
935 || xbt_dynar_is_empty(process_list)) {
936 process_function_set = xbt_dict_new();
937 process_list = xbt_dynar_new(sizeof(s_process_t), s_process_free);
938 machine_set = xbt_dict_new();
941 xbt_dict_set(machine_set, process_host, NULL, NULL);
942 xbt_dict_set(process_function_set, process_function, NULL, NULL);
945 process.argv = xbt_new(char *, 1);
946 process.argv[0] = xbt_strdup(process_function);
947 process.host = strdup(process_host);
950 while (lua_next(L, 3) != 0) {
951 arg = lua_tostring(L, -1);
954 xbt_realloc(process.argv, (process.argc) * sizeof(char *));
955 process.argv[(process.argc) - 1] = xbt_strdup(arg);
957 XBT_DEBUG("index = %f , arg = %s \n", lua_tonumber(L, -2),
958 lua_tostring(L, -1));
962 //add to the process list
963 xbt_dynar_push(process_list, &process);
968 static int gras_generate(lua_State * L)
970 const char *project_name = luaL_checkstring(L, 1);
971 generate_sim(project_name);
972 generate_rl(project_name);
973 generate_makefile_local(project_name);
977 /***********************************
979 **********************************/
980 static int trace_start(lua_State *L)
988 static int trace_category(lua_State * L)
991 TRACE_category(luaL_checkstring(L, 1));
996 static int trace_set_task_category(lua_State *L)
999 TRACE_msg_set_task_category(checkTask(L, -2), luaL_checkstring(L, -1));
1004 static int trace_end(lua_State *L)
1012 // *********** Register Methods ******************************************* //
1017 static const luaL_reg Host_methods[] = {
1018 {"getByName", Host_get_by_name},
1019 {"name", Host_get_name},
1020 {"number", Host_number},
1022 {"self", Host_self},
1023 {"getPropValue", Host_get_property_value},
1024 {"sleep", Host_sleep},
1025 {"destroy", Host_destroy},
1026 // Bypass XML Methods
1027 {"setFunction", console_set_function},
1028 {"setProperty", console_host_set_property},
1032 static int Host_gc(lua_State * L)
1034 m_host_t ht = checkHost(L, -1);
1040 static int Host_tostring(lua_State * L)
1042 lua_pushfstring(L, "Host :%p", lua_touserdata(L, 1));
1046 static const luaL_reg Host_meta[] = {
1048 {"__tostring", Host_tostring},
1055 static const luaL_reg AS_methods[] = {
1056 {"new", console_add_AS},
1057 {"addHost", console_add_host},
1058 {"addLink", console_add_link},
1059 {"addRoute", console_add_route},
1066 static const luaL_reg Trace_methods[] = {
1067 {"start", trace_start},
1068 {"category", trace_category},
1069 {"setTaskCategory", trace_set_task_category},
1070 {"finish", trace_end},
1075 * Environment related
1079 * @brief Runs a Lua function as a new simulated process.
1080 * @param argc number of arguments of the function
1081 * @param argv name of the Lua function and array of its arguments
1082 * @return result of the function
1084 static int run_lua_code(int argc, char **argv)
1086 XBT_DEBUG("Run lua code %s", argv[0]);
1088 lua_State *L = clone_lua_state(lua_maestro_state);
1091 /* start the function */
1092 lua_getglobal(L, argv[0]);
1093 xbt_assert(lua_isfunction(L, -1),
1094 "The lua function %s does not seem to exist", argv[0]);
1096 /* push arguments onto the stack */
1098 for (i = 1; i < argc; i++)
1099 lua_pushstring(L, argv[i]);
1101 /* call the function */
1103 err = lua_pcall(L, argc - 1, 1, 0);
1104 xbt_assert(err == 0, "error running function `%s': %s", argv[0],
1105 lua_tostring(L, -1));
1107 /* retrieve result */
1108 if (lua_isnumber(L, -1)) {
1109 res = lua_tonumber(L, -1);
1110 lua_pop(L, 1); /* pop returned value */
1113 XBT_DEBUG("Execution of Lua code %s is over", (argv ? argv[0] : "(null)"));
1118 static int launch_application(lua_State * L)
1120 const char *file = luaL_checkstring(L, 1);
1121 MSG_function_register_default(run_lua_code);
1122 MSG_launch_application(file);
1126 static int create_environment(lua_State * L)
1128 const char *file = luaL_checkstring(L, 1);
1129 XBT_DEBUG("Loading environment file %s", file);
1130 MSG_create_environment(file);
1134 static int debug(lua_State * L)
1136 const char *str = luaL_checkstring(L, 1);
1137 XBT_DEBUG("%s", str);
1141 static int info(lua_State * L)
1143 const char *str = luaL_checkstring(L, 1);
1144 XBT_INFO("%s", str);
1148 static int run(lua_State * L)
1154 static int clean(lua_State * L)
1161 * Bypass XML Parser (lua console)
1165 * Register platform for MSG
1167 static int msg_register_platform(lua_State * L)
1169 /* Tell Simgrid we dont wanna use its parser */
1170 surf_parse = console_parse_platform;
1171 surf_parse_reset_callbacks();
1172 surf_config_models_setup(NULL);
1173 MSG_create_environment(NULL);
1178 * Register platform for Simdag
1181 static int sd_register_platform(lua_State * L)
1183 surf_parse = console_parse_platform_wsL07;
1184 surf_parse_reset_callbacks();
1185 surf_config_models_setup(NULL);
1186 SD_create_environment(NULL);
1191 * Register platform for gras
1193 static int gras_register_platform(lua_State * L)
1195 /* Tell Simgrid we dont wanna use surf parser */
1196 surf_parse = console_parse_platform;
1197 surf_parse_reset_callbacks();
1198 surf_config_models_setup(NULL);
1199 gras_create_environment(NULL);
1204 * Register applicaiton for MSG
1206 static int msg_register_application(lua_State * L)
1208 MSG_function_register_default(run_lua_code);
1209 surf_parse = console_parse_application;
1210 MSG_launch_application(NULL);
1215 * Register application for gras
1217 static int gras_register_application(lua_State * L)
1219 gras_function_register_default(run_lua_code);
1220 surf_parse = console_parse_application;
1221 gras_launch_application(NULL);
1225 static const luaL_Reg simgrid_funcs[] = {
1226 {"create_environment", create_environment},
1227 {"launch_application", launch_application},
1233 {"platform", create_environment},
1234 {"application", launch_application},
1235 /* methods to bypass XML parser */
1236 {"msg_register_platform", msg_register_platform},
1237 {"sd_register_platform", sd_register_platform},
1238 {"msg_register_application", msg_register_application},
1239 {"gras_register_platform", gras_register_platform},
1240 {"gras_register_application", gras_register_application},
1241 /* gras sub generator method */
1242 {"gras_set_process_function", gras_add_process_function},
1243 {"gras_generate", gras_generate},
1247 /* ********************************************************************************* */
1248 /* module management functions */
1249 /* ********************************************************************************* */
1251 #define LUA_MAX_ARGS_COUNT 10 /* maximum amount of arguments we can get from lua on command line */
1253 int luaopen_simgrid(lua_State *L); // Fuck gcc: we don't need that prototype
1256 * This function is called automatically by the Lua interpreter when some Lua code requires
1257 * the "simgrid" module.
1258 * @param L the Lua state
1260 int luaopen_simgrid(lua_State *L)
1262 XBT_DEBUG("luaopen_simgrid *****");
1264 /* Get the command line arguments from the lua interpreter */
1265 char **argv = malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT);
1267 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? */
1269 lua_getglobal(L, "arg");
1270 /* if arg is a null value, it means we use lua only as a script to init platform
1271 * else it should be a table and then take arg in consideration
1273 if (lua_istable(L, -1)) {
1277 lua_pushinteger(L, argc - 2);
1278 lua_gettable(L, -2);
1279 if (lua_isnil(L, -1)) {
1282 xbt_assert(lua_isstring(L, -1),
1283 "argv[%d] got from lua is no string", argc - 1);
1284 xbt_assert(argc < LUA_MAX_ARGS_COUNT,
1285 "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",
1286 __FILE__, LUA_MAX_ARGS_COUNT - 1);
1287 argv[argc - 1] = (char *) luaL_checkstring(L, -1);
1289 XBT_DEBUG("Got command line argument %s from lua", argv[argc - 1]);
1292 argv[argc--] = NULL;
1294 /* Initialize the MSG core */
1295 MSG_global_init(&argc, argv);
1296 XBT_DEBUG("Still %d arguments on command line", argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid
1299 /* Keep the context mechanism informed of our lua world today */
1300 lua_maestro_state = L;
1302 register_c_functions(L);
1308 * Makes the appropriate Simgrid functions available to the Lua world.
1309 * @param L a Lua world
1311 void register_c_functions(lua_State *L) {
1313 /* register the core C functions to lua */
1314 luaL_register(L, "simgrid", simgrid_funcs);
1316 /* register the task methods to lua */
1317 luaL_openlib(L, TASK_MODULE_NAME, Task_methods, 0); // create methods table, add it to the globals
1318 luaL_newmetatable(L, TASK_MODULE_NAME); // create metatable for Task, add it to the Lua registry
1319 luaL_openlib(L, 0, Task_meta, 0); // fill metatable
1320 lua_pushliteral(L, "__index");
1321 lua_pushvalue(L, -3); // dup methods table
1322 lua_rawset(L, -3); // matatable.__index = methods
1323 lua_pushliteral(L, "__metatable");
1324 lua_pushvalue(L, -3); // dup methods table
1325 lua_rawset(L, -3); // hide metatable:metatable.__metatable = methods
1326 lua_pop(L, 1); // drop metatable
1328 /* register the hosts methods to lua */
1329 luaL_openlib(L, HOST_MODULE_NAME, Host_methods, 0);
1330 luaL_newmetatable(L, HOST_MODULE_NAME);
1331 luaL_openlib(L, 0, Host_meta, 0);
1332 lua_pushliteral(L, "__index");
1333 lua_pushvalue(L, -3);
1335 lua_pushliteral(L, "__metatable");
1336 lua_pushvalue(L, -3);
1340 /* register the links methods to lua */
1341 luaL_openlib(L, AS_MODULE_NAME, AS_methods, 0);
1342 luaL_newmetatable(L, AS_MODULE_NAME);
1345 /* register the Tracing functions to lua */
1346 luaL_openlib(L, TRACE_MODULE_NAME, Trace_methods, 0);
1347 luaL_newmetatable(L, TRACE_MODULE_NAME);