Logo AND Algorithmique Numérique Distribuée

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