Logo AND Algorithmique Numérique Distribuée

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