Logo AND Algorithmique Numérique Distribuée

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