Logo AND Algorithmique Numérique Distribuée

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