Logo AND Algorithmique Numérique Distribuée

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