Logo AND Algorithmique Numérique Distribuée

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