Logo AND Algorithmique Numérique Distribuée

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