Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1df2436ef54168258c982f4e08df4d8cb7a4e2cc
[simgrid.git] / src / bindings / lua / lua_platf.cpp
1 /* Copyright (c) 2010-2021. 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   auto link = std::make_unique<simgrid::kernel::routing::LinkCreationArgs>();
83   lua_Debug ar;
84   lua_getstack(L, 1, &ar);
85   lua_getinfo(L, "Sl", &ar);
86
87   lua_ensure(lua_istable(L, -1),"Bad Arguments to create backbone in Lua. Should be a table with named arguments.");
88
89   lua_pushstring(L, "id");
90   int type = lua_gettable(L, -2);
91   lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for backbone and must be a string.");
92   link->id = lua_tostring(L, -1);
93   lua_pop(L, 1);
94
95   lua_pushstring(L, "bandwidth");
96   type = lua_gettable(L, -2);
97   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
98       "Attribute 'bandwidth' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
99   link->bandwidths.push_back(
100       xbt_parse_get_bandwidth(ar.short_src, ar.currentline, lua_tostring(L, -1), "bandwidth of backbone " + link->id));
101   lua_pop(L, 1);
102
103   lua_pushstring(L, "lat");
104   type = lua_gettable(L, -2);
105   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
106       "Attribute 'lat' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
107   link->latency =
108       xbt_parse_get_time(ar.short_src, ar.currentline, lua_tostring(L, -1), "latency of backbone " + link->id);
109   lua_pop(L, 1);
110
111   lua_pushstring(L, "sharing_policy");
112   lua_gettable(L, -2);
113   const char* policy = lua_tostring(L, -1);
114   lua_pop(L, 1);
115   link->policy = link_policy_get_by_name(policy);
116
117   routing_cluster_add_backbone(std::move(link));
118
119   return 0;
120 }
121
122 int console_add_host___link(lua_State *L) {
123   simgrid::kernel::routing::HostLinkCreationArgs hostlink;
124   int type;
125
126   lua_ensure(lua_istable(L, -1), "Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
127
128   lua_pushstring(L, "id");
129   type = lua_gettable(L, -2);
130   lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any host_link and must be a string.");
131   hostlink.id = lua_tostring(L, -1);
132   lua_pop(L, 1);
133
134   lua_pushstring(L, "up");
135   type = lua_gettable(L, -2);
136   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
137       "Attribute 'up' must be specified for host_link and must either be a string or a number.");
138   hostlink.link_up = lua_tostring(L, -1);
139   lua_pop(L, 1);
140
141   lua_pushstring(L, "down");
142   type = lua_gettable(L, -2);
143   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
144       "Attribute 'down' must be specified for host_link and must either be a string or a number.");
145   hostlink.link_down = lua_tostring(L, -1);
146   lua_pop(L, 1);
147
148   XBT_DEBUG("Create a host_link for host %s", hostlink.id.c_str());
149   sg_platf_new_hostlink(&hostlink);
150
151   return 0;
152 }
153
154 int console_add_host(lua_State *L) {
155   simgrid::kernel::routing::HostCreationArgs host;
156   int type;
157   lua_Debug ar;
158   lua_getstack(L, 1, &ar);
159   lua_getinfo(L, "Sl", &ar);
160
161   // we get values from the table passed as argument
162   lua_ensure(lua_istable(L, -1),
163       "Bad Arguments to create host. Should be a table with named arguments");
164
165   // get Id Value
166   lua_pushstring(L, "id");
167   type = lua_gettable(L, -2);
168   lua_ensure(type == LUA_TSTRING,
169       "Attribute 'id' must be specified for any host and must be a string.");
170   host.id = lua_tostring(L, -1);
171   lua_pop(L, 1);
172
173   // get power value
174   lua_pushstring(L, "speed");
175   type = lua_gettable(L, -2);
176   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
177       "Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
178   if (type == LUA_TNUMBER)
179     host.speed_per_pstate.push_back(lua_tonumber(L, -1));
180   else // LUA_TSTRING
181     host.speed_per_pstate.push_back(
182         xbt_parse_get_speed(ar.short_src, ar.currentline, lua_tostring(L, -1), "speed of host " + host.id));
183   lua_pop(L, 1);
184
185   // get core
186   lua_pushstring(L, "core");
187   lua_gettable(L, -2);
188   if (not lua_isnumber(L, -1))
189     host.core_amount = 1; // Default value
190   else
191     host.core_amount = static_cast<int>(lua_tointeger(L, -1));
192   if (host.core_amount == 0)
193     host.core_amount = 1;
194   lua_pop(L, 1);
195
196   //get power_trace
197   lua_pushstring(L, "availability_file");
198   lua_gettable(L, -2);
199   const char *filename = lua_tostring(L, -1);
200   if (filename)
201     host.speed_trace = simgrid::kernel::profile::Profile::from_file(filename);
202   lua_pop(L, 1);
203
204   //get trace state
205   lua_pushstring(L, "state_file");
206   lua_gettable(L, -2);
207   filename = lua_tostring(L, -1);
208     if (filename)
209       host.state_trace = simgrid::kernel::profile::Profile::from_file(filename);
210   lua_pop(L, 1);
211
212   sg_platf_new_host_begin(&host);
213   sg_platf_new_host_seal(0);
214
215   return 0;
216 }
217
218 int  console_add_link(lua_State *L) {
219   simgrid::kernel::routing::LinkCreationArgs link;
220   lua_Debug ar;
221   lua_getstack(L, 1, &ar);
222   lua_getinfo(L, "Sl", &ar);
223
224   const char* policy;
225
226   lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
227
228   // get Id Value
229   lua_pushstring(L, "id");
230   int type = lua_gettable(L, -2);
231   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
232       "Attribute 'id' must be specified for any link and must be a string.");
233   link.id = lua_tostring(L, -1);
234   lua_pop(L, 1);
235
236   // get bandwidth value
237   lua_pushstring(L, "bandwidth");
238   type = lua_gettable(L, -2);
239   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
240       "Attribute 'bandwidth' must be specified for any link and must either be either a string (in the right format; see docs) or a number.");
241   if (type == LUA_TNUMBER)
242     link.bandwidths.push_back(lua_tonumber(L, -1));
243   else // LUA_TSTRING
244     link.bandwidths.push_back(
245         xbt_parse_get_bandwidth(ar.short_src, ar.currentline, lua_tostring(L, -1), "bandwidth of link " + link.id));
246   lua_pop(L, 1);
247
248   //get latency value
249   lua_pushstring(L, "lat");
250   type = lua_gettable(L, -2);
251   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
252       "Attribute 'lat' must be specified for any link and must either be a string (in the right format; see docs) or a number.");
253   if (type == LUA_TNUMBER)
254     link.latency = lua_tonumber(L, -1);
255   else // LUA_TSTRING
256     link.latency = xbt_parse_get_time(ar.short_src, ar.currentline, lua_tostring(L, -1), "latency of link " + link.id);
257   lua_pop(L, 1);
258
259   /*Optional Arguments  */
260
261   //get bandwidth_trace value
262   lua_pushstring(L, "bandwidth_file");
263   lua_gettable(L, -2);
264   const char *filename = lua_tostring(L, -1);
265   if (filename)
266     link.bandwidth_trace = simgrid::kernel::profile::Profile::from_file(filename);
267   lua_pop(L, 1);
268
269   //get latency_trace value
270   lua_pushstring(L, "latency_file");
271   lua_gettable(L, -2);
272   filename = lua_tostring(L, -1);
273   if (filename)
274     link.latency_trace = simgrid::kernel::profile::Profile::from_file(filename);
275   lua_pop(L, 1);
276
277   //get state_trace value
278   lua_pushstring(L, "state_file");
279   lua_gettable(L, -2);
280   filename = lua_tostring(L, -1);
281   if (filename)
282     link.state_trace = simgrid::kernel::profile::Profile::from_file(filename);
283   lua_pop(L, 1);
284
285   //get policy value
286   lua_pushstring(L, "sharing_policy");
287   lua_gettable(L, -2);
288   policy = lua_tostring(L, -1);
289   lua_pop(L, 1);
290   link.policy = link_policy_get_by_name(policy);
291
292   sg_platf_new_link(&link);
293
294   return 0;
295 }
296 /**
297  * add Router to AS components
298  */
299 int console_add_router(lua_State* L) {
300   lua_ensure(lua_istable(L, -1), "Bad Arguments to create router, Should be a table with named arguments");
301
302   lua_pushstring(L, "id");
303   int type = lua_gettable(L, -2);
304   lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any link and must be a string.");
305   const char* name = lua_tostring(L, -1);
306   lua_pop(L,1);
307
308   lua_pushstring(L,"coord");
309   lua_gettable(L,-2);
310   const char* coords = lua_tostring(L, -1);
311   lua_pop(L,1);
312
313   sg_platf_new_router(name, coords ? coords : "");
314
315   return 0;
316 }
317
318 int console_add_route(lua_State *L) {
319   XBT_DEBUG("Adding route");
320   simgrid::kernel::routing::RouteCreationArgs route;
321   int type;
322
323   lua_ensure(lua_istable(L, -1), "Bad Arguments to add a route. Should be a table with named arguments");
324
325   lua_pushstring(L,"src");
326   type = lua_gettable(L,-2);
327   lua_ensure(type == LUA_TSTRING, "Attribute 'src' must be specified for any route and must be a string.");
328   const char *srcName = lua_tostring(L, -1);
329   route.src           = sg_netpoint_by_name_or_null(srcName);
330   lua_ensure(route.src != nullptr, "Attribute 'src=%s' of route does not name a node.", srcName);
331   lua_pop(L,1);
332
333   lua_pushstring(L,"dest");
334   type = lua_gettable(L,-2);
335   lua_ensure(type == LUA_TSTRING, "Attribute 'dest' must be specified for any route and must be a string.");
336   const char *dstName = lua_tostring(L, -1);
337   route.dst           = sg_netpoint_by_name_or_null(dstName);
338   lua_ensure(route.dst != nullptr, "Attribute 'dst=%s' of route does not name a node.", dstName);
339   lua_pop(L,1);
340
341   lua_pushstring(L,"links");
342   type = lua_gettable(L,-2);
343   lua_ensure(type == LUA_TSTRING,
344       "Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
345   std::vector<std::string> names;
346   const char* str = lua_tostring(L, -1);
347   boost::split(names, str, boost::is_any_of(", \t\r\n"));
348   if (names.empty()) {
349     /* unique name */
350     route.link_list.push_back(simgrid::s4u::Link::by_name(lua_tostring(L, -1))->get_impl());
351   } else {
352     // Several names separated by , \t\r\n
353     for (auto const& name : names) {
354       if (name.length() > 0) {
355         simgrid::kernel::resource::LinkImpl* link = simgrid::s4u::Link::by_name(name)->get_impl();
356         route.link_list.push_back(link);
357       }
358     }
359   }
360   lua_pop(L,1);
361
362   /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
363    * Et ouais mon pote. That's the way it goes. F34R.
364    *
365    * (Note that above this function, there is a #include statement. Is this
366    * comment related to that statement?)
367    */
368   lua_pushstring(L,"symmetrical");
369   lua_gettable(L,-2);
370   route.symmetrical = (not lua_isstring(L, -1) || strcasecmp("YES", lua_tostring(L, -1)) == 0);
371   lua_pop(L,1);
372
373   sg_platf_new_route(&route);
374
375   return 0;
376 }
377
378 int console_add_ASroute(lua_State *L) {
379   simgrid::kernel::routing::RouteCreationArgs ASroute;
380
381   lua_pushstring(L, "src");
382   lua_gettable(L, -2);
383   const char *srcName = lua_tostring(L, -1);
384   ASroute.src         = sg_netpoint_by_name_or_null(srcName);
385   lua_ensure(ASroute.src != nullptr, "Attribute 'src=%s' of AS route does not name a node.", srcName);
386   lua_pop(L, 1);
387
388   lua_pushstring(L, "dst");
389   lua_gettable(L, -2);
390   const char *dstName = lua_tostring(L, -1);
391   ASroute.dst         = sg_netpoint_by_name_or_null(dstName);
392   lua_ensure(ASroute.dst != nullptr, "Attribute 'dst=%s' of AS route does not name a node.", dstName);
393   lua_pop(L, 1);
394
395   lua_pushstring(L, "gw_src");
396   lua_gettable(L, -2);
397   const char* pname = lua_tostring(L, -1);
398   ASroute.gw_src    = sg_netpoint_by_name_or_null(pname);
399   lua_ensure(ASroute.gw_src, "Attribute 'gw_src=%s' of AS route does not name a valid node", pname);
400   lua_pop(L, 1);
401
402   lua_pushstring(L, "gw_dst");
403   lua_gettable(L, -2);
404   pname          = lua_tostring(L, -1);
405   ASroute.gw_dst = sg_netpoint_by_name_or_null(pname);
406   lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst=%s' of AS route does not name a valid node", pname);
407   lua_pop(L, 1);
408
409   lua_pushstring(L,"links");
410   lua_gettable(L,-2);
411   std::vector<std::string> names;
412   const char* str = lua_tostring(L, -1);
413   boost::split(names, str, boost::is_any_of(", \t\r\n"));
414   if (names.empty()) {
415     /* unique name with no comma */
416     ASroute.link_list.push_back(simgrid::s4u::Link::by_name(lua_tostring(L, -1))->get_impl());
417   } else {
418     // Several names separated by , \t\r\n
419     for (auto const& name : names) {
420       if (name.length() > 0) {
421         simgrid::kernel::resource::LinkImpl* link = simgrid::s4u::Link::by_name(name)->get_impl();
422         ASroute.link_list.push_back(link);
423       }
424     }
425   }
426   lua_pop(L,1);
427
428   lua_pushstring(L,"symmetrical");
429   lua_gettable(L,-2);
430   ASroute.symmetrical = (not lua_isstring(L, -1) || strcasecmp("YES", lua_tostring(L, -1)) == 0);
431   lua_pop(L,1);
432
433   sg_platf_new_route(&ASroute);
434
435   return 0;
436 }
437
438 int console_AS_open(lua_State *L) {
439  XBT_DEBUG("Opening AS");
440
441  lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
442
443  lua_pushstring(L, "id");
444  int type = lua_gettable(L, -2);
445  lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
446  const char* id = lua_tostring(L, -1);
447  lua_pop(L, 1);
448
449  lua_pushstring(L, "mode");
450  lua_gettable(L, -2);
451  const char* mode = lua_tostring(L, -1);
452  lua_pop(L, 1);
453
454  simgrid::kernel::routing::ZoneCreationArgs AS;
455  AS.id = id;
456  AS.routing                                    = mode;
457  simgrid::kernel::routing::NetZoneImpl* new_as = sg_platf_new_Zone_begin(&AS);
458
459  /* Build a Lua representation of the new AS on the stack */
460  lua_newtable(L);
461  auto* lua_as = static_cast<simgrid::kernel::routing::NetZoneImpl**>(
462      lua_newuserdata(L, sizeof(simgrid::kernel::routing::NetZoneImpl*))); /* table userdatum */
463  *lua_as = new_as;
464  luaL_getmetatable(L, PLATF_MODULE_NAME); /* table userdatum metatable */
465  lua_setmetatable(L, -2);                 /* table userdatum */
466  lua_setfield(L, -2, AS_FIELDNAME);       /* table -- put the userdata as field of the table */
467
468  return 1;
469 }
470
471 int console_AS_seal(lua_State*)
472 {
473   XBT_DEBUG("Sealing AS");
474   sg_platf_new_Zone_seal();
475   return 0;
476 }
477
478 int console_host_set_property(lua_State *L) {
479   lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
480
481   // get Host id
482   lua_pushstring(L, "host");
483   lua_gettable(L, -2);
484   const char* name = lua_tostring(L, -1);
485   lua_pop(L, 1);
486
487   // get prop Name
488   lua_pushstring(L, "prop");
489   lua_gettable(L, -2);
490   const char* prop_id = lua_tostring(L, -1);
491   lua_pop(L, 1);
492   //get args
493   lua_pushstring(L,"value");
494   lua_gettable(L, -2);
495   const char* prop_value = lua_tostring(L, -1);
496   lua_pop(L, 1);
497
498   sg_host_t host = sg_host_by_name(name);
499   lua_ensure(host, "no host '%s' found",name);
500   host->set_property(prop_id, prop_value);
501
502   return 0;
503 }
504
505 /**
506  * @brief Registers the platform functions into the table simgrid.platf.
507  * @param L a lua state
508  */
509 void sglua_register_platf_functions(lua_State* L)
510 {
511   lua_getglobal(L, "simgrid");     /* simgrid */
512   luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
513   lua_setfield(L, -2, "engine");   /* simgrid */
514
515   lua_pop(L, 1);                   /* -- */
516 }