Logo AND Algorithmique Numérique Distribuée

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