Logo AND Algorithmique Numérique Distribuée

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