Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move parts of the kernel to the right subdir
[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/surf/xml/platf_private.hpp"
11 #include "src/surf/network_interface.hpp"
12 #include "surf/surf_routing.h"
13 #include <string.h>
14 #include <ctype.h>
15
16 extern "C" {
17 #include <lauxlib.h>
18 }
19
20 #include <simgrid/host.h>
21 #include "src/surf/surf_private.h"
22
23 XBT_LOG_NEW_DEFAULT_CATEGORY(lua_platf, "Lua bindings (platform module)");
24
25 #define PLATF_MODULE_NAME "simgrid.engine"
26 #define AS_FIELDNAME   "__simgrid_as"
27
28 /* ********************************************************************************* */
29 /*                               simgrid.platf API                                   */
30 /* ********************************************************************************* */
31
32 static const luaL_Reg platf_functions[] = {
33     {"open", console_open},
34     {"close", console_close},
35     {"AS_open", console_AS_open},
36     {"AS_seal", console_AS_seal},
37     {"backbone_new", console_add_backbone},
38     {"host_link_new", console_add_host___link},
39     {"host_new", console_add_host},
40     {"link_new", console_add_link},
41     {"router_new", console_add_router},
42     {"route_new", console_add_route},
43     {"ASroute_new", console_add_ASroute},
44     {nullptr, nullptr}
45 };
46
47 int console_open(lua_State *L) {
48   sg_platf_init();
49   sg_platf_begin();
50
51   storage_register_callbacks();
52   routing_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   host.speed_per_pstate = xbt_dynar_new(sizeof(double), nullptr);
166   if (type == LUA_TNUMBER)
167     xbt_dynar_push_as(host.speed_per_pstate, double, lua_tointeger(L, -1));
168   else // LUA_TSTRING
169     xbt_dynar_push_as(host.speed_per_pstate, double, 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(!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   xbt_dynar_free(&host.speed_per_pstate);
201
202   return 0;
203 }
204
205 int  console_add_link(lua_State *L) {
206   s_sg_platf_link_cbarg_t link;
207   memset(&link,0,sizeof(link));
208
209   int type;
210   const char* policy;
211
212   lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
213
214   // get Id Value
215   lua_pushstring(L, "id");
216   type = lua_gettable(L, -2);
217   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
218       "Attribute 'id' must be specified for any link and must be a string.");
219   link.id = lua_tostring(L, -1);
220   lua_pop(L, 1);
221
222   // get bandwidth value
223   lua_pushstring(L, "bandwidth");
224   type = lua_gettable(L, -2);
225   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
226       "Attribute 'bandwidth' must be specified for any link and must either be either a string (in the right format; see docs) or a number.");
227   if (type == LUA_TNUMBER)
228     link.bandwidth = lua_tonumber(L, -1);
229   else // LUA_TSTRING
230     link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1),"bandwidth of link", link.id);
231   lua_pop(L, 1);
232
233   //get latency value
234   lua_pushstring(L, "lat");
235   type = lua_gettable(L, -2);
236   lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER,
237       "Attribute 'lat' must be specified for any link and must either be a string (in the right format; see docs) or a number.");
238   if (type == LUA_TNUMBER)
239     link.latency = lua_tonumber(L, -1);
240   else // LUA_TSTRING
241     link.latency = surf_parse_get_time(lua_tostring(L, -1),"latency of link", link.id);
242   lua_pop(L, 1);
243
244   /*Optional Arguments  */
245
246   //get bandwidth_trace value
247   lua_pushstring(L, "bandwidth_file");
248   lua_gettable(L, -2);
249   const char *filename = lua_tostring(L, -1);
250   if (filename)
251     link.bandwidth_trace = tmgr_trace_new_from_file(filename);
252   lua_pop(L, 1);
253
254   //get latency_trace value
255   lua_pushstring(L, "latency_file");
256   lua_gettable(L, -2);
257   filename = lua_tostring(L, -1);
258   if (filename)
259     link.latency_trace = tmgr_trace_new_from_file(filename);
260   lua_pop(L, 1);
261
262   //get state_trace value
263   lua_pushstring(L, "state_file");
264   lua_gettable(L, -2);
265   filename = lua_tostring(L, -1);
266   if (filename)
267     link.state_trace = tmgr_trace_new_from_file(filename);
268   lua_pop(L, 1);
269
270   //get policy value
271   lua_pushstring(L, "sharing_policy");
272   lua_gettable(L, -2);
273   policy = lua_tostring(L, -1);
274   lua_pop(L, 1);
275   if (policy && !strcmp(policy,"FULLDUPLEX")) {
276     link.policy = SURF_LINK_FULLDUPLEX;
277   } else if (policy && !strcmp(policy,"FATPIPE")) {
278     link.policy = SURF_LINK_FATPIPE;
279   } else {
280     link.policy = SURF_LINK_SHARED;
281   }
282
283   sg_platf_new_link(&link);
284
285   return 0;
286 }
287 /**
288  * add Router to AS components
289  */
290 int console_add_router(lua_State* L) {
291   s_sg_platf_router_cbarg_t router;
292   memset(&router,0,sizeof(router));
293   int type;
294
295   lua_ensure(lua_istable(L, -1),
296       "Bad Arguments to create router, Should be a table with named arguments");
297
298   lua_pushstring(L, "id");
299   type = lua_gettable(L, -2);
300   lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any link and must be a string.");
301   router.id = lua_tostring(L, -1);
302   lua_pop(L,1);
303
304   lua_pushstring(L,"coord");
305   lua_gettable(L,-2);
306   router.coord = lua_tostring(L, -1);
307   lua_pop(L,1);
308
309   sg_platf_new_router(&router);
310
311   return 0;
312 }
313
314 int console_add_route(lua_State *L) {
315   XBT_DEBUG("Adding route");
316   s_sg_platf_route_cbarg_t route;
317   memset(&route,0,sizeof(route));
318   int type;
319
320   /* allocating memory for the buffer, I think 2kB should be enough */
321   surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
322
323   lua_ensure(lua_istable(L, -1), "Bad Arguments to add a route. Should be a table with named arguments");
324
325   lua_pushstring(L,"src");
326   type = lua_gettable(L,-2);
327   lua_ensure(type == LUA_TSTRING, "Attribute 'src' must be specified for any route and must be a string.");
328   const char *srcName = lua_tostring(L, -1);
329   route.src = sg_netcard_by_name_or_null(srcName);
330   lua_ensure(route.src != nullptr, "Attribute 'src=%s' of route does not name a node.", srcName);
331   lua_pop(L,1);
332
333   lua_pushstring(L,"dest");
334   type = lua_gettable(L,-2);
335   lua_ensure(type == LUA_TSTRING, "Attribute 'dest' must be specified for any route and must be a string.");
336   const char *dstName = lua_tostring(L, -1);
337   route.dst = sg_netcard_by_name_or_null(dstName);
338   lua_ensure(route.dst != nullptr, "Attribute 'dst=%s' of route does not name a node.", dstName);
339   lua_pop(L,1);
340
341   lua_pushstring(L,"links");
342   type = lua_gettable(L,-2);
343   lua_ensure(type == LUA_TSTRING,
344       "Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
345   route.link_list = new std::vector<Link*>();
346   xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
347   if (xbt_dynar_is_empty(names)) {
348     /* unique name */
349     route.link_list->push_back(Link::byName(lua_tostring(L, -1)));
350   } else {
351     // Several names separated by , \t\r\n
352     unsigned int cpt;
353     char *name;
354     xbt_dynar_foreach(names, cpt, name) {
355       if (strlen(name)>0) {
356         Link *link = Link::byName(name);
357         route.link_list->push_back(link);
358       }
359     }
360   }
361   lua_pop(L,1);
362
363   /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
364    * Et ouais mon pote. That's the way it goes. F34R.
365    *
366    * (Note that above this function, there is a #include statement. Is this
367    * comment related to that statement?)
368    */
369   lua_pushstring(L,"symmetrical");
370   lua_gettable(L,-2);
371   if (lua_isstring(L, -1)) {
372     const char* value = lua_tostring(L, -1);
373     if (strcmp("YES", value) == 0)
374       route.symmetrical = true;
375     else
376       route.symmetrical = false;
377   }
378   else {
379     route.symmetrical = true;
380   }
381   lua_pop(L,1);
382
383   route.gw_src = nullptr;
384   route.gw_dst = nullptr;
385
386   sg_platf_new_route(&route);
387
388   return 0;
389 }
390
391 int console_add_ASroute(lua_State *L) {
392   s_sg_platf_route_cbarg_t ASroute;
393   memset(&ASroute,0,sizeof(ASroute));
394
395   lua_pushstring(L, "src");
396   lua_gettable(L, -2);
397   const char *srcName = lua_tostring(L, -1);
398   ASroute.src = sg_netcard_by_name_or_null(srcName);
399   lua_ensure(ASroute.src != nullptr, "Attribute 'src=%s' of AS route does not name a node.", srcName);
400   lua_pop(L, 1);
401
402   lua_pushstring(L, "dst");
403   lua_gettable(L, -2);
404   const char *dstName = lua_tostring(L, -1);
405   ASroute.dst = sg_netcard_by_name_or_null(dstName);
406   lua_ensure(ASroute.dst != nullptr, "Attribute 'dst=%s' of AS route does not name a node.", dstName);
407   lua_pop(L, 1);
408
409   lua_pushstring(L, "gw_src");
410   lua_gettable(L, -2);
411   const char *name = lua_tostring(L, -1);
412   ASroute.gw_src = sg_netcard_by_name_or_null(name);
413   lua_ensure(ASroute.gw_src, "Attribute 'gw_src=%s' of AS route does not name a valid node", name);
414   lua_pop(L, 1);
415
416   lua_pushstring(L, "gw_dst");
417   lua_gettable(L, -2);
418   name = lua_tostring(L, -1);
419   ASroute.gw_dst = sg_netcard_by_name_or_null(name);
420   lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst=%s' of AS route does not name a valid node", name);
421   lua_pop(L, 1);
422
423   lua_pushstring(L,"links");
424   lua_gettable(L,-2);
425   ASroute.link_list = new std::vector<Link*>();
426   xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
427   if (xbt_dynar_is_empty(names)) {
428     /* unique name with no comma */
429     ASroute.link_list->push_back(Link::byName(lua_tostring(L, -1)));
430   } else {
431     // Several names separated by , \t\r\n
432     unsigned int cpt;
433     char *name;
434     xbt_dynar_foreach(names, cpt, name) {
435       if (strlen(name)>0) {
436         Link *link = Link::byName(name);
437         ASroute.link_list->push_back(link);
438       }
439     }
440   }
441   lua_pop(L,1);
442
443   lua_pushstring(L,"symmetrical");
444   lua_gettable(L,-2);
445   if (lua_isstring(L, -1)) {
446     const char* value = lua_tostring(L, -1);
447     if (strcmp("YES", value) == 0)
448       ASroute.symmetrical = true;
449     else
450       ASroute.symmetrical = false;
451   }
452   else {
453     ASroute.symmetrical = true;
454   }
455   lua_pop(L,1);
456
457   sg_platf_new_route(&ASroute);
458
459   return 0;
460 }
461
462 int console_AS_open(lua_State *L) {
463  const char *id;
464  const char *mode;
465  int type;
466
467  XBT_DEBUG("Opening AS");
468
469  lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
470
471  lua_pushstring(L, "id");
472  type = lua_gettable(L, -2);
473  lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
474  id = lua_tostring(L, -1);
475  lua_pop(L, 1);
476
477  lua_pushstring(L, "mode");
478  lua_gettable(L, -2);
479  mode = lua_tostring(L, -1);
480  lua_pop(L, 1);
481
482  int mode_int = A_surfxml_AS_routing_None;
483  if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
484  else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
485  else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
486  else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
487  else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
488  else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
489  else if(!strcmp(mode,"none")) 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::As *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::As **lua_as = (simgrid::s4u::As **) lua_newuserdata(L, sizeof(simgrid::s4u::As *)); /* table userdatum */
500  *lua_as = new_as;
501  luaL_getmetatable(L, PLATF_MODULE_NAME); /* table userdatum metatable */
502  lua_setmetatable(L, -2);                 /* table userdatum */
503  lua_setfield(L, -2, AS_FIELDNAME);       /* table -- put the userdata as field of the table */
504
505  return 1;
506 }
507 int console_AS_seal(lua_State *L) {
508   XBT_DEBUG("Sealing AS");
509   sg_platf_new_AS_seal();
510   return 0;
511 }
512
513 int console_host_set_property(lua_State *L) {
514   const char* name ="";
515   const char* prop_id = "";
516   const char* prop_value = "";
517   lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
518
519   // get Host id
520   lua_pushstring(L, "host");
521   lua_gettable(L, -2);
522   name = lua_tostring(L, -1);
523   lua_pop(L, 1);
524
525   // get prop Name
526   lua_pushstring(L, "prop");
527   lua_gettable(L, -2);
528   prop_id = lua_tostring(L, -1);
529   lua_pop(L, 1);
530   //get args
531   lua_pushstring(L,"value");
532   lua_gettable(L, -2);
533   prop_value = lua_tostring(L,-1);
534   lua_pop(L, 1);
535
536   sg_host_t host = sg_host_by_name(name);
537   lua_ensure(host, "no host '%s' found",name);
538   xbt_dict_t props = sg_host_get_properties(host);
539   xbt_dict_set(props,prop_id,xbt_strdup(prop_value),nullptr);
540
541   return 0;
542 }
543
544 /**
545  * \brief Registers the platform functions into the table simgrid.platf.
546  * \param L a lua state
547  */
548 void sglua_register_platf_functions(lua_State* L)
549 {
550   lua_getglobal(L, "simgrid");     /* simgrid */
551   luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
552   lua_setfield(L, -2, "engine");   /* simgrid */
553
554   lua_pop(L, 1);                   /* -- */
555 }