Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Added support for using lua platform files from C
[simgrid.git] / src / bindings / lua / lua_platf.c
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 "simgrid/platf_interface.h"
11 #include "surf/surfxml_parse.h"
12 #include "surf/surf_routing.h"
13 #include <string.h>
14 #include <ctype.h>
15 #include <lauxlib.h>
16
17 #include <msg/msg_private.h>
18 #include <simix/smx_host_private.h>
19 #include <surf/surf_private.h>
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_platf, bindings, "Lua bindings (platform module)");
22
23 #define PLATF_MODULE_NAME "simgrid.platf"
24
25 /* ********************************************************************************* */
26 /*                               simgrid.platf API                                   */
27 /* ********************************************************************************* */
28
29 static const luaL_reg platf_functions[] = {
30     {"open", console_open},
31     {"close", console_close},
32     {"AS_open", console_AS_open},
33     {"AS_close", console_AS_close},
34     {"host_new", console_add_host},
35     {"link_new", console_add_link},
36     {"router_new", console_add_router},
37     {"route_new", console_add_route},
38     {NULL, NULL}
39 };
40
41 int console_open(lua_State *L) {
42   sg_platf_init();
43   sg_platf_begin();
44
45   storage_register_callbacks();
46   routing_register_callbacks();
47
48   gpu_register_callbacks();
49
50   return 0;
51 }
52
53 int console_close(lua_State *L) {
54   sg_platf_end();
55   sg_platf_exit();
56
57   xbt_lib_cursor_t cursor;
58   void **data;
59   char *name;
60
61   /* Initialize MSG and WKS hosts */
62   XBT_DEBUG("Initialize MSG and WKS hosts");
63   xbt_lib_foreach(host_lib, cursor, name, data) {
64     if(data[SURF_HOST_LEVEL]){
65       XBT_DEBUG("\tSee surf host %s",name);
66       SIMIX_host_create(name);
67       // THIS IS BRAINDEAD. There is no sg_host_t in that level, but a smx_host_priv. So commenting out for now.
68       // Lua is broken anyway. Christian will fix it
69       // __MSG_host_create((sg_host_t)data[SIMIX_HOST_LEVEL]);
70     }
71   }
72
73   return 0;
74 }
75
76 int console_add_host(lua_State *L) {
77   s_sg_platf_host_cbarg_t host;
78   memset(&host,0,sizeof(host));
79   int state;
80
81   // we get values from the table passed as argument
82   if (!lua_istable(L, -1)) {
83     XBT_ERROR
84         ("Bad Arguments to create host, Should be a table with named arguments");
85     return -1;
86   }
87
88   // get Id Value
89   lua_pushstring(L, "id");
90   lua_gettable(L, -2);
91   host.id = lua_tostring(L, -1);
92   lua_pop(L, 1);
93
94   // get power value
95   lua_pushstring(L, "power");
96   lua_gettable(L, -2);
97   host.power_peak = xbt_dynar_new(sizeof(double), NULL);
98   xbt_dynar_push_as(host.power_peak, double, lua_tonumber(L, -1));
99   lua_pop(L, 1);
100
101   // get core
102   lua_pushstring(L, "core");
103   lua_gettable(L, -2);
104   if(!lua_isnumber(L,-1)) host.core_amount = 1;// Default value
105   else host.core_amount = lua_tonumber(L, -1);
106   if (host.core_amount == 0)
107     host.core_amount = 1;
108   lua_pop(L, 1);
109
110   //get power_scale
111   lua_pushstring(L, "power_scale");
112   lua_gettable(L, -2);
113   if(!lua_isnumber(L,-1)) host.power_scale = 1;// Default value
114   else host.power_scale = lua_tonumber(L, -1);
115   lua_pop(L, 1);
116
117   //get power_trace
118   lua_pushstring(L, "power_trace");
119   lua_gettable(L, -2);
120   host.power_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
121   lua_pop(L, 1);
122
123   //get state initial
124   lua_pushstring(L, "state_initial");
125   lua_gettable(L, -2);
126   if(!lua_isnumber(L,-1)) state = 1;// Default value
127   else state = lua_tonumber(L, -1);
128   lua_pop(L, 1);
129
130   if (state)
131     host.initial_state = SURF_RESOURCE_ON;
132   else
133     host.initial_state = SURF_RESOURCE_OFF;
134
135   //get trace state
136   lua_pushstring(L, "state_trace");
137   lua_gettable(L, -2);
138   host.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
139   lua_pop(L, 1);
140
141   sg_platf_new_host(&host);
142
143   return 0;
144 }
145
146 int  console_add_link(lua_State *L) {
147   s_sg_platf_link_cbarg_t link;
148   memset(&link,0,sizeof(link));
149
150   const char* policy;
151
152   if (! lua_istable(L, -1)) {
153     XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
154     return -1;
155   }
156
157   // get Id Value
158   lua_pushstring(L, "id");
159   lua_gettable(L, -2);
160   link.id = lua_tostring(L, -1);
161   lua_pop(L, 1);
162
163   // get bandwidth value
164   lua_pushstring(L, "bandwidth");
165   lua_gettable(L, -2);
166   link.bandwidth = lua_tonumber(L, -1);
167   lua_pop(L, 1);
168
169   //get latency value
170   lua_pushstring(L, "latency");
171   lua_gettable(L, -2);
172   link.latency = lua_tonumber(L, -1);
173   lua_pop(L, 1);
174
175   /*Optional Arguments  */
176
177   //get bandwidth_trace value
178   lua_pushstring(L, "bandwidth_trace");
179   lua_gettable(L, -2);
180   link.bandwidth_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
181   lua_pop(L, 1);
182
183   //get latency_trace value
184   lua_pushstring(L, "latency_trace");
185   lua_gettable(L, -2);
186   link.latency_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
187   lua_pop(L, 1);
188
189   //get state_trace value
190   lua_pushstring(L, "state_trace");
191   lua_gettable(L, -2);
192   link.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
193   lua_pop(L, 1);
194
195   //get state_initial value
196   lua_pushstring(L, "state_initial");
197   lua_gettable(L, -2);
198   if (!lua_isnumber(L,-1) || lua_tonumber(L, -1))
199     link.state = SURF_RESOURCE_ON;
200   else
201     link.state = SURF_RESOURCE_OFF;
202   lua_pop(L, 1);
203
204   //get policy value
205   lua_pushstring(L, "policy");
206   lua_gettable(L, -2);
207   policy = lua_tostring(L, -1);
208   lua_pop(L, 1);
209   if (policy && !strcmp(policy,"FULLDUPLEX")) {
210     link.policy = SURF_LINK_FULLDUPLEX;
211   } else if (policy && !strcmp(policy,"FATPIPE")) {
212     link.policy = SURF_LINK_FATPIPE;
213   } else {
214     link.policy = SURF_LINK_SHARED;
215   }
216
217   sg_platf_new_link(&link);
218
219   return 0;
220 }
221 /**
222  * add Router to AS components
223  */
224 int console_add_router(lua_State* L) {
225   s_sg_platf_router_cbarg_t router;
226   memset(&router,0,sizeof(router));
227
228   if (! lua_istable(L, -1)) {
229     XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
230     return -1;
231   }
232
233   lua_pushstring(L, "id");
234   lua_gettable(L, -2);
235   router.id = lua_tostring(L, -1);
236   lua_pop(L,1);
237
238   lua_pushstring(L,"coord");
239   lua_gettable(L,-2);
240   router.coord = lua_tostring(L, -1);
241   lua_pop(L,1);
242
243   sg_platf_new_router(&router);
244
245   return 0;
246 }
247
248 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
249
250 int console_add_route(lua_State *L) {
251   XBT_DEBUG("Adding route");
252   s_sg_platf_route_cbarg_t route;
253   memset(&route,0,sizeof(route));
254
255   /* allocating memory for the buffer, I think 2kB should be enough */
256   surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
257
258   if (! lua_istable(L, -1)) {
259     XBT_ERROR("Bad Arguments to create a route, Should be a table with named arguments");
260     return -1;
261   }
262
263   lua_pushstring(L,"src");
264   lua_gettable(L,-2);
265   route.src = lua_tostring(L, -1);
266   lua_pop(L,1);
267
268   lua_pushstring(L,"dest");
269   lua_gettable(L,-2);
270   route.dst = lua_tostring(L, -1);
271   lua_pop(L,1);
272
273   lua_pushstring(L,"links");
274   lua_gettable(L,-2);
275   route.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
276   if (xbt_dynar_is_empty(route.link_list))
277     xbt_dynar_push_as(route.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
278   lua_pop(L,1);
279
280   /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
281    * Et ouais mon pote. That's the way it goes. F34R.
282    */
283   lua_pushstring(L,"symmetrical");
284   lua_gettable(L,-2);
285   if (lua_isstring(L, -1)) {
286     const char* value = lua_tostring(L, -1);
287     if (strcmp("YES", value) == 0) {
288       route.symmetrical = TRUE;
289     }
290     else
291       route.symmetrical = FALSE;
292   }
293   else {
294     route.symmetrical = TRUE;
295   }
296   lua_pop(L,1);
297
298   route.gw_src = NULL;
299   route.gw_dst = NULL;
300
301   sg_platf_new_route(&route);
302
303   return 0;
304 }
305
306 int console_AS_open(lua_State *L) {
307  const char *id;
308  const char *mode;
309
310  XBT_DEBUG("Opening AS");
311
312  if (! lua_istable(L, 1)) {
313    XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
314    return -1;
315  }
316
317  lua_pushstring(L, "id");
318  lua_gettable(L, -2);
319  id = lua_tostring(L, -1);
320  lua_pop(L, 1);
321
322  lua_pushstring(L, "mode");
323  lua_gettable(L, -2);
324  mode = lua_tostring(L, -1);
325  lua_pop(L, 1);
326
327  int mode_int = A_surfxml_AS_routing_None;
328  if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
329  else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
330  else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
331  else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
332  else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
333  else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
334  else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
335  else xbt_die("Don't have the model name '%s'",mode);
336
337  s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
338  AS.id = id;
339  AS.routing = mode_int;
340  sg_platf_new_AS_begin(&AS);
341
342  return 0;
343 }
344 int console_AS_close(lua_State *L) {
345   XBT_DEBUG("Closing AS");
346   sg_platf_new_AS_end();
347   return 0;
348 }
349
350 int console_set_function(lua_State *L) {
351
352   const char *host_id ;
353   const char *function_id;
354   xbt_dynar_t args;
355
356   if (! lua_istable(L, 1)) {
357     XBT_ERROR("Bad Arguments to AS.new, Should be a table with named arguments");
358     return -1;
359   }
360
361   // get Host id
362   lua_pushstring(L, "host");
363   lua_gettable(L, -2);
364   host_id = lua_tostring(L, -1);
365   lua_pop(L, 1);
366
367   // get Function Name
368   lua_pushstring(L, "fct");
369   lua_gettable(L, -2);
370   function_id = lua_tostring(L, -1);
371   lua_pop(L, 1);
372
373   //get args
374   lua_pushstring(L,"args");
375   lua_gettable(L, -2);
376   args = xbt_str_split_str( lua_tostring(L,-1) , ",");
377   lua_pop(L, 1);
378
379   msg_host_t host = MSG_host_by_name(host_id);
380   if (!host) {
381     XBT_ERROR("no host '%s' found",host_id);
382     return -1;
383   }
384
385   // FIXME: use sg_platf_new_process directly (warning: find a way to check hostname)
386   MSG_set_function(host_id, function_id, args);
387
388   return 0;
389 }
390
391 int console_host_set_property(lua_State *L) {
392   const char* name ="";
393   const char* prop_id = "";
394   const char* prop_value = "";
395   if (!lua_istable(L, -1)) {
396     XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
397     return -1;
398   }
399
400
401   // get Host id
402   lua_pushstring(L, "host");
403   lua_gettable(L, -2);
404   name = lua_tostring(L, -1);
405   lua_pop(L, 1);
406
407   // get prop Name
408   lua_pushstring(L, "prop");
409   lua_gettable(L, -2);
410   prop_id = lua_tostring(L, -1);
411   lua_pop(L, 1);
412   //get args
413   lua_pushstring(L,"value");
414   lua_gettable(L, -2);
415   prop_value = lua_tostring(L,-1);
416   lua_pop(L, 1);
417
418   msg_host_t host = MSG_host_by_name(name);
419   if (!host) {
420     XBT_ERROR("no host '%s' found",name);
421     return -1;
422   }
423   xbt_dict_t props = MSG_host_get_properties(host);
424   xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
425
426   return 0;
427 }
428
429 /**
430  * \brief Registers the platform functions into the table simgrid.platf.
431  * \param L a lua state
432  */
433 void sglua_register_platf_functions(lua_State* L)
434 {
435   luaL_openlib(L, PLATF_MODULE_NAME, platf_functions, 0);
436                                   /* simgrid.platf */
437   lua_pop(L, 1);
438 }
439