Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
41f408c8f8df88a28d7f9053c97bc2879f8eabbb
[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 <simgrid/host.h>
22 #include "src/surf/surf_private.h"
23
24 XBT_LOG_NEW_DEFAULT_CATEGORY(lua_platf, "Lua bindings (platform module)");
25
26 #define PLATF_MODULE_NAME "simgrid.engine"
27 #define AS_FIELDNAME   "__simgrid_as"
28
29 /* ********************************************************************************* */
30 /*                               simgrid.platf API                                   */
31 /* ********************************************************************************* */
32
33 static const luaL_Reg platf_functions[] = {
34     {"open", console_open},
35     {"close", console_close},
36     {"AS_open", console_AS_open},
37     {"AS_seal", console_AS_seal},
38     {"backbone_new", console_add_backbone},
39     {"host_link_new", console_add_host___link},
40     {"host_new", console_add_host},
41     {"link_new", console_add_link},
42     {"router_new", console_add_router},
43     {"route_new", console_add_route},
44     {"ASroute_new", console_add_ASroute},
45     {nullptr, nullptr}
46 };
47
48 int console_open(lua_State *L) {
49   sg_platf_init();
50   sg_platf_begin();
51
52   storage_register_callbacks();
53
54   return 0;
55 }
56
57 int console_close(lua_State *L) {
58   sg_platf_end();
59   sg_platf_exit();
60   return 0;
61 }
62
63 int console_add_backbone(lua_State *L) {
64   s_sg_platf_link_cbarg_t link;
65   memset(&link,0,sizeof(link));
66   int type;
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   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);
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);
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 && !strcmp(policy,"FULLDUPLEX")) {
96     link.policy = SURF_LINK_FULLDUPLEX;
97   } else if (policy && !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));
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(!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   s_sg_platf_link_cbarg_t link;
205   memset(&link,0,sizeof(link));
206
207   int type;
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   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);
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);
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 && !strcmp(policy,"FULLDUPLEX")) {
274     link.policy = SURF_LINK_FULLDUPLEX;
275   } else if (policy && !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   xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
338   if (xbt_dynar_is_empty(names)) {
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     unsigned int cpt;
344     char *name;
345     xbt_dynar_foreach(names, cpt, name) {
346       if (strlen(name)>0) {
347         simgrid::surf::LinkImpl* link = simgrid::surf::LinkImpl::byName(name);
348         route.link_list->push_back(link);
349       }
350     }
351   }
352   xbt_dynar_free(&names);
353   lua_pop(L,1);
354
355   /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
356    * Et ouais mon pote. That's the way it goes. F34R.
357    *
358    * (Note that above this function, there is a #include statement. Is this
359    * comment related to that statement?)
360    */
361   lua_pushstring(L,"symmetrical");
362   lua_gettable(L,-2);
363   if (lua_isstring(L, -1)) {
364     const char* value = lua_tostring(L, -1);
365     if (strcmp("YES", value) == 0)
366       route.symmetrical = true;
367     else
368       route.symmetrical = false;
369   }
370   else {
371     route.symmetrical = true;
372   }
373   lua_pop(L,1);
374
375   route.gw_src = nullptr;
376   route.gw_dst = nullptr;
377
378   sg_platf_new_route(&route);
379   delete route.link_list;
380
381   return 0;
382 }
383
384 int console_add_ASroute(lua_State *L) {
385   s_sg_platf_route_cbarg_t ASroute;
386   memset(&ASroute,0,sizeof(ASroute));
387
388   lua_pushstring(L, "src");
389   lua_gettable(L, -2);
390   const char *srcName = lua_tostring(L, -1);
391   ASroute.src         = sg_netpoint_by_name_or_null(srcName);
392   lua_ensure(ASroute.src != nullptr, "Attribute 'src=%s' of AS route does not name a node.", srcName);
393   lua_pop(L, 1);
394
395   lua_pushstring(L, "dst");
396   lua_gettable(L, -2);
397   const char *dstName = lua_tostring(L, -1);
398   ASroute.dst         = sg_netpoint_by_name_or_null(dstName);
399   lua_ensure(ASroute.dst != nullptr, "Attribute 'dst=%s' of AS route does not name a node.", dstName);
400   lua_pop(L, 1);
401
402   lua_pushstring(L, "gw_src");
403   lua_gettable(L, -2);
404   const char *name = lua_tostring(L, -1);
405   ASroute.gw_src   = sg_netpoint_by_name_or_null(name);
406   lua_ensure(ASroute.gw_src, "Attribute 'gw_src=%s' of AS route does not name a valid node", name);
407   lua_pop(L, 1);
408
409   lua_pushstring(L, "gw_dst");
410   lua_gettable(L, -2);
411   name = lua_tostring(L, -1);
412   ASroute.gw_dst = sg_netpoint_by_name_or_null(name);
413   lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst=%s' of AS route does not name a valid node", name);
414   lua_pop(L, 1);
415
416   lua_pushstring(L,"links");
417   lua_gettable(L,-2);
418   ASroute.link_list = new std::vector<simgrid::surf::LinkImpl*>();
419   xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
420   if (xbt_dynar_is_empty(names)) {
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     unsigned int cpt;
426     char *name;
427     xbt_dynar_foreach(names, cpt, name) {
428       if (strlen(name)>0) {
429         simgrid::surf::LinkImpl* link = simgrid::surf::LinkImpl::byName(name);
430         ASroute.link_list->push_back(link);
431       }
432     }
433   }
434   lua_pop(L,1);
435
436   lua_pushstring(L,"symmetrical");
437   lua_gettable(L,-2);
438   if (lua_isstring(L, -1)) {
439     const char* value = lua_tostring(L, -1);
440     if (strcmp("YES", value) == 0)
441       ASroute.symmetrical = true;
442     else
443       ASroute.symmetrical = false;
444   }
445   else {
446     ASroute.symmetrical = true;
447   }
448   lua_pop(L,1);
449
450   sg_platf_new_route(&ASroute);
451   delete ASroute.link_list;
452
453   return 0;
454 }
455
456 int console_AS_open(lua_State *L) {
457  const char *id;
458  const char *mode;
459  int type;
460
461  XBT_DEBUG("Opening AS");
462
463  lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
464
465  lua_pushstring(L, "id");
466  type = lua_gettable(L, -2);
467  lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
468  id = lua_tostring(L, -1);
469  lua_pop(L, 1);
470
471  lua_pushstring(L, "mode");
472  lua_gettable(L, -2);
473  mode = lua_tostring(L, -1);
474  lua_pop(L, 1);
475
476  int mode_int = A_surfxml_AS_routing_None;
477  if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
478  else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
479  else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
480  else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
481  else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
482  else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
483  else if(!strcmp(mode,"none")) 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   const char* name ="";
510   const char* prop_id = "";
511   const char* prop_value = "";
512   lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
513
514   // get Host id
515   lua_pushstring(L, "host");
516   lua_gettable(L, -2);
517   name = lua_tostring(L, -1);
518   lua_pop(L, 1);
519
520   // get prop Name
521   lua_pushstring(L, "prop");
522   lua_gettable(L, -2);
523   prop_id = lua_tostring(L, -1);
524   lua_pop(L, 1);
525   //get args
526   lua_pushstring(L,"value");
527   lua_gettable(L, -2);
528   prop_value = lua_tostring(L,-1);
529   lua_pop(L, 1);
530
531   sg_host_t host = sg_host_by_name(name);
532   lua_ensure(host, "no host '%s' found",name);
533   xbt_dict_t props = sg_host_get_properties(host);
534   xbt_dict_set(props,prop_id,xbt_strdup(prop_value),nullptr);
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 }