Logo AND Algorithmique Numérique Distribuée

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