Logo AND Algorithmique Numérique Distribuée

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