Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
79f81fbd77ff4704bcb173da3e4de00587c37f72
[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 "src/msg/msg_private.h"
18 #include "src/simix/smx_host_private.h"
19 #include "src/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   int type;
83
84   link.properties = NULL;
85
86   if (!lua_istable(L, -1)) {
87     XBT_ERROR
88         ("Bad Arguments to create backbone in Lua. Should be a table with named arguments.");
89     return -1;
90   }
91
92   lua_pushstring(L, "id");
93   type = lua_gettable(L, -2);
94   if (type != LUA_TSTRING) {
95     XBT_ERROR("Attribute 'id' must be specified for backbone and must be a string.");
96   }
97   link.id = lua_tostring(L, -1);
98   lua_pop(L, 1);
99
100   lua_pushstring(L, "bandwidth");
101   type = lua_gettable(L, -2);
102   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
103     XBT_ERROR("Attribute 'bandwidth' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
104   }
105   link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1));
106   lua_pop(L, 1);
107
108   lua_pushstring(L, "lat");
109   type = lua_gettable(L, -2);
110   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
111     XBT_ERROR("Attribute 'lat' must be specified for backbone and must either be a string (in the right format; see docs) or a number.");
112   }
113   link.latency = surf_parse_get_time(lua_tostring(L, -1));
114   lua_pop(L, 1);
115
116   link.state = SURF_RESOURCE_ON;
117
118   lua_pushstring(L, "sharing_policy");
119   type = lua_gettable(L, -2);
120   const char* policy = lua_tostring(L, -1);
121   if (policy && !strcmp(policy,"FULLDUPLEX")) {
122     link.policy = SURF_LINK_FULLDUPLEX;
123   } else if (policy && !strcmp(policy,"FATPIPE")) {
124     link.policy = SURF_LINK_FATPIPE;
125   } else {
126     link.policy = SURF_LINK_SHARED;
127   }
128
129   sg_platf_new_link(&link);
130   routing_cluster_add_backbone(sg_link_by_name(link.id));
131
132   return 0;
133 }
134
135 int console_add_host___link(lua_State *L) {
136   s_sg_platf_host_link_cbarg_t host_link;
137   memset(&host_link,0,sizeof(host_link));
138   int type;
139
140   // we get values from the table passed as argument
141   if (!lua_istable(L, -1)) {
142     XBT_ERROR
143         ("Bad Arguments to create host_link in Lua. Should be a table with named arguments.");
144     return -1;
145   }
146
147   lua_pushstring(L, "id");
148   type = lua_gettable(L, -2);
149   if (type != LUA_TSTRING) {
150     XBT_ERROR("Attribute 'id' must be specified for any host_link and must be a string.");
151   }
152   host_link.id = lua_tostring(L, -1);
153   lua_pop(L, 1);
154
155   lua_pushstring(L, "up");
156   type = lua_gettable(L, -2);
157   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
158     XBT_ERROR("Attribute 'up' must be specified for host_link and must either be a string or a number.");
159   }
160   host_link.link_up = lua_tostring(L, -1);
161   lua_pop(L, 1);
162
163   lua_pushstring(L, "down");
164   type = lua_gettable(L, -2);
165   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
166     XBT_ERROR("Attribute 'down' must be specified for host_link and must either be a string or a number.");
167   }
168   host_link.link_down = lua_tostring(L, -1);
169   lua_pop(L, 1);
170
171   XBT_DEBUG("Create a host_link for host %s", host_link.id);
172   sg_platf_new_host_link(&host_link);
173
174   return 0;
175 }
176
177 int console_add_host(lua_State *L) {
178   s_sg_platf_host_cbarg_t host;
179   memset(&host,0,sizeof(host));
180   int state, type;
181
182   // we get values from the table passed as argument
183   if (!lua_istable(L, -1)) {
184     XBT_ERROR
185         ("Bad Arguments to create host. Should be a table with named arguments");
186     return -1;
187   }
188
189   // get Id Value
190   lua_pushstring(L, "id");
191   type = lua_gettable(L, -2);
192   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
193     XBT_ERROR("Attribute 'id' must be specified for any host and must be a string.");
194   }
195   host.id = lua_tostring(L, -1);
196   lua_pop(L, 1);
197
198   // get power value
199   lua_pushstring(L, "power");
200   type = lua_gettable(L, -2);
201   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
202     XBT_ERROR("Attribute 'power' must be specified for host and must either be a string (in the correct format; check documentation) or a number.");
203   }
204   host.power_peak = xbt_dynar_new(sizeof(double), NULL);
205   xbt_dynar_push_as(host.power_peak, double, get_cpu_power(lua_tostring(L, -1)));
206   lua_pop(L, 1);
207
208   // get core
209   lua_pushstring(L, "core");
210   lua_gettable(L, -2);
211   if(!lua_isnumber(L,-1)) {
212       host.core_amount = 1;// Default value
213   }
214   else host.core_amount = lua_tonumber(L, -1);
215   if (host.core_amount == 0)
216     host.core_amount = 1;
217   lua_pop(L, 1);
218
219   //get power_scale
220   lua_pushstring(L, "availability");
221   lua_gettable(L, -2);
222   if(!lua_isnumber(L,-1)) host.power_scale = 1;// Default value
223   else host.power_scale = lua_tonumber(L, -1);
224   lua_pop(L, 1);
225
226   //get power_trace
227   lua_pushstring(L, "availability_file");
228   lua_gettable(L, -2);
229   host.power_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
230   lua_pop(L, 1);
231
232   //get state initial
233   lua_pushstring(L, "state");
234   lua_gettable(L, -2);
235   if(!lua_isnumber(L,-1)) state = 1;// Default value
236   else state = lua_tonumber(L, -1);
237   lua_pop(L, 1);
238
239   if (state)
240     host.initial_state = SURF_RESOURCE_ON;
241   else
242     host.initial_state = SURF_RESOURCE_OFF;
243
244   //get trace state
245   lua_pushstring(L, "state_file");
246   lua_gettable(L, -2);
247   host.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
248   lua_pop(L, 1);
249
250   sg_platf_new_host(&host);
251   xbt_dynar_free(&host.power_peak);
252
253   return 0;
254 }
255
256 int  console_add_link(lua_State *L) {
257   s_sg_platf_link_cbarg_t link;
258   memset(&link,0,sizeof(link));
259
260   int type;
261   const char* policy;
262
263   if (! lua_istable(L, -1)) {
264     XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
265     return -1;
266   }
267
268   // get Id Value
269   lua_pushstring(L, "id");
270   type = lua_gettable(L, -2);
271   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
272     XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
273   }
274   link.id = lua_tostring(L, -1);
275   lua_pop(L, 1);
276
277   // get bandwidth value
278   lua_pushstring(L, "bandwidth");
279   type = lua_gettable(L, -2);
280   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
281     XBT_ERROR("Attribute 'bandwidth' must be specified for any link and must either be either a string (in the right format; see docs) or a number.");
282   }
283   link.bandwidth = surf_parse_get_bandwidth(lua_tostring(L, -1));
284   lua_pop(L, 1);
285
286   //get latency value
287   lua_pushstring(L, "lat");
288   type = lua_gettable(L, -2);
289   if (type != LUA_TSTRING && type != LUA_TNUMBER) {
290     XBT_ERROR("Attribute 'lat' must be specified for any link and must either be a string (in the right format; see docs) or a number.");
291   }
292   link.latency = surf_parse_get_time(lua_tostring(L, -1));
293   lua_pop(L, 1);
294
295   /*Optional Arguments  */
296
297   //get bandwidth_trace value
298   lua_pushstring(L, "bandwidth_file");
299   lua_gettable(L, -2);
300   link.bandwidth_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
301   lua_pop(L, 1);
302
303   //get latency_trace value
304   lua_pushstring(L, "latency_file");
305   lua_gettable(L, -2);
306   link.latency_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
307   lua_pop(L, 1);
308
309   //get state_trace value
310   lua_pushstring(L, "state_file");
311   lua_gettable(L, -2);
312   link.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1));
313   lua_pop(L, 1);
314
315   //get state_initial value
316   lua_pushstring(L, "state");
317   lua_gettable(L, -2);
318   if (!lua_isnumber(L,-1) || lua_tonumber(L, -1))
319     link.state = SURF_RESOURCE_ON;
320   else
321     link.state = SURF_RESOURCE_OFF;
322   lua_pop(L, 1);
323
324   //get policy value
325   lua_pushstring(L, "sharing_policy");
326   lua_gettable(L, -2);
327   policy = lua_tostring(L, -1);
328   lua_pop(L, 1);
329   if (policy && !strcmp(policy,"FULLDUPLEX")) {
330     link.policy = SURF_LINK_FULLDUPLEX;
331   } else if (policy && !strcmp(policy,"FATPIPE")) {
332     link.policy = SURF_LINK_FATPIPE;
333   } else {
334     link.policy = SURF_LINK_SHARED;
335   }
336
337   sg_platf_new_link(&link);
338
339   return 0;
340 }
341 /**
342  * add Router to AS components
343  */
344 int console_add_router(lua_State* L) {
345   s_sg_platf_router_cbarg_t router;
346   memset(&router,0,sizeof(router));
347   int type;
348
349   if (! lua_istable(L, -1)) {
350     XBT_ERROR("Bad Arguments to create router, Should be a table with named arguments");
351     return -1;
352   }
353
354   lua_pushstring(L, "id");
355   type = lua_gettable(L, -2);
356   if (type != LUA_TSTRING) {
357     XBT_ERROR("Attribute 'id' must be specified for any link and must be a string.");
358   }
359   router.id = lua_tostring(L, -1);
360   lua_pop(L,1);
361
362   lua_pushstring(L,"coord");
363   lua_gettable(L,-2);
364   router.coord = lua_tostring(L, -1);
365   lua_pop(L,1);
366
367   sg_platf_new_router(&router);
368
369   return 0;
370 }
371
372 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
373
374 int console_add_route(lua_State *L) {
375   XBT_DEBUG("Adding route");
376   s_sg_platf_route_cbarg_t route;
377   memset(&route,0,sizeof(route));
378   int type;
379
380   /* allocating memory for the buffer, I think 2kB should be enough */
381   surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
382
383   if (! lua_istable(L, -1)) {
384     XBT_ERROR("Bad Arguments to create a route. Should be a table with named arguments");
385     return -1;
386   }
387
388   lua_pushstring(L,"src");
389   type = lua_gettable(L,-2);
390   if (type != LUA_TSTRING) {
391     XBT_ERROR("Attribute 'src' must be specified for any route and must be a string.");
392   }
393   route.src = lua_tostring(L, -1);
394   lua_pop(L,1);
395
396   lua_pushstring(L,"dest");
397   type = lua_gettable(L,-2);
398   if (type != LUA_TSTRING) {
399     XBT_ERROR("Attribute 'dest' must be specified for any route and must be a string.");
400   }
401   route.dst = lua_tostring(L, -1);
402   lua_pop(L,1);
403
404   lua_pushstring(L,"links");
405   type = lua_gettable(L,-2);
406   if (type != LUA_TSTRING) {
407     XBT_ERROR("Attribute 'links' must be specified for any route and must be a string (different links separated by commas or single spaces.");
408   }
409   route.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
410   if (xbt_dynar_is_empty(route.link_list))
411     xbt_dynar_push_as(route.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
412   lua_pop(L,1);
413
414   /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
415    * Et ouais mon pote. That's the way it goes. F34R.
416    *
417    * (Note that above this function, there is a #include statement. Is this
418    * comment related to that statement?)
419    */
420   lua_pushstring(L,"symmetrical");
421   lua_gettable(L,-2);
422   if (lua_isstring(L, -1)) {
423     const char* value = lua_tostring(L, -1);
424     if (strcmp("YES", value) == 0) {
425       route.symmetrical = TRUE;
426     }
427     else
428       route.symmetrical = FALSE;
429   }
430   else {
431     route.symmetrical = TRUE;
432   }
433   lua_pop(L,1);
434
435   route.gw_src = NULL;
436   route.gw_dst = NULL;
437
438   sg_platf_new_route(&route);
439
440   return 0;
441 }
442
443 int console_add_ASroute(lua_State *L) {
444   s_sg_platf_route_cbarg_t ASroute;
445   memset(&ASroute,0,sizeof(ASroute));
446
447   lua_pushstring(L, "src");
448   lua_gettable(L, -2);
449   ASroute.src = lua_tostring(L, -1);
450   lua_pop(L, 1);
451
452   lua_pushstring(L, "dst");
453   lua_gettable(L, -2);
454   ASroute.dst = lua_tostring(L, -1);
455   lua_pop(L, 1);
456
457   lua_pushstring(L, "gw_src");
458   lua_gettable(L, -2);
459   ASroute.gw_src = sg_routing_edge_by_name_or_null(lua_tostring(L, -1));
460   lua_pop(L, 1);
461
462   lua_pushstring(L, "gw_dst");
463   lua_gettable(L, -2);
464   ASroute.gw_dst = sg_routing_edge_by_name_or_null(lua_tostring(L, -1));
465   lua_pop(L, 1);
466
467   /*if (A_surfxml_ASroute_gw___src && !ASroute.gw_src)*/
468     /*surf_parse_error("gw_src=\"%s\" not found for ASroute from \"%s\" to \"%s\"",*/
469                      /*A_surfxml_ASroute_gw___src, ASroute.src, ASroute.dst);*/
470   /*if (A_surfxml_ASroute_gw___dst && !ASroute.gw_dst)*/
471     /*surf_parse_error("gw_dst=\"%s\" not found for ASroute from \"%s\" to \"%s\"",*/
472                      /*A_surfxml_ASroute_gw___dst, ASroute.src, ASroute.dst);*/
473
474   lua_pushstring(L,"links");
475   lua_gettable(L,-2);
476   ASroute.link_list = xbt_str_split(lua_tostring(L, -1), ", \t\r\n");
477   if (xbt_dynar_is_empty(ASroute.link_list))
478     xbt_dynar_push_as(ASroute.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
479   lua_pop(L,1);
480
481   lua_pushstring(L,"symmetrical");
482   lua_gettable(L,-2);
483   if (lua_isstring(L, -1)) {
484     const char* value = lua_tostring(L, -1);
485     if (strcmp("YES", value) == 0) {
486       ASroute.symmetrical = TRUE;
487     }
488     else
489       ASroute.symmetrical = FALSE;
490   }
491   else {
492     ASroute.symmetrical = TRUE;
493   }
494   lua_pop(L,1);
495
496   sg_platf_new_ASroute(&ASroute);
497
498   return 0;
499 }
500
501 int console_AS_open(lua_State *L) {
502  const char *id;
503  const char *mode;
504  int type;
505
506  XBT_DEBUG("Opening AS");
507
508  if (! lua_istable(L, 1)) {
509    XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
510    return -1;
511  }
512
513  lua_pushstring(L, "id");
514  type = lua_gettable(L, -2);
515   if (type != LUA_TSTRING) {
516     XBT_ERROR("Attribute 'id' must be specified for any AS and must be a string.");
517   }
518  id = lua_tostring(L, -1);
519  lua_pop(L, 1);
520
521  lua_pushstring(L, "mode");
522  lua_gettable(L, -2);
523  mode = lua_tostring(L, -1);
524  lua_pop(L, 1);
525
526  int mode_int = A_surfxml_AS_routing_None;
527  if(!strcmp(mode,"Full")) mode_int = A_surfxml_AS_routing_Full;
528  else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
529  else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
530  else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
531  else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
532  else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
533  else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
534  else xbt_die("Don't have the model name '%s'",mode);
535
536  s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
537  AS.id = id;
538  AS.routing = mode_int;
539
540  sg_platf_new_AS_begin(&AS);
541
542  return 0;
543 }
544 int console_AS_close(lua_State *L) {
545   XBT_DEBUG("Closing AS");
546   sg_platf_new_AS_end();
547   return 0;
548 }
549
550 int console_set_function(lua_State *L) {
551
552   const char *host_id ;
553   const char *function_id;
554   xbt_dynar_t args;
555
556   if (! lua_istable(L, 1)) {
557     XBT_ERROR("Bad Arguments to AS.new, Should be a table with named arguments");
558     return -1;
559   }
560
561   // get Host id
562   lua_pushstring(L, "host");
563   lua_gettable(L, -2);
564   host_id = lua_tostring(L, -1);
565   lua_pop(L, 1);
566
567   // get Function Name
568   lua_pushstring(L, "fct");
569   lua_gettable(L, -2);
570   function_id = lua_tostring(L, -1);
571   lua_pop(L, 1);
572
573   //get args
574   lua_pushstring(L,"args");
575   lua_gettable(L, -2);
576   args = xbt_str_split_str( lua_tostring(L,-1) , ",");
577   lua_pop(L, 1);
578
579   msg_host_t host = MSG_host_by_name(host_id);
580   if (!host) {
581     XBT_ERROR("no host '%s' found",host_id);
582     return -1;
583   }
584
585   // FIXME: use sg_platf_new_process directly (warning: find a way to check hostname)
586   MSG_set_function(host_id, function_id, args);
587
588   return 0;
589 }
590
591 int console_host_set_property(lua_State *L) {
592   const char* name ="";
593   const char* prop_id = "";
594   const char* prop_value = "";
595   if (!lua_istable(L, -1)) {
596     XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
597     return -1;
598   }
599
600
601   // get Host id
602   lua_pushstring(L, "host");
603   lua_gettable(L, -2);
604   name = lua_tostring(L, -1);
605   lua_pop(L, 1);
606
607   // get prop Name
608   lua_pushstring(L, "prop");
609   lua_gettable(L, -2);
610   prop_id = lua_tostring(L, -1);
611   lua_pop(L, 1);
612   //get args
613   lua_pushstring(L,"value");
614   lua_gettable(L, -2);
615   prop_value = lua_tostring(L,-1);
616   lua_pop(L, 1);
617
618   msg_host_t host = MSG_host_by_name(name);
619   if (!host) {
620     XBT_ERROR("no host '%s' found",name);
621     return -1;
622   }
623   xbt_dict_t props = MSG_host_get_properties(host);
624   xbt_dict_set(props,prop_id,xbt_strdup(prop_value),NULL);
625
626   return 0;
627 }
628
629 /**
630  * \brief Registers the platform functions into the table simgrid.platf.
631  * \param L a lua state
632  */
633 void sglua_register_platf_functions(lua_State* L)
634 {
635   lua_getglobal(L, "simgrid");     /* simgrid */
636   luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */
637   lua_setfield(L, -2, "platf");    /* simgrid */
638
639   lua_pop(L, 1);                   /* -- */
640 }
641