Logo AND Algorithmique Numérique Distribuée

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