Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove spurious else..
[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   if (state_init == -1)
49     state_initial = SURF_RESOURCE_OFF;
50   else
51     state_initial = SURF_RESOURCE_ON;
52   if (power_tr)
53     power_trace = tmgr_trace_new(power_tr);
54   else
55     power_trace = tmgr_trace_new("");
56   if (state_tr)
57     state_trace = tmgr_trace_new(state_tr);
58   else
59     state_trace = tmgr_trace_new("");
60   current_property_set = xbt_dict_new();
61   surf_host_create_resource(xbt_strdup(id), power_peak, power_scale,
62                             power_trace, core_nb, state_initial, state_trace,
63                             current_property_set);
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   current_property_set = xbt_dict_new();
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
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   const char *id;
188   const char *mode;
189   if (lua_istable(L, 1)) {
190     lua_pushstring(L, "id");
191     lua_gettable(L, -2);
192     id = lua_tostring(L, -1);
193     lua_pop(L, 1);
194
195     lua_pushstring(L, "mode");
196     lua_gettable(L, -2);
197     mode = lua_tostring(L, -1);
198     lua_pop(L, 1);
199   } else {
200     XBT_ERROR
201         ("Bad Arguments to AS.new, Should be a table with named arguments");
202     return -1;
203   }
204   AS = malloc(sizeof(AS_attr));
205   AS->id = id;
206   AS->mode = mode;
207
208   return 0;
209 }
210
211 /*
212  * add new host to platform hosts list
213  */
214 static int Host_new(lua_State * L)
215 {
216
217   if (xbt_dynar_is_empty(host_list_d))
218     host_list_d = xbt_dynar_new(sizeof(p_host_attr), &xbt_free_ref);
219
220   p_host_attr host;
221   const char *id;
222   const char *power_trace;
223   const char *state_trace;
224   double power, power_scale;
225   int state_initial,core;
226   //get values from the table passed as argument
227   if (lua_istable(L, -1)) {
228
229     // get Id Value
230     lua_pushstring(L, "id");
231     lua_gettable(L, -2);
232     id = lua_tostring(L, -1);
233     lua_pop(L, 1);
234
235     // get power value
236     lua_pushstring(L, "power");
237     lua_gettable(L, -2);
238     power = lua_tonumber(L, -1);
239     lua_pop(L, 1);
240
241     //get power_scale
242     lua_pushstring(L, "power_scale");
243     lua_gettable(L, -2);
244     power_scale = lua_tonumber(L, -1);
245     lua_pop(L, 1);
246
247     //get power_trace
248     lua_pushstring(L, "power_trace");
249     lua_gettable(L, -2);
250     power_trace = lua_tostring(L, -1);
251     lua_pop(L, 1);
252
253     lua_pushstring(L, "core");
254     lua_gettable(L, -2);
255     core = lua_tonumber(L, -1);
256     lua_pop(L, 1);
257
258     //get state initial
259     lua_pushstring(L, "state_initial");
260     lua_gettable(L, -2);
261     state_initial = lua_tonumber(L, -1);
262     lua_pop(L, 1);
263
264     //get trace state
265     lua_pushstring(L, "state_trace");
266     lua_gettable(L, -2);
267     state_trace = lua_tostring(L, -1);
268     lua_pop(L, 1);
269
270   } else {
271     XBT_ERROR
272         ("Bad Arguments to create host, Should be a table with named arguments");
273     return -1;
274   }
275
276   host = malloc(sizeof(host_attr));
277   host->id = id;
278   host->power_peak = power;
279   host->power_scale = power_scale;
280   host->power_trace = power_trace;
281   host->core = core;
282   host->state_initial = state_initial;
283   host->state_trace = state_trace;
284   host->function = NULL;
285   xbt_dynar_push(host_list_d, &host);
286
287   return 0;
288 }
289
290 /**
291  * add link to platform links list
292  */
293 static int Link_new(lua_State * L)      // (id,bandwidth,latency)
294 {
295
296   if (xbt_dynar_is_empty(link_list_d))
297     link_list_d = xbt_dynar_new(sizeof(p_link_attr), &xbt_free_ref);
298
299
300   const char* id;
301   double bandwidth, latency;
302   const char *bandwidth_trace;
303   const char *latency_trace;
304   const char *state_trace;
305   int state_initial, policy;
306
307   //get values from the table passed as argument
308   if (lua_istable(L, -1)) {
309     // get Id Value
310     lua_pushstring(L, "id");
311     lua_gettable(L, -2);
312     id = lua_tostring(L, -1);
313     lua_pop(L, 1);
314
315     // get bandwidth value
316     lua_pushstring(L, "bandwidth");
317     lua_gettable(L, -2);
318     bandwidth = lua_tonumber(L, -1);
319     lua_pop(L, 1);
320
321     //get latency value
322     lua_pushstring(L, "latency");
323     lua_gettable(L, -2);
324     latency = lua_tonumber(L, -1);
325     lua_pop(L, 1);
326
327     /*Optional Arguments  */
328
329     //get bandwidth_trace value
330     lua_pushstring(L, "bandwidth_trace");
331     lua_gettable(L, -2);
332     bandwidth_trace = lua_tostring(L, -1);
333     lua_pop(L, 1);
334
335     //get latency_trace value
336     lua_pushstring(L, "latency_trace");
337     lua_gettable(L, -2);
338     latency_trace = lua_tostring(L, -1);
339     lua_pop(L, 1);
340
341     //get state_trace value
342     lua_pushstring(L, "state_trace");
343     lua_gettable(L, -2);
344     state_trace = lua_tostring(L, -1);
345     lua_pop(L, 1);
346
347     //get state_initial value
348     lua_pushstring(L, "state_initial");
349     lua_gettable(L, -2);
350     state_initial = lua_tonumber(L, -1);
351     lua_pop(L, 1);
352
353     //get policy value
354     lua_pushstring(L, "policy");
355     lua_gettable(L, -2);
356     policy = lua_tonumber(L, -1);
357     lua_pop(L, 1);
358
359   } else {
360     XBT_ERROR
361         ("Bad Arguments to create link, Should be a table with named arguments");
362     return -1;
363   }
364
365   p_link_attr link = malloc(sizeof(link_attr));
366   link->id = id;
367   link->bandwidth = bandwidth;
368   link->latency = latency;
369   link->bandwidth_trace = bandwidth_trace;
370   link->latency_trace = latency_trace;
371   link->state_trace = state_trace;
372   link->state_initial = state_initial;
373   link->policy = policy;
374   xbt_dynar_push(link_list_d, &link);
375
376   return 0;
377 }
378
379 /**
380  * add route to platform routes list
381  */
382 static int Route_new(lua_State * L)     // (src_id,dest_id,links_number,link_table)
383 {
384  if (xbt_dynar_is_empty(route_list_d))
385             route_list_d = xbt_dynar_new(sizeof(p_route_attr), &xbt_free_ref);
386
387          const char *links;
388          const char* link_id;
389          p_route_attr route = malloc(sizeof(route_attr));
390
391
392   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)
393      // get Source Value
394      lua_pushstring(L, "src");
395      lua_gettable(L, -2);
396      route->src_id = lua_tostring(L, -1);
397      lua_pop(L, 1);
398
399      // get Destination Value
400      lua_pushstring(L, "dest");
401      lua_gettable(L, -2);
402      route->dest_id = lua_tostring(L, -1);
403      lua_pop(L, 1);
404
405      // get Links Table (char* to be splited later)
406      lua_pushstring(L, "links");
407      lua_gettable(L, -2);
408      links = lua_tostring(L, -1);
409      lua_pop(L,1);
410
411      route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
412
413      char *tmp_links = xbt_strdup(links);
414      link_id = strtok(tmp_links,",");   //tmp_link = strtok((char*)links,",");
415      while(link_id != NULL)
416        {
417           xbt_dynar_push(route->links_id, &link_id);
418           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.
419         }
420      xbt_dynar_push(route_list_d, &route);
421      return 0;
422
423       }
424   else { // Route.new is declared as a function
425      //const char* link_id;
426      route->src_id = luaL_checkstring(L, 1);
427      route->dest_id = luaL_checkstring(L, 2);
428      route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
429      lua_pushnil(L);
430      while (lua_next(L, 3) != 0)
431                 {
432          link_id = lua_tostring(L, -1);
433          xbt_dynar_push(route->links_id, &link_id);
434          XBT_DEBUG("index = %f , Link_id = %s \n", lua_tonumber(L, -2),
435          lua_tostring(L, -1));
436          lua_pop(L, 1);
437         }
438          lua_pop(L, 1);
439           //add route to platform's route list
440           xbt_dynar_push(route_list_d, &route);
441           return 0;
442     }
443
444   return -1;
445 }
446 /**
447  * set function to process
448  */
449 static int Host_set_function(lua_State * L)     //(host,function,nb_args,list_args)
450 {
451         p_host_attr p_host;
452         const char *host;
453         const char *function;
454         const char *args;
455         char * tmp_arg;
456         unsigned int i;
457
458    if (lua_istable(L, -1)) {
459          // get Host id
460          lua_pushstring(L, "host");
461          lua_gettable(L, -2);
462          host = lua_tostring(L, -1);
463          lua_pop(L, 1);
464          // get Function Name
465          lua_pushstring(L, "fct");
466          lua_gettable(L, -2);
467      function = lua_tostring(L, -1);
468      lua_pop(L, 1);
469      //get args
470      lua_pushstring(L,"args");
471      lua_gettable(L, -2);
472      args = lua_tostring(L,-1);
473      lua_pop(L, 1);
474    }
475    else {
476            XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
477            return -1;
478    }
479
480   // look for the index of host in host_list
481   xbt_dynar_foreach(host_list_d, i, p_host) {
482           if (p_host->id == host) {
483       p_host->function = function;
484       p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
485       // split & fill the args list
486       tmp_arg = strtok((char*)args,",");
487       while (tmp_arg != NULL) {
488           xbt_dynar_push(p_host->args_list, &tmp_arg);
489           tmp_arg = strtok(NULL,",");
490         }
491       return 0;
492            }
493       }
494           XBT_ERROR("Host : %s Not Found !!", host);
495           return 1;
496
497 }
498 /*
499  * surf parse bypass platform
500  * through CPU/network Models
501  */
502
503 static int surf_parse_bypass_platform()
504 {
505   unsigned int i;
506   p_host_attr p_host;
507   p_link_attr p_link;
508   p_route_attr p_route;
509
510   // Init routing mode
511   create_AS(AS->id, AS->mode);
512
513   // Add Hosts
514   xbt_dynar_foreach(host_list_d, i, p_host) {
515     create_host(p_host->id, p_host->power_peak, p_host->power_scale,
516                 p_host->power_trace, p_host->core, p_host->state_initial,
517                 p_host->state_trace);
518     //add to routing model host list
519     surf_route_add_host((char *) p_host->id);
520
521   }
522
523   //add Links
524   xbt_dynar_foreach(link_list_d, i, p_link) {
525     create_link(p_link->id, p_link->bandwidth, p_link->bandwidth_trace,
526                 p_link->latency, p_link->latency_trace,
527                 p_link->state_initial, p_link->state_trace,
528                 p_link->policy);
529   }
530   // add route
531   xbt_dynar_foreach(route_list_d, i, p_route) {
532     surf_routing_add_route((char *) p_route->src_id,
533                            (char *) p_route->dest_id, p_route->links_id);
534   }
535   /* </platform> */
536
537   // Finalize AS
538   surf_AS_finalize(AS->id);
539
540   // add traces
541   surf_add_host_traces();
542   surf_add_link_traces();
543
544   return 0;                     // must return 0 ?!!
545
546 }
547
548 /**
549  *
550  * surf parse bypass platform
551  * through workstation_ptask_L07 Model
552  */
553
554 static int surf_wsL07_parse_bypass_platform()
555 {
556
557   unsigned int i;
558   p_host_attr p_host;
559   p_link_attr p_link;
560   p_route_attr p_route;
561
562   // Init routing mode
563   create_AS(AS->id, AS->mode);
564
565   // Add Hosts
566   xbt_dynar_foreach(host_list_d, i, p_host) {
567     create_host_wsL07(p_host->id, p_host->power_peak, p_host->power_scale,
568                       p_host->power_trace, p_host->state_initial,
569                       p_host->state_trace);
570     //add to routing model host list
571     surf_route_add_host((char *) p_host->id);
572   }
573
574   //add Links
575   xbt_dynar_foreach(link_list_d, i, p_link) {
576     create_link_wsL07(p_link->id, p_link->bandwidth,
577                       p_link->bandwidth_trace, p_link->latency,
578                       p_link->latency_trace, p_link->state_initial,
579                       p_link->state_trace, p_link->policy);
580   }
581   // add route
582   xbt_dynar_foreach(route_list_d, i, p_route) {
583     surf_routing_add_route((char *) p_route->src_id,
584                            (char *) p_route->dest_id, p_route->links_id);
585   }
586   /* </platform> */
587
588   // Finalize AS
589   surf_AS_finalize(AS->id);
590   // add traces
591   surf_wsL07_add_traces();
592
593   return 0;
594 }
595
596 /*
597  * surf parse bypass application for MSG Module
598  */
599 static int surf_parse_bypass_application()
600 {
601   unsigned int i;
602   p_host_attr p_host;
603   xbt_dynar_foreach(host_list_d, i, p_host) {
604     if (p_host->function)
605       MSG_set_function(p_host->id, p_host->function, p_host->args_list);
606   }
607   return 0;
608 }
609
610 /*
611  * Public Methods
612  */
613
614 int console_add_host(lua_State *L)
615 {
616         return Host_new(L);
617 }
618
619 int  console_add_link(lua_State *L)
620 {
621         return Link_new(L);
622 }
623
624 int console_add_route(lua_State *L)
625 {
626         return Route_new(L);
627 }
628
629 int console_add_AS(lua_State *L)
630 {
631         return AS_new(L);
632 }
633
634 int console_set_function(lua_State *L)
635 {
636         return Host_set_function(L);
637 }
638
639 int console_parse_platform()
640 {
641         return surf_parse_bypass_platform();
642 }
643
644 int  console_parse_application()
645 {
646         return surf_parse_bypass_application();
647 }
648
649 int console_parse_platform_wsL07()
650 {
651         return surf_wsL07_parse_bypass_platform();
652 }