Logo AND Algorithmique Numérique Distribuée

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