Logo AND Algorithmique Numérique Distribuée

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