Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use a linear sweep waitall when MC is enabled.
[simgrid.git] / src / bindings / lua / simgrid_lua.c
1 /* SimGrid Lua bindings                                                     */
2
3 /* Copyright (c) 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua, bindings, "Lua Bindings");
11
12 lua_State *simgrid_lua_state;
13
14 #define TASK_MODULE_NAME "simgrid.Task"
15 #define HOST_MODULE_NAME "simgrid.Host"
16 // Surf ( bypass XML )
17 #define LINK_MODULE_NAME "simgrid.Link"
18 #define ROUTE_MODULE_NAME "simgrid.Route"
19 #define AS_MODULE_NAME "simgrid.AS"
20 #define TRACE_MODULE_NAME "simgrid.Trace"
21
22 /* ********************************************************************************* */
23 /*                            helper functions                                       */
24 /* ********************************************************************************* */
25 static void stackDump(const char *msg, lua_State * L)
26 {
27   char buff[2048];
28   char *p = buff;
29   int i;
30   int top = lua_gettop(L);
31
32   fflush(stdout);
33   p += sprintf(p, "STACK(top=%d): ", top);
34
35   for (i = 1; i <= top; i++) {  /* repeat for each level */
36     int t = lua_type(L, i);
37     switch (t) {
38
39     case LUA_TSTRING:          /* strings */
40       p += sprintf(p, "`%s'", lua_tostring(L, i));
41       break;
42
43     case LUA_TBOOLEAN:         /* booleans */
44       p += sprintf(p, lua_toboolean(L, i) ? "true" : "false");
45       break;
46
47     case LUA_TNUMBER:          /* numbers */
48       p += sprintf(p, "%g", lua_tonumber(L, i));
49       break;
50
51     case LUA_TTABLE:
52       p += sprintf(p, "Table");
53       break;
54
55     default:                   /* other values */
56       p += sprintf(p, "???");
57 /*      if ((ptr = luaL_checkudata(L,i,TASK_MODULE_NAME))) {
58         p+=sprintf(p,"task");
59       } else {
60         p+=printf(p,"%s", lua_typename(L, t));
61       }*/
62       break;
63
64     }
65     p += sprintf(p, "  ");      /* put a separator */
66   }
67   INFO2("%s%s", msg, buff);
68 }
69
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)
72 {
73   m_task_t *pi, tk;
74   luaL_checktype(L, index, LUA_TTABLE);
75   lua_getfield(L, index, "__simgrid_task");
76   pi = (m_task_t *) luaL_checkudata(L, -1, TASK_MODULE_NAME);
77   if (pi == NULL)
78     luaL_typerror(L, index, TASK_MODULE_NAME);
79   tk = *pi;
80   if (!tk)
81     luaL_error(L, "null Task");
82   lua_pop(L, 1);
83   return tk;
84 }
85
86 /* ********************************************************************************* */
87 /*                           wrapper functions                                       */
88 /* ********************************************************************************* */
89
90 /**
91  * A task is either something to compute somewhere, or something to exchange between two hosts (or both).
92  * It is defined by a computing amount and a message size.
93  *
94  */
95
96 /* *              * *
97  * * Constructors * *
98  * *              * */
99 /**
100  * Construct an new task with the specified processing amount and amount
101  * of data needed.
102  *
103  * @param name  Task's name
104  *
105  * @param computeDuration       A value of the processing amount (in flop) needed to process the task.
106  *                              If 0, then it cannot be executed with the execute() method.
107  *                              This value has to be >= 0.
108  *
109  * @param messageSize           A value of amount of data (in bytes) needed to transfert this task.
110  *                              If 0, then it cannot be transfered with the get() and put() methods.
111  *                              This value has to be >= 0.
112  */
113 static int Task_new(lua_State * L)
114 {
115   const char *name = luaL_checkstring(L, 1);
116   int comp_size = luaL_checkint(L, 2);
117   int msg_size = luaL_checkint(L, 3);
118   m_task_t msg_task = MSG_task_create(name, comp_size, msg_size, NULL);
119   lua_newtable(L);              /* create a table, put the userdata on top of it */
120   m_task_t *lua_task = (m_task_t *) lua_newuserdata(L, sizeof(m_task_t));
121   *lua_task = msg_task;
122   luaL_getmetatable(L, TASK_MODULE_NAME);
123   lua_setmetatable(L, -2);
124   lua_setfield(L, -2, "__simgrid_task");        /* put the userdata as field of the table */
125   /* remove the args from the stack */
126   lua_remove(L, 1);
127   lua_remove(L, 1);
128   lua_remove(L, 1);
129   return 1;
130 }
131
132 static int Task_get_name(lua_State * L)
133 {
134   m_task_t tk = checkTask(L, -1);
135   lua_pushstring(L, MSG_task_get_name(tk));
136   return 1;
137 }
138
139 static int Task_computation_duration(lua_State * L)
140 {
141   m_task_t tk = checkTask(L, -1);
142   lua_pushnumber(L, MSG_task_get_compute_duration(tk));
143   return 1;
144 }
145
146 static int Task_execute(lua_State * L)
147 {
148   m_task_t tk = checkTask(L, -1);
149   int res = MSG_task_execute(tk);
150   lua_pushnumber(L, res);
151   return 1;
152 }
153
154 static int Task_destroy(lua_State * L)
155 {
156   m_task_t tk = checkTask(L, -1);
157   int res = MSG_task_destroy(tk);
158   lua_pushnumber(L, res);
159   return 1;
160 }
161
162 static int Task_send(lua_State * L)
163 {
164   //stackDump("send ",L);
165   m_task_t tk = checkTask(L, -2);
166   const char *mailbox = luaL_checkstring(L, -1);
167   lua_pop(L, 1);                // remove the string so that the task is on top of it
168   MSG_task_set_data(tk, L);     // Copy my stack into the task, so that the receiver can copy the lua task directly
169   MSG_error_t res = MSG_task_send(tk, mailbox);
170   while (MSG_task_get_data(tk) != NULL) // Don't mess up with my stack: the receiver didn't copy the data yet
171     MSG_process_sleep(0);       // yield
172
173   if (res != MSG_OK)
174     switch (res) {
175     case MSG_TIMEOUT:
176       ERROR0("MSG_task_send failed : Timeout");
177       break;
178     case MSG_TRANSFER_FAILURE:
179       ERROR0("MSG_task_send failed : Transfer Failure");
180       break;
181     case MSG_HOST_FAILURE:
182       ERROR0("MSG_task_send failed : Host Failure ");
183       break;
184     default:
185       ERROR0
186           ("MSG_task_send failed : Unexpected error , please report this bug");
187       break;
188     }
189   return 0;
190 }
191
192 static int Task_recv(lua_State * L)
193 {
194   m_task_t tk = NULL;
195   const char *mailbox = luaL_checkstring(L, -1);
196   MSG_error_t res = MSG_task_receive(&tk, mailbox);
197
198   lua_State *sender_stack = MSG_task_get_data(tk);
199   lua_xmove(sender_stack, L, 1);        // copy the data directly from sender's stack
200   MSG_task_set_data(tk, NULL);
201
202   if (res != MSG_OK)
203     switch (res) {
204     case MSG_TIMEOUT:
205       ERROR0("MSG_task_receive failed : Timeout");
206       break;
207     case MSG_TRANSFER_FAILURE:
208       ERROR0("MSG_task_receive failed : Transfer Failure");
209       break;
210     case MSG_HOST_FAILURE:
211       ERROR0("MSG_task_receive failed : Host Failure ");
212       break;
213     default:
214       ERROR0
215           ("MSG_task_receive failed : Unexpected error , please report this bug");
216       break;
217     }
218
219   return 1;
220 }
221
222 static const luaL_reg Task_methods[] = {
223   {"new", Task_new},
224   {"name", Task_get_name},
225   {"computation_duration", Task_computation_duration},
226   {"execute", Task_execute},
227   {"destroy", Task_destroy},
228   {"send", Task_send},
229   {"recv", Task_recv},
230   {0, 0}
231 };
232
233 static int Task_gc(lua_State * L)
234 {
235   m_task_t tk = checkTask(L, -1);
236   if (tk)
237     MSG_task_destroy(tk);
238   return 0;
239 }
240
241 static int Task_tostring(lua_State * L)
242 {
243   lua_pushfstring(L, "Task :%p", lua_touserdata(L, 1));
244   return 1;
245 }
246
247 static const luaL_reg Task_meta[] = {
248   {"__gc", Task_gc},
249   {"__tostring", Task_tostring},
250   {0, 0}
251 };
252
253 /**
254  * Host
255  */
256 static m_host_t checkHost(lua_State * L, int index)
257 {
258   m_host_t *pi, ht;
259   luaL_checktype(L, index, LUA_TTABLE);
260   lua_getfield(L, index, "__simgrid_host");
261   pi = (m_host_t *) luaL_checkudata(L, -1, HOST_MODULE_NAME);
262   if (pi == NULL)
263     luaL_typerror(L, index, HOST_MODULE_NAME);
264   ht = *pi;
265   if (!ht)
266     luaL_error(L, "null Host");
267   lua_pop(L, 1);
268   return ht;
269 }
270
271 static int Host_get_by_name(lua_State * L)
272 {
273   const char *name = luaL_checkstring(L, 1);
274   DEBUG0("Getting Host from name...");
275   m_host_t msg_host = MSG_get_host_by_name(name);
276   if (!msg_host) {
277     luaL_error(L, "null Host : MSG_get_host_by_name failled");
278   }
279   lua_newtable(L);              /* create a table, put the userdata on top of it */
280   m_host_t *lua_host = (m_host_t *) lua_newuserdata(L, sizeof(m_host_t));
281   *lua_host = msg_host;
282   luaL_getmetatable(L, HOST_MODULE_NAME);
283   lua_setmetatable(L, -2);
284   lua_setfield(L, -2, "__simgrid_host");        /* put the userdata as field of the table */
285   /* remove the args from the stack */
286   lua_remove(L, 1);
287   return 1;
288 }
289
290
291 static int Host_get_name(lua_State * L)
292 {
293   m_host_t ht = checkHost(L, -1);
294   lua_pushstring(L, MSG_host_get_name(ht));
295   return 1;
296 }
297
298 static int Host_number(lua_State * L)
299 {
300   lua_pushnumber(L, MSG_get_host_number());
301   return 1;
302 }
303
304 static int Host_at(lua_State * L)
305 {
306   int index = luaL_checkinteger(L, 1);
307   m_host_t host = MSG_get_host_table()[index - 1];      // lua indexing start by 1 (lua[1] <=> C[0])
308   lua_newtable(L);              /* create a table, put the userdata on top of it */
309   m_host_t *lua_host = (m_host_t *) lua_newuserdata(L, sizeof(m_host_t));
310   *lua_host = host;
311   luaL_getmetatable(L, HOST_MODULE_NAME);
312   lua_setmetatable(L, -2);
313   lua_setfield(L, -2, "__simgrid_host");        /* put the userdata as field of the table */
314   return 1;
315
316 }
317
318 static int Host_self(lua_State * L)
319 {
320         m_host_t host = MSG_host_self();
321         lua_newtable(L);
322         m_host_t *lua_host =(m_host_t *)lua_newuserdata(L,sizeof(m_host_t));
323         *lua_host = host;
324         luaL_getmetatable(L, HOST_MODULE_NAME);
325         lua_setmetatable(L, -2);
326         lua_setfield(L, -2, "__simgrid_host");
327         return 1;
328
329 }
330
331 static int Host_get_property_value(lua_State * L)
332 {
333         m_host_t ht = checkHost(L, -2);
334         const char *prop = luaL_checkstring(L, -1);
335         lua_pushstring(L,MSG_host_get_property_value(ht,prop));
336         return 1;
337 }
338
339 /* ********************************************************************************* */
340 /*                           lua_stub_generator functions                            */
341 /* ********************************************************************************* */
342
343 xbt_dict_t process_function_set;
344 xbt_dynar_t process_list;
345 xbt_dict_t machine_set;
346 static s_process_t process;
347
348 void s_process_free(void *process)
349 {
350   s_process_t *p = (s_process_t *) process;
351   int i;
352   for (i = 0; i < p->argc; i++)
353     free(p->argv[i]);
354   free(p->argv);
355   free(p->host);
356 }
357
358 static int gras_add_process_function(lua_State * L)
359 {
360   const char *arg;
361   const char *process_host = luaL_checkstring(L, 1);
362   const char *process_function = luaL_checkstring(L, 2);
363
364   if (xbt_dict_is_empty(machine_set)
365       || xbt_dict_is_empty(process_function_set)
366       || xbt_dynar_is_empty(process_list)) {
367     process_function_set = xbt_dict_new();
368     process_list = xbt_dynar_new(sizeof(s_process_t), s_process_free);
369     machine_set = xbt_dict_new();
370   }
371
372   xbt_dict_set(machine_set, process_host, NULL, NULL);
373   xbt_dict_set(process_function_set, process_function, NULL, NULL);
374
375   process.argc = 1;
376   process.argv = xbt_new(char *, 1);
377   process.argv[0] = xbt_strdup(process_function);
378   process.host = strdup(process_host);
379
380   lua_pushnil(L);
381   while (lua_next(L, 3) != 0) {
382     arg = lua_tostring(L, -1);
383     process.argc++;
384     process.argv =
385         xbt_realloc(process.argv, (process.argc) * sizeof(char *));
386     process.argv[(process.argc) - 1] = xbt_strdup(arg);
387
388     DEBUG2("index = %f , arg = %s \n", lua_tonumber(L, -2),
389            lua_tostring(L, -1));
390     lua_pop(L, 1);
391   }
392   lua_pop(L, 1);
393   //add to the process list
394   xbt_dynar_push(process_list, &process);
395
396   return 0;
397
398 }
399
400
401 static int gras_generate(lua_State * L)
402 {
403   const char *project_name = luaL_checkstring(L, 1);
404   generate_sim(project_name);
405   generate_rl(project_name);
406   generate_makefile_local(project_name);
407   return 0;
408 }
409
410 /***********************************
411  *      Tracing
412  **********************************/
413 static int trace_start(lua_State *L)
414 {
415 #ifdef HAVE_TRACING
416   TRACE_start();
417 #endif
418   return 1;
419 }
420
421 static int trace_category(lua_State * L)
422 {
423 #ifdef HAVE_TRACING
424   TRACE_category(luaL_checkstring(L, 1));
425 #endif
426   return 1;
427 }
428
429 static int trace_set_task_category(lua_State *L)
430 {
431 #ifdef HAVE_TRACING
432   TRACE_msg_set_task_category(checkTask(L, -2), luaL_checkstring(L, -1));
433 #endif
434   return 1;
435 }
436
437 static int trace_end(lua_State *L)
438 {
439 #ifdef HAVE_TRACING
440   TRACE_end();
441 #endif
442   return 1;
443 }
444 //***********Register Methods *******************************************//
445 /*
446  * Host Methods
447  */
448 static const luaL_reg Host_methods[] = {
449   {"getByName", Host_get_by_name},
450   {"name", Host_get_name},
451   {"number", Host_number},
452   {"at", Host_at},
453   {"self",Host_self},
454   {"getPropValue",Host_get_property_value},
455   // Bypass XML Methods
456   {"new", console_add_host},
457   {"setFunction", console_set_function},
458   {0, 0}
459 };
460
461 static int Host_gc(lua_State * L)
462 {
463   m_host_t ht = checkHost(L, -1);
464   if (ht)
465     ht = NULL;
466   return 0;
467 }
468
469 static int Host_tostring(lua_State * L)
470 {
471   lua_pushfstring(L, "Host :%p", lua_touserdata(L, 1));
472   return 1;
473 }
474
475 static const luaL_reg Host_meta[] = {
476   {"__gc", Host_gc},
477   {"__tostring", Host_tostring},
478   {0, 0}
479 };
480
481 /*
482  * AS Methods
483  */
484 static const luaL_reg AS_methods[] = {
485   {"new", console_add_AS},
486   {0, 0}
487 };
488
489
490 /*
491  * Link Methods
492  */
493 static const luaL_reg Link_methods[] = {
494   {"new", console_add_link},
495   {0, 0}
496 };
497
498 /*
499  * Route Methods
500  */
501 static const luaL_reg Route_methods[] = {
502   {"new", console_add_route},
503   {0, 0}
504 };
505
506 /**
507  * Tracing Functions
508  */
509 static const luaL_reg Trace_methods[] = {
510                 {"start",trace_start},
511                 {"category",trace_category},
512                 {"setTaskCategory",trace_set_task_category},
513                 {"finish",trace_end},
514                 {0,0}
515 };
516 /*
517  * Environment related
518  */
519
520 extern lua_State *simgrid_lua_state;
521
522 static int run_lua_code(int argc, char **argv)
523 {
524   DEBUG1("Run lua code %s", argv[0]);
525   lua_State *L = lua_newthread(simgrid_lua_state);
526   int ref = luaL_ref(simgrid_lua_state, LUA_REGISTRYINDEX);     // protect the thread from being garbage collected
527   int res = 1;
528
529   /* Start the co-routine */
530   lua_getglobal(L, argv[0]);
531   xbt_assert1(lua_isfunction(L, -1),
532               "The lua function %s does not seem to exist", argv[0]);
533
534   // push arguments onto the stack
535   int i;
536   for (i = 1; i < argc; i++)
537     lua_pushstring(L, argv[i]);
538
539   // Call the function (in resume)
540   xbt_assert2(lua_pcall(L, argc - 1, 1, 0) == 0,
541               "error running function `%s': %s", argv[0], lua_tostring(L,
542                                                                        -1));
543
544   /* retrieve result */
545   if (lua_isnumber(L, -1)) {
546     res = lua_tonumber(L, -1);
547     lua_pop(L, 1);              /* pop returned value */
548   }
549   // cleanups
550   luaL_unref(simgrid_lua_state, LUA_REGISTRYINDEX, ref);
551   DEBUG1("Execution of lua code %s is over", (argv ? argv[0] : "(null)"));
552   return res;
553 }
554
555 static int launch_application(lua_State * L)
556 {
557   const char *file = luaL_checkstring(L, 1);
558   MSG_function_register_default(run_lua_code);
559   MSG_launch_application(file);
560   return 0;
561 }
562
563 #include "simix/simix.h"        //FIXME: KILLME when debugging on simix internals become useless
564 static int create_environment(lua_State * L)
565 {
566   const char *file = luaL_checkstring(L, 1);
567   DEBUG1("Loading environment file %s", file);
568   MSG_create_environment(file);
569
570 /*
571   xbt_dict_t hosts = SIMIX_host_get_dict();
572   smx_host_t host;
573   xbt_dict_cursor_t c;
574   const char *name;
575
576   xbt_dict_foreach(hosts, c, name, host) {
577     DEBUG1("We have an host %s", SIMIX_host_get_name(host));
578   }
579 */
580
581   return 0;
582 }
583
584 static int debug(lua_State * L)
585 {
586   const char *str = luaL_checkstring(L, 1);
587   DEBUG1("%s", str);
588   return 0;
589 }
590
591 static int info(lua_State * L)
592 {
593   const char *str = luaL_checkstring(L, 1);
594   INFO1("%s", str);
595   return 0;
596 }
597
598 static int run(lua_State * L)
599 {
600   MSG_main();
601   return 0;
602 }
603
604 static int clean(lua_State * L)
605 {
606   MSG_clean();
607   return 0;
608 }
609
610 /*
611  * Bypass XML Parser (lua console)
612  */
613
614 /*
615  * Register platform for MSG
616  */
617 static int msg_register_platform(lua_State * L)
618 {
619   /* Tell Simgrid we dont wanna use its parser */
620   surf_parse = console_parse_platform;
621   MSG_create_environment(NULL);
622   return 0;
623 }
624
625 /*
626  * Register platform for Simdag
627  */
628
629 static int sd_register_platform(lua_State * L)
630 {
631   surf_parse = console_parse_platform_wsL07;
632   SD_create_environment(NULL);
633   return 0;
634 }
635
636 /*
637  * Register platform for gras
638  */
639 static int gras_register_platform(lua_State * L)
640 {
641   /* Tell Simgrid we dont wanna use surf parser */
642   surf_parse = console_parse_platform;
643   gras_create_environment(NULL);
644   return 0;
645 }
646
647 /**
648  * Register applicaiton for MSG
649  */
650 static int msg_register_application(lua_State * L)
651 {
652   MSG_function_register_default(run_lua_code);
653   surf_parse = console_parse_application;
654   MSG_launch_application(NULL);
655   return 0;
656 }
657
658 /*
659  * Register application for gras
660  */
661 static int gras_register_application(lua_State * L)
662 {
663   gras_function_register_default(run_lua_code);
664   surf_parse = console_parse_application;
665   gras_launch_application(NULL);
666   return 0;
667 }
668
669 static const luaL_Reg simgrid_funcs[] = {
670   {"create_environment", create_environment},
671   {"launch_application", launch_application},
672   {"debug", debug},
673   {"info", info},
674   {"run", run},
675   {"clean", clean},
676   /* short names */
677   {"platform", create_environment},
678   {"application", launch_application},
679   /* methods to bypass XML parser */
680   {"msg_register_platform", msg_register_platform},
681   {"sd_register_platform", sd_register_platform},
682   {"msg_register_application", msg_register_application},
683   {"gras_register_platform", gras_register_platform},
684   {"gras_register_application", gras_register_application},
685   /* gras sub generator method */
686   {"gras_set_process_function", gras_add_process_function},
687   {"gras_generate", gras_generate},
688   {NULL, NULL}
689 };
690
691 /* ********************************************************************************* */
692 /*                       module management functions                                 */
693 /* ********************************************************************************* */
694
695
696 #define LUA_MAX_ARGS_COUNT 10   /* maximum amount of arguments we can get from lua on command line */
697 #define TEST
698 int luaopen_simgrid(lua_State * L);     // Fuck gcc: we don't need that prototype
699 int luaopen_simgrid(lua_State * L)
700 {
701
702   char **argv = malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT);
703   int argc = 1;
704   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? */
705   /* Get the command line arguments from the lua interpreter */
706   lua_getglobal(L, "arg");
707   /* if arg is a null value, it means we use lua only as a script to init platform
708    * else it should be a table and then take arg in consideration
709    */
710   if (lua_istable(L, -1)) {
711     int done = 0;
712     while (!done) {
713       argc++;
714       lua_pushinteger(L, argc - 2);
715       lua_gettable(L, -2);
716       if (lua_isnil(L, -1)) {
717         done = 1;
718       } else {
719         xbt_assert1(lua_isstring(L, -1),
720                     "argv[%d] got from lua is no string", argc - 1);
721         xbt_assert2(argc < LUA_MAX_ARGS_COUNT,
722                     "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",
723                     __FILE__, LUA_MAX_ARGS_COUNT - 1);
724         argv[argc - 1] = (char *) luaL_checkstring(L, -1);
725         lua_pop(L, 1);
726         DEBUG1("Got command line argument %s from lua", argv[argc - 1]);
727       }
728     }
729     argv[argc--] = NULL;
730
731     /* Initialize the MSG core */
732     MSG_global_init(&argc, argv);
733     DEBUG1("Still %d arguments on command line", argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid
734   }
735   /* register the core C functions to lua */
736   luaL_register(L, "simgrid", simgrid_funcs);
737   /* register the task methods to lua */
738   luaL_openlib(L, TASK_MODULE_NAME, Task_methods, 0);   //create methods table,add it to the globals
739   luaL_newmetatable(L, TASK_MODULE_NAME);       //create metatable for Task,add it to the Lua registry
740   luaL_openlib(L, 0, Task_meta, 0);     // fill metatable
741   lua_pushliteral(L, "__index");
742   lua_pushvalue(L, -3);         //dup methods table
743   lua_rawset(L, -3);            //matatable.__index = methods
744   lua_pushliteral(L, "__metatable");
745   lua_pushvalue(L, -3);         //dup methods table
746   lua_rawset(L, -3);            //hide metatable:metatable.__metatable = methods
747   lua_pop(L, 1);                //drop metatable
748
749   /* register the hosts methods to lua */
750   luaL_openlib(L, HOST_MODULE_NAME, Host_methods, 0);
751   luaL_newmetatable(L, HOST_MODULE_NAME);
752   luaL_openlib(L, 0, Host_meta, 0);
753   lua_pushliteral(L, "__index");
754   lua_pushvalue(L, -3);
755   lua_rawset(L, -3);
756   lua_pushliteral(L, "__metatable");
757   lua_pushvalue(L, -3);
758   lua_rawset(L, -3);
759   lua_pop(L, 1);
760
761   /* register the links methods to lua */
762   luaL_openlib(L, AS_MODULE_NAME, AS_methods, 0);
763   luaL_newmetatable(L, AS_MODULE_NAME);
764   lua_pop(L, 1);
765
766   /* register the links methods to lua */
767   luaL_openlib(L, LINK_MODULE_NAME, Link_methods, 0);
768   luaL_newmetatable(L, LINK_MODULE_NAME);
769   lua_pop(L, 1);
770
771   /*register the routes methods to lua */
772   luaL_openlib(L, ROUTE_MODULE_NAME, Route_methods, 0);
773   luaL_newmetatable(L, ROUTE_MODULE_NAME);
774   lua_pop(L, 1);
775
776   /*register the Tracing functions to lua */
777   luaL_openlib(L, TRACE_MODULE_NAME, Trace_methods, 0);
778   luaL_newmetatable(L, TRACE_MODULE_NAME);
779   lua_pop(L, 1);
780
781   /* Keep the context mechanism informed of our lua world today */
782   simgrid_lua_state = L;
783   return 1;
784 }