Logo AND Algorithmique Numérique Distribuée

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