Logo AND Algorithmique Numérique Distribuée

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