Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
lua: rename the platf module to 'engine' to align on s4u
[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     {NULL, NULL}
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 = NULL;
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), NULL);
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   route.src = lua_tostring(L, -1);
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   route.dst = lua_tostring(L, -1);
335   lua_pop(L,1);
336
337   lua_pushstring(L,"links");
338   type = lua_gettable(L,-2);
339   lua_ensure(type == LUA_TSTRING,
340       "Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
341   route.link_list = new std::vector<Link*>();
342   xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
343   if (xbt_dynar_is_empty(names)) {
344     /* unique name */
345     route.link_list->push_back(Link::byName(lua_tostring(L, -1)));
346   } else {
347     // Several names separated by , \t\r\n
348     unsigned int cpt;
349     char *name;
350     xbt_dynar_foreach(names, cpt, name) {
351       if (strlen(name)>0) {
352         Link *link = Link::byName(name);
353         route.link_list->push_back(link);
354       }
355     }
356   }
357   lua_pop(L,1);
358
359   /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
360    * Et ouais mon pote. That's the way it goes. F34R.
361    *
362    * (Note that above this function, there is a #include statement. Is this
363    * comment related to that statement?)
364    */
365   lua_pushstring(L,"symmetrical");
366   lua_gettable(L,-2);
367   if (lua_isstring(L, -1)) {
368     const char* value = lua_tostring(L, -1);
369     if (strcmp("YES", value) == 0)
370       route.symmetrical = true;
371     else
372       route.symmetrical = false;
373   }
374   else {
375     route.symmetrical = true;
376   }
377   lua_pop(L,1);
378
379   route.gw_src = NULL;
380   route.gw_dst = NULL;
381
382   sg_platf_new_route(&route);
383
384   return 0;
385 }
386
387 int console_add_ASroute(lua_State *L) {
388   s_sg_platf_route_cbarg_t ASroute;
389   memset(&ASroute,0,sizeof(ASroute));
390
391   lua_pushstring(L, "src");
392   lua_gettable(L, -2);
393   ASroute.src = lua_tostring(L, -1);
394   lua_pop(L, 1);
395
396   lua_pushstring(L, "dst");
397   lua_gettable(L, -2);
398   ASroute.dst = lua_tostring(L, -1);
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_netcard_by_name_or_null(name);
405   lua_ensure(ASroute.gw_src, "Attribute 'gw_src' of AS route does not name a valid machine: %s", 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_netcard_by_name_or_null(name);
412   lua_ensure(ASroute.gw_dst, "Attribute 'gw_dst' of AS route does not name a valid machine: %s", name);
413   lua_pop(L, 1);
414
415   lua_pushstring(L,"links");
416   lua_gettable(L,-2);
417   ASroute.link_list = new std::vector<Link*>();
418   xbt_dynar_t names = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
419   if (xbt_dynar_is_empty(names)) {
420     /* unique name with no comma */
421     ASroute.link_list->push_back(Link::byName(lua_tostring(L, -1)));
422   } else {
423     // Several names separated by , \t\r\n
424     unsigned int cpt;
425     char *name;
426     xbt_dynar_foreach(names, cpt, name) {
427       if (strlen(name)>0) {
428         Link *link = Link::byName(name);
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
451   return 0;
452 }
453
454 int console_AS_open(lua_State *L) {
455  const char *id;
456  const char *mode;
457  int type;
458
459  XBT_DEBUG("Opening AS");
460
461  lua_ensure(lua_istable(L, 1), "Bad Arguments to AS_open, Should be a table with named arguments");
462
463  lua_pushstring(L, "id");
464  type = lua_gettable(L, -2);
465  lua_ensure(type == LUA_TSTRING, "Attribute 'id' must be specified for any AS and must be a string.");
466  id = lua_tostring(L, -1);
467  lua_pop(L, 1);
468
469  lua_pushstring(L, "mode");
470  lua_gettable(L, -2);
471  mode = lua_tostring(L, -1);
472  lua_pop(L, 1);
473
474  int mode_int = A_surfxml_AS_routing_None;
475  if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
476  else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
477  else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
478  else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
479  else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
480  else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
481  else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
482  else xbt_die("Don't have the model name '%s'",mode);
483
484  s_sg_platf_AS_cbarg_t AS;
485  AS.id = id;
486  AS.routing = mode_int;
487  simgrid::s4u::As *new_as = sg_platf_new_AS_begin(&AS);
488
489  /* Build a Lua representation of the new AS on the stack */
490  lua_newtable(L);
491  simgrid::s4u::As **lua_as = (simgrid::s4u::As **) lua_newuserdata(L, sizeof(simgrid::s4u::As *)); /* table userdatum */
492  *lua_as = new_as;
493  luaL_getmetatable(L, PLATF_MODULE_NAME); /* table userdatum metatable */
494  lua_setmetatable(L, -2);                 /* table userdatum */
495  lua_setfield(L, -2, AS_FIELDNAME);       /* table -- put the userdata as field of the table */
496
497  return 1;
498 }
499 int console_AS_seal(lua_State *L) {
500   XBT_DEBUG("Sealing AS");
501   sg_platf_new_AS_seal();
502   return 0;
503 }
504
505 int console_host_set_property(lua_State *L) {
506   const char* name ="";
507   const char* prop_id = "";
508   const char* prop_value = "";
509   lua_ensure(lua_istable(L, -1), "Bad Arguments to create link, Should be a table with named arguments");
510
511   // get Host id
512   lua_pushstring(L, "host");
513   lua_gettable(L, -2);
514   name = lua_tostring(L, -1);
515   lua_pop(L, 1);
516
517   // get prop Name
518   lua_pushstring(L, "prop");
519   lua_gettable(L, -2);
520   prop_id = lua_tostring(L, -1);
521   lua_pop(L, 1);
522   //get args
523   lua_pushstring(L,"value");
524   lua_gettable(L, -2);
525   prop_value = lua_tostring(L,-1);
526   lua_pop(L, 1);
527
528   sg_host_t host = sg_host_by_name(name);
529   lua_ensure(host, "no host '%s' found",name);
530   xbt_dict_t props = sg_host_get_properties(host);
531   xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
532
533   return 0;
534 }
535
536 /**
537  * \brief Registers the platform functions into the table simgrid.platf.
538  * \param L a lua state
539  */
540 void sglua_register_platf_functions(lua_State* L)
541 {
542   lua_getglobal(L, "simgrid");     /* simgrid */
543   luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
544   lua_setfield(L, -2, "platf");    /* simgrid */
545
546   lua_pop(L, 1);                   /* -- */
547 }