Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Lua: Changed latency attribute to lat and others.
[simgrid.git] / src / bindings / lua / lua_platf.c
1 /* Copyright (c) 2010, 2012-2015. 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     {"backbone_new", console_add_backbone},
35     {"host_link_new", console_add_host___link},
36     {"host_new", console_add_host},
37     {"link_new", console_add_link},
38     {"router_new", console_add_router},
39     {"route_new", console_add_route},
40     {"ASroute_new", console_add_ASroute},
41     {NULL, NULL}
42 };
43
44 int console_open(lua_State *L) {
45   sg_platf_init();
46   sg_platf_begin();
47
48   storage_register_callbacks();
49   routing_register_callbacks();
50
51   gpu_register_callbacks();
52
53   return 0;
54 }
55
56 int console_close(lua_State *L) {
57   sg_platf_end();
58   sg_platf_exit();
59
60   xbt_lib_cursor_t cursor;
61   void **data;
62   char *name;
63
64   /* Initialize MSG and WKS hosts */
65   XBT_DEBUG("Initialize MSG and WKS hosts");
66   xbt_lib_foreach(host_lib, cursor, name, data) {
67     if(data[SURF_HOST_LEVEL]){
68       XBT_DEBUG("\tSee surf host %s",name);
69       SIMIX_host_create(name);
70       // THIS IS BRAINDEAD. There is no sg_host_t in that level, but a smx_host_priv. So commenting out for now.
71       // Lua is broken anyway. Christian will fix it
72       // __MSG_host_create((sg_host_t)data[SIMIX_HOST_LEVEL]);
73     }
74   }
75
76   return 0;
77 }
78
79 int console_add_backbone(lua_State *L) {
80   s_sg_platf_link_cbarg_t link;
81   memset(&link,0,sizeof(link));
82
83   link.properties = NULL;
84
85   lua_pushstring(L, "id");
86   lua_gettable(L, -2);
87   link.id = lua_tostring(L, -1);
88   lua_pop(L, 1);
89
90   lua_pushstring(L, "bandwidth");
91   lua_gettable(L, -2);
92   link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1));
93   lua_pop(L, 1);
94
95   lua_pushstring(L, "lat");
96   lua_gettable(L, -2);
97   link.latency = surf_parse_get_time(lua_tostring(L, -1));
98   lua_pop(L, 1);
99
100   link.state = SURF_RESOURCE_ON;
101
102   lua_pushstring(L, "sharing_policy");
103   type = lua_gettable(L, -2);
104   char* policy = lua_tostring(L, -1);
105   if (policy && !strcmp(policy,"FULLDUPLEX")) {
106     link.policy = SURF_LINK_FULLDUPLEX;
107   } else if (policy && !strcmp(policy,"FATPIPE")) {
108     link.policy = SURF_LINK_FATPIPE;
109   } else {
110     link.policy = SURF_LINK_SHARED;
111   }
112
113   sg_platf_new_link(&link);
114   routing_cluster_add_backbone(xbt_lib_get_or_null(link_lib, link.id, SURF_LINK_LEVEL));
115
116   return 0;
117 }
118
119 int console_add_host___link(lua_State *L) {
120   s_sg_platf_host_link_cbarg_t host_link;
121   memset(&host_link,0,sizeof(host_link));
122
123   // we get values from the table passed as argument
124   if (!lua_istable(L, -1)) {
125     XBT_ERROR
126         ("Bad Arguments to create host, Should be a table with named arguments");
127     return -1;
128   }
129
130   lua_pushstring(L, "id");
131   lua_gettable(L, -2);
132   host_link.id = lua_tostring(L, -1);
133   lua_pop(L, 1);
134
135   lua_pushstring(L, "up");
136   lua_gettable(L, -2);
137   host_link.link_up = lua_tostring(L, -1);
138   lua_pop(L, 1);
139
140   lua_pushstring(L, "down");
141   lua_gettable(L, -2);
142   host_link.link_down = lua_tostring(L, -1);
143   lua_pop(L, 1);
144
145   XBT_DEBUG("Create a host_link for host %s", host_link.id);
146   sg_platf_new_host_link(&host_link);
147
148   return 0;
149 }
150
151 int console_add_host(lua_State *L) {
152   s_sg_platf_host_cbarg_t host;
153   memset(&host,0,sizeof(host));
154   int state;
155
156   // we get values from the table passed as argument
157   if (!lua_istable(L, -1)) {
158     XBT_ERROR
159         ("Bad Arguments to create host, Should be a table with named arguments");
160     return -1;
161   }
162
163   // get Id Value
164   lua_pushstring(L, "id");
165   lua_gettable(L, -2);
166   host.id = lua_tostring(L, -1);
167   lua_pop(L, 1);
168
169   // get power value
170   lua_pushstring(L, "power");
171   lua_gettable(L, -2);
172   host.power_peak = xbt_dynar_new(sizeof(double), NULL);
173   xbt_dynar_push_as(host.power_peak, double, get_cpu_power(lua_tostring(L, -1)));
174   lua_pop(L, 1);
175
176   // get core
177   lua_pushstring(L, "core");
178   lua_gettable(L, -2);
179   if(!lua_isnumber(L,-1)) host.core_amount = 1;// Default value
180   else host.core_amount = lua_tonumber(L, -1);
181   if (host.core_amount == 0)
182     host.core_amount = 1;
183   lua_pop(L, 1);
184
185   //get power_scale
186   lua_pushstring(L, "availability");
187   lua_gettable(L, -2);
188   if(!lua_isnumber(L,-1)) host.power_scale = 1;// Default value
189   else host.power_scale = lua_tonumber(L, -1);
190   lua_pop(L, 1);
191
192   //get power_trace
193   lua_pushstring(L, "availability_file");
194   lua_gettable(L, -2);
195   host.power_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
196   lua_pop(L, 1);
197
198   //get state initial
199   lua_pushstring(L, "state");
200   lua_gettable(L, -2);
201   if(!lua_isnumber(L,-1)) state = 1;// Default value
202   else state = lua_tonumber(L, -1);
203   lua_pop(L, 1);
204
205   if (state)
206     host.initial_state = SURF_RESOURCE_ON;
207   else
208     host.initial_state = SURF_RESOURCE_OFF;
209
210   //get trace state
211   lua_pushstring(L, "state_file");
212   lua_gettable(L, -2);
213   host.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
214   lua_pop(L, 1);
215
216   sg_platf_new_host(&host);
217
218   return 0;
219 }
220
221 int  console_add_link(lua_State *L) {
222   s_sg_platf_link_cbarg_t link;
223   memset(&link,0,sizeof(link));
224
225   const char* policy;
226
227   if (! lua_istable(L, -1)) {
228     XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
229     return -1;
230   }
231
232   // get Id Value
233   lua_pushstring(L, "id");
234   lua_gettable(L, -2);
235   link.id = lua_tostring(L, -1);
236   lua_pop(L, 1);
237
238   // get bandwidth value
239   lua_pushstring(L, "bandwidth");
240   lua_gettable(L, -2);
241   link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1));
242   lua_pop(L, 1);
243
244   //get latency value
245   lua_pushstring(L, "lat");
246   lua_gettable(L, -2);
247   link.latency = surf_parse_get_time(lua_tostring(L, -1));
248   lua_pop(L, 1);
249
250   /*Optional Arguments  */
251
252   //get bandwidth_trace value
253   lua_pushstring(L, "bandwidth_file");
254   lua_gettable(L, -2);
255   link.bandwidth_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
256   lua_pop(L, 1);
257
258   //get latency_trace value
259   lua_pushstring(L, "latency_file");
260   lua_gettable(L, -2);
261   link.latency_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
262   lua_pop(L, 1);
263
264   //get state_trace value
265   lua_pushstring(L, "state_file");
266   lua_gettable(L, -2);
267   link.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
268   lua_pop(L, 1);
269
270   //get state_initial value
271   lua_pushstring(L, "state");
272   lua_gettable(L, -2);
273   if (!lua_isnumber(L,-1) || lua_tonumber(L, -1))
274     link.state = SURF_RESOURCE_ON;
275   else
276     link.state = SURF_RESOURCE_OFF;
277   lua_pop(L, 1);
278
279   //get policy value
280   lua_pushstring(L, "sharing_policy");
281   lua_gettable(L, -2);
282   policy = lua_tostring(L, -1);
283   lua_pop(L, 1);
284   if (policy && !strcmp(policy,"FULLDUPLEX")) {
285     link.policy = SURF_LINK_FULLDUPLEX;
286   } else if (policy && !strcmp(policy,"FATPIPE")) {
287     link.policy = SURF_LINK_FATPIPE;
288   } else {
289     link.policy = SURF_LINK_SHARED;
290   }
291
292   sg_platf_new_link(&link);
293
294   return 0;
295 }
296 /**
297  * add Router to AS components
298  */
299 int console_add_router(lua_State* L) {
300   s_sg_platf_router_cbarg_t router;
301   memset(&router,0,sizeof(router));
302
303   if (! lua_istable(L, -1)) {
304     XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
305     return -1;
306   }
307
308   lua_pushstring(L, "id");
309   lua_gettable(L, -2);
310   router.id = lua_tostring(L, -1);
311   lua_pop(L,1);
312
313   lua_pushstring(L,"coord");
314   lua_gettable(L,-2);
315   router.coord = lua_tostring(L, -1);
316   lua_pop(L,1);
317
318   sg_platf_new_router(&router);
319
320   return 0;
321 }
322
323 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
324
325 int console_add_route(lua_State *L) {
326   XBT_DEBUG("Adding route");
327   s_sg_platf_route_cbarg_t route;
328   memset(&route,0,sizeof(route));
329
330   /* allocating memory for the buffer, I think 2kB should be enough */
331   surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
332
333   if (! lua_istable(L, -1)) {
334     XBT_ERROR("Bad Arguments to create a route, Should be a table with named arguments");
335     return -1;
336   }
337
338   lua_pushstring(L,"src");
339   lua_gettable(L,-2);
340   route.src = lua_tostring(L, -1);
341   lua_pop(L,1);
342
343   lua_pushstring(L,"dest");
344   lua_gettable(L,-2);
345   route.dst = lua_tostring(L, -1);
346   lua_pop(L,1);
347
348   lua_pushstring(L,"links");
349   lua_gettable(L,-2);
350   route.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
351   if (xbt_dynar_is_empty(route.link_list))
352     xbt_dynar_push_as(route.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
353   lua_pop(L,1);
354
355   /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
356    * Et ouais mon pote. That's the way it goes. F34R.
357    *
358    * (Note that above this function, there is a #include statement. Is this
359    * comment related to that statement?)
360    */
361   lua_pushstring(L,"symmetrical");
362   lua_gettable(L,-2);
363   if (lua_isstring(L, -1)) {
364     const char* value = lua_tostring(L, -1);
365     if (strcmp("YES", value) == 0) {
366       route.symmetrical = TRUE;
367     }
368     else
369       route.symmetrical = FALSE;
370   }
371   else {
372     route.symmetrical = TRUE;
373   }
374   lua_pop(L,1);
375
376   route.gw_src = NULL;
377   route.gw_dst = NULL;
378
379   sg_platf_new_route(&route);
380
381   return 0;
382 }
383
384 int console_add_ASroute(lua_State *L) {
385   s_sg_platf_route_cbarg_t ASroute;
386   memset(&ASroute,0,sizeof(ASroute));
387
388   lua_pushstring(L, "src");
389   lua_gettable(L, -2);
390   ASroute.src = lua_tostring(L, -1);
391   lua_pop(L, 1);
392
393   lua_pushstring(L, "dst");
394   lua_gettable(L, -2);
395   ASroute.dst = lua_tostring(L, -1);
396   lua_pop(L, 1);
397
398   lua_pushstring(L, "gw_src");
399   lua_gettable(L, -2);
400   ASroute.gw_src = sg_routing_edge_by_name_or_null(lua_tostring(L, -1));
401   lua_pop(L, 1);
402
403   lua_pushstring(L, "gw_dst");
404   lua_gettable(L, -2);
405   ASroute.gw_dst = sg_routing_edge_by_name_or_null(lua_tostring(L, -1));
406   lua_pop(L, 1);
407
408   /*if (A_surfxml_ASroute_gw___src && !ASroute.gw_src)*/
409     /*surf_parse_error("gw_src=\"%s\" not found for ASroute from \"%s\" to \"%s\"",*/
410                      /*A_surfxml_ASroute_gw___src, ASroute.src, ASroute.dst);*/
411   /*if (A_surfxml_ASroute_gw___dst && !ASroute.gw_dst)*/
412     /*surf_parse_error("gw_dst=\"%s\" not found for ASroute from \"%s\" to \"%s\"",*/
413                      /*A_surfxml_ASroute_gw___dst, ASroute.src, ASroute.dst);*/
414
415   lua_pushstring(L,"links");
416   lua_gettable(L,-2);
417   ASroute.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
418   if (xbt_dynar_is_empty(ASroute.link_list))
419     xbt_dynar_push_as(ASroute.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
420   lua_pop(L,1);
421
422   lua_pushstring(L,"symmetrical");
423   lua_gettable(L,-2);
424   if (lua_isstring(L, -1)) {
425     const char* value = lua_tostring(L, -1);
426     if (strcmp("YES", value) == 0) {
427       ASroute.symmetrical = TRUE;
428     }
429     else
430       ASroute.symmetrical = FALSE;
431   }
432   else {
433     ASroute.symmetrical = TRUE;
434   }
435   lua_pop(L,1);
436
437   sg_platf_new_ASroute(&ASroute);
438
439   return 0;
440 }
441
442 int console_AS_open(lua_State *L) {
443  const char *id;
444  const char *mode;
445
446  XBT_DEBUG("Opening AS");
447
448  if (! lua_istable(L, 1)) {
449    XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
450    return -1;
451  }
452
453  lua_pushstring(L, "id");
454  lua_gettable(L, -2);
455  id = lua_tostring(L, -1);
456  lua_pop(L, 1);
457
458  lua_pushstring(L, "mode");
459  lua_gettable(L, -2);
460  mode = lua_tostring(L, -1);
461  lua_pop(L, 1);
462
463  int mode_int = A_surfxml_AS_routing_None;
464  if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
465  else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
466  else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
467  else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
468  else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
469  else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
470  else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
471  else xbt_die("Don't have the model name '%s'",mode);
472
473  s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
474  AS.id = id;
475  AS.routing = mode_int;
476
477  sg_platf_new_AS_begin(&AS);
478
479  return 0;
480 }
481 int console_AS_close(lua_State *L) {
482   XBT_DEBUG("Closing AS");
483   sg_platf_new_AS_end();
484   return 0;
485 }
486
487 int console_set_function(lua_State *L) {
488
489   const char *host_id ;
490   const char *function_id;
491   xbt_dynar_t args;
492
493   if (! lua_istable(L, 1)) {
494     XBT_ERROR("Bad Arguments to AS.new, Should be a table with named arguments");
495     return -1;
496   }
497
498   // get Host id
499   lua_pushstring(L, "host");
500   lua_gettable(L, -2);
501   host_id = lua_tostring(L, -1);
502   lua_pop(L, 1);
503
504   // get Function Name
505   lua_pushstring(L, "fct");
506   lua_gettable(L, -2);
507   function_id = lua_tostring(L, -1);
508   lua_pop(L, 1);
509
510   //get args
511   lua_pushstring(L,"args");
512   lua_gettable(L, -2);
513   args = xbt_str_split_str( lua_tostring(L,-1) , ",");
514   lua_pop(L, 1);
515
516   msg_host_t host = MSG_host_by_name(host_id);
517   if (!host) {
518     XBT_ERROR("no host '%s' found",host_id);
519     return -1;
520   }
521
522   // FIXME: use sg_platf_new_process directly (warning: find a way to check hostname)
523   MSG_set_function(host_id, function_id, args);
524
525   return 0;
526 }
527
528 int console_host_set_property(lua_State *L) {
529   const char* name ="";
530   const char* prop_id = "";
531   const char* prop_value = "";
532   if (!lua_istable(L, -1)) {
533     XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
534     return -1;
535   }
536
537
538   // get Host id
539   lua_pushstring(L, "host");
540   lua_gettable(L, -2);
541   name = lua_tostring(L, -1);
542   lua_pop(L, 1);
543
544   // get prop Name
545   lua_pushstring(L, "prop");
546   lua_gettable(L, -2);
547   prop_id = lua_tostring(L, -1);
548   lua_pop(L, 1);
549   //get args
550   lua_pushstring(L,"value");
551   lua_gettable(L, -2);
552   prop_value = lua_tostring(L,-1);
553   lua_pop(L, 1);
554
555   msg_host_t host = MSG_host_by_name(name);
556   if (!host) {
557     XBT_ERROR("no host '%s' found",name);
558     return -1;
559   }
560   xbt_dict_t props = MSG_host_get_properties(host);
561   xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
562
563   return 0;
564 }
565
566 /**
567  * \brief Registers the platform functions into the table simgrid.platf.
568  * \param L a lua state
569  */
570 void sglua_register_platf_functions(lua_State* L)
571 {
572   lua_getglobal(L, "simgrid");     /* simgrid */
573   luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
574   lua_setfield(L, -2, "platf");    /* simgrid */
575
576   lua_pop(L, 1);                   /* -- */
577 }
578