Logo AND Algorithmique Numérique Distribuée

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