Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
90a2afa876561afad5effb72786d52bdfc3b1ce3
[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 #include <lauxlib.h>
16
17 #include "src/surf/surf_private.hpp"
18 #include <boost/algorithm/string/classification.hpp>
19 #include <boost/algorithm/string/split.hpp>
20 #include <simgrid/s4u/Host.hpp>
21 #include <string>
22 #include <vector>
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   simgrid::kernel::routing::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   lua_gettable(L, -2);
92   const char* policy = lua_tostring(L, -1);
93   lua_pop(L, 1);
94   if (policy && not strcmp(policy, "FULLDUPLEX")) {
95     XBT_WARN("Please update your platform to use SPLITDUPLEX instead of FULLDUPLEX");
96     link.policy = SURF_LINK_SPLITDUPLEX;
97   } else if (policy && not strcmp(policy, "SPLITDUPLEX")) {
98     link.policy = SURF_LINK_SPLITDUPLEX;
99   } else if (policy && not strcmp(policy, "FATPIPE")) {
100     link.policy = SURF_LINK_FATPIPE;
101   } else {
102     link.policy = SURF_LINK_SHARED;
103   }
104
105   sg_platf_new_link(&link);
106   routing_cluster_add_backbone(simgrid::surf::LinkImpl::byName(link.id));
107
108   return 0;
109 }
110
111 int console_add_host___link(lua_State *L) {
112   simgrid::kernel::routing::HostLinkCreationArgs hostlink;
113   int type;
114
115   lua_ensure(lua_istable(L, -1), "Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
116
117   lua_pushstring(L, "id");
118   type = lua_gettable(L, -2);
119   lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any host_link and must be a string.");
120   hostlink.id = lua_tostring(L, -1);
121   lua_pop(L, 1);
122
123   lua_pushstring(L, "up");
124   type = lua_gettable(L, -2);
125   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
126       "Attribute 'up' must be specified for host_link and must either be a string or a number.");
127   hostlink.link_up = lua_tostring(L, -1);
128   lua_pop(L, 1);
129
130   lua_pushstring(L, "down");
131   type = lua_gettable(L, -2);
132   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
133       "Attribute 'down' must be specified for host_link and must either be a string or a number.");
134   hostlink.link_down = lua_tostring(L, -1);
135   lua_pop(L, 1);
136
137   XBT_DEBUG("Create a host_link for host %s", hostlink.id.c_str());
138   sg_platf_new_hostlink(&hostlink);
139
140   return 0;
141 }
142
143 int console_add_host(lua_State *L) {
144   simgrid::kernel::routing::HostCreationArgs host;
145   int type;
146
147   // we get values from the table passed as argument
148   lua_ensure(lua_istable(L, -1),
149       "Bad Arguments to create host. Should be a table with named arguments");
150
151   // get Id Value
152   lua_pushstring(L, "id");
153   type = lua_gettable(L, -2);
154   lua_ensure(type == LUA_TSTRING,
155       "Attribute 'id' must be specified for any host and must be a string.");
156   host.id = lua_tostring(L, -1);
157   lua_pop(L, 1);
158
159   // get power value
160   lua_pushstring(L, "speed");
161   type = lua_gettable(L, -2);
162   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
163       "Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
164   if (type == LUA_TNUMBER)
165     host.speed_per_pstate.push_back(lua_tointeger(L, -1));
166   else // LUA_TSTRING
167     host.speed_per_pstate.push_back(surf_parse_get_speed(lua_tostring(L, -1), "speed of host", host.id));
168   lua_pop(L, 1);
169
170   // get core
171   lua_pushstring(L, "core");
172   lua_gettable(L, -2);
173   if (not lua_isnumber(L, -1))
174     host.core_amount = 1; // Default value
175   else
176     host.core_amount = lua_tonumber(L, -1);
177   if (host.core_amount == 0)
178     host.core_amount = 1;
179   lua_pop(L, 1);
180
181   //get power_trace
182   lua_pushstring(L, "availability_file");
183   lua_gettable(L, -2);
184   const char *filename = lua_tostring(L, -1);
185   if (filename)
186     host.speed_trace = tmgr_trace_new_from_file(filename);
187   lua_pop(L, 1);
188
189   //get trace state
190   lua_pushstring(L, "state_file");
191   lua_gettable(L, -2);
192   filename = lua_tostring(L, -1);
193     if (filename)
194       host.state_trace = tmgr_trace_new_from_file(filename);
195   lua_pop(L, 1);
196
197   sg_platf_new_host(&host);
198
199   return 0;
200 }
201
202 int  console_add_link(lua_State *L) {
203   simgrid::kernel::routing::LinkCreationArgs link;
204
205   const char* policy;
206
207   lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
208
209   // get Id Value
210   lua_pushstring(L, "id");
211   int type = lua_gettable(L, -2);
212   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
213       "Attribute 'id' must be specified for any link and must be a string.");
214   link.id = lua_tostring(L, -1);
215   lua_pop(L, 1);
216
217   // get bandwidth value
218   lua_pushstring(L, "bandwidth");
219   type = lua_gettable(L, -2);
220   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
221       "Attribute 'bandwidth' must be specified for any link and must either be either a string (in the right format; see docs) or a number.");
222   if (type == LUA_TNUMBER)
223     link.bandwidth = lua_tonumber(L, -1);
224   else // LUA_TSTRING
225     link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of link", link.id.c_str());
226   lua_pop(L, 1);
227
228   //get latency value
229   lua_pushstring(L, "lat");
230   type = lua_gettable(L, -2);
231   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
232       "Attribute 'lat' must be specified for any link and must either be a string (in the right format; see docs) or a number.");
233   if (type == LUA_TNUMBER)
234     link.latency = lua_tonumber(L, -1);
235   else // LUA_TSTRING
236     link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of link", link.id.c_str());
237   lua_pop(L, 1);
238
239   /*Optional Arguments  */
240
241   //get bandwidth_trace value
242   lua_pushstring(L, "bandwidth_file");
243   lua_gettable(L, -2);
244   const char *filename = lua_tostring(L, -1);
245   if (filename)
246     link.bandwidth_trace = tmgr_trace_new_from_file(filename);
247   lua_pop(L, 1);
248
249   //get latency_trace value
250   lua_pushstring(L, "latency_file");
251   lua_gettable(L, -2);
252   filename = lua_tostring(L, -1);
253   if (filename)
254     link.latency_trace = tmgr_trace_new_from_file(filename);
255   lua_pop(L, 1);
256
257   //get state_trace value
258   lua_pushstring(L, "state_file");
259   lua_gettable(L, -2);
260   filename = lua_tostring(L, -1);
261   if (filename)
262     link.state_trace = tmgr_trace_new_from_file(filename);
263   lua_pop(L, 1);
264
265   //get policy value
266   lua_pushstring(L, "sharing_policy");
267   lua_gettable(L, -2);
268   policy = lua_tostring(L, -1);
269   lua_pop(L, 1);
270   if (policy && not strcmp(policy, "FULLDUPLEX")) {
271     XBT_WARN("Please update your platform to use SPLITDUPLEX instead of FULLDUPLEX");
272     link.policy = SURF_LINK_SPLITDUPLEX;
273   } else if (policy && not strcmp(policy, "SPLITDUPLEX")) {
274     link.policy = SURF_LINK_SPLITDUPLEX;
275   } else if (policy && not strcmp(policy, "FATPIPE")) {
276     link.policy = SURF_LINK_FATPIPE;
277   } else {
278     link.policy = SURF_LINK_SHARED;
279   }
280
281   sg_platf_new_link(&link);
282
283   return 0;
284 }
285 /**
286  * add Router to AS components
287  */
288 int console_add_router(lua_State* L) {
289   lua_ensure(lua_istable(L, -1), "Bad Arguments to create router, Should be a table with named arguments");
290
291   lua_pushstring(L, "id");
292   int type = lua_gettable(L, -2);
293   lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any link and must be a string.");
294   const char* name = lua_tostring(L, -1);
295   lua_pop(L,1);
296
297   lua_pushstring(L,"coord");
298   lua_gettable(L,-2);
299   const char* coords = lua_tostring(L, -1);
300   lua_pop(L,1);
301
302   sg_platf_new_router(name, coords);
303
304   return 0;
305 }
306
307 int console_add_route(lua_State *L) {
308   XBT_DEBUG("Adding route");
309   simgrid::kernel::routing::RouteCreationArgs route;
310   int type;
311
312   lua_ensure(lua_istable(L, -1), "Bad Arguments to add a route. Should be a table with named arguments");
313
314   lua_pushstring(L,"src");
315   type = lua_gettable(L,-2);
316   lua_ensure(type == LUA_TSTRING, "Attribute 'src' must be specified for any route and must be a string.");
317   const char *srcName = lua_tostring(L, -1);
318   route.src           = sg_netpoint_by_name_or_null(srcName);
319   lua_ensure(route.src != nullptr, "Attribute 'src=%s' of route does not name a node.", srcName);
320   lua_pop(L,1);
321
322   lua_pushstring(L,"dest");
323   type = lua_gettable(L,-2);
324   lua_ensure(type == LUA_TSTRING, "Attribute 'dest' must be specified for any route and must be a string.");
325   const char *dstName = lua_tostring(L, -1);
326   route.dst           = sg_netpoint_by_name_or_null(dstName);
327   lua_ensure(route.dst != nullptr, "Attribute 'dst=%s' of route does not name a node.", dstName);
328   lua_pop(L,1);
329
330   lua_pushstring(L,"links");
331   type = lua_gettable(L,-2);
332   lua_ensure(type == LUA_TSTRING,
333       "Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
334   std::vector<std::string> names;
335   const char* str = lua_tostring(L, -1);
336   boost::split(names, str, boost::is_any_of(", \t\r\n"));
337   if (names.empty()) {
338     /* unique name */
339     route.link_list.push_back(simgrid::surf::LinkImpl::byName(lua_tostring(L, -1)));
340   } else {
341     // Several names separated by , \t\r\n
342     for (auto const& name : names) {
343       if (name.length() > 0) {
344         simgrid::surf::LinkImpl* link = simgrid::surf::LinkImpl::byName(name);
345         route.link_list.push_back(link);
346       }
347     }
348   }
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
376   return 0;
377 }
378
379 int console_add_ASroute(lua_State *L) {
380   simgrid::kernel::routing::RouteCreationArgs ASroute;
381
382   lua_pushstring(L, "src");
383   lua_gettable(L, -2);
384   const char *srcName = lua_tostring(L, -1);
385   ASroute.src         = sg_netpoint_by_name_or_null(srcName);
386   lua_ensure(ASroute.src != nullptr, "Attribute 'src=%s' of AS route does not name a node.", srcName);
387   lua_pop(L, 1);
388
389   lua_pushstring(L, "dst");
390   lua_gettable(L, -2);
391   const char *dstName = lua_tostring(L, -1);
392   ASroute.dst         = sg_netpoint_by_name_or_null(dstName);
393   lua_ensure(ASroute.dst != nullptr, "Attribute 'dst=%s' of AS route does not name a node.", dstName);
394   lua_pop(L, 1);
395
396   lua_pushstring(L, "gw_src");
397   lua_gettable(L, -2);
398   const char *name = lua_tostring(L, -1);
399   ASroute.gw_src   = sg_netpoint_by_name_or_null(name);
400   lua_ensure(ASroute.gw_src, "Attribute 'gw_src=%s' of AS route does not name a valid node", name);
401   lua_pop(L, 1);
402
403   lua_pushstring(L, "gw_dst");
404   lua_gettable(L, -2);
405   name = lua_tostring(L, -1);
406   ASroute.gw_dst = sg_netpoint_by_name_or_null(name);
407   lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst=%s' of AS route does not name a valid node", name);
408   lua_pop(L, 1);
409
410   lua_pushstring(L,"links");
411   lua_gettable(L,-2);
412   std::vector<std::string> names;
413   const char* str = lua_tostring(L, -1);
414   boost::split(names, str, boost::is_any_of(", \t\r\n"));
415   if (names.empty()) {
416     /* unique name with no comma */
417     ASroute.link_list.push_back(simgrid::surf::LinkImpl::byName(lua_tostring(L, -1)));
418   } else {
419     // Several names separated by , \t\r\n
420     for (auto const& name : names) {
421       if (name.length() > 0) {
422         simgrid::surf::LinkImpl* link = simgrid::surf::LinkImpl::byName(name);
423         ASroute.link_list.push_back(link);
424       }
425     }
426   }
427   lua_pop(L,1);
428
429   lua_pushstring(L,"symmetrical");
430   lua_gettable(L,-2);
431   if (lua_isstring(L, -1)) {
432     const char* value = lua_tostring(L, -1);
433     if (strcmp("YES", value) == 0)
434       ASroute.symmetrical = true;
435     else
436       ASroute.symmetrical = false;
437   }
438   else {
439     ASroute.symmetrical = true;
440   }
441   lua_pop(L,1);
442
443   sg_platf_new_route(&ASroute);
444
445   return 0;
446 }
447
448 int console_AS_open(lua_State *L) {
449  XBT_DEBUG("Opening AS");
450
451  lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
452
453  lua_pushstring(L, "id");
454  int type = lua_gettable(L, -2);
455  lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
456  const char* id = lua_tostring(L, -1);
457  lua_pop(L, 1);
458
459  lua_pushstring(L, "mode");
460  lua_gettable(L, -2);
461  const char* mode = lua_tostring(L, -1);
462  lua_pop(L, 1);
463
464  int mode_int = A_surfxml_AS_routing_None;
465  if (not strcmp(mode, "Full"))
466    mode_int = A_surfxml_AS_routing_Full;
467  else if (not strcmp(mode, "Floyd"))
468    mode_int = A_surfxml_AS_routing_Floyd;
469  else if (not strcmp(mode, "Dijkstra"))
470    mode_int = A_surfxml_AS_routing_Dijkstra;
471  else if (not strcmp(mode, "DijkstraCache"))
472    mode_int = A_surfxml_AS_routing_DijkstraCache;
473  else if (not strcmp(mode, "Vivaldi"))
474    mode_int = A_surfxml_AS_routing_Vivaldi;
475  else if (not strcmp(mode, "Cluster"))
476    mode_int = A_surfxml_AS_routing_Cluster;
477  else if (not strcmp(mode, "none"))
478    mode_int = A_surfxml_AS_routing_None;
479  else xbt_die("Don't have the model name '%s'",mode);
480
481  simgrid::kernel::routing::ZoneCreationArgs AS;
482  AS.id = id;
483  AS.routing = mode_int;
484  simgrid::s4u::NetZone* new_as = sg_platf_new_Zone_begin(&AS);
485
486  /* Build a Lua representation of the new AS on the stack */
487  lua_newtable(L);
488  simgrid::s4u::NetZone** lua_as =
489      (simgrid::s4u::NetZone**)lua_newuserdata(L, sizeof(simgrid::s4u::NetZone*)); /* table userdatum */
490  *lua_as = new_as;
491  luaL_getmetatable(L, PLATF_MODULE_NAME); /* table userdatum metatable */
492  lua_setmetatable(L, -2);                 /* table userdatum */
493  lua_setfield(L, -2, AS_FIELDNAME);       /* table -- put the userdata as field of the table */
494
495  return 1;
496 }
497 int console_AS_seal(lua_State *L) {
498   XBT_DEBUG("Sealing AS");
499   sg_platf_new_Zone_seal();
500   return 0;
501 }
502
503 int console_host_set_property(lua_State *L) {
504   lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
505
506   // get Host id
507   lua_pushstring(L, "host");
508   lua_gettable(L, -2);
509   const char* name = lua_tostring(L, -1);
510   lua_pop(L, 1);
511
512   // get prop Name
513   lua_pushstring(L, "prop");
514   lua_gettable(L, -2);
515   const char* prop_id = lua_tostring(L, -1);
516   lua_pop(L, 1);
517   //get args
518   lua_pushstring(L,"value");
519   lua_gettable(L, -2);
520   const char* prop_value = lua_tostring(L, -1);
521   lua_pop(L, 1);
522
523   sg_host_t host = sg_host_by_name(name);
524   lua_ensure(host, "no host '%s' found",name);
525   host->setProperty(prop_id, prop_value);
526
527   return 0;
528 }
529
530 /**
531  * \brief Registers the platform functions into the table simgrid.platf.
532  * \param L a lua state
533  */
534 void sglua_register_platf_functions(lua_State* L)
535 {
536   lua_getglobal(L, "simgrid");     /* simgrid */
537   luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
538   lua_setfield(L, -2, "engine");   /* simgrid */
539
540   lua_pop(L, 1);                   /* -- */
541 }