Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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_private.hpp"
11 #include "src/surf/network_interface.hpp"
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_CATEGORY(lua_platf, "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   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_hostlink(&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 type;
162
163   // we get values from the table passed as argument
164   if (!lua_istable(L, -1)) {
165     XBT_ERROR("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) {
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   if (type == LUA_TNUMBER)
186     xbt_dynar_push_as(host.speed_peak, double, lua_tointeger(L, -1));
187   else // LUA_TSTRING
188     xbt_dynar_push_as(host.speed_peak, double, surf_parse_get_speed(lua_tostring(L, -1), "speed of host", host.id));
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_trace
203   lua_pushstring(L, "availability_file");
204   lua_gettable(L, -2);
205   host.speed_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
206   lua_pop(L, 1);
207
208   //get trace state
209   lua_pushstring(L, "state_file");
210   lua_gettable(L, -2);
211   host.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
212   lua_pop(L, 1);
213
214   sg_platf_new_host(&host);
215   xbt_dynar_free(&host.speed_peak);
216
217   return 0;
218 }
219
220 int  console_add_link(lua_State *L) {
221   s_sg_platf_link_cbarg_t link;
222   memset(&link,0,sizeof(link));
223
224   int type;
225   const char* policy;
226
227   if (! lua_istable(L, -1)) {
228     XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
229     return -1;
230   }
231
232   // get Id Value
233   lua_pushstring(L, "id");
234   type = lua_gettable(L, -2);
235   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
236     XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
237   }
238   link.id = lua_tostring(L, -1);
239   lua_pop(L, 1);
240
241   // get bandwidth value
242   lua_pushstring(L, "bandwidth");
243   type = lua_gettable(L, -2);
244   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
245     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.");
246   }
247   if (type == LUA_TNUMBER)
248     link.bandwidth = lua_tonumber(L, -1);
249   else // LUA_TSTRING
250     link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1),"bandwidth of link", link.id);
251   lua_pop(L, 1);
252
253   //get latency value
254   lua_pushstring(L, "lat");
255   type = lua_gettable(L, -2);
256   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
257     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.");
258   }
259   if (type == LUA_TNUMBER)
260     link.latency = lua_tonumber(L, -1);
261   else // LUA_TSTRING
262     link.latency = surf_parse_get_time(lua_tostring(L, -1),"latency of link", link.id);
263   lua_pop(L, 1);
264
265   /*Optional Arguments  */
266
267   //get bandwidth_trace value
268   lua_pushstring(L, "bandwidth_file");
269   lua_gettable(L, -2);
270   link.bandwidth_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
271   lua_pop(L, 1);
272
273   //get latency_trace value
274   lua_pushstring(L, "latency_file");
275   lua_gettable(L, -2);
276   link.latency_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
277   lua_pop(L, 1);
278
279   //get state_trace value
280   lua_pushstring(L, "state_file");
281   lua_gettable(L, -2);
282   link.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
283   lua_pop(L, 1);
284
285   //get policy value
286   lua_pushstring(L, "sharing_policy");
287   lua_gettable(L, -2);
288   policy = lua_tostring(L, -1);
289   lua_pop(L, 1);
290   if (policy && !strcmp(policy,"FULLDUPLEX")) {
291     link.policy = SURF_LINK_FULLDUPLEX;
292   } else if (policy && !strcmp(policy,"FATPIPE")) {
293     link.policy = SURF_LINK_FATPIPE;
294   } else {
295     link.policy = SURF_LINK_SHARED;
296   }
297
298   sg_platf_new_link(&link);
299
300   return 0;
301 }
302 /**
303  * add Router to AS components
304  */
305 int console_add_router(lua_State* L) {
306   s_sg_platf_router_cbarg_t router;
307   memset(&router,0,sizeof(router));
308   int type;
309
310   if (! lua_istable(L, -1)) {
311     XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
312     return -1;
313   }
314
315   lua_pushstring(L, "id");
316   type = lua_gettable(L, -2);
317   if (type != LUA_TSTRING) {
318     XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
319   }
320   router.id = lua_tostring(L, -1);
321   lua_pop(L,1);
322
323   lua_pushstring(L,"coord");
324   lua_gettable(L,-2);
325   router.coord = lua_tostring(L, -1);
326   lua_pop(L,1);
327
328   sg_platf_new_router(&router);
329
330   return 0;
331 }
332
333 int console_add_route(lua_State *L) {
334   XBT_DEBUG("Adding route");
335   s_sg_platf_route_cbarg_t route;
336   memset(&route,0,sizeof(route));
337   int type;
338
339   /* allocating memory for the buffer, I think 2kB should be enough */
340   surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
341
342   if (! lua_istable(L, -1)) {
343     XBT_ERROR("Bad Arguments to add a route. Should be a table with named arguments");
344     return -1;
345   }
346
347   lua_pushstring(L,"src");
348   type = lua_gettable(L,-2);
349   if (type != LUA_TSTRING) {
350     XBT_ERROR("Attribute 'src' must be specified for any route and must be a string.");
351   }
352   route.src = lua_tostring(L, -1);
353   lua_pop(L,1);
354
355   lua_pushstring(L,"dest");
356   type = lua_gettable(L,-2);
357   if (type != LUA_TSTRING) {
358     XBT_ERROR("Attribute 'dest' must be specified for any route and must be a string.");
359   }
360   route.dst = lua_tostring(L, -1);
361   lua_pop(L,1);
362
363   lua_pushstring(L,"links");
364   type = lua_gettable(L,-2);
365   if (type != LUA_TSTRING) {
366     XBT_ERROR("Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
367   }
368   route.link_list = new std::vector<Link*>();
369   xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
370   if (xbt_dynar_is_empty(names)) {
371     /* unique name */
372     route.link_list->push_back(Link::byName(lua_tostring(L, -1)));
373   } else {
374     // Several names separated by , \t\r\n
375     unsigned int cpt;
376     char *name;
377     xbt_dynar_foreach(names, cpt, name) {
378       if (strlen(name)>0) {
379         Link *link = Link::byName(name);
380         route.link_list->push_back(link);
381       }
382     }
383   }
384   lua_pop(L,1);
385
386   /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
387    * Et ouais mon pote. That's the way it goes. F34R.
388    *
389    * (Note that above this function, there is a #include statement. Is this
390    * comment related to that statement?)
391    */
392   lua_pushstring(L,"symmetrical");
393   lua_gettable(L,-2);
394   if (lua_isstring(L, -1)) {
395     const char* value = lua_tostring(L, -1);
396     if (strcmp("YES", value) == 0) {
397       route.symmetrical = TRUE;
398     }
399     else
400       route.symmetrical = FALSE;
401   }
402   else {
403     route.symmetrical = TRUE;
404   }
405   lua_pop(L,1);
406
407   route.gw_src = NULL;
408   route.gw_dst = NULL;
409
410   sg_platf_new_route(&route);
411
412   return 0;
413 }
414
415 int console_add_ASroute(lua_State *L) {
416   s_sg_platf_route_cbarg_t ASroute;
417   memset(&ASroute,0,sizeof(ASroute));
418
419   lua_pushstring(L, "src");
420   lua_gettable(L, -2);
421   ASroute.src = lua_tostring(L, -1);
422   lua_pop(L, 1);
423
424   lua_pushstring(L, "dst");
425   lua_gettable(L, -2);
426   ASroute.dst = lua_tostring(L, -1);
427   lua_pop(L, 1);
428
429   lua_pushstring(L, "gw_src");
430   lua_gettable(L, -2);
431   const char *name = lua_tostring(L, -1);
432   ASroute.gw_src = sg_netcard_by_name_or_null(name);
433   if (ASroute.gw_src == NULL) {
434     XBT_ERROR("Attribute 'gw_src' of AS route does not name a valid machine: %s", name);
435     return -1;
436   }
437   lua_pop(L, 1);
438
439   lua_pushstring(L, "gw_dst");
440   lua_gettable(L, -2);
441   name = lua_tostring(L, -1);
442   ASroute.gw_dst = sg_netcard_by_name_or_null(name);
443   if (ASroute.gw_dst == NULL) {
444     XBT_ERROR("Attribute 'gw_dst' of AS route does not name a valid machine: %s", name);
445     return -1;
446   }
447   lua_pop(L, 1);
448
449   lua_pushstring(L,"links");
450   lua_gettable(L,-2);
451   ASroute.link_list = new std::vector<Link*>();
452   xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
453   if (xbt_dynar_is_empty(names)) {
454     /* unique name */
455     ASroute.link_list->push_back(Link::byName(lua_tostring(L, -1)));
456   } else {
457     // Several names separated by , \t\r\n
458     unsigned int cpt;
459     char *name;
460     xbt_dynar_foreach(names, cpt, name) {
461       if (strlen(name)>0) {
462         Link *link = Link::byName(name);
463         ASroute.link_list->push_back(link);
464       }
465     }
466   }
467   lua_pop(L,1);
468
469   lua_pushstring(L,"symmetrical");
470   lua_gettable(L,-2);
471   if (lua_isstring(L, -1)) {
472     const char* value = lua_tostring(L, -1);
473     if (strcmp("YES", value) == 0)
474       ASroute.symmetrical = TRUE;
475     else
476       ASroute.symmetrical = FALSE;
477   }
478   else {
479     ASroute.symmetrical = TRUE;
480   }
481   lua_pop(L,1);
482
483   sg_platf_new_route(&ASroute);
484
485   return 0;
486 }
487
488 int console_AS_open(lua_State *L) {
489  const char *id;
490  const char *mode;
491  int type;
492
493  XBT_DEBUG("Opening AS");
494
495  if (! lua_istable(L, 1)) {
496    XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
497    return -1;
498  }
499
500  lua_pushstring(L, "id");
501  type = lua_gettable(L, -2);
502   if (type != LUA_TSTRING) {
503     XBT_ERROR("Attribute 'id' must be specified for any AS and must be a string.");
504   }
505  id = lua_tostring(L, -1);
506  lua_pop(L, 1);
507
508  lua_pushstring(L, "mode");
509  lua_gettable(L, -2);
510  mode = lua_tostring(L, -1);
511  lua_pop(L, 1);
512
513  int mode_int = A_surfxml_AS_routing_None;
514  if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
515  else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
516  else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
517  else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
518  else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
519  else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
520  else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
521  else xbt_die("Don't have the model name '%s'",mode);
522
523  s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
524  AS.id = id;
525  AS.routing = mode_int;
526
527  sg_platf_new_AS_begin(&AS);
528
529  return 0;
530 }
531 int console_AS_close(lua_State *L) {
532   XBT_DEBUG("Closing AS");
533   sg_platf_new_AS_end();
534   return 0;
535 }
536
537 int console_host_set_property(lua_State *L) {
538   const char* name ="";
539   const char* prop_id = "";
540   const char* prop_value = "";
541   if (!lua_istable(L, -1)) {
542     XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
543     return -1;
544   }
545
546
547   // get Host id
548   lua_pushstring(L, "host");
549   lua_gettable(L, -2);
550   name = lua_tostring(L, -1);
551   lua_pop(L, 1);
552
553   // get prop Name
554   lua_pushstring(L, "prop");
555   lua_gettable(L, -2);
556   prop_id = lua_tostring(L, -1);
557   lua_pop(L, 1);
558   //get args
559   lua_pushstring(L,"value");
560   lua_gettable(L, -2);
561   prop_value = lua_tostring(L,-1);
562   lua_pop(L, 1);
563
564   sg_host_t host = sg_host_by_name(name);
565   if (!host) {
566     XBT_ERROR("no host '%s' found",name);
567     return -1;
568   }
569   xbt_dict_t props = sg_host_get_properties(host);
570   xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
571
572   return 0;
573 }
574
575 /**
576  * \brief Registers the platform functions into the table simgrid.platf.
577  * \param L a lua state
578  */
579 void sglua_register_platf_functions(lua_State* L)
580 {
581   lua_getglobal(L, "simgrid");     /* simgrid */
582   luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
583   lua_setfield(L, -2, "platf");    /* simgrid */
584
585   lua_pop(L, 1);                   /* -- */
586 }
587
588 //void sglua_register_routing_constants(lua_State* L)
589 //{
590 //  lua_getglobal(L, "simgrid");     /* simgrid */
591 //  lua_newtable(L);                 /* simgrid simgrid.routing */
592 //
593 //  lua_pushstring(L, "Cluster");    /* simgrid simgrid.routing Cluster */
594 //  lua_setfield(L, -2, "CLUSTER");     /* simgrid simgrid.routing */
595 //
596 //  lua_pushstring(L, "Dijkstra");    /* simgrid simgrid.routing Dijkstra */
597 //  lua_setfield(L, -2, "DIJKSTRA");     /* simgrid simgrid.routing */
598 //
599 //  lua_pushstring(L, "DijkstraCache");    /* simgrid simgrid.routing DijkstraCache */
600 //  lua_setfield(L, -2, "DIJKSTRA_CACHE");     /* simgrid simgrid.routing */
601 //
602 //  lua_pushstring(L, "Floyd");    /* simgrid simgrid.routing Floyd */
603 //  lua_setfield(L, -2, "FLOYD");     /* simgrid simgrid.routing */
604 //
605 //  lua_pushstring(L, "Full");       /* simgrid simgrid.routing Full */
606 //  lua_setfield(L, -2, "FULL");     /* simgrid simgrid.routing */
607 //
608 //  lua_pushstring(L, "None");    /* simgrid simgrid.routing None */
609 //  lua_setfield(L, -2, "NONE");     /* simgrid simgrid.routing */
610 //
611 //  lua_pushstring(L, "Vivaldi");    /* simgrid simgrid.routing Vivaldi */
612 //  lua_setfield(L, -2, "FULL");     /* simgrid simgrid.routing */
613 //
614 //  lua_setfield(L, -2, "routing");  /* simgrid */
615 //
616 //  lua_pop(L, 1);                   /* -- */
617 //}