Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
471e322640da23b4ec50b0c25e3e70f2002a14ee
[simgrid.git] / src / bindings / lua / lua_platf.cpp
1 /* Copyright (c) 2010-2017. 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 "src/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 extern "C" {
16 #include <lauxlib.h>
17 }
18
19 #include "src/surf/surf_private.hpp"
20 #include <boost/algorithm/string/classification.hpp>
21 #include <boost/algorithm/string/split.hpp>
22 #include <simgrid/s4u/Host.hpp>
23 #include <string>
24 #include <vector>
25
26 XBT_LOG_NEW_DEFAULT_CATEGORY(lua_platf, "Lua bindings (platform module)");
27
28 #define PLATF_MODULE_NAME "simgrid.engine"
29 #define AS_FIELDNAME   "__simgrid_as"
30
31 /* ********************************************************************************* */
32 /*                               simgrid.platf API                                   */
33 /* ********************************************************************************* */
34
35 static const luaL_Reg platf_functions[] = {
36     {"open", console_open},
37     {"close", console_close},
38     {"AS_open", console_AS_open},
39     {"AS_seal", console_AS_seal},
40     {"backbone_new", console_add_backbone},
41     {"host_link_new", console_add_host___link},
42     {"host_new", console_add_host},
43     {"link_new", console_add_link},
44     {"router_new", console_add_router},
45     {"route_new", console_add_route},
46     {"ASroute_new", console_add_ASroute},
47     {nullptr, nullptr}
48 };
49
50 int console_open(lua_State *L) {
51   sg_platf_init();
52   sg_platf_begin();
53
54   storage_register_callbacks();
55
56   return 0;
57 }
58
59 int console_close(lua_State *L) {
60   sg_platf_end();
61   sg_platf_exit();
62   return 0;
63 }
64
65 int console_add_backbone(lua_State *L) {
66   LinkCreationArgs link;
67
68   link.properties = nullptr;
69
70   lua_ensure(lua_istable(L, -1),"Bad Arguments to create backbone in Lua. Should be a table with named arguments.");
71
72   lua_pushstring(L, "id");
73   int type = lua_gettable(L, -2);
74   lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for backbone and must be a string.");
75   link.id = lua_tostring(L, -1);
76   lua_pop(L, 1);
77
78   lua_pushstring(L, "bandwidth");
79   type = lua_gettable(L, -2);
80   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
81       "Attribute 'bandwidth' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
82   link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of backbone", link.id.c_str());
83   lua_pop(L, 1);
84
85   lua_pushstring(L, "lat");
86   type = lua_gettable(L, -2);
87   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
88       "Attribute 'lat' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
89   link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of backbone", link.id.c_str());
90   lua_pop(L, 1);
91
92   lua_pushstring(L, "sharing_policy");
93   lua_gettable(L, -2);
94   const char* policy = lua_tostring(L, -1);
95   lua_pop(L, 1);
96   if (policy && not strcmp(policy, "FULLDUPLEX")) {
97     link.policy = SURF_LINK_FULLDUPLEX;
98   } else if (policy && not strcmp(policy, "FATPIPE")) {
99     link.policy = SURF_LINK_FATPIPE;
100   } else {
101     link.policy = SURF_LINK_SHARED;
102   }
103
104   sg_platf_new_link(&link);
105   routing_cluster_add_backbone(simgrid::surf::LinkImpl::byName(link.id));
106
107   return 0;
108 }
109
110 int console_add_host___link(lua_State *L) {
111   HostLinkCreationArgs hostlink;
112   int type;
113
114   lua_ensure(lua_istable(L, -1), "Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
115
116   lua_pushstring(L, "id");
117   type = lua_gettable(L, -2);
118   lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any host_link and must be a string.");
119   hostlink.id = lua_tostring(L, -1);
120   lua_pop(L, 1);
121
122   lua_pushstring(L, "up");
123   type = lua_gettable(L, -2);
124   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
125       "Attribute 'up' must be specified for host_link and must either be a string or a number.");
126   hostlink.link_up = lua_tostring(L, -1);
127   lua_pop(L, 1);
128
129   lua_pushstring(L, "down");
130   type = lua_gettable(L, -2);
131   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
132       "Attribute 'down' must be specified for host_link and must either be a string or a number.");
133   hostlink.link_down = lua_tostring(L, -1);
134   lua_pop(L, 1);
135
136   XBT_DEBUG("Create a host_link for host %s", hostlink.id.c_str());
137   sg_platf_new_hostlink(&hostlink);
138
139   return 0;
140 }
141
142 int console_add_host(lua_State *L) {
143   s_sg_platf_host_cbarg_t host{};
144   int type;
145
146   // we get values from the table passed as argument
147   lua_ensure(lua_istable(L, -1),
148       "Bad Arguments to create host. Should be a table with named arguments");
149
150   // get Id Value
151   lua_pushstring(L, "id");
152   type = lua_gettable(L, -2);
153   lua_ensure(type == LUA_TSTRING,
154       "Attribute 'id' must be specified for any host and must be a string.");
155   host.id = lua_tostring(L, -1);
156   lua_pop(L, 1);
157
158   // get power value
159   lua_pushstring(L, "speed");
160   type = lua_gettable(L, -2);
161   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
162       "Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
163   if (type == LUA_TNUMBER)
164     host.speed_per_pstate.push_back(lua_tointeger(L, -1));
165   else // LUA_TSTRING
166     host.speed_per_pstate.push_back(surf_parse_get_speed(lua_tostring(L, -1), "speed of host", host.id));
167   lua_pop(L, 1);
168
169   // get core
170   lua_pushstring(L, "core");
171   lua_gettable(L, -2);
172   if (not lua_isnumber(L, -1))
173     host.core_amount = 1; // Default value
174   else
175     host.core_amount = lua_tonumber(L, -1);
176   if (host.core_amount == 0)
177     host.core_amount = 1;
178   lua_pop(L, 1);
179
180   //get power_trace
181   lua_pushstring(L, "availability_file");
182   lua_gettable(L, -2);
183   const char *filename = lua_tostring(L, -1);
184   if (filename)
185     host.speed_trace = tmgr_trace_new_from_file(filename);
186   lua_pop(L, 1);
187
188   //get trace state
189   lua_pushstring(L, "state_file");
190   lua_gettable(L, -2);
191   filename = lua_tostring(L, -1);
192     if (filename)
193       host.state_trace = tmgr_trace_new_from_file(filename);
194   lua_pop(L, 1);
195
196   sg_platf_new_host(&host);
197
198   return 0;
199 }
200
201 int  console_add_link(lua_State *L) {
202   LinkCreationArgs link;
203
204   const char* policy;
205
206   lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
207
208   // get Id Value
209   lua_pushstring(L, "id");
210   int type = lua_gettable(L, -2);
211   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
212       "Attribute 'id' must be specified for any link and must be a string.");
213   link.id = lua_tostring(L, -1);
214   lua_pop(L, 1);
215
216   // get bandwidth value
217   lua_pushstring(L, "bandwidth");
218   type = lua_gettable(L, -2);
219   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
220       "Attribute 'bandwidth' must be specified for any link and must either be either a string (in the right format; see docs) or a number.");
221   if (type == LUA_TNUMBER)
222     link.bandwidth = lua_tonumber(L, -1);
223   else // LUA_TSTRING
224     link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of link", link.id.c_str());
225   lua_pop(L, 1);
226
227   //get latency value
228   lua_pushstring(L, "lat");
229   type = lua_gettable(L, -2);
230   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
231       "Attribute 'lat' must be specified for any link and must either be a string (in the right format; see docs) or a number.");
232   if (type == LUA_TNUMBER)
233     link.latency = lua_tonumber(L, -1);
234   else // LUA_TSTRING
235     link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of link", link.id.c_str());
236   lua_pop(L, 1);
237
238   /*Optional Arguments  */
239
240   //get bandwidth_trace value
241   lua_pushstring(L, "bandwidth_file");
242   lua_gettable(L, -2);
243   const char *filename = lua_tostring(L, -1);
244   if (filename)
245     link.bandwidth_trace = tmgr_trace_new_from_file(filename);
246   lua_pop(L, 1);
247
248   //get latency_trace value
249   lua_pushstring(L, "latency_file");
250   lua_gettable(L, -2);
251   filename = lua_tostring(L, -1);
252   if (filename)
253     link.latency_trace = tmgr_trace_new_from_file(filename);
254   lua_pop(L, 1);
255
256   //get state_trace value
257   lua_pushstring(L, "state_file");
258   lua_gettable(L, -2);
259   filename = lua_tostring(L, -1);
260   if (filename)
261     link.state_trace = tmgr_trace_new_from_file(filename);
262   lua_pop(L, 1);
263
264   //get policy value
265   lua_pushstring(L, "sharing_policy");
266   lua_gettable(L, -2);
267   policy = lua_tostring(L, -1);
268   lua_pop(L, 1);
269   if (policy && not strcmp(policy, "FULLDUPLEX")) {
270     link.policy = SURF_LINK_FULLDUPLEX;
271   } else if (policy && not strcmp(policy, "FATPIPE")) {
272     link.policy = SURF_LINK_FATPIPE;
273   } else {
274     link.policy = SURF_LINK_SHARED;
275   }
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   s_sg_platf_route_cbarg_t 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   route.link_list   = new std::vector<simgrid::surf::LinkImpl*>();
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::surf::LinkImpl::byName(lua_tostring(L, -1)));
337   } else {
338     // Several names separated by , \t\r\n
339     for (auto const& name : names) {
340       if (name.length() > 0) {
341         simgrid::surf::LinkImpl* link = simgrid::surf::LinkImpl::byName(name);
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   delete route.link_list;
373
374   return 0;
375 }
376
377 int console_add_ASroute(lua_State *L) {
378   s_sg_platf_route_cbarg_t ASroute{};
379
380   lua_pushstring(L, "src");
381   lua_gettable(L, -2);
382   const char *srcName = lua_tostring(L, -1);
383   ASroute.src         = sg_netpoint_by_name_or_null(srcName);
384   lua_ensure(ASroute.src != nullptr, "Attribute 'src=%s' of AS route does not name a node.", srcName);
385   lua_pop(L, 1);
386
387   lua_pushstring(L, "dst");
388   lua_gettable(L, -2);
389   const char *dstName = lua_tostring(L, -1);
390   ASroute.dst         = sg_netpoint_by_name_or_null(dstName);
391   lua_ensure(ASroute.dst != nullptr, "Attribute 'dst=%s' of AS route does not name a node.", dstName);
392   lua_pop(L, 1);
393
394   lua_pushstring(L, "gw_src");
395   lua_gettable(L, -2);
396   const char *name = lua_tostring(L, -1);
397   ASroute.gw_src   = sg_netpoint_by_name_or_null(name);
398   lua_ensure(ASroute.gw_src, "Attribute 'gw_src=%s' of AS route does not name a valid node", name);
399   lua_pop(L, 1);
400
401   lua_pushstring(L, "gw_dst");
402   lua_gettable(L, -2);
403   name = lua_tostring(L, -1);
404   ASroute.gw_dst = sg_netpoint_by_name_or_null(name);
405   lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst=%s' of AS route does not name a valid node", name);
406   lua_pop(L, 1);
407
408   lua_pushstring(L,"links");
409   lua_gettable(L,-2);
410   ASroute.link_list = new std::vector<simgrid::surf::LinkImpl*>();
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::surf::LinkImpl::byName(lua_tostring(L, -1)));
417   } else {
418     // Several names separated by , \t\r\n
419     for (auto const& name : names) {
420       if (name.length() > 0) {
421         simgrid::surf::LinkImpl* link = simgrid::surf::LinkImpl::byName(name);
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   delete ASroute.link_list;
444
445   return 0;
446 }
447
448 int console_AS_open(lua_State *L) {
449  XBT_DEBUG("Opening AS");
450
451  lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
452
453  lua_pushstring(L, "id");
454  int type = lua_gettable(L, -2);
455  lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
456  const char* id = lua_tostring(L, -1);
457  lua_pop(L, 1);
458
459  lua_pushstring(L, "mode");
460  lua_gettable(L, -2);
461  const char* mode = lua_tostring(L, -1);
462  lua_pop(L, 1);
463
464  int mode_int = A_surfxml_AS_routing_None;
465  if (not strcmp(mode, "Full"))
466    mode_int = A_surfxml_AS_routing_Full;
467  else if (not strcmp(mode, "Floyd"))
468    mode_int = A_surfxml_AS_routing_Floyd;
469  else if (not strcmp(mode, "Dijkstra"))
470    mode_int = A_surfxml_AS_routing_Dijkstra;
471  else if (not strcmp(mode, "DijkstraCache"))
472    mode_int = A_surfxml_AS_routing_DijkstraCache;
473  else if (not strcmp(mode, "Vivaldi"))
474    mode_int = A_surfxml_AS_routing_Vivaldi;
475  else if (not strcmp(mode, "Cluster"))
476    mode_int = A_surfxml_AS_routing_Cluster;
477  else if (not strcmp(mode, "none"))
478    mode_int = A_surfxml_AS_routing_None;
479  else xbt_die("Don't have the model name '%s'",mode);
480
481  ZoneCreationArgs AS;
482  AS.id = id;
483  AS.routing = mode_int;
484  simgrid::s4u::NetZone* new_as = sg_platf_new_Zone_begin(&AS);
485
486  /* Build a Lua representation of the new AS on the stack */
487  lua_newtable(L);
488  simgrid::s4u::NetZone** lua_as =
489      (simgrid::s4u::NetZone**)lua_newuserdata(L, sizeof(simgrid::s4u::NetZone*)); /* table userdatum */
490  *lua_as = new_as;
491  luaL_getmetatable(L, PLATF_MODULE_NAME); /* table userdatum metatable */
492  lua_setmetatable(L, -2);                 /* table userdatum */
493  lua_setfield(L, -2, AS_FIELDNAME);       /* table -- put the userdata as field of the table */
494
495  return 1;
496 }
497 int console_AS_seal(lua_State *L) {
498   XBT_DEBUG("Sealing AS");
499   sg_platf_new_Zone_seal();
500   return 0;
501 }
502
503 int console_host_set_property(lua_State *L) {
504   lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
505
506   // get Host id
507   lua_pushstring(L, "host");
508   lua_gettable(L, -2);
509   const char* name = lua_tostring(L, -1);
510   lua_pop(L, 1);
511
512   // get prop Name
513   lua_pushstring(L, "prop");
514   lua_gettable(L, -2);
515   const char* prop_id = lua_tostring(L, -1);
516   lua_pop(L, 1);
517   //get args
518   lua_pushstring(L,"value");
519   lua_gettable(L, -2);
520   const char* prop_value = lua_tostring(L, -1);
521   lua_pop(L, 1);
522
523   sg_host_t host = sg_host_by_name(name);
524   lua_ensure(host, "no host '%s' found",name);
525   host->setProperty(prop_id, prop_value);
526
527   return 0;
528 }
529
530 /**
531  * \brief Registers the platform functions into the table simgrid.platf.
532  * \param L a lua state
533  */
534 void sglua_register_platf_functions(lua_State* L)
535 {
536   lua_getglobal(L, "simgrid");     /* simgrid */
537   luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
538   lua_setfield(L, -2, "engine");   /* simgrid */
539
540   lua_pop(L, 1);                   /* -- */
541 }