Logo AND Algorithmique Numérique Distribuée

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