Logo AND Algorithmique Numérique Distribuée

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