Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[Lua] Renamed host 'power' to 'speed' for hosts
[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 "src/msg/msg_private.h"
18 #include "src/simix/smx_host_private.h"
19 #include "src/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   return 0;
52 }
53
54 int console_close(lua_State *L) {
55   sg_platf_end();
56   sg_platf_exit();
57   return 0;
58 }
59
60 int console_add_backbone(lua_State *L) {
61   s_sg_platf_link_cbarg_t link;
62   memset(&link,0,sizeof(link));
63   int type;
64
65   link.properties = NULL;
66
67   if (!lua_istable(L, -1)) {
68     XBT_ERROR
69         ("Bad Arguments to create backbone in Lua. Should be a table with named arguments.");
70     return -1;
71   }
72
73   lua_pushstring(L, "id");
74   type = lua_gettable(L, -2);
75   if (type != LUA_TSTRING) {
76     XBT_ERROR("Attribute 'id' must be specified for backbone and must be a string.");
77   }
78   link.id = lua_tostring(L, -1);
79   lua_pop(L, 1);
80
81   lua_pushstring(L, "bandwidth");
82   type = lua_gettable(L, -2);
83   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
84     XBT_ERROR("Attribute 'bandwidth' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
85   }
86   link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1));
87   lua_pop(L, 1);
88
89   lua_pushstring(L, "lat");
90   type = lua_gettable(L, -2);
91   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
92     XBT_ERROR("Attribute 'lat' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
93   }
94   link.latency = surf_parse_get_time(lua_tostring(L, -1));
95   lua_pop(L, 1);
96
97   link.initiallyOn = 1;
98
99   lua_pushstring(L, "sharing_policy");
100   type = lua_gettable(L, -2);
101   const char* policy = lua_tostring(L, -1);
102   if (policy && !strcmp(policy,"FULLDUPLEX")) {
103     link.policy = SURF_LINK_FULLDUPLEX;
104   } else if (policy && !strcmp(policy,"FATPIPE")) {
105     link.policy = SURF_LINK_FATPIPE;
106   } else {
107     link.policy = SURF_LINK_SHARED;
108   }
109
110   sg_platf_new_link(&link);
111   routing_cluster_add_backbone(sg_link_by_name(link.id));
112
113   return 0;
114 }
115
116 int console_add_host___link(lua_State *L) {
117   s_sg_platf_host_link_cbarg_t netcard;
118   memset(&netcard,0,sizeof(netcard));
119   int type;
120
121   // we get values from the table passed as argument
122   if (!lua_istable(L, -1)) {
123     XBT_ERROR
124         ("Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
125     return -1;
126   }
127
128   lua_pushstring(L, "id");
129   type = lua_gettable(L, -2);
130   if (type != LUA_TSTRING) {
131     XBT_ERROR("Attribute 'id' must be specified for any host_link and must be a string.");
132   }
133   netcard.id = lua_tostring(L, -1);
134   lua_pop(L, 1);
135
136   lua_pushstring(L, "up");
137   type = lua_gettable(L, -2);
138   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
139     XBT_ERROR("Attribute 'up' must be specified for host_link and must either be a string or a number.");
140   }
141   netcard.link_up = lua_tostring(L, -1);
142   lua_pop(L, 1);
143
144   lua_pushstring(L, "down");
145   type = lua_gettable(L, -2);
146   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
147     XBT_ERROR("Attribute 'down' must be specified for host_link and must either be a string or a number.");
148   }
149   netcard.link_down = lua_tostring(L, -1);
150   lua_pop(L, 1);
151
152   XBT_DEBUG("Create a host_link for host %s", netcard.id);
153   sg_platf_new_netcard(&netcard);
154
155   return 0;
156 }
157
158 int console_add_host(lua_State *L) {
159   s_sg_platf_host_cbarg_t host;
160   memset(&host,0,sizeof(host));
161   int state, type;
162
163   // we get values from the table passed as argument
164   if (!lua_istable(L, -1)) {
165     XBT_ERROR
166         ("Bad Arguments to create host. Should be a table with named arguments");
167     return -1;
168   }
169
170   // get Id Value
171   lua_pushstring(L, "id");
172   type = lua_gettable(L, -2);
173   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
174     XBT_ERROR("Attribute 'id' must be specified for any host and must be a string.");
175   }
176   host.id = lua_tostring(L, -1);
177   lua_pop(L, 1);
178
179   // get power value
180   lua_pushstring(L, "speed");
181   type = lua_gettable(L, -2);
182   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
183     XBT_ERROR("Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
184   }
185   host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
186   xbt_dynar_push_as(host.speed_peak, double, get_cpu_speed(lua_tostring(L, -1)));
187   lua_pop(L, 1);
188
189   // get core
190   lua_pushstring(L, "core");
191   lua_gettable(L, -2);
192   if(!lua_isnumber(L,-1)) {
193       host.core_amount = 1;// Default value
194   }
195   else host.core_amount = lua_tonumber(L, -1);
196   if (host.core_amount == 0)
197     host.core_amount = 1;
198   lua_pop(L, 1);
199
200   //get power_scale
201   lua_pushstring(L, "availability");
202   lua_gettable(L, -2);
203   if(!lua_isnumber(L,-1)) host.speed_scale = 1;// Default value
204   else host.speed_scale = lua_tonumber(L, -1);
205   lua_pop(L, 1);
206
207   //get power_trace
208   lua_pushstring(L, "availability_file");
209   lua_gettable(L, -2);
210   host.speed_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
211   lua_pop(L, 1);
212
213   //get state initial
214   lua_pushstring(L, "state");
215   lua_gettable(L, -2);
216   if(!lua_isnumber(L,-1)) state = 1;// Default value
217   else state = lua_tonumber(L, -1);
218   lua_pop(L, 1);
219
220   if (state)
221     host.initiallyOn = 1;
222   else
223     host.initiallyOn = 0;
224
225   //get trace state
226   lua_pushstring(L, "state_file");
227   lua_gettable(L, -2);
228   host.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
229   lua_pop(L, 1);
230
231   sg_platf_new_host(&host);
232   xbt_dynar_free(&host.speed_peak);
233
234   return 0;
235 }
236
237 int  console_add_link(lua_State *L) {
238   s_sg_platf_link_cbarg_t link;
239   memset(&link,0,sizeof(link));
240
241   int type;
242   const char* policy;
243
244   if (! lua_istable(L, -1)) {
245     XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
246     return -1;
247   }
248
249   // get Id Value
250   lua_pushstring(L, "id");
251   type = lua_gettable(L, -2);
252   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
253     XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
254   }
255   link.id = lua_tostring(L, -1);
256   lua_pop(L, 1);
257
258   // get bandwidth value
259   lua_pushstring(L, "bandwidth");
260   type = lua_gettable(L, -2);
261   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
262     XBT_ERROR("Attribute 'bandwidth' must be specified for any link and must either be either a string (in the right format; see docs) or a number.");
263   }
264   link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1));
265   lua_pop(L, 1);
266
267   //get latency value
268   lua_pushstring(L, "lat");
269   type = lua_gettable(L, -2);
270   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
271     XBT_ERROR("Attribute 'lat' must be specified for any link and must either be a string (in the right format; see docs) or a number.");
272   }
273   link.latency = surf_parse_get_time(lua_tostring(L, -1));
274   lua_pop(L, 1);
275
276   /*Optional Arguments  */
277
278   //get bandwidth_trace value
279   lua_pushstring(L, "bandwidth_file");
280   lua_gettable(L, -2);
281   link.bandwidth_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
282   lua_pop(L, 1);
283
284   //get latency_trace value
285   lua_pushstring(L, "latency_file");
286   lua_gettable(L, -2);
287   link.latency_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
288   lua_pop(L, 1);
289
290   //get state_trace value
291   lua_pushstring(L, "state_file");
292   lua_gettable(L, -2);
293   link.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
294   lua_pop(L, 1);
295
296   //get state_initial value
297   lua_pushstring(L, "state");
298   lua_gettable(L, -2);
299   if (!lua_isnumber(L,-1) || lua_tonumber(L, -1))
300     link.initiallyOn = 1;
301   else
302     link.initiallyOn = 0;
303   lua_pop(L, 1);
304
305   //get policy value
306   lua_pushstring(L, "sharing_policy");
307   lua_gettable(L, -2);
308   policy = lua_tostring(L, -1);
309   lua_pop(L, 1);
310   if (policy && !strcmp(policy,"FULLDUPLEX")) {
311     link.policy = SURF_LINK_FULLDUPLEX;
312   } else if (policy && !strcmp(policy,"FATPIPE")) {
313     link.policy = SURF_LINK_FATPIPE;
314   } else {
315     link.policy = SURF_LINK_SHARED;
316   }
317
318   sg_platf_new_link(&link);
319
320   return 0;
321 }
322 /**
323  * add Router to AS components
324  */
325 int console_add_router(lua_State* L) {
326   s_sg_platf_router_cbarg_t router;
327   memset(&router,0,sizeof(router));
328   int type;
329
330   if (! lua_istable(L, -1)) {
331     XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
332     return -1;
333   }
334
335   lua_pushstring(L, "id");
336   type = lua_gettable(L, -2);
337   if (type != LUA_TSTRING) {
338     XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
339   }
340   router.id = lua_tostring(L, -1);
341   lua_pop(L,1);
342
343   lua_pushstring(L,"coord");
344   lua_gettable(L,-2);
345   router.coord = lua_tostring(L, -1);
346   lua_pop(L,1);
347
348   sg_platf_new_router(&router);
349
350   return 0;
351 }
352
353 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
354
355 int console_add_route(lua_State *L) {
356   XBT_DEBUG("Adding route");
357   s_sg_platf_route_cbarg_t route;
358   memset(&route,0,sizeof(route));
359   int type;
360
361   /* allocating memory for the buffer, I think 2kB should be enough */
362   surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
363
364   if (! lua_istable(L, -1)) {
365     XBT_ERROR("Bad Arguments to create a route. Should be a table with named arguments");
366     return -1;
367   }
368
369   lua_pushstring(L,"src");
370   type = lua_gettable(L,-2);
371   if (type != LUA_TSTRING) {
372     XBT_ERROR("Attribute 'src' must be specified for any route and must be a string.");
373   }
374   route.src = lua_tostring(L, -1);
375   lua_pop(L,1);
376
377   lua_pushstring(L,"dest");
378   type = lua_gettable(L,-2);
379   if (type != LUA_TSTRING) {
380     XBT_ERROR("Attribute 'dest' must be specified for any route and must be a string.");
381   }
382   route.dst = lua_tostring(L, -1);
383   lua_pop(L,1);
384
385   lua_pushstring(L,"links");
386   type = lua_gettable(L,-2);
387   if (type != LUA_TSTRING) {
388     XBT_ERROR("Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
389   }
390   route.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
391   if (xbt_dynar_is_empty(route.link_list))
392     xbt_dynar_push_as(route.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
393   lua_pop(L,1);
394
395   /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
396    * Et ouais mon pote. That's the way it goes. F34R.
397    *
398    * (Note that above this function, there is a #include statement. Is this
399    * comment related to that statement?)
400    */
401   lua_pushstring(L,"symmetrical");
402   lua_gettable(L,-2);
403   if (lua_isstring(L, -1)) {
404     const char* value = lua_tostring(L, -1);
405     if (strcmp("YES", value) == 0) {
406       route.symmetrical = TRUE;
407     }
408     else
409       route.symmetrical = FALSE;
410   }
411   else {
412     route.symmetrical = TRUE;
413   }
414   lua_pop(L,1);
415
416   route.gw_src = NULL;
417   route.gw_dst = NULL;
418
419   sg_platf_new_route(&route);
420
421   return 0;
422 }
423
424 int console_add_ASroute(lua_State *L) {
425   s_sg_platf_route_cbarg_t ASroute;
426   memset(&ASroute,0,sizeof(ASroute));
427
428   lua_pushstring(L, "src");
429   lua_gettable(L, -2);
430   ASroute.src = lua_tostring(L, -1);
431   lua_pop(L, 1);
432
433   lua_pushstring(L, "dst");
434   lua_gettable(L, -2);
435   ASroute.dst = lua_tostring(L, -1);
436   lua_pop(L, 1);
437
438   lua_pushstring(L, "gw_src");
439   lua_gettable(L, -2);
440   ASroute.gw_src = sg_netcard_by_name_or_null(lua_tostring(L, -1));
441   lua_pop(L, 1);
442
443   lua_pushstring(L, "gw_dst");
444   lua_gettable(L, -2);
445   ASroute.gw_dst = sg_netcard_by_name_or_null(lua_tostring(L, -1));
446   lua_pop(L, 1);
447
448   /*if (A_surfxml_ASroute_gw___src && !ASroute.gw_src)*/
449     /*surf_parse_error("gw_src=\"%s\" not found for ASroute from \"%s\" to \"%s\"",*/
450                      /*A_surfxml_ASroute_gw___src, ASroute.src, ASroute.dst);*/
451   /*if (A_surfxml_ASroute_gw___dst && !ASroute.gw_dst)*/
452     /*surf_parse_error("gw_dst=\"%s\" not found for ASroute from \"%s\" to \"%s\"",*/
453                      /*A_surfxml_ASroute_gw___dst, ASroute.src, ASroute.dst);*/
454
455   lua_pushstring(L,"links");
456   lua_gettable(L,-2);
457   ASroute.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
458   if (xbt_dynar_is_empty(ASroute.link_list))
459     xbt_dynar_push_as(ASroute.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
460   lua_pop(L,1);
461
462   lua_pushstring(L,"symmetrical");
463   lua_gettable(L,-2);
464   if (lua_isstring(L, -1)) {
465     const char* value = lua_tostring(L, -1);
466     if (strcmp("YES", value) == 0) {
467       ASroute.symmetrical = TRUE;
468     }
469     else
470       ASroute.symmetrical = FALSE;
471   }
472   else {
473     ASroute.symmetrical = TRUE;
474   }
475   lua_pop(L,1);
476
477   sg_platf_new_ASroute(&ASroute);
478
479   return 0;
480 }
481
482 int console_AS_open(lua_State *L) {
483  const char *id;
484  const char *mode;
485  int type;
486
487  XBT_DEBUG("Opening AS");
488
489  if (! lua_istable(L, 1)) {
490    XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
491    return -1;
492  }
493
494  lua_pushstring(L, "id");
495  type = lua_gettable(L, -2);
496   if (type != LUA_TSTRING) {
497     XBT_ERROR("Attribute 'id' must be specified for any AS and must be a string.");
498   }
499  id = lua_tostring(L, -1);
500  lua_pop(L, 1);
501
502  lua_pushstring(L, "mode");
503  lua_gettable(L, -2);
504  mode = lua_tostring(L, -1);
505  lua_pop(L, 1);
506
507  int mode_int = A_surfxml_AS_routing_None;
508  if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
509  else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
510  else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
511  else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
512  else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
513  else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
514  else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
515  else xbt_die("Don't have the model name '%s'",mode);
516
517  s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
518  AS.id = id;
519  AS.routing = mode_int;
520
521  sg_platf_new_AS_begin(&AS);
522
523  return 0;
524 }
525 int console_AS_close(lua_State *L) {
526   XBT_DEBUG("Closing AS");
527   sg_platf_new_AS_end();
528   return 0;
529 }
530
531 int console_set_function(lua_State *L) {
532
533   const char *host_id ;
534   const char *function_id;
535   xbt_dynar_t args;
536
537   if (! lua_istable(L, 1)) {
538     XBT_ERROR("Bad Arguments to AS.new, Should be a table with named arguments");
539     return -1;
540   }
541
542   // get Host id
543   lua_pushstring(L, "host");
544   lua_gettable(L, -2);
545   host_id = lua_tostring(L, -1);
546   lua_pop(L, 1);
547
548   // get Function Name
549   lua_pushstring(L, "fct");
550   lua_gettable(L, -2);
551   function_id = lua_tostring(L, -1);
552   lua_pop(L, 1);
553
554   //get args
555   lua_pushstring(L,"args");
556   lua_gettable(L, -2);
557   args = xbt_str_split_str( lua_tostring(L,-1) , ",");
558   lua_pop(L, 1);
559
560   msg_host_t host = MSG_host_by_name(host_id);
561   if (!host) {
562     XBT_ERROR("no host '%s' found",host_id);
563     return -1;
564   }
565
566   // FIXME: use sg_platf_new_process directly (warning: find a way to check hostname)
567   MSG_set_function(host_id, function_id, args);
568
569   return 0;
570 }
571
572 int console_host_set_property(lua_State *L) {
573   const char* name ="";
574   const char* prop_id = "";
575   const char* prop_value = "";
576   if (!lua_istable(L, -1)) {
577     XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
578     return -1;
579   }
580
581
582   // get Host id
583   lua_pushstring(L, "host");
584   lua_gettable(L, -2);
585   name = lua_tostring(L, -1);
586   lua_pop(L, 1);
587
588   // get prop Name
589   lua_pushstring(L, "prop");
590   lua_gettable(L, -2);
591   prop_id = lua_tostring(L, -1);
592   lua_pop(L, 1);
593   //get args
594   lua_pushstring(L,"value");
595   lua_gettable(L, -2);
596   prop_value = lua_tostring(L,-1);
597   lua_pop(L, 1);
598
599   msg_host_t host = MSG_host_by_name(name);
600   if (!host) {
601     XBT_ERROR("no host '%s' found",name);
602     return -1;
603   }
604   xbt_dict_t props = MSG_host_get_properties(host);
605   xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
606
607   return 0;
608 }
609
610 /**
611  * \brief Registers the platform functions into the table simgrid.platf.
612  * \param L a lua state
613  */
614 void sglua_register_platf_functions(lua_State* L)
615 {
616   lua_getglobal(L, "simgrid");     /* simgrid */
617   luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
618   lua_setfield(L, -2, "platf");    /* simgrid */
619
620   lua_pop(L, 1);                   /* -- */
621 }
622