Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7514c7121d5fb81a86248c06d48bd25e293c8f40
[simgrid.git] / src / bindings / lua / lua_platf.c
1 /* Copyright (c) 2010. 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   surf_parse_init_callbacks();
45   
46   storage_register_callbacks();  
47   routing_register_callbacks();
48
49   gpu_register_callbacks();
50   
51   return 0;
52 }
53
54 int console_close(lua_State *L) {
55   sg_platf_end();
56   sg_platf_exit();
57
58   xbt_lib_cursor_t cursor;
59   void **data;
60   char *name;
61
62   /* Initialize MSG and WKS hosts */
63   XBT_DEBUG("Initialize MSG and WKS hosts");
64   xbt_lib_foreach(host_lib, cursor, name, data) {
65     if(data[SURF_WKS_LEVEL]){
66       XBT_DEBUG("\tSee surf host %s",name);
67       SIMIX_host_create(name, data[SURF_WKS_LEVEL], NULL);
68       __MSG_host_create((smx_host_t)data[SIMIX_HOST_LEVEL]);
69     }
70   }
71
72   return 0;
73 }
74
75 int console_add_host(lua_State *L) {
76   s_sg_platf_host_cbarg_t host;
77   memset(&host,0,sizeof(host));
78   int state;
79
80   // we get values from the table passed as argument
81   if (!lua_istable(L, -1)) {
82     XBT_ERROR
83         ("Bad Arguments to create host, Should be a table with named arguments");
84     return -1;
85   }
86
87   // get Id Value
88   lua_pushstring(L, "id");
89   lua_gettable(L, -2);
90   host.id = lua_tostring(L, -1);
91   lua_pop(L, 1);
92
93   // get power value
94   lua_pushstring(L, "power");
95   lua_gettable(L, -2);
96   host.power_peak = 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,"RuleBased")) mode_int = A_surfxml_AS_routing_RuleBased;
325  else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
326  else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
327  else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
328  else xbt_die("Don't have the model name '%s'",mode);
329
330  s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
331  AS.id = id;
332  AS.routing = mode_int;
333  sg_platf_new_AS_begin(&AS);
334
335  return 0;
336 }
337 int console_AS_close(lua_State *L) {
338   sg_platf_new_AS_end();
339   return 0;
340 }
341
342 int console_set_function(lua_State *L) {
343
344   const char *host_id ;
345   const char *function_id;
346   xbt_dynar_t args;
347
348   if (! lua_istable(L, 1)) {
349     XBT_ERROR("Bad Arguments to AS.new, Should be a table with named arguments");
350     return -1;
351   }
352
353   // get Host id
354   lua_pushstring(L, "host");
355   lua_gettable(L, -2);
356   host_id = lua_tostring(L, -1);
357   lua_pop(L, 1);
358
359   // get Function Name
360   lua_pushstring(L, "fct");
361   lua_gettable(L, -2);
362   function_id = lua_tostring(L, -1);
363   lua_pop(L, 1);
364
365   //get args
366   lua_pushstring(L,"args");
367   lua_gettable(L, -2);
368   args = xbt_str_split_quoted( lua_tostring(L,-1) );
369   lua_pop(L, 1);
370
371   // FIXME: hackish to go under MSG that way
372   msg_host_t host = xbt_lib_get_or_null(host_lib,host_id,MSG_HOST_LEVEL);
373   if (!host) {
374     XBT_ERROR("no host '%s' found",host_id);
375     return -1;
376   }
377
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