Logo AND Algorithmique Numérique Distribuée

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