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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_platf, bindings, "Lua bindings (platform module)");
18
19 #define PLATF_MODULE_NAME "simgrid.platf"
20
21 /* ********************************************************************************* */
22 /*                               simgrid.platf API                                   */
23 /* ********************************************************************************* */
24
25 static const luaL_reg platf_functions[] = {
26     {"open", console_open},
27     {"close", console_close},
28     {"AS_open", console_AS_open},
29     {"AS_close", console_AS_close},
30     {"host_new", console_add_host},
31     {"link_new", console_add_link},
32     {"router_new", console_add_router},
33     {"route_new", console_add_route},
34     {NULL, NULL}
35 };
36
37 int console_open(lua_State *L) {
38   sg_platf_init();
39   sg_platf_begin();
40   surf_parse_init_callbacks();
41   routing_register_callbacks();
42
43   return 0;
44 }
45
46 int console_close(lua_State *L) {
47   sg_platf_end();
48   sg_platf_exit();
49
50   xbt_lib_cursor_t cursor;
51   void **data;
52   char *name;
53
54   /* Initialize MSG and WKS hosts */
55   XBT_DEBUG("Initialize MSG and WKS hosts");
56   xbt_lib_foreach(host_lib, cursor, name, data) {
57     if(data[SURF_WKS_LEVEL]){
58       XBT_DEBUG("\tSee surf host %s",name);
59       SIMIX_host_create(name, data[SURF_WKS_LEVEL], NULL);
60       __MSG_host_create((smx_host_t)data[SIMIX_HOST_LEVEL]);
61     }
62   }
63
64   return 0;
65 }
66
67 int console_add_host(lua_State *L) {
68   s_sg_platf_host_cbarg_t host;
69   memset(&host,0,sizeof(host));
70   int state;
71
72   // we get values from the table passed as argument
73   if (!lua_istable(L, -1)) {
74     XBT_ERROR
75         ("Bad Arguments to create host, Should be a table with named arguments");
76     return -1;
77   }
78
79   // get Id Value
80   lua_pushstring(L, "id");
81   lua_gettable(L, -2);
82   host.id = lua_tostring(L, -1);
83   lua_pop(L, 1);
84
85   // get power value
86   lua_pushstring(L, "power");
87   lua_gettable(L, -2);
88   host.power_peak = lua_tonumber(L, -1);
89   lua_pop(L, 1);
90
91   // get core
92   lua_pushstring(L, "core");
93   lua_gettable(L, -2);
94   if(!lua_isnumber(L,-1)) host.core_amount = 1;// Default value
95   else host.core_amount = lua_tonumber(L, -1);
96   if (host.core_amount == 0)
97     host.core_amount = 1;
98   lua_pop(L, 1);
99
100   //get power_scale
101   lua_pushstring(L, "power_scale");
102   lua_gettable(L, -2);
103   host.power_scale = lua_tonumber(L, -1);
104   lua_pop(L, 1);
105
106   //get power_trace
107   lua_pushstring(L, "power_trace");
108   lua_gettable(L, -2);
109   host.power_trace = tmgr_trace_new(lua_tostring(L, -1));
110   lua_pop(L, 1);
111
112   //get state initial
113   lua_pushstring(L, "state_initial");
114   lua_gettable(L, -2);
115   if(!lua_isnumber(L,-1)) state = 1;// Default value
116   else state = lua_tonumber(L, -1);
117   lua_pop(L, 1);
118
119   if (state)
120     host.initial_state = SURF_RESOURCE_ON;
121   else
122     host.initial_state = SURF_RESOURCE_OFF;
123
124   //get trace state
125   lua_pushstring(L, "state_trace");
126   lua_gettable(L, -2);
127   host.state_trace = tmgr_trace_new(lua_tostring(L, -1));
128   lua_pop(L, 1);
129
130   sg_platf_new_host(&host);
131
132   return 0;
133 }
134
135 int  console_add_link(lua_State *L) {
136   s_sg_platf_link_cbarg_t link;
137   memset(&link,0,sizeof(link));
138
139   const char* policy;
140
141   if (! lua_istable(L, -1)) {
142     XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
143     return -1;
144   }
145
146   // get Id Value
147   lua_pushstring(L, "id");
148   lua_gettable(L, -2);
149   link.id = lua_tostring(L, -1);
150   lua_pop(L, 1);
151
152   // get bandwidth value
153   lua_pushstring(L, "bandwidth");
154   lua_gettable(L, -2);
155   link.bandwidth = lua_tonumber(L, -1);
156   lua_pop(L, 1);
157
158   //get latency value
159   lua_pushstring(L, "latency");
160   lua_gettable(L, -2);
161   link.latency = lua_tonumber(L, -1);
162   lua_pop(L, 1);
163
164   /*Optional Arguments  */
165
166   //get bandwidth_trace value
167   lua_pushstring(L, "bandwidth_trace");
168   lua_gettable(L, -2);
169   link.bandwidth_trace = tmgr_trace_new(lua_tostring(L, -1));
170   lua_pop(L, 1);
171
172   //get latency_trace value
173   lua_pushstring(L, "latency_trace");
174   lua_gettable(L, -2);
175   link.latency_trace = tmgr_trace_new(lua_tostring(L, -1));
176   lua_pop(L, 1);
177
178   //get state_trace value
179   lua_pushstring(L, "state_trace");
180   lua_gettable(L, -2);
181   link.state_trace = tmgr_trace_new(lua_tostring(L, -1));
182   lua_pop(L, 1);
183
184   //get state_initial value
185   lua_pushstring(L, "state_initial");
186   lua_gettable(L, -2);
187   if (lua_tonumber(L, -1))
188     link.state = SURF_RESOURCE_ON;
189   else
190     link.state = SURF_RESOURCE_OFF;
191   lua_pop(L, 1);
192
193   //get policy value
194   lua_pushstring(L, "policy");
195   lua_gettable(L, -2);
196   policy = lua_tostring(L, -1);
197   lua_pop(L, 1);
198   if (policy && !strcmp(policy,"FULLDUPLEX")) {
199     link.policy = SURF_LINK_FULLDUPLEX;
200   } else if (policy && !strcmp(policy,"FATPIPE")) {
201     link.policy = SURF_LINK_FATPIPE;
202   } else {
203     link.policy = SURF_LINK_SHARED;
204   }
205
206   sg_platf_new_link(&link);
207
208   return 0;
209 }
210 /**
211  * add Router to AS components
212  */
213 int console_add_router(lua_State* L) {
214   s_sg_platf_router_cbarg_t router;
215   memset(&router,0,sizeof(router));
216
217   if (! lua_istable(L, -1)) {
218     XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
219     return -1;
220   }
221
222   lua_pushstring(L, "id");
223   lua_gettable(L, -2);
224   router.id = lua_tostring(L, -1);
225   lua_pop(L,1);
226
227   lua_pushstring(L,"coord");
228   lua_gettable(L,-2);
229   router.coord = lua_tostring(L, -1);
230   lua_pop(L,1);
231
232   sg_platf_new_router(&router);
233
234   return 0;
235 }
236
237 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
238
239 int console_add_route(lua_State *L) {
240   static int AX_ptr = 0;
241   static int surfxml_bufferstack_size = 2048;
242
243   /* allocating memory for the buffer, I think 2kB should be enough */
244   surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
245
246   const char*src;
247   const char*dst;
248   int is_symmetrical;
249   xbt_dynar_t links;
250   unsigned int cursor;
251   char *link_id;
252
253   if (! lua_istable(L, -1)) {
254     XBT_ERROR("Bad Arguments to create a route, Should be a table with named arguments");
255     return -1;
256   }
257
258   lua_pushstring(L,"src");
259   lua_gettable(L,-2);
260   src = lua_tostring(L, -1);
261   lua_pop(L,1);
262
263   lua_pushstring(L,"dest");
264   lua_gettable(L,-2);
265   dst = lua_tostring(L, -1);
266   lua_pop(L,1);
267
268   lua_pushstring(L,"links");
269   lua_gettable(L,-2);
270   links = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
271   if (xbt_dynar_is_empty(links))
272     xbt_dynar_push_as(links,char*,xbt_strdup(lua_tostring(L, -1)));
273   lua_pop(L,1);
274
275   lua_pushstring(L,"symmetrical");
276   lua_gettable(L,-2);
277   is_symmetrical = lua_tointeger(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   SURFXML_BUFFER_SET(route_src, src);
284   SURFXML_BUFFER_SET(route_dst, dst);
285   if (is_symmetrical)
286     A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_YES;
287   else
288     A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
289   SURFXML_START_TAG(route);
290
291   xbt_dynar_foreach(links,cursor,link_id) {
292     SURFXML_BUFFER_SET(link_ctn_id, link_id);
293     A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
294     SURFXML_START_TAG(link_ctn);
295     SURFXML_END_TAG(link_ctn);
296   }
297   SURFXML_END_TAG(route);
298
299   xbt_dynar_free(&links);
300   free(surfxml_bufferstack);
301
302   return 0;
303 }
304
305 int console_AS_open(lua_State *L) {
306  const char *id;
307  const char *mode;
308
309  if (! lua_istable(L, 1)) {
310    XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
311    return -1;
312  }
313
314  lua_pushstring(L, "id");
315  lua_gettable(L, -2);
316  id = lua_tostring(L, -1);
317  lua_pop(L, 1);
318
319  lua_pushstring(L, "mode");
320  lua_gettable(L, -2);
321  mode = lua_tostring(L, -1);
322  lua_pop(L, 1);
323
324  sg_platf_new_AS_begin(id,mode);
325
326  return 0;
327 }
328 int console_AS_close(lua_State *L) {
329   sg_platf_new_AS_end();
330   return 0;
331 }
332
333 int console_set_function(lua_State *L) {
334
335   const char *host_id ;
336   const char *function_id;
337   xbt_dynar_t args;
338
339   if (! lua_istable(L, 1)) {
340     XBT_ERROR("Bad Arguments to AS.new, Should be a table with named arguments");
341     return -1;
342   }
343
344   // get Host id
345   lua_pushstring(L, "host");
346   lua_gettable(L, -2);
347   host_id = lua_tostring(L, -1);
348   lua_pop(L, 1);
349
350   // get Function Name
351   lua_pushstring(L, "fct");
352   lua_gettable(L, -2);
353   function_id = lua_tostring(L, -1);
354   lua_pop(L, 1);
355
356   //get args
357   lua_pushstring(L,"args");
358   lua_gettable(L, -2);
359   args = xbt_str_split_quoted( lua_tostring(L,-1) );
360   lua_pop(L, 1);
361
362   // FIXME: hackish to go under MSG that way
363   m_host_t host = xbt_lib_get_or_null(host_lib,host_id,MSG_HOST_LEVEL);
364   if (!host) {
365     XBT_ERROR("no host '%s' found",host_id);
366     return -1;
367   }
368
369   MSG_set_function(host_id, function_id, args);
370
371   return 0;
372 }
373
374 int console_host_set_property(lua_State *L) {
375   const char* name ="";
376   const char* prop_id = "";
377   const char* prop_value = "";
378   if (!lua_istable(L, -1)) {
379     XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
380     return -1;
381   }
382
383
384   // get Host id
385   lua_pushstring(L, "host");
386   lua_gettable(L, -2);
387   name = lua_tostring(L, -1);
388   lua_pop(L, 1);
389
390   // get prop Name
391   lua_pushstring(L, "prop");
392   lua_gettable(L, -2);
393   prop_id = lua_tostring(L, -1);
394   lua_pop(L, 1);
395   //get args
396   lua_pushstring(L,"value");
397   lua_gettable(L, -2);
398   prop_value = lua_tostring(L,-1);
399   lua_pop(L, 1);
400
401   // FIXME: hackish to go under MSG that way
402   m_host_t host = xbt_lib_get_or_null(host_lib,name,MSG_HOST_LEVEL);
403   if (!host) {
404     XBT_ERROR("no host '%s' found",name);
405     return -1;
406   }
407   xbt_dict_t props = MSG_host_get_properties(host);
408   xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
409
410   return 0;
411 }
412
413 /**
414  * \brief Registers the platform functions into the table simgrid.platf.
415  * \param L a lua state
416  */
417 void sglua_register_platf_functions(lua_State* L)
418 {
419   luaL_openlib(L, PLATF_MODULE_NAME, platf_functions, 0);
420                                   /* simgrid.platf */
421   lua_pop(L, 1);
422 }
423