Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b086e4bbbf227f0af4939bfe8ae076051f06f0d2
[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
17 //AS List
18 static xbt_dynar_t as_list_d;
19
20 /**
21  * create host resource via CPU model [for MSG]
22  */
23
24 static void create_host(const char *id, double power_peak, double power_sc,
25                         const char *power_tr,int core,int state_init,
26                         const char *state_tr,xbt_dict_t properties)
27 {
28   double power_scale = 1.0;
29   int core_nb = 1; //default value
30   tmgr_trace_t power_trace = NULL;
31   e_surf_resource_state_t state_initial;
32   tmgr_trace_t state_trace;
33   if (power_sc)                 // !=0
34     power_scale = power_sc;
35   if (core)
36           core_nb = core; //default value
37   if (state_init == -1)
38     state_initial = SURF_RESOURCE_OFF;
39   else
40     state_initial = SURF_RESOURCE_ON;
41   if (power_tr)
42     power_trace = tmgr_trace_new(power_tr);
43   else
44     power_trace = tmgr_trace_new("");
45   if (state_tr)
46     state_trace = tmgr_trace_new(state_tr);
47   else
48     state_trace = tmgr_trace_new("");
49
50   surf_host_create_resource(xbt_strdup(id), power_peak, power_scale,
51                             power_trace, core_nb, state_initial, state_trace,
52                             properties);
53 }
54
55 /**
56  * create link resource via network model
57  */
58 static void create_link(const char *name,
59                         double bw_initial, const char *trace,
60                         double lat_initial, const char *latency_trace,
61                         int state_init, const char *state_trace,
62                         int policy)
63 {
64   tmgr_trace_t bw_trace;
65   tmgr_trace_t lat_trace;
66   e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
67   e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
68   tmgr_trace_t st_trace;
69   if (trace)
70     bw_trace = tmgr_trace_new(trace);
71   else
72     bw_trace = tmgr_trace_new("");
73
74   if (latency_trace)
75     lat_trace = tmgr_trace_new(latency_trace);
76   else
77     lat_trace = tmgr_trace_new("");
78
79   if (state_trace)
80     st_trace = tmgr_trace_new(state_trace);
81   else
82     st_trace = tmgr_trace_new("");
83
84   if (state_init == -1)
85     state_initial_link = SURF_RESOURCE_OFF;
86   if (policy == -1)
87     policy_initial_link = SURF_LINK_FATPIPE;
88
89   surf_link_create_resource(xbt_strdup(name), bw_initial, bw_trace,
90                             lat_initial, lat_trace, state_initial_link,
91                             st_trace, policy_initial_link, xbt_dict_new());
92 }
93
94 /*
95  *create host resource via workstation_ptask_L07 model [for SimDag]
96  */
97 static void create_host_wsL07(const char *id, double power_peak,
98                               double power_sc, const char *power_tr,
99                               int state_init, const char *state_tr)
100 {
101   double power_scale = 1.0;
102   tmgr_trace_t power_trace = NULL;
103   e_surf_resource_state_t state_initial;
104   tmgr_trace_t state_trace;
105   if (power_sc)                 // !=0
106     power_scale = power_sc;
107   if (state_init == -1)
108     state_initial = SURF_RESOURCE_OFF;
109   else
110     state_initial = SURF_RESOURCE_ON;
111   if (power_tr)
112     power_trace = tmgr_trace_new(power_tr);
113   else
114     power_trace = tmgr_trace_new("");
115   if (state_tr)
116     state_trace = tmgr_trace_new(state_tr);
117   else
118     state_trace = tmgr_trace_new("");
119
120   surf_wsL07_host_create_resource(xbt_strdup(id), power_peak, power_scale,
121                                   power_trace, state_initial, state_trace,
122                                   current_property_set);
123   current_property_set = NULL;
124 }
125
126 /**
127  * create link resource via workstation_ptask_L07 model [for SimDag]
128  */
129
130 static void create_link_wsL07(const char *name,
131                               double bw_initial, const char *trace,
132                               double lat_initial,
133                               const char *latency_trace, int state_init,
134                               const char *state_trace, int policy)
135 {
136   tmgr_trace_t bw_trace;
137   tmgr_trace_t lat_trace;
138   e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
139   e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
140   tmgr_trace_t st_trace;
141   if (trace)
142     bw_trace = tmgr_trace_new(trace);
143   else
144     bw_trace = tmgr_trace_new("");
145
146   if (latency_trace)
147     lat_trace = tmgr_trace_new(latency_trace);
148   else
149     lat_trace = tmgr_trace_new("");
150
151   if (state_trace)
152     st_trace = tmgr_trace_new(state_trace);
153   else
154     st_trace = tmgr_trace_new("");
155
156   if (state_init == -1)
157     state_initial_link = SURF_RESOURCE_OFF;
158   if (policy == -1)
159     policy_initial_link = SURF_LINK_FATPIPE;
160
161   surf_wsL07_link_create_resource(xbt_strdup(name), bw_initial, bw_trace,
162                                   lat_initial, lat_trace,
163                                   state_initial_link, st_trace,
164                                   policy_initial_link, xbt_dict_new());
165 }
166
167 /*
168  * surf parse bypass application for MSG Module
169  */
170 static int surf_parse_bypass_application()
171 {
172   unsigned int i,j;
173   p_AS_attr p_as;
174   p_host_attr p_host;
175   xbt_dynar_foreach(as_list_d, i, p_as)
176   {
177           xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
178                   if (p_host->function)
179                           MSG_set_function(p_host->id, p_host->function, p_host->args_list);
180           }
181   }
182   return 0;
183 }
184
185 /*
186  * Public Methods
187  */
188 int console_add_host(lua_State *L)
189 {
190   p_host_attr host;
191   unsigned int i;
192   p_AS_attr p_as,current_as = NULL;
193   const char *AS_id;
194   const char *id;
195   const char *power_trace;
196   const char *state_trace;
197   double power, power_scale;
198   int state_initial,core;
199   //get values from the table passed as argument
200   if (lua_istable(L, -1)) {
201   // get AS id
202   lua_pushstring(L, "AS");
203   lua_gettable(L, -2);
204   AS_id = lua_tostring(L, -1);
205   lua_pop(L,1);
206
207     // get Id Value
208     lua_pushstring(L, "id");
209     lua_gettable(L, -2);
210     id = lua_tostring(L, -1);
211     lua_pop(L, 1);
212
213     // get power value
214     lua_pushstring(L, "power");
215     lua_gettable(L, -2);
216     power = lua_tonumber(L, -1);
217     lua_pop(L, 1);
218
219     //get power_scale
220     lua_pushstring(L, "power_scale");
221     lua_gettable(L, -2);
222     power_scale = lua_tonumber(L, -1);
223     lua_pop(L, 1);
224
225     //get power_trace
226     lua_pushstring(L, "power_trace");
227     lua_gettable(L, -2);
228     power_trace = lua_tostring(L, -1);
229     lua_pop(L, 1);
230
231     lua_pushstring(L, "core");
232     lua_gettable(L, -2);
233     core = lua_tonumber(L, -1);
234     lua_pop(L, 1);
235
236     //get state initial
237     lua_pushstring(L, "state_initial");
238     lua_gettable(L, -2);
239     state_initial = lua_tonumber(L, -1);
240     lua_pop(L, 1);
241
242     //get trace state
243     lua_pushstring(L, "state_trace");
244     lua_gettable(L, -2);
245     state_trace = lua_tostring(L, -1);
246     lua_pop(L, 1);
247
248   } else {
249     XBT_ERROR
250         ("Bad Arguments to create host, Should be a table with named arguments");
251     return -1;
252   }
253   xbt_dynar_foreach(as_list_d, i, p_as){
254     if (p_as->id == AS_id){
255       current_as = p_as;
256       break;
257     }
258   }
259
260   if (!current_as)
261   {
262     XBT_ERROR("No AS_id :%s found",AS_id);
263     return -2;
264   }
265
266   host = malloc(sizeof(host_attr));
267   host->id = id;
268   host->power_peak = power;
269   host->power_scale = power_scale;
270   host->power_trace = power_trace;
271   host->core = core;
272   host->state_initial = state_initial;
273   host->state_trace = state_trace;
274   host->function = NULL;
275   host->properties = xbt_dict_new();
276   xbt_dynar_push(current_as->host_list_d, &host);
277
278   return 0;
279 }
280
281 int  console_add_link(lua_State *L) {
282   const char *AS_id;
283   unsigned int i;
284   p_AS_attr p_as,current_as = NULL;
285   const char* id;
286   double bandwidth, latency;
287   const char *bandwidth_trace;
288   const char *latency_trace;
289   const char *state_trace;
290   int state_initial, policy;
291
292   //get values from the table passed as argument
293   if (lua_istable(L, -1)) {
294
295   //get AS id
296   lua_pushstring(L, "AS");
297   lua_gettable(L, -2);
298   AS_id = lua_tostring(L, -1);
299   lua_pop(L, 1);
300
301     // get Id Value
302     lua_pushstring(L, "id");
303     lua_gettable(L, -2);
304     id = lua_tostring(L, -1);
305     lua_pop(L, 1);
306
307     // get bandwidth value
308     lua_pushstring(L, "bandwidth");
309     lua_gettable(L, -2);
310     bandwidth = lua_tonumber(L, -1);
311     lua_pop(L, 1);
312
313     //get latency value
314     lua_pushstring(L, "latency");
315     lua_gettable(L, -2);
316     latency = lua_tonumber(L, -1);
317     lua_pop(L, 1);
318
319     /*Optional Arguments  */
320
321     //get bandwidth_trace value
322     lua_pushstring(L, "bandwidth_trace");
323     lua_gettable(L, -2);
324     bandwidth_trace = lua_tostring(L, -1);
325     lua_pop(L, 1);
326
327     //get latency_trace value
328     lua_pushstring(L, "latency_trace");
329     lua_gettable(L, -2);
330     latency_trace = lua_tostring(L, -1);
331     lua_pop(L, 1);
332
333     //get state_trace value
334     lua_pushstring(L, "state_trace");
335     lua_gettable(L, -2);
336     state_trace = lua_tostring(L, -1);
337     lua_pop(L, 1);
338
339     //get state_initial value
340     lua_pushstring(L, "state_initial");
341     lua_gettable(L, -2);
342     state_initial = lua_tonumber(L, -1);
343     lua_pop(L, 1);
344
345     //get policy value
346     lua_pushstring(L, "policy");
347     lua_gettable(L, -2);
348     policy = lua_tonumber(L, -1);
349     lua_pop(L, 1);
350
351   } else {
352     XBT_ERROR
353         ("Bad Arguments to create link, Should be a table with named arguments");
354     return -1;
355   }
356   xbt_dynar_foreach(as_list_d, i, p_as){
357     if (p_as->id == AS_id){
358       current_as = p_as;
359       break;
360     }
361   }
362
363   if (!current_as)
364   {
365     XBT_ERROR("No AS_id :%s found",AS_id);
366     return -2;
367   }
368   p_link_attr link = malloc(sizeof(link_attr));
369   link->id = id;
370   link->bandwidth = bandwidth;
371   link->latency = latency;
372   link->bandwidth_trace = bandwidth_trace;
373   link->latency_trace = latency_trace;
374   link->state_trace = state_trace;
375   link->state_initial = state_initial;
376   link->policy = policy;
377   xbt_dynar_push(current_as->link_list_d, &link);
378
379   return 0;
380 }
381 /**
382  * add Router to AS components
383  */
384 int console_add_router(lua_State* L) {
385   p_router_attr router;
386   const char* AS_id;
387   unsigned int i;
388   p_AS_attr p_as,current_as = NULL;
389   const char* id;
390   if (lua_istable(L, -1)) {
391     // get AS id
392     lua_pushstring(L, "AS");
393     lua_gettable(L, -2);
394     AS_id = lua_tostring(L, -1);
395     lua_pop(L,1);
396
397     lua_pushstring(L, "id");
398     lua_gettable(L, -2);
399     id = lua_tostring(L, -1);
400     lua_pop(L,1);
401   }
402   xbt_dynar_foreach(as_list_d, i, p_as){
403       if (p_as->id == AS_id){
404         current_as = p_as;
405         break;
406       }
407     }
408
409     if (!current_as)
410     {
411       XBT_ERROR("No AS_id '%s' found",AS_id);
412       return -2;
413     }
414   router = malloc(sizeof(router_attr));
415   router->id = id;
416   xbt_dynar_push(current_as->router_list_d, &router);
417   return 0;
418
419 }
420
421
422 int console_add_route(lua_State *L)
423 {
424   const char* AS_id;
425   unsigned int i;
426   p_AS_attr p_as,current_as = NULL;
427   const char *links;
428   const char* link_id;
429   p_route_attr route = malloc(sizeof(route_attr));
430
431
432   if (!lua_istable(L, 4)) { // if Route.new is declared as an indexed table (FIXME : we check the third arg if it's not a table)
433
434    //get AS_id
435    lua_pushstring(L, "AS");
436    lua_gettable(L, -2);
437    AS_id = lua_tostring(L, -1);
438    lua_pop(L, 1);
439
440    xbt_dynar_foreach(as_list_d, i, p_as){
441     if (p_as->id == AS_id){
442       current_as = p_as;
443       break;
444     }
445    }
446
447    if (!current_as)
448    {
449     XBT_ERROR("addRoute: No AS_id :%s found",AS_id);
450     return -2;
451    }
452    // get Source Value
453      lua_pushstring(L, "src");
454      lua_gettable(L, -2);
455      route->src_id = lua_tostring(L, -1);
456      lua_pop(L, 1);
457
458      // get Destination Value
459      lua_pushstring(L, "dest");
460      lua_gettable(L, -2);
461      route->dest_id = lua_tostring(L, -1);
462      lua_pop(L, 1);
463
464      // get Links Table (char* to be splited later)
465      lua_pushstring(L, "links");
466      lua_gettable(L, -2);
467      links = lua_tostring(L, -1);
468      lua_pop(L,1);
469
470      route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
471
472      char *tmp_links = xbt_strdup(links);
473      link_id = strtok(tmp_links,",");   //tmp_link = strtok((char*)links,",");
474      while(link_id != NULL)
475        {
476           xbt_dynar_push(route->links_id, &link_id);
477           link_id = strtok(NULL,","); //Alternatively, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.
478         }
479      xbt_dynar_push(current_as->route_list_d, &route);
480      return 0;
481       }
482   else { // Route.new is declared as a function
483    AS_id = luaL_checkstring(L, 1);
484    xbt_dynar_foreach(as_list_d, i, p_as){
485       if (p_as->id == AS_id){
486         current_as = p_as;
487         break;
488       }
489      }
490
491    if (!current_as)
492    {
493     XBT_ERROR("addRoute: No AS_id :%s found",AS_id);
494     return -2;
495    }
496      route->src_id = luaL_checkstring(L, 2);
497      route->dest_id = luaL_checkstring(L, 3);
498      route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
499      lua_pushnil(L);
500      while (lua_next(L, 4) != 0)
501     {
502        link_id = lua_tostring(L, -1);
503        xbt_dynar_push(route->links_id, &link_id);
504          XBT_DEBUG("index = %f , Link_id = %s \n", lua_tonumber(L, -2),
505        lua_tostring(L, -1));
506        lua_pop(L, 1);
507       }
508     lua_pop(L, 1);
509     //add route to platform's route list
510     xbt_dynar_push(current_as->route_list_d, &route);
511     return 0;
512     }
513
514   return -1;
515 }
516
517 int console_add_AS(lua_State *L)
518 {
519   if (xbt_dynar_is_empty(as_list_d))
520      as_list_d = xbt_dynar_new(sizeof(p_AS_attr), &xbt_free_ref);
521
522  p_AS_attr AS;
523  const char *id;
524  const char *mode;
525  if (lua_istable(L, 1)) {
526    lua_pushstring(L, "id");
527    lua_gettable(L, -2);
528    id = lua_tostring(L, -1);
529    lua_pop(L, 1);
530
531    lua_pushstring(L, "mode");
532    lua_gettable(L, -2);
533    mode = lua_tostring(L, -1);
534    lua_pop(L, 1);
535  } else {
536    XBT_ERROR
537        ("Bad Arguments to AS.new, Should be a table with named arguments");
538    return -1;
539  }
540  AS = malloc(sizeof(AS_attr));
541  AS->id = id;
542  AS->mode = mode;
543  AS->host_list_d = xbt_dynar_new(sizeof(p_host_attr),&xbt_free_ref);
544  AS->link_list_d = xbt_dynar_new(sizeof(p_link_attr),&xbt_free_ref);
545  AS->route_list_d = xbt_dynar_new(sizeof(p_route_attr),&xbt_free_ref);
546  AS->router_list_d = xbt_dynar_new(sizeof(p_router_attr),&xbt_free_ref);
547  AS->sub_as_list_id = xbt_dynar_new(sizeof(p_AS_attr),&xbt_free_ref);
548  xbt_dynar_push(as_list_d, &AS);
549
550  return 0;
551 }
552
553 int console_set_function(lua_State *L) {
554   p_AS_attr p_as;
555   p_host_attr p_host;
556   unsigned int i,j;
557   const char *host_id ;
558   const char *function_id;
559   const char *args;
560   char * tmp_arg;
561
562    if (lua_istable(L, -1)) {
563    // get Host id
564    lua_pushstring(L, "host");
565    lua_gettable(L, -2);
566    host_id = lua_tostring(L, -1);
567    lua_pop(L, 1);
568    // get Function Name
569    lua_pushstring(L, "fct");
570    lua_gettable(L, -2);
571      function_id = lua_tostring(L, -1);
572      lua_pop(L, 1);
573      //get args
574      lua_pushstring(L,"args");
575      lua_gettable(L, -2);
576      args = lua_tostring(L,-1);
577      lua_pop(L, 1);
578    }
579    else {
580      XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
581      return -1;
582    }
583
584   // look for the index of host in host_list for each AS
585    xbt_dynar_foreach(as_list_d, i, p_as)
586    {
587      xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
588        if (p_host->id == host_id) {
589          p_host->function = function_id;
590          p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
591          // split & fill the args list
592          tmp_arg = strtok((char*)args,",");
593          while (tmp_arg != NULL) {
594            xbt_dynar_push(p_host->args_list, &tmp_arg);
595            tmp_arg = strtok(NULL,",");
596          }
597          return 0;
598        }
599         }
600    }
601     XBT_ERROR("Host : %s Not Found !!", host_id);
602     return 1;
603 }
604
605 int console_host_set_property(lua_State *L) {
606   p_AS_attr p_as;
607   p_host_attr p_host;
608   unsigned int i,j;
609   const char* host_id ="";
610   const char* prop_id = "";
611   const char* prop_value = "";
612   if (lua_istable(L, -1)) {
613      // get Host id
614      lua_pushstring(L, "host");
615      lua_gettable(L, -2);
616      host_id = lua_tostring(L, -1);
617      lua_pop(L, 1);
618      // get Function Name
619      lua_pushstring(L, "prop_id");
620      lua_gettable(L, -2);
621        prop_id = lua_tostring(L, -1);
622        lua_pop(L, 1);
623        //get args
624        lua_pushstring(L,"prop_value");
625        lua_gettable(L, -2);
626        prop_value = lua_tostring(L,-1);
627        lua_pop(L, 1);
628    }
629   xbt_dynar_foreach(as_list_d, i, p_as) {
630     xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
631       if (p_host->id == host_id) {
632         xbt_dict_set(p_host->properties, prop_id, xbt_strdup(prop_value), free);
633       }
634     }
635   }
636   return 1;
637 }
638
639 int console_parse_platform() {
640   unsigned int i,j;
641   p_AS_attr p_as;
642   p_host_attr p_host;
643   p_link_attr p_link;
644   p_route_attr p_route;
645
646
647   // Add AS
648   xbt_dynar_foreach(as_list_d, i,p_as)
649   {
650     sg_platf_new_AS_open(p_as->id,p_as->mode);
651     // add associated Hosts
652     xbt_dynar_foreach(p_as->host_list_d, j, p_host){
653       create_host(p_host->id, p_host->power_peak, p_host->power_scale,
654                       p_host->power_trace, p_host->core, p_host->state_initial,
655                       p_host->state_trace, p_host->properties);
656
657
658        //FIXME: should use sg_platf instead. That would add to routing model host list, amongst other benefits
659     }
660     // add associated Links
661     xbt_dynar_foreach(p_as->link_list_d, j, p_link){
662       create_link(p_link->id, p_link->bandwidth, p_link->bandwidth_trace,
663                       p_link->latency, p_link->latency_trace,
664                       p_link->state_initial, p_link->state_trace,
665                       p_link->policy);
666     }
667     // add associated Routes
668     xbt_dynar_foreach(p_as->route_list_d, j, p_route){
669       surf_routing_add_route((char *) p_route->src_id,
670                                  (char *) p_route->dest_id, p_route->links_id);
671     }
672
673     // Finalize AS
674     sg_platf_new_AS_close();
675   }
676
677   // add traces
678   surf_add_host_traces();
679   surf_add_link_traces();
680
681   return 0;                     // must return 0 ?!!
682 }
683
684 int  console_parse_application()
685 {
686         return surf_parse_bypass_application();
687 }
688
689 int console_parse_platform_wsL07()
690 {
691   unsigned int i,j;
692   p_AS_attr p_as, p_sub_as;
693   p_host_attr p_host;
694   p_link_attr p_link;
695   p_route_attr p_route;
696
697   xbt_dynar_foreach(as_list_d, i, p_as)
698   {
699     // Init AS
700     sg_platf_new_AS_open(p_as->id,p_as->mode);
701
702     // add Sub AS
703
704     // Add Hosts
705     xbt_dynar_foreach(p_as->sub_as_list_id, j, p_sub_as) {
706         //...
707     }
708     xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
709       create_host_wsL07(p_host->id, p_host->power_peak, p_host->power_scale,
710                         p_host->power_trace, p_host->state_initial,
711                         p_host->state_trace);
712       //add to routing model host list
713     }
714     //add Links
715     xbt_dynar_foreach(p_as->link_list_d, j, p_link) {
716         create_link_wsL07(p_link->id, p_link->bandwidth,
717                           p_link->bandwidth_trace, p_link->latency,
718                           p_link->latency_trace, p_link->state_initial,
719                           p_link->state_trace, p_link->policy);
720       }
721       // add route
722     xbt_dynar_foreach(p_as->route_list_d, j, p_route) {
723           surf_routing_add_route((char *) p_route->src_id,
724                                  (char *) p_route->dest_id, p_route->links_id);
725         }
726     /* </AS> */
727     // Finalize AS
728     sg_platf_new_AS_close();
729   }
730   // add traces
731   surf_wsL07_add_traces();
732   return 0;
733 }