Logo AND Algorithmique Numérique Distribuée

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