Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move some storage-related content out of routing into storage area
[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/NetCard.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(sg_link_by_name(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   s_sg_platf_router_cbarg_t router;
290   memset(&router,0,sizeof(router));
291   int type;
292
293   lua_ensure(lua_istable(L, -1),
294       "Bad Arguments to create router, Should be a table with named arguments");
295
296   lua_pushstring(L, "id");
297   type = lua_gettable(L, -2);
298   lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any link and must be a string.");
299   router.id = lua_tostring(L, -1);
300   lua_pop(L,1);
301
302   lua_pushstring(L,"coord");
303   lua_gettable(L,-2);
304   router.coord = lua_tostring(L, -1);
305   lua_pop(L,1);
306
307   sg_platf_new_router(&router);
308
309   return 0;
310 }
311
312 int console_add_route(lua_State *L) {
313   XBT_DEBUG("Adding route");
314   s_sg_platf_route_cbarg_t route;
315   memset(&route,0,sizeof(route));
316   int type;
317
318   /* allocating memory for the buffer, I think 2kB should be enough */
319   surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
320
321   lua_ensure(lua_istable(L, -1), "Bad Arguments to add a route. Should be a table with named arguments");
322
323   lua_pushstring(L,"src");
324   type = lua_gettable(L,-2);
325   lua_ensure(type == LUA_TSTRING, "Attribute 'src' must be specified for any route and must be a string.");
326   const char *srcName = lua_tostring(L, -1);
327   route.src = sg_netcard_by_name_or_null(srcName);
328   lua_ensure(route.src != nullptr, "Attribute 'src=%s' of route does not name a node.", srcName);
329   lua_pop(L,1);
330
331   lua_pushstring(L,"dest");
332   type = lua_gettable(L,-2);
333   lua_ensure(type == LUA_TSTRING, "Attribute 'dest' must be specified for any route and must be a string.");
334   const char *dstName = lua_tostring(L, -1);
335   route.dst = sg_netcard_by_name_or_null(dstName);
336   lua_ensure(route.dst != nullptr, "Attribute 'dst=%s' of route does not name a node.", dstName);
337   lua_pop(L,1);
338
339   lua_pushstring(L,"links");
340   type = lua_gettable(L,-2);
341   lua_ensure(type == LUA_TSTRING,
342       "Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
343   route.link_list = new std::vector<Link*>();
344   xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
345   if (xbt_dynar_is_empty(names)) {
346     /* unique name */
347     route.link_list->push_back(Link::byName(lua_tostring(L, -1)));
348   } else {
349     // Several names separated by , \t\r\n
350     unsigned int cpt;
351     char *name;
352     xbt_dynar_foreach(names, cpt, name) {
353       if (strlen(name)>0) {
354         Link *link = Link::byName(name);
355         route.link_list->push_back(link);
356       }
357     }
358   }
359   lua_pop(L,1);
360
361   /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
362    * Et ouais mon pote. That's the way it goes. F34R.
363    *
364    * (Note that above this function, there is a #include statement. Is this
365    * comment related to that statement?)
366    */
367   lua_pushstring(L,"symmetrical");
368   lua_gettable(L,-2);
369   if (lua_isstring(L, -1)) {
370     const char* value = lua_tostring(L, -1);
371     if (strcmp("YES", value) == 0)
372       route.symmetrical = true;
373     else
374       route.symmetrical = false;
375   }
376   else {
377     route.symmetrical = true;
378   }
379   lua_pop(L,1);
380
381   route.gw_src = nullptr;
382   route.gw_dst = nullptr;
383
384   sg_platf_new_route(&route);
385
386   return 0;
387 }
388
389 int console_add_ASroute(lua_State *L) {
390   s_sg_platf_route_cbarg_t ASroute;
391   memset(&ASroute,0,sizeof(ASroute));
392
393   lua_pushstring(L, "src");
394   lua_gettable(L, -2);
395   const char *srcName = lua_tostring(L, -1);
396   ASroute.src = sg_netcard_by_name_or_null(srcName);
397   lua_ensure(ASroute.src != nullptr, "Attribute 'src=%s' of AS route does not name a node.", srcName);
398   lua_pop(L, 1);
399
400   lua_pushstring(L, "dst");
401   lua_gettable(L, -2);
402   const char *dstName = lua_tostring(L, -1);
403   ASroute.dst = sg_netcard_by_name_or_null(dstName);
404   lua_ensure(ASroute.dst != nullptr, "Attribute 'dst=%s' of AS route does not name a node.", dstName);
405   lua_pop(L, 1);
406
407   lua_pushstring(L, "gw_src");
408   lua_gettable(L, -2);
409   const char *name = lua_tostring(L, -1);
410   ASroute.gw_src = sg_netcard_by_name_or_null(name);
411   lua_ensure(ASroute.gw_src, "Attribute 'gw_src=%s' of AS route does not name a valid node", name);
412   lua_pop(L, 1);
413
414   lua_pushstring(L, "gw_dst");
415   lua_gettable(L, -2);
416   name = lua_tostring(L, -1);
417   ASroute.gw_dst = sg_netcard_by_name_or_null(name);
418   lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst=%s' of AS route does not name a valid node", name);
419   lua_pop(L, 1);
420
421   lua_pushstring(L,"links");
422   lua_gettable(L,-2);
423   ASroute.link_list = new std::vector<Link*>();
424   xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
425   if (xbt_dynar_is_empty(names)) {
426     /* unique name with no comma */
427     ASroute.link_list->push_back(Link::byName(lua_tostring(L, -1)));
428   } else {
429     // Several names separated by , \t\r\n
430     unsigned int cpt;
431     char *name;
432     xbt_dynar_foreach(names, cpt, name) {
433       if (strlen(name)>0) {
434         Link *link = Link::byName(name);
435         ASroute.link_list->push_back(link);
436       }
437     }
438   }
439   lua_pop(L,1);
440
441   lua_pushstring(L,"symmetrical");
442   lua_gettable(L,-2);
443   if (lua_isstring(L, -1)) {
444     const char* value = lua_tostring(L, -1);
445     if (strcmp("YES", value) == 0)
446       ASroute.symmetrical = true;
447     else
448       ASroute.symmetrical = false;
449   }
450   else {
451     ASroute.symmetrical = true;
452   }
453   lua_pop(L,1);
454
455   sg_platf_new_route(&ASroute);
456
457   return 0;
458 }
459
460 int console_AS_open(lua_State *L) {
461  const char *id;
462  const char *mode;
463  int type;
464
465  XBT_DEBUG("Opening AS");
466
467  lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
468
469  lua_pushstring(L, "id");
470  type = lua_gettable(L, -2);
471  lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
472  id = lua_tostring(L, -1);
473  lua_pop(L, 1);
474
475  lua_pushstring(L, "mode");
476  lua_gettable(L, -2);
477  mode = lua_tostring(L, -1);
478  lua_pop(L, 1);
479
480  int mode_int = A_surfxml_AS_routing_None;
481  if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
482  else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
483  else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
484  else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
485  else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
486  else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
487  else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
488  else xbt_die("Don't have the model name '%s'",mode);
489
490  s_sg_platf_AS_cbarg_t AS;
491  AS.id = id;
492  AS.routing = mode_int;
493  simgrid::s4u::As *new_as = sg_platf_new_AS_begin(&AS);
494
495  /* Build a Lua representation of the new AS on the stack */
496  lua_newtable(L);
497  simgrid::s4u::As **lua_as = (simgrid::s4u::As **) lua_newuserdata(L, sizeof(simgrid::s4u::As *)); /* table userdatum */
498  *lua_as = new_as;
499  luaL_getmetatable(L, PLATF_MODULE_NAME); /* table userdatum metatable */
500  lua_setmetatable(L, -2);                 /* table userdatum */
501  lua_setfield(L, -2, AS_FIELDNAME);       /* table -- put the userdata as field of the table */
502
503  return 1;
504 }
505 int console_AS_seal(lua_State *L) {
506   XBT_DEBUG("Sealing AS");
507   sg_platf_new_AS_seal();
508   return 0;
509 }
510
511 int console_host_set_property(lua_State *L) {
512   const char* name ="";
513   const char* prop_id = "";
514   const char* prop_value = "";
515   lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
516
517   // get Host id
518   lua_pushstring(L, "host");
519   lua_gettable(L, -2);
520   name = lua_tostring(L, -1);
521   lua_pop(L, 1);
522
523   // get prop Name
524   lua_pushstring(L, "prop");
525   lua_gettable(L, -2);
526   prop_id = lua_tostring(L, -1);
527   lua_pop(L, 1);
528   //get args
529   lua_pushstring(L,"value");
530   lua_gettable(L, -2);
531   prop_value = lua_tostring(L,-1);
532   lua_pop(L, 1);
533
534   sg_host_t host = sg_host_by_name(name);
535   lua_ensure(host, "no host '%s' found",name);
536   xbt_dict_t props = sg_host_get_properties(host);
537   xbt_dict_set(props,prop_id,xbt_strdup(prop_value),nullptr);
538
539   return 0;
540 }
541
542 /**
543  * \brief Registers the platform functions into the table simgrid.platf.
544  * \param L a lua state
545  */
546 void sglua_register_platf_functions(lua_State* L)
547 {
548   lua_getglobal(L, "simgrid");     /* simgrid */
549   luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
550   lua_setfield(L, -2, "engine");   /* simgrid */
551
552   lua_pop(L, 1);                   /* -- */
553 }