Logo AND Algorithmique Numérique Distribuée

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