Logo AND Algorithmique Numérique Distribuée

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