Logo AND Algorithmique Numérique Distribuée

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