Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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   memset(&host,0,sizeof(host));
145   int type;
146
147   // we get values from the table passed as argument
148   lua_ensure(lua_istable(L, -1),
149       "Bad Arguments to create host. Should be a table with named arguments");
150
151   // get Id Value
152   lua_pushstring(L, "id");
153   type = lua_gettable(L, -2);
154   lua_ensure(type == LUA_TSTRING,
155       "Attribute 'id' must be specified for any host and must be a string.");
156   host.id = lua_tostring(L, -1);
157   lua_pop(L, 1);
158
159   // get power value
160   lua_pushstring(L, "speed");
161   type = lua_gettable(L, -2);
162   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
163       "Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
164   if (type == LUA_TNUMBER)
165     host.speed_per_pstate.push_back(lua_tointeger(L, -1));
166   else // LUA_TSTRING
167     host.speed_per_pstate.push_back(surf_parse_get_speed(lua_tostring(L, -1), "speed of host", host.id));
168   lua_pop(L, 1);
169
170   // get core
171   lua_pushstring(L, "core");
172   lua_gettable(L, -2);
173   if (not lua_isnumber(L, -1))
174     host.core_amount = 1; // Default value
175   else
176     host.core_amount = lua_tonumber(L, -1);
177   if (host.core_amount == 0)
178     host.core_amount = 1;
179   lua_pop(L, 1);
180
181   //get power_trace
182   lua_pushstring(L, "availability_file");
183   lua_gettable(L, -2);
184   const char *filename = lua_tostring(L, -1);
185   if (filename)
186     host.speed_trace = tmgr_trace_new_from_file(filename);
187   lua_pop(L, 1);
188
189   //get trace state
190   lua_pushstring(L, "state_file");
191   lua_gettable(L, -2);
192   filename = lua_tostring(L, -1);
193     if (filename)
194       host.state_trace = tmgr_trace_new_from_file(filename);
195   lua_pop(L, 1);
196
197   sg_platf_new_host(&host);
198
199   return 0;
200 }
201
202 int  console_add_link(lua_State *L) {
203   LinkCreationArgs link;
204
205   const char* policy;
206
207   lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
208
209   // get Id Value
210   lua_pushstring(L, "id");
211   int type = lua_gettable(L, -2);
212   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
213       "Attribute 'id' must be specified for any link and must be a string.");
214   link.id = lua_tostring(L, -1);
215   lua_pop(L, 1);
216
217   // get bandwidth value
218   lua_pushstring(L, "bandwidth");
219   type = lua_gettable(L, -2);
220   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
221       "Attribute 'bandwidth' must be specified for any link and must either be either a string (in the right format; see docs) or a number.");
222   if (type == LUA_TNUMBER)
223     link.bandwidth = lua_tonumber(L, -1);
224   else // LUA_TSTRING
225     link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1), "bandwidth of link", link.id.c_str());
226   lua_pop(L, 1);
227
228   //get latency value
229   lua_pushstring(L, "lat");
230   type = lua_gettable(L, -2);
231   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
232       "Attribute 'lat' must be specified for any link and must either be a string (in the right format; see docs) or a number.");
233   if (type == LUA_TNUMBER)
234     link.latency = lua_tonumber(L, -1);
235   else // LUA_TSTRING
236     link.latency = surf_parse_get_time(lua_tostring(L, -1), "latency of link", link.id.c_str());
237   lua_pop(L, 1);
238
239   /*Optional Arguments  */
240
241   //get bandwidth_trace value
242   lua_pushstring(L, "bandwidth_file");
243   lua_gettable(L, -2);
244   const char *filename = lua_tostring(L, -1);
245   if (filename)
246     link.bandwidth_trace = tmgr_trace_new_from_file(filename);
247   lua_pop(L, 1);
248
249   //get latency_trace value
250   lua_pushstring(L, "latency_file");
251   lua_gettable(L, -2);
252   filename = lua_tostring(L, -1);
253   if (filename)
254     link.latency_trace = tmgr_trace_new_from_file(filename);
255   lua_pop(L, 1);
256
257   //get state_trace value
258   lua_pushstring(L, "state_file");
259   lua_gettable(L, -2);
260   filename = lua_tostring(L, -1);
261   if (filename)
262     link.state_trace = tmgr_trace_new_from_file(filename);
263   lua_pop(L, 1);
264
265   //get policy value
266   lua_pushstring(L, "sharing_policy");
267   lua_gettable(L, -2);
268   policy = lua_tostring(L, -1);
269   lua_pop(L, 1);
270   if (policy && not strcmp(policy, "FULLDUPLEX")) {
271     link.policy = SURF_LINK_FULLDUPLEX;
272   } else if (policy && not strcmp(policy, "FATPIPE")) {
273     link.policy = SURF_LINK_FATPIPE;
274   } else {
275     link.policy = SURF_LINK_SHARED;
276   }
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   s_sg_platf_route_cbarg_t route;
307   memset(&route,0,sizeof(route));
308   int type;
309
310   lua_ensure(lua_istable(L, -1), "Bad Arguments to add a route. Should be a table with named arguments");
311
312   lua_pushstring(L,"src");
313   type = lua_gettable(L,-2);
314   lua_ensure(type == LUA_TSTRING, "Attribute 'src' must be specified for any route and must be a string.");
315   const char *srcName = lua_tostring(L, -1);
316   route.src           = sg_netpoint_by_name_or_null(srcName);
317   lua_ensure(route.src != nullptr, "Attribute 'src=%s' of route does not name a node.", srcName);
318   lua_pop(L,1);
319
320   lua_pushstring(L,"dest");
321   type = lua_gettable(L,-2);
322   lua_ensure(type == LUA_TSTRING, "Attribute 'dest' must be specified for any route and must be a string.");
323   const char *dstName = lua_tostring(L, -1);
324   route.dst           = sg_netpoint_by_name_or_null(dstName);
325   lua_ensure(route.dst != nullptr, "Attribute 'dst=%s' of route does not name a node.", dstName);
326   lua_pop(L,1);
327
328   lua_pushstring(L,"links");
329   type = lua_gettable(L,-2);
330   lua_ensure(type == LUA_TSTRING,
331       "Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
332   route.link_list   = new std::vector<simgrid::surf::LinkImpl*>();
333   std::vector<std::string> names;
334   const char* str = lua_tostring(L, -1);
335   boost::split(names, str, boost::is_any_of(", \t\r\n"));
336   if (names.empty()) {
337     /* unique name */
338     route.link_list->push_back(simgrid::surf::LinkImpl::byName(lua_tostring(L, -1)));
339   } else {
340     // Several names separated by , \t\r\n
341     for (auto const& name : names) {
342       if (name.length() > 0) {
343         simgrid::surf::LinkImpl* link = simgrid::surf::LinkImpl::byName(name);
344         route.link_list->push_back(link);
345       }
346     }
347   }
348   lua_pop(L,1);
349
350   /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
351    * Et ouais mon pote. That's the way it goes. F34R.
352    *
353    * (Note that above this function, there is a #include statement. Is this
354    * comment related to that statement?)
355    */
356   lua_pushstring(L,"symmetrical");
357   lua_gettable(L,-2);
358   if (lua_isstring(L, -1)) {
359     const char* value = lua_tostring(L, -1);
360     if (strcmp("YES", value) == 0)
361       route.symmetrical = true;
362     else
363       route.symmetrical = false;
364   }
365   else {
366     route.symmetrical = true;
367   }
368   lua_pop(L,1);
369
370   route.gw_src = nullptr;
371   route.gw_dst = nullptr;
372
373   sg_platf_new_route(&route);
374   delete route.link_list;
375
376   return 0;
377 }
378
379 int console_add_ASroute(lua_State *L) {
380   s_sg_platf_route_cbarg_t ASroute;
381   memset(&ASroute,0,sizeof(ASroute));
382
383   lua_pushstring(L, "src");
384   lua_gettable(L, -2);
385   const char *srcName = lua_tostring(L, -1);
386   ASroute.src         = sg_netpoint_by_name_or_null(srcName);
387   lua_ensure(ASroute.src != nullptr, "Attribute 'src=%s' of AS route does not name a node.", srcName);
388   lua_pop(L, 1);
389
390   lua_pushstring(L, "dst");
391   lua_gettable(L, -2);
392   const char *dstName = lua_tostring(L, -1);
393   ASroute.dst         = sg_netpoint_by_name_or_null(dstName);
394   lua_ensure(ASroute.dst != nullptr, "Attribute 'dst=%s' of AS route does not name a node.", dstName);
395   lua_pop(L, 1);
396
397   lua_pushstring(L, "gw_src");
398   lua_gettable(L, -2);
399   const char *name = lua_tostring(L, -1);
400   ASroute.gw_src   = sg_netpoint_by_name_or_null(name);
401   lua_ensure(ASroute.gw_src, "Attribute 'gw_src=%s' of AS route does not name a valid node", name);
402   lua_pop(L, 1);
403
404   lua_pushstring(L, "gw_dst");
405   lua_gettable(L, -2);
406   name = lua_tostring(L, -1);
407   ASroute.gw_dst = sg_netpoint_by_name_or_null(name);
408   lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst=%s' of AS route does not name a valid node", name);
409   lua_pop(L, 1);
410
411   lua_pushstring(L,"links");
412   lua_gettable(L,-2);
413   ASroute.link_list = new std::vector<simgrid::surf::LinkImpl*>();
414   std::vector<std::string> names;
415   const char* str = lua_tostring(L, -1);
416   boost::split(names, str, boost::is_any_of(", \t\r\n"));
417   if (names.empty()) {
418     /* unique name with no comma */
419     ASroute.link_list->push_back(simgrid::surf::LinkImpl::byName(lua_tostring(L, -1)));
420   } else {
421     // Several names separated by , \t\r\n
422     for (auto const& name : names) {
423       if (name.length() > 0) {
424         simgrid::surf::LinkImpl* link = simgrid::surf::LinkImpl::byName(name);
425         ASroute.link_list->push_back(link);
426       }
427     }
428   }
429   lua_pop(L,1);
430
431   lua_pushstring(L,"symmetrical");
432   lua_gettable(L,-2);
433   if (lua_isstring(L, -1)) {
434     const char* value = lua_tostring(L, -1);
435     if (strcmp("YES", value) == 0)
436       ASroute.symmetrical = true;
437     else
438       ASroute.symmetrical = false;
439   }
440   else {
441     ASroute.symmetrical = true;
442   }
443   lua_pop(L,1);
444
445   sg_platf_new_route(&ASroute);
446   delete ASroute.link_list;
447
448   return 0;
449 }
450
451 int console_AS_open(lua_State *L) {
452  XBT_DEBUG("Opening AS");
453
454  lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
455
456  lua_pushstring(L, "id");
457  int type = lua_gettable(L, -2);
458  lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
459  const char* id = lua_tostring(L, -1);
460  lua_pop(L, 1);
461
462  lua_pushstring(L, "mode");
463  lua_gettable(L, -2);
464  const char* mode = lua_tostring(L, -1);
465  lua_pop(L, 1);
466
467  int mode_int = A_surfxml_AS_routing_None;
468  if (not strcmp(mode, "Full"))
469    mode_int = A_surfxml_AS_routing_Full;
470  else if (not strcmp(mode, "Floyd"))
471    mode_int = A_surfxml_AS_routing_Floyd;
472  else if (not strcmp(mode, "Dijkstra"))
473    mode_int = A_surfxml_AS_routing_Dijkstra;
474  else if (not strcmp(mode, "DijkstraCache"))
475    mode_int = A_surfxml_AS_routing_DijkstraCache;
476  else if (not strcmp(mode, "Vivaldi"))
477    mode_int = A_surfxml_AS_routing_Vivaldi;
478  else if (not strcmp(mode, "Cluster"))
479    mode_int = A_surfxml_AS_routing_Cluster;
480  else if (not strcmp(mode, "none"))
481    mode_int = A_surfxml_AS_routing_None;
482  else xbt_die("Don't have the model name '%s'",mode);
483
484  ZoneCreationArgs AS;
485  AS.id = id;
486  AS.routing = mode_int;
487  simgrid::s4u::NetZone* new_as = sg_platf_new_Zone_begin(&AS);
488
489  /* Build a Lua representation of the new AS on the stack */
490  lua_newtable(L);
491  simgrid::s4u::NetZone** lua_as =
492      (simgrid::s4u::NetZone**)lua_newuserdata(L, sizeof(simgrid::s4u::NetZone*)); /* table userdatum */
493  *lua_as = new_as;
494  luaL_getmetatable(L, PLATF_MODULE_NAME); /* table userdatum metatable */
495  lua_setmetatable(L, -2);                 /* table userdatum */
496  lua_setfield(L, -2, AS_FIELDNAME);       /* table -- put the userdata as field of the table */
497
498  return 1;
499 }
500 int console_AS_seal(lua_State *L) {
501   XBT_DEBUG("Sealing AS");
502   sg_platf_new_Zone_seal();
503   return 0;
504 }
505
506 int console_host_set_property(lua_State *L) {
507   lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
508
509   // get Host id
510   lua_pushstring(L, "host");
511   lua_gettable(L, -2);
512   const char* name = lua_tostring(L, -1);
513   lua_pop(L, 1);
514
515   // get prop Name
516   lua_pushstring(L, "prop");
517   lua_gettable(L, -2);
518   const char* prop_id = lua_tostring(L, -1);
519   lua_pop(L, 1);
520   //get args
521   lua_pushstring(L,"value");
522   lua_gettable(L, -2);
523   const char* prop_value = lua_tostring(L, -1);
524   lua_pop(L, 1);
525
526   sg_host_t host = sg_host_by_name(name);
527   lua_ensure(host, "no host '%s' found",name);
528   host->setProperty(prop_id, prop_value);
529
530   return 0;
531 }
532
533 /**
534  * \brief Registers the platform functions into the table simgrid.platf.
535  * \param L a lua state
536  */
537 void sglua_register_platf_functions(lua_State* L)
538 {
539   lua_getglobal(L, "simgrid");     /* simgrid */
540   luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
541   lua_setfield(L, -2, "engine");   /* simgrid */
542
543   lua_pop(L, 1);                   /* -- */
544 }