Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update tesh files with process autorestart.
[simgrid.git] / src / bindings / lua / lua_task.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "lua_private.h"
8 #include "lua_utils.h"
9 #include "lua_state_cloner.h"
10 #include <lauxlib.h>
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_task, bindings, "Lua bindings (task module)");
13
14 #define TASK_MODULE_NAME "simgrid.task"
15
16 /* ********************************************************************************* */
17 /*                                simgrid.task API                                   */
18 /* ********************************************************************************* */
19
20 /**
21  * \brief Ensures that a value in the stack is a valid task and returns it.
22  * \param L a Lua state
23  * \param index an index in the Lua stack
24  * \return the C task corresponding to this Lua task
25  */
26 m_task_t sglua_check_task(lua_State* L, int index)
27 {
28   sglua_stack_dump("check task: ", L);
29   luaL_checktype(L, index, LUA_TTABLE);
30                                   /* ... task ... */
31   lua_getfield(L, index, "__simgrid_task");
32                                   /* ... task ... ctask */
33   m_task_t task = *((m_task_t*) luaL_checkudata(L, -1, TASK_MODULE_NAME));
34   lua_pop(L, 1);
35                                   /* ... task ... */
36
37   if (task == NULL) {
38     luaL_error(L, "This task was sent to someone else, you cannot access it anymore");
39   }
40
41   return task;
42 }
43
44 /**
45  * \brief Creates a new task and leaves it onto the stack.
46  * \param L a Lua state
47  * \return number of values returned to Lua
48  *
49  * - Argument 1 (string): name of the task
50  * - Argument 2 (number): computation size
51  * - Argument 3 (number): communication size
52  * - Return value (task): the task created
53  *
54  * A Lua task is a regular table with a full userdata inside, and both share
55  * the same metatable. For the regular table, the metatable allows OO-style
56  * writing such as your_task:send(someone).
57  * For the userdata, the metatable is used to check its type.
58  * TODO: make the task name an optional last parameter
59  */
60 static int l_task_new(lua_State* L)
61 {
62   XBT_DEBUG("Task new");
63   const char* name = luaL_checkstring(L, 1);
64   int comp_size = luaL_checkint(L, 2);
65   int msg_size = luaL_checkint(L, 3);
66                                   /* name comp comm */
67   lua_settop(L, 0);
68                                   /* -- */
69   m_task_t msg_task = MSG_task_create(name, comp_size, msg_size, NULL);
70
71   lua_newtable(L);
72                                   /* task */
73   luaL_getmetatable(L, TASK_MODULE_NAME);
74                                   /* task mt */
75   lua_setmetatable(L, -2);
76                                   /* task */
77   m_task_t* lua_task = (m_task_t*) lua_newuserdata(L, sizeof(m_task_t));
78                                   /* task ctask */
79   *lua_task = msg_task;
80   luaL_getmetatable(L, TASK_MODULE_NAME);
81                                   /* task ctask mt */
82   lua_setmetatable(L, -2);
83                                   /* task ctask */
84   lua_setfield(L, -2, "__simgrid_task");
85                                   /* task */
86   return 1;
87 }
88
89 /**
90  * \brief Returns the name of a task.
91  * \param L a Lua state
92  * \return number of values returned to Lua
93  *
94  * - Argument 1 (task): a task
95  * - Return value (string): name of the task
96  */
97 static int l_task_get_name(lua_State* L)
98 {
99   m_task_t task = sglua_check_task(L, 1);
100   lua_pushstring(L, MSG_task_get_name(task));
101   return 1;
102 }
103
104 /**
105  * \brief Returns the computation duration of a task.
106  * \param L a Lua state
107  * \return number of values returned to Lua
108  *
109  * - Argument 1 (task): a task
110  * - Return value (number): computation duration of this task
111  */
112 static int l_task_get_computation_duration(lua_State* L)
113 {
114   m_task_t task = sglua_check_task(L, 1);
115   lua_pushnumber(L, MSG_task_get_compute_duration(task));
116   return 1;
117 }
118
119 /**
120  * \brief Executes a task.
121  * \param L a Lua state
122  * \return number of values returned to Lua
123  *
124  * - Argument 1 (task): the task to execute
125  * - Return value (nil or string): nil if the task was successfully executed,
126  * or an error string in case of failure, which may be "task canceled" or
127  * "host failure"
128  */
129 static int l_task_execute(lua_State* L)
130 {
131   m_task_t task = sglua_check_task(L, 1);
132   MSG_error_t res = MSG_task_execute(task);
133
134   if (res == MSG_OK) {
135     return 0;
136   }
137   else {
138     lua_pushstring(L, sglua_get_msg_error(res));
139     return 1;
140   }
141 }
142
143 /**
144  * \brief Pops the Lua task from the stack and registers it so that the
145  * process can retrieve it later knowing the C task.
146  * \param L a lua state
147  */
148 void sglua_task_register(lua_State* L) {
149
150   m_task_t task = sglua_check_task(L, -1);
151                                   /* ... task */
152   /* put in the C task a ref to the lua task so that the receiver finds it */
153   unsigned long ref = luaL_ref(L, LUA_REGISTRYINDEX);
154                                   /* ... */
155   MSG_task_set_data(task, (void*) ref);
156 }
157
158 /**
159  * \brief Pushes onto the stack the Lua task corresponding to a C task.
160  *
161  * The Lua task must have been previously registered with task_register so
162  * that it can be retrieved knowing the C task.
163  *
164  * \param L a lua state
165  * \param task a C task
166  */
167 void sglua_task_unregister(lua_State* L, m_task_t task) {
168
169                                   /* ... */
170   /* the task is in my registry, put it onto my stack */
171   unsigned long ref = (unsigned long) MSG_task_get_data(task);
172   lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
173                                   /* ... task */
174   luaL_unref(L, LUA_REGISTRYINDEX, ref);
175   MSG_task_set_data(task, NULL);
176 }
177
178 /**
179  * \brief This function is called when a C task has just been copied.
180  *
181  * This callback is used to move the corresponding Lua task from the sender
182  * process to the receiver process.
183  * It is executed in SIMIX kernel mode when the communication finishes,
184  * before both processes are awaken. Thus, this function is thread-safe when
185  * user processes are executed in parallel, though it modifies the Lua
186  * stack of both processes to move the task.
187  * After this function, both Lua stacks are restored in their previous state.
188  * The task is moved from the registry of the sender to the registry of the
189  * receiver.
190  *
191  * \param task the task copied
192  * \param src_process the sender
193  * \param dst_process the receiver
194  */
195 static void task_copy_callback(m_task_t task, m_process_t src_process,
196     m_process_t dst_process) {
197
198   lua_State* src = MSG_process_get_data(src_process);
199   lua_State* dst = MSG_process_get_data(dst_process);
200
201                                   /* src: ...
202                                      dst: ... */
203   sglua_task_unregister(src, task);
204                                   /* src: ... task */
205   sglua_copy_value(src, dst);
206                                   /* src: ... task
207                                      dst: ... task */
208   sglua_task_register(dst);
209                                   /* dst: ... */
210
211   /* the receiver is now the owner of the task and may destroy it:
212    * make the sender forget the C task so that it doesn't garbage */
213   lua_getfield(src, -1, "__simgrid_task");
214                                   /* src: ... task ctask */
215   m_task_t* udata = (m_task_t*) luaL_checkudata(src, -1, TASK_MODULE_NAME);
216   *udata = NULL;
217   lua_pop(src, 2);
218                                   /* src: ... */
219 }
220
221 /**
222  * \brief Sends a task to a mailbox and waits for its completion.
223  * \param L a Lua state
224  * \return number of values returned to Lua
225  *
226  * - Argument 1 (task): the task to send
227  * - Argument 2 (string or compatible): mailbox name, as a real string or any
228  * type convertible to string (numbers always are)
229  * - Argument 3 (number, optional): timeout (default is no timeout)
230  * - Return values (boolean + string): true if the communication was successful,
231  * or false plus an error string in case of failure, which may be "timeout",
232  * "host failure" or "transfer failure"
233  */
234 static int l_task_send(lua_State* L)
235 {
236   m_task_t task = sglua_check_task(L, 1);
237   const char* mailbox = luaL_checkstring(L, 2);
238   double timeout;
239   if (lua_gettop(L) >= 3) {
240     timeout = luaL_checknumber(L, 3);
241   }
242   else {
243     timeout = -1;
244     /* no timeout by default */
245   }
246   lua_settop(L, 1);
247                                   /* task */
248   sglua_task_register(L);
249                                   /* -- */
250   MSG_error_t res = MSG_task_send_with_timeout(task, mailbox, timeout);
251
252   if (res == MSG_OK) {
253     lua_pushboolean(L, 1);
254                                   /* true */
255     return 1;
256   }
257   else {
258     /* the communication has failed, I'm still the owner of the task */
259     sglua_task_unregister(L, task);
260                                   /* task */
261     lua_pushboolean(L, 0);
262                                   /* task false */
263     lua_pushstring(L, sglua_get_msg_error(res));
264                                   /* task false error */
265     return 2;
266   }
267 }
268
269 /**
270  * \brief Sends a task on a mailbox.
271  * \param L a Lua state
272  * \return number of values returned to Lua
273  *
274  * This is a non-blocking function: use simgrid.comm.wait() or
275  * simgrid.comm.test() to end the communication.
276  *
277  * - Argument 1 (task): the task to send
278  * - Argument 2 (string or compatible): mailbox name, as a real string or any
279  * type convertible to string (numbers always are)
280  * - Return value (comm): a communication object to be used later with wait or test
281  */
282 static int l_task_isend(lua_State* L)
283 {
284   m_task_t task = sglua_check_task(L, 1);
285   const char* mailbox = luaL_checkstring(L, 2);
286                                   /* task mailbox ... */
287   lua_settop(L, 1);
288                                   /* task */
289   sglua_task_register(L);
290                                   /* -- */
291   msg_comm_t comm = MSG_task_isend(task, mailbox);
292
293   sglua_push_comm(L, comm);
294                                   /* comm */
295   return 1;
296 }
297
298 /**
299  * \brief Sends a task on a mailbox on a best effort way (detached send).
300  * \param L a Lua state
301  * \return number of values returned to Lua
302  *
303  * Like simgrid.task.isend, this is a non-blocking function.
304  * You can use this function if you don't care about when the communication
305  * ends and whether it succeeds.
306  * FIXME: isn't this equivalent to calling simgrid.task.isend() and ignoring
307  * the result?
308  *
309  * - Argument 1 (task): the task to send
310  * - Argument 2 (string or compatible): mailbox name, as a real string or any
311  * type convertible to string (numbers always are)
312  */
313 static int l_task_dsend(lua_State* L)
314 {
315   m_task_t task = sglua_check_task(L, 1);
316   const char* mailbox = luaL_checkstring(L, 2);
317                                   /* task mailbox ... */
318   lua_settop(L, 1);
319                                   /* task */
320   sglua_task_register(L);
321                                   /* -- */
322   MSG_task_dsend(task, mailbox, NULL);
323   return 0;
324 }
325
326 /**
327  * \brief Receives a task.
328  * \param L a Lua state
329  * \return number of values returned to Lua
330  *
331  * - Argument 1 (string or compatible): mailbox name, as a real string or any
332  * type convertible to string (numbers always are)
333  * - Argument 2 (number, optional): timeout (default is no timeout)
334  * - Return values (task or nil + string): the task received, or nil plus an
335  * error message if the communication has failed
336  */
337 static int l_task_recv(lua_State* L)
338 {
339   m_task_t task = NULL;
340   const char* mailbox = luaL_checkstring(L, 1);
341   double timeout;
342   if (lua_gettop(L) >= 2) {
343                                   /* mailbox timeout ... */
344     timeout = luaL_checknumber(L, 2);
345   }
346   else {
347                                   /* mailbox */
348     timeout = -1;
349     /* no timeout by default */
350   }
351                                   /* mailbox ... */
352   MSG_error_t res = MSG_task_receive_with_timeout(&task, mailbox, timeout);
353
354   if (res == MSG_OK) {
355     sglua_task_unregister(L, task);
356                                   /* mailbox ... task */
357     return 1;
358   }
359   else {
360     lua_pushnil(L);
361                                   /* mailbox ... nil */
362     lua_pushstring(L, sglua_get_msg_error(res));
363                                   /* mailbox ... nil error */
364     return 2;
365   }
366 }
367
368 /**
369  * \brief Asynchronously receives a task on a mailbox.
370  * \param L a Lua state
371  * \return number of values returned to Lua
372  *
373  * This is a non-blocking function: use simgrid.comm.wait() or
374  * simgrid.comm.test() to end the communication and get the task in case of
375  * success.
376  *
377  * - Argument 1 (string or compatible): mailbox name, as a real string or any
378  * type convertible to string (numbers always are)
379  * - Return value (comm): a communication object to be used later with wait or test
380  */
381
382 static int l_task_irecv(lua_State* L)
383 {
384   const char* mailbox = luaL_checkstring(L, 1);
385                                   /* mailbox ... */
386   m_task_t* task = xbt_new0(m_task_t, 1); // FIXME fix this leak
387   msg_comm_t comm = MSG_task_irecv(task, mailbox);
388   sglua_push_comm(L, comm);
389                                   /* mailbox ... comm */
390   return 1;
391 }
392
393 static const luaL_reg task_functions[] = {
394   {"new", l_task_new},
395   {"get_name", l_task_get_name},
396   {"get_computation_duration", l_task_get_computation_duration},
397   {"execute", l_task_execute},
398   {"send", l_task_send},
399   {"isend", l_task_isend},
400   {"dsend", l_task_dsend},
401   {"recv", l_task_recv},
402   {"irecv", l_task_irecv},
403   {NULL, NULL}
404 };
405
406 /**
407  * \brief Finalizes the userdata of a task.
408  * \param L a Lua state
409  * \return number of values returned to Lua
410  *
411  * - Argument 1 (userdata): a C task, possibly NULL if it was sent to another
412  * Lua state
413  */
414 static int l_task_gc(lua_State* L)
415 {
416                                   /* ctask */
417   m_task_t task = *((m_task_t*) luaL_checkudata(L, 1, TASK_MODULE_NAME));
418   /* the task is NULL if I sent it to someone else */
419   if (task != NULL) {
420     MSG_task_destroy(task);
421   }
422   return 0;
423 }
424
425 /**
426  * \brief Returns a string representation of a C task.
427  * \param L a Lua state
428  * \return number of values returned to Lua
429  *
430  * - Argument 1 (userdata): a task
431  * - Return value (string): a string describing this task
432  */
433 static int l_task_tostring(lua_State* L)
434 {
435   m_task_t task = *((m_task_t*) luaL_checkudata(L, 1, TASK_MODULE_NAME));
436   lua_pushfstring(L, "Task: %p", task);
437   return 1;
438 }
439
440 /**
441  * \brief Metamethods of both a task table and the userdata inside it.
442  */
443 static const luaL_reg task_meta[] = {
444   {"__gc", l_task_gc}, /* will be called only for userdata */
445   {"__tostring", l_task_tostring},
446   {NULL, NULL}
447 };
448
449 /**
450  * \brief Registers the task functions into the table simgrid.task.
451  *
452  * Also initialize the metatable of the task userdata type.
453  *
454  * \param L a lua state
455  */
456 void sglua_register_task_functions(lua_State* L)
457 {
458   /* create a table simgrid.task and fill it with task functions */
459   luaL_openlib(L, TASK_MODULE_NAME, task_functions, 0);
460                                   /* simgrid.task */
461
462   /* create the metatable for tasks, add it to the Lua registry */
463   luaL_newmetatable(L, TASK_MODULE_NAME);
464                                   /* simgrid.task mt */
465   /* fill the metatable */
466   luaL_openlib(L, NULL, task_meta, 0);
467                                   /* simgrid.task mt */
468   lua_pushvalue(L, -2);
469                                   /* simgrid.task mt simgrid.task */
470   /* metatable.__index = simgrid.task
471    * we put the task functions inside the task itself:
472    * this allows to write my_task:method(args) for
473    * simgrid.task.method(my_task, args) */
474   lua_setfield(L, -2, "__index");
475                                   /* simgrid.task mt */
476   lua_pop(L, 2);
477                                   /* -- */
478
479   /* set up MSG to copy Lua tasks between states */
480   MSG_task_set_copy_callback(task_copy_callback);
481 }
482