Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reimplement the lua console on top of sg_platf
[simgrid.git] / src / bindings / lua / lua_console.c
1 /* SimGrid Lua Console                                                    */
2
3 /* Copyright (c) 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "simgrid_lua.h"
10 #include "simgrid/platf.h"
11 #include <string.h>
12 #include <ctype.h>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_console, bindings, "Lua Bindings");
15
16 int console_open(lua_State *L) {
17   sg_platf_init();
18   sg_platf_open();
19   surf_parse_init_callbacks();
20   return 0;
21 }
22
23 int console_close(lua_State *L) {
24   sg_platf_close();
25   sg_platf_exit();
26   return 0;
27 }
28
29 int console_add_host(lua_State *L) {
30   s_sg_platf_host_cbarg_t host;
31   memset(&host,0,sizeof(host));
32   int state;
33
34   // we get values from the table passed as argument
35   if (!lua_istable(L, -1)) {
36     XBT_ERROR
37         ("Bad Arguments to create host, Should be a table with named arguments");
38     return -1;
39   }
40
41   // get Id Value
42   lua_pushstring(L, "id");
43   lua_gettable(L, -2);
44   host.V_host_id = lua_tostring(L, -1);
45   lua_pop(L, 1);
46
47   // get power value
48   lua_pushstring(L, "power");
49   lua_gettable(L, -2);
50   host.V_host_power_peak = lua_tonumber(L, -1);
51   lua_pop(L, 1);
52
53   //get power_scale
54   lua_pushstring(L, "power_scale");
55   lua_gettable(L, -2);
56   host.V_host_power_scale = lua_tonumber(L, -1);
57   lua_pop(L, 1);
58
59   //get power_trace
60   lua_pushstring(L, "power_trace");
61   lua_gettable(L, -2);
62   host.V_host_power_trace = tmgr_trace_new(lua_tostring(L, -1));
63   lua_pop(L, 1);
64
65   lua_pushstring(L, "core");
66   lua_gettable(L, -2);
67   host.V_host_core = lua_tonumber(L, -1);
68   if (host.V_host_core == 0)
69     host.V_host_core = 1;
70   lua_pop(L, 1);
71
72   //get state initial
73   lua_pushstring(L, "state_initial");
74   lua_gettable(L, -2);
75   state = lua_tonumber(L, -1);
76   lua_pop(L, 1);
77   if (state)
78     host.V_host_state_initial = SURF_RESOURCE_ON;
79   else
80     host.V_host_state_initial = SURF_RESOURCE_OFF;
81
82   //get trace state
83   lua_pushstring(L, "state_trace");
84   lua_gettable(L, -2);
85   host.V_host_state_trace = tmgr_trace_new(lua_tostring(L, -1));
86   lua_pop(L, 1);
87
88   sg_platf_new_host(&host);
89
90   return 0;
91 }
92
93 int  console_add_link(lua_State *L) {
94   s_sg_platf_link_cbarg_t link;
95   memset(&link,0,sizeof(link));
96
97   const char* policy;
98
99   if (! lua_istable(L, -1)) {
100     XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
101     return -1;
102   }
103
104   // get Id Value
105   lua_pushstring(L, "id");
106   lua_gettable(L, -2);
107   link.V_link_id = lua_tostring(L, -1);
108   lua_pop(L, 1);
109
110   // get bandwidth value
111   lua_pushstring(L, "bandwidth");
112   lua_gettable(L, -2);
113   link.V_link_bandwidth = lua_tonumber(L, -1);
114   lua_pop(L, 1);
115
116   //get latency value
117   lua_pushstring(L, "latency");
118   lua_gettable(L, -2);
119   link.V_link_latency = lua_tonumber(L, -1);
120   lua_pop(L, 1);
121
122   /*Optional Arguments  */
123
124   //get bandwidth_trace value
125   lua_pushstring(L, "bandwidth_trace");
126   lua_gettable(L, -2);
127   link.V_link_bandwidth_file = tmgr_trace_new(lua_tostring(L, -1));
128   lua_pop(L, 1);
129
130   //get latency_trace value
131   lua_pushstring(L, "latency_trace");
132   lua_gettable(L, -2);
133   link.V_link_latency_file = tmgr_trace_new(lua_tostring(L, -1));
134   lua_pop(L, 1);
135
136   //get state_trace value
137   lua_pushstring(L, "state_trace");
138   lua_gettable(L, -2);
139   link.V_link_state_file = tmgr_trace_new(lua_tostring(L, -1));
140   lua_pop(L, 1);
141
142   //get state_initial value
143   lua_pushstring(L, "state_initial");
144   lua_gettable(L, -2);
145   if (lua_tonumber(L, -1))
146     link.V_link_state = SURF_RESOURCE_ON;
147   else
148     link.V_link_state = SURF_RESOURCE_OFF;
149   lua_pop(L, 1);
150
151   //get policy value
152   lua_pushstring(L, "policy");
153   lua_gettable(L, -2);
154   policy = lua_tostring(L, -1);
155   lua_pop(L, 1);
156   if (policy && !strcmp(policy,"FULLDUPLEX")) {
157     link.V_link_sharing_policy = SURF_LINK_FULLDUPLEX;
158   } else if (policy && !strcmp(policy,"FATPIPE")) {
159     link.V_link_sharing_policy = SURF_LINK_FATPIPE;
160   } else {
161     link.V_link_sharing_policy = SURF_LINK_SHARED;
162   }
163
164   sg_platf_new_link(&link);
165
166   return 0;
167 }
168 /**
169  * add Router to AS components
170  */
171 int console_add_router(lua_State* L) {
172   s_sg_platf_router_cbarg_t router;
173   memset(&router,0,sizeof(router));
174
175   if (! lua_istable(L, -1)) {
176     XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
177     return -1;
178   }
179
180   lua_pushstring(L, "id");
181   lua_gettable(L, -2);
182   router.V_router_id = lua_tostring(L, -1);
183   lua_pop(L,1);
184
185   lua_pushstring(L,"coord");
186   lua_gettable(L,-2);
187   router.V_router_coord = lua_tostring(L, -1);
188   lua_pop(L,1);
189
190   sg_platf_new_router(&router);
191
192   return 0;
193 }
194
195 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
196
197 int console_add_route(lua_State *L) {
198   static int AX_ptr = 0;
199   static int surfxml_bufferstack_size = 2048;
200
201   /* allocating memory for the buffer, I think 2kB should be enough */
202   surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
203
204   const char*src;
205   const char*dst;
206   int is_symmetrical;
207   xbt_dynar_t links;
208   unsigned int cursor;
209   char *link_id;
210
211   if (! lua_istable(L, -1)) {
212     XBT_ERROR("Bad Arguments to create a route, Should be a table with named arguments");
213     return -1;
214   }
215
216   lua_pushstring(L,"src");
217   lua_gettable(L,-2);
218   src = lua_tostring(L, -1);
219   lua_pop(L,1);
220
221   lua_pushstring(L,"dest");
222   lua_gettable(L,-2);
223   dst = lua_tostring(L, -1);
224   lua_pop(L,1);
225
226   lua_pushstring(L,"links");
227   lua_gettable(L,-2);
228   links = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
229   if (xbt_dynar_length(links)==0)
230     xbt_dynar_push_as(links,char*,xbt_strdup(lua_tostring(L, -1)));
231   lua_pop(L,1);
232
233   lua_pushstring(L,"symmetrical");
234   lua_gettable(L,-2);
235   is_symmetrical = lua_tointeger(L, -1);
236   lua_pop(L,1);
237
238   /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
239    * Et ouais mon pote. That's the way it goes. F34R.
240    */
241   SURFXML_BUFFER_SET(route_src, src);
242   SURFXML_BUFFER_SET(route_dst, dst);
243   if (is_symmetrical)
244     A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_YES;
245   else
246     A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
247   SURFXML_START_TAG(route);
248
249   xbt_dynar_foreach(links,cursor,link_id) {
250     SURFXML_BUFFER_SET(link_ctn_id, link_id);
251     A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
252     SURFXML_START_TAG(link_ctn);
253     SURFXML_END_TAG(link_ctn);
254   }
255   SURFXML_END_TAG(route);
256
257   xbt_dynar_free(&links);
258   free(surfxml_bufferstack);
259
260   return 0;
261 }
262
263 int console_AS_open(lua_State *L) {
264  const char *id;
265  const char *mode;
266
267  if (! lua_istable(L, 1)) {
268    XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
269    return -1;
270  }
271
272  lua_pushstring(L, "id");
273  lua_gettable(L, -2);
274  id = lua_tostring(L, -1);
275  lua_pop(L, 1);
276
277  lua_pushstring(L, "mode");
278  lua_gettable(L, -2);
279  mode = lua_tostring(L, -1);
280  lua_pop(L, 1);
281
282  sg_platf_new_AS_open(id,mode);
283
284  return 0;
285 }
286 int console_AS_close(lua_State *L) {
287   sg_platf_new_AS_close();
288   return 0;
289 }
290
291 int console_set_function(lua_State *L) {
292
293   const char *host_id ;
294   const char *function_id;
295   xbt_dynar_t args;
296
297   if (! lua_istable(L, 1)) {
298     XBT_ERROR("Bad Arguments to AS.new, Should be a table with named arguments");
299     return -1;
300   }
301
302   // get Host id
303   lua_pushstring(L, "host");
304   lua_gettable(L, -2);
305   host_id = lua_tostring(L, -1);
306   lua_pop(L, 1);
307
308   // get Function Name
309   lua_pushstring(L, "fct");
310   lua_gettable(L, -2);
311   function_id = lua_tostring(L, -1);
312   lua_pop(L, 1);
313
314   //get args
315   lua_pushstring(L,"args");
316   lua_gettable(L, -2);
317   args = xbt_str_split_quoted( lua_tostring(L,-1) );
318   lua_pop(L, 1);
319
320   // FIXME: hackish to go under MSG that way
321   m_host_t host = xbt_lib_get_or_null(host_lib,host_id,MSG_HOST_LEVEL);
322   if (!host) {
323     XBT_ERROR("no host '%s' found",host_id);
324     return -1;
325   }
326
327   MSG_set_function(host_id, function_id, args);
328
329   return 0;
330 }
331
332 int console_host_set_property(lua_State *L) {
333   const char* name ="";
334   const char* prop_id = "";
335   const char* prop_value = "";
336   if (!lua_istable(L, -1)) {
337     XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
338     return -1;
339   }
340
341
342   // get Host id
343   lua_pushstring(L, "host");
344   lua_gettable(L, -2);
345   name = lua_tostring(L, -1);
346   lua_pop(L, 1);
347
348   // get prop Name
349   lua_pushstring(L, "prop");
350   lua_gettable(L, -2);
351   prop_id = lua_tostring(L, -1);
352   lua_pop(L, 1);
353   //get args
354   lua_pushstring(L,"value");
355   lua_gettable(L, -2);
356   prop_value = lua_tostring(L,-1);
357   lua_pop(L, 1);
358
359   // FIXME: hackish to go under MSG that way
360   m_host_t host = xbt_lib_get_or_null(host_lib,name,MSG_HOST_LEVEL);
361   if (!host) {
362     XBT_ERROR("no host '%s' found",name);
363     return -1;
364   }
365   xbt_dict_t props = MSG_host_get_properties(host);
366   xbt_dict_set(props,prop_id,xbt_strdup(prop_value),free);
367
368   return 0;
369 }
370