Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Added new case to lua_state_cloner.c: LUA_TNONE
[simgrid.git] / src / bindings / lua / lua_platf.c
1 /* Copyright (c) 2010, 2012-2015. 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 /* SimGrid Lua bindings                                                     */
8
9 #include "lua_private.h"
10 #include "simgrid/platf_interface.h"
11 #include "surf/surfxml_parse.h"
12 #include "surf/surf_routing.h"
13 #include <string.h>
14 #include <ctype.h>
15 #include <lauxlib.h>
16
17 #include <msg/msg_private.h>
18 #include <simix/smx_host_private.h>
19 #include <surf/surf_private.h>
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_platf, bindings, "Lua bindings (platform module)");
22
23 #define PLATF_MODULE_NAME "simgrid.platf"
24
25 /* ********************************************************************************* */
26 /*                               simgrid.platf API                                   */
27 /* ********************************************************************************* */
28
29 static const luaL_Reg platf_functions[] = {
30     {"open", console_open},
31     {"close", console_close},
32     {"AS_open", console_AS_open},
33     {"AS_close", console_AS_close},
34     {"backbone_new", console_add_backbone},
35     {"host_link_new", console_add_host___link},
36     {"host_new", console_add_host},
37     {"link_new", console_add_link},
38     {"router_new", console_add_router},
39     {"route_new", console_add_route},
40     {"ASroute_new", console_add_ASroute},
41     {NULL, NULL}
42 };
43
44 int console_open(lua_State *L) {
45   sg_platf_init();
46   sg_platf_begin();
47
48   storage_register_callbacks();
49   routing_register_callbacks();
50
51   gpu_register_callbacks();
52
53   return 0;
54 }
55
56 int console_close(lua_State *L) {
57   sg_platf_end();
58   sg_platf_exit();
59
60   xbt_lib_cursor_t cursor;
61   void **data;
62   char *name;
63
64   /* Initialize MSG and WKS hosts */
65   XBT_DEBUG("Initialize MSG and WKS hosts");
66   xbt_lib_foreach(host_lib, cursor, name, data) {
67     if(data[SURF_HOST_LEVEL]){
68       XBT_DEBUG("\tSee surf host %s",name);
69       SIMIX_host_create(name);
70       // THIS IS BRAINDEAD. There is no sg_host_t in that level, but a smx_host_priv. So commenting out for now.
71       // Lua is broken anyway. Christian will fix it
72       // __MSG_host_create((sg_host_t)data[SIMIX_HOST_LEVEL]);
73     }
74   }
75
76   return 0;
77 }
78
79 int console_add_backbone(lua_State *L) {
80   s_sg_platf_link_cbarg_t link;
81   memset(&link,0,sizeof(link));
82
83   link.properties = NULL;
84
85   lua_pushstring(L, "id");
86   lua_gettable(L, -2);
87   link.id = lua_tostring(L, -1);
88   lua_pop(L, 1);
89
90   lua_pushstring(L, "bandwidth");
91   lua_gettable(L, -2);
92   link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1));
93   lua_pop(L, 1);
94
95   lua_pushstring(L, "latency");
96   lua_gettable(L, -2);
97   link.latency = surf_parse_get_time(lua_tostring(L, -1));
98   lua_pop(L, 1);
99
100   link.state = SURF_RESOURCE_ON;
101   link.policy = SURF_LINK_SHARED;
102
103   sg_platf_new_link(&link);
104   routing_cluster_add_backbone(xbt_lib_get_or_null(link_lib, link.id, SURF_LINK_LEVEL));
105
106   return 0;
107 }
108
109 int console_add_host___link(lua_State *L) {
110   s_sg_platf_host_link_cbarg_t host_link;
111   memset(&host_link,0,sizeof(host_link));
112
113   // we get values from the table passed as argument
114   if (!lua_istable(L, -1)) {
115     XBT_ERROR
116         ("Bad Arguments to create host, Should be a table with named arguments");
117     return -1;
118   }
119
120   lua_pushstring(L, "id");
121   lua_gettable(L, -2);
122   host_link.id = lua_tostring(L, -1);
123   lua_pop(L, 1);
124
125   lua_pushstring(L, "up");
126   lua_gettable(L, -2);
127   host_link.link_up = lua_tostring(L, -1);
128   lua_pop(L, 1);
129
130   lua_pushstring(L, "down");
131   lua_gettable(L, -2);
132   host_link.link_down = lua_tostring(L, -1);
133   lua_pop(L, 1);
134
135   XBT_DEBUG("Create a host_link for host %s", host_link.id);
136   sg_platf_new_host_link(&host_link);
137
138   return 0;
139 }
140
141 int console_add_host(lua_State *L) {
142   s_sg_platf_host_cbarg_t host;
143   memset(&host,0,sizeof(host));
144   int state;
145
146   // we get values from the table passed as argument
147   if (!lua_istable(L, -1)) {
148     XBT_ERROR
149         ("Bad Arguments to create host, Should be a table with named arguments");
150     return -1;
151   }
152
153   // get Id Value
154   lua_pushstring(L, "id");
155   lua_gettable(L, -2);
156   host.id = lua_tostring(L, -1);
157   lua_pop(L, 1);
158
159   // get power value
160   lua_pushstring(L, "power");
161   lua_gettable(L, -2);
162   host.power_peak = xbt_dynar_new(sizeof(double), NULL);
163   xbt_dynar_push_as(host.power_peak, double, get_cpu_power(lua_tostring(L, -1)));
164   lua_pop(L, 1);
165
166   // get core
167   lua_pushstring(L, "core");
168   lua_gettable(L, -2);
169   if(!lua_isnumber(L,-1)) host.core_amount = 1;// Default value
170   else host.core_amount = lua_tonumber(L, -1);
171   if (host.core_amount == 0)
172     host.core_amount = 1;
173   lua_pop(L, 1);
174
175   //get power_scale
176   lua_pushstring(L, "power_scale");
177   lua_gettable(L, -2);
178   if(!lua_isnumber(L,-1)) host.power_scale = 1;// Default value
179   else host.power_scale = lua_tonumber(L, -1);
180   lua_pop(L, 1);
181
182   //get power_trace
183   lua_pushstring(L, "power_trace");
184   lua_gettable(L, -2);
185   host.power_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
186   lua_pop(L, 1);
187
188   //get state initial
189   lua_pushstring(L, "state_initial");
190   lua_gettable(L, -2);
191   if(!lua_isnumber(L,-1)) state = 1;// Default value
192   else state = lua_tonumber(L, -1);
193   lua_pop(L, 1);
194
195   if (state)
196     host.initial_state = SURF_RESOURCE_ON;
197   else
198     host.initial_state = SURF_RESOURCE_OFF;
199
200   //get trace state
201   lua_pushstring(L, "state_trace");
202   lua_gettable(L, -2);
203   host.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
204   lua_pop(L, 1);
205
206   sg_platf_new_host(&host);
207
208   return 0;
209 }
210
211 int  console_add_link(lua_State *L) {
212   s_sg_platf_link_cbarg_t link;
213   memset(&link,0,sizeof(link));
214
215   const char* policy;
216
217   if (! lua_istable(L, -1)) {
218     XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
219     return -1;
220   }
221
222   // get Id Value
223   lua_pushstring(L, "id");
224   lua_gettable(L, -2);
225   link.id = lua_tostring(L, -1);
226   lua_pop(L, 1);
227
228   // get bandwidth value
229   lua_pushstring(L, "bandwidth");
230   lua_gettable(L, -2);
231   link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1));
232   lua_pop(L, 1);
233
234   //get latency value
235   lua_pushstring(L, "latency");
236   lua_gettable(L, -2);
237   link.latency = surf_parse_get_time(lua_tostring(L, -1));
238   lua_pop(L, 1);
239
240   /*Optional Arguments  */
241
242   //get bandwidth_trace value
243   lua_pushstring(L, "bandwidth_trace");
244   lua_gettable(L, -2);
245   link.bandwidth_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
246   lua_pop(L, 1);
247
248   //get latency_trace value
249   lua_pushstring(L, "latency_trace");
250   lua_gettable(L, -2);
251   link.latency_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
252   lua_pop(L, 1);
253
254   //get state_trace value
255   lua_pushstring(L, "state_trace");
256   lua_gettable(L, -2);
257   link.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
258   lua_pop(L, 1);
259
260   //get state_initial value
261   lua_pushstring(L, "state_initial");
262   lua_gettable(L, -2);
263   if (!lua_isnumber(L,-1) || lua_tonumber(L, -1))
264     link.state = SURF_RESOURCE_ON;
265   else
266     link.state = SURF_RESOURCE_OFF;
267   lua_pop(L, 1);
268
269   //get policy value
270   lua_pushstring(L, "policy");
271   lua_gettable(L, -2);
272   policy = lua_tostring(L, -1);
273   lua_pop(L, 1);
274   if (policy && !strcmp(policy,"FULLDUPLEX")) {
275     link.policy = SURF_LINK_FULLDUPLEX;
276   } else if (policy && !strcmp(policy,"FATPIPE")) {
277     link.policy = SURF_LINK_FATPIPE;
278   } else {
279     link.policy = SURF_LINK_SHARED;
280   }
281
282   sg_platf_new_link(&link);
283
284   return 0;
285 }
286 /**
287  * add Router to AS components
288  */
289 int console_add_router(lua_State* L) {
290   s_sg_platf_router_cbarg_t router;
291   memset(&router,0,sizeof(router));
292
293   if (! lua_istable(L, -1)) {
294     XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
295     return -1;
296   }
297
298   lua_pushstring(L, "id");
299   lua_gettable(L, -2);
300   router.id = lua_tostring(L, -1);
301   lua_pop(L,1);
302
303   lua_pushstring(L,"coord");
304   lua_gettable(L,-2);
305   router.coord = lua_tostring(L, -1);
306   lua_pop(L,1);
307
308   sg_platf_new_router(&router);
309
310   return 0;
311 }
312
313 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
314
315 int console_add_route(lua_State *L) {
316   XBT_DEBUG("Adding route");
317   s_sg_platf_route_cbarg_t route;
318   memset(&route,0,sizeof(route));
319
320   /* allocating memory for the buffer, I think 2kB should be enough */
321   surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
322
323   if (! lua_istable(L, -1)) {
324     XBT_ERROR("Bad Arguments to create a route, Should be a table with named arguments");
325     return -1;
326   }
327
328   lua_pushstring(L,"src");
329   lua_gettable(L,-2);
330   route.src = lua_tostring(L, -1);
331   lua_pop(L,1);
332
333   lua_pushstring(L,"dest");
334   lua_gettable(L,-2);
335   route.dst = lua_tostring(L, -1);
336   lua_pop(L,1);
337
338   lua_pushstring(L,"links");
339   lua_gettable(L,-2);
340   route.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
341   if (xbt_dynar_is_empty(route.link_list))
342     xbt_dynar_push_as(route.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
343   lua_pop(L,1);
344
345   /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
346    * Et ouais mon pote. That's the way it goes. F34R.
347    *
348    * (Note that above this function, there is a #include statement. Is this
349    * comment related to that statement?)
350    */
351   lua_pushstring(L,"symmetrical");
352   lua_gettable(L,-2);
353   if (lua_isstring(L, -1)) {
354     const char* value = lua_tostring(L, -1);
355     if (strcmp("YES", value) == 0) {
356       route.symmetrical = TRUE;
357     }
358     else
359       route.symmetrical = FALSE;
360   }
361   else {
362     route.symmetrical = TRUE;
363   }
364   lua_pop(L,1);
365
366   route.gw_src = NULL;
367   route.gw_dst = NULL;
368
369   sg_platf_new_route(&route);
370
371   return 0;
372 }
373
374 int console_add_ASroute(lua_State *L) {
375   s_sg_platf_route_cbarg_t ASroute;
376   memset(&ASroute,0,sizeof(ASroute));
377
378   lua_pushstring(L, "src");
379   lua_gettable(L, -2);
380   ASroute.src = lua_tostring(L, -1);
381   lua_pop(L, 1);
382
383   lua_pushstring(L, "dst");
384   lua_gettable(L, -2);
385   ASroute.dst = lua_tostring(L, -1);
386   lua_pop(L, 1);
387
388   lua_pushstring(L, "gw_src");
389   lua_gettable(L, -2);
390   ASroute.gw_src = sg_routing_edge_by_name_or_null(lua_tostring(L, -1));
391   lua_pop(L, 1);
392
393   lua_pushstring(L, "gw_dst");
394   lua_gettable(L, -2);
395   ASroute.gw_dst = sg_routing_edge_by_name_or_null(lua_tostring(L, -1));
396   lua_pop(L, 1);
397
398   /*if (A_surfxml_ASroute_gw___src && !ASroute.gw_src)*/
399     /*surf_parse_error("gw_src=\"%s\" not found for ASroute from \"%s\" to \"%s\"",*/
400                      /*A_surfxml_ASroute_gw___src, ASroute.src, ASroute.dst);*/
401   /*if (A_surfxml_ASroute_gw___dst && !ASroute.gw_dst)*/
402     /*surf_parse_error("gw_dst=\"%s\" not found for ASroute from \"%s\" to \"%s\"",*/
403                      /*A_surfxml_ASroute_gw___dst, ASroute.src, ASroute.dst);*/
404
405   lua_pushstring(L,"links");
406   lua_gettable(L,-2);
407   ASroute.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
408   if (xbt_dynar_is_empty(ASroute.link_list))
409     xbt_dynar_push_as(ASroute.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
410   lua_pop(L,1);
411
412   lua_pushstring(L,"symmetrical");
413   lua_gettable(L,-2);
414   if (lua_isstring(L, -1)) {
415     const char* value = lua_tostring(L, -1);
416     if (strcmp("YES", value) == 0) {
417       ASroute.symmetrical = TRUE;
418     }
419     else
420       ASroute.symmetrical = FALSE;
421   }
422   else {
423     ASroute.symmetrical = TRUE;
424   }
425   lua_pop(L,1);
426
427   sg_platf_new_ASroute(&ASroute);
428
429   return 0;
430 }
431
432 int console_AS_open(lua_State *L) {
433  const char *id;
434  const char *mode;
435
436  XBT_DEBUG("Opening AS");
437
438  if (! lua_istable(L, 1)) {
439    XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
440    return -1;
441  }
442
443  lua_pushstring(L, "id");
444  lua_gettable(L, -2);
445  id = lua_tostring(L, -1);
446  lua_pop(L, 1);
447
448  lua_pushstring(L, "mode");
449  lua_gettable(L, -2);
450  mode = lua_tostring(L, -1);
451  lua_pop(L, 1);
452
453  int mode_int = A_surfxml_AS_routing_None;
454  if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
455  else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
456  else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
457  else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
458  else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
459  else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
460  else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
461  else xbt_die("Don't have the model name '%s'",mode);
462
463  s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
464  AS.id = id;
465  AS.routing = mode_int;
466
467  sg_platf_new_AS_begin(&AS);
468
469  return 0;
470 }
471 int console_AS_close(lua_State *L) {
472   XBT_DEBUG("Closing AS");
473   sg_platf_new_AS_end();
474   return 0;
475 }
476
477 int console_set_function(lua_State *L) {
478
479   const char *host_id ;
480   const char *function_id;
481   xbt_dynar_t args;
482
483   if (! lua_istable(L, 1)) {
484     XBT_ERROR("Bad Arguments to AS.new, Should be a table with named arguments");
485     return -1;
486   }
487
488   // get Host id
489   lua_pushstring(L, "host");
490   lua_gettable(L, -2);
491   host_id = lua_tostring(L, -1);
492   lua_pop(L, 1);
493
494   // get Function Name
495   lua_pushstring(L, "fct");
496   lua_gettable(L, -2);
497   function_id = lua_tostring(L, -1);
498   lua_pop(L, 1);
499
500   //get args
501   lua_pushstring(L,"args");
502   lua_gettable(L, -2);
503   args = xbt_str_split_str( lua_tostring(L,-1) , ",");
504   lua_pop(L, 1);
505
506   msg_host_t host = MSG_host_by_name(host_id);
507   if (!host) {
508     XBT_ERROR("no host '%s' found",host_id);
509     return -1;
510   }
511
512   // FIXME: use sg_platf_new_process directly (warning: find a way to check hostname)
513   MSG_set_function(host_id, function_id, args);
514
515   return 0;
516 }
517
518 int console_host_set_property(lua_State *L) {
519   const char* name ="";
520   const char* prop_id = "";
521   const char* prop_value = "";
522   if (!lua_istable(L, -1)) {
523     XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
524     return -1;
525   }
526
527
528   // get Host id
529   lua_pushstring(L, "host");
530   lua_gettable(L, -2);
531   name = lua_tostring(L, -1);
532   lua_pop(L, 1);
533
534   // get prop Name
535   lua_pushstring(L, "prop");
536   lua_gettable(L, -2);
537   prop_id = lua_tostring(L, -1);
538   lua_pop(L, 1);
539   //get args
540   lua_pushstring(L,"value");
541   lua_gettable(L, -2);
542   prop_value = lua_tostring(L,-1);
543   lua_pop(L, 1);
544
545   msg_host_t host = MSG_host_by_name(name);
546   if (!host) {
547     XBT_ERROR("no host '%s' found",name);
548     return -1;
549   }
550   xbt_dict_t props = MSG_host_get_properties(host);
551   xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
552
553   return 0;
554 }
555
556 /**
557  * \brief Registers the platform functions into the table simgrid.platf.
558  * \param L a lua state
559  */
560 void sglua_register_platf_functions(lua_State* L)
561 {
562   lua_getglobal(L, "simgrid");     /* simgrid */
563   luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
564   lua_setfield(L, -2, "platf");    /* simgrid */
565
566   lua_pop(L, 1);                   /* -- */
567 }
568