Logo AND Algorithmique Numérique Distribuée

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