Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use new style logging macros.
[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 = malloc(sizeof(char)*strlen(links)+1);//use xbt
415      strcpy(tmp_links,links);
416      link_id = strtok(tmp_links,",");   //tmp_link = strtok((char*)links,",");
417      while(link_id != NULL)
418        {
419           xbt_dynar_push(route->links_id, &link_id);
420           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.
421         }
422      xbt_dynar_push(route_list_d, &route);
423      return 0;
424
425       }
426   else { // Route.new is declared as a function
427      //const char* link_id;
428      route->src_id = luaL_checkstring(L, 1);
429      route->dest_id = luaL_checkstring(L, 2);
430      route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
431      lua_pushnil(L);
432      while (lua_next(L, 3) != 0)
433                 {
434          link_id = lua_tostring(L, -1);
435          xbt_dynar_push(route->links_id, &link_id);
436          XBT_DEBUG("index = %f , Link_id = %s \n", lua_tonumber(L, -2),
437          lua_tostring(L, -1));
438          lua_pop(L, 1);
439         }
440          lua_pop(L, 1);
441           //add route to platform's route list
442           xbt_dynar_push(route_list_d, &route);
443           return 0;
444     }
445
446   return -1;
447 }
448 /**
449  * set function to process
450  */
451 static int Host_set_function(lua_State * L)     //(host,function,nb_args,list_args)
452 {
453         p_host_attr p_host;
454         const char *host;
455         const char *function;
456         const char *args;
457         char * tmp_arg;
458         unsigned int i;
459
460    if (lua_istable(L, -1)) {
461          // get Host id
462          lua_pushstring(L, "host");
463          lua_gettable(L, -2);
464          host = lua_tostring(L, -1);
465          lua_pop(L, 1);
466          // get Function Name
467          lua_pushstring(L, "fct");
468          lua_gettable(L, -2);
469      function = lua_tostring(L, -1);
470      lua_pop(L, 1);
471      //get args
472      lua_pushstring(L,"args");
473      lua_gettable(L, -2);
474      args = lua_tostring(L,-1);
475      lua_pop(L, 1);
476    }
477    else {
478            XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
479            return -1;
480    }
481
482   // look for the index of host in host_list
483   xbt_dynar_foreach(host_list_d, i, p_host) {
484           if (p_host->id == host) {
485       p_host->function = function;
486       p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
487       // split & fill the args list
488       tmp_arg = strtok((char*)args,",");
489       while (tmp_arg != NULL) {
490           xbt_dynar_push(p_host->args_list, &tmp_arg);
491           tmp_arg = strtok(NULL,",");
492         }
493       return 0;
494            }
495       }
496           XBT_ERROR("Host : %s Not Found !!", host);
497           return 1;
498
499 }
500 /*
501  * surf parse bypass platform
502  * through CPU/network Models
503  */
504
505 static int surf_parse_bypass_platform()
506 {
507   unsigned int i;
508   p_host_attr p_host;
509   p_link_attr p_link;
510   p_route_attr p_route;
511
512   // Init routing mode
513   create_AS(AS->id, AS->mode);
514
515   // Add Hosts
516   xbt_dynar_foreach(host_list_d, i, p_host) {
517     create_host(p_host->id, p_host->power_peak, p_host->power_scale,
518                 p_host->power_trace, p_host->core, p_host->state_initial,
519                 p_host->state_trace);
520     //add to routing model host list
521     surf_route_add_host((char *) p_host->id);
522
523   }
524
525   //add Links
526   xbt_dynar_foreach(link_list_d, i, p_link) {
527     create_link(p_link->id, p_link->bandwidth, p_link->bandwidth_trace,
528                 p_link->latency, p_link->latency_trace,
529                 p_link->state_initial, p_link->state_trace,
530                 p_link->policy);
531   }
532   // add route
533   xbt_dynar_foreach(route_list_d, i, p_route) {
534     surf_routing_add_route((char *) p_route->src_id,
535                            (char *) p_route->dest_id, p_route->links_id);
536   }
537   /* </platform> */
538
539   // Finalize AS
540   surf_AS_finalize(AS->id);
541
542   // add traces
543   surf_add_host_traces();
544   surf_add_link_traces();
545
546   return 0;                     // must return 0 ?!!
547
548 }
549
550 /**
551  *
552  * surf parse bypass platform
553  * through workstation_ptask_L07 Model
554  */
555
556 static int surf_wsL07_parse_bypass_platform()
557 {
558
559   unsigned int i;
560   p_host_attr p_host;
561   p_link_attr p_link;
562   p_route_attr p_route;
563
564   // Init routing mode
565   create_AS(AS->id, AS->mode);
566
567   // Add Hosts
568   xbt_dynar_foreach(host_list_d, i, p_host) {
569     create_host_wsL07(p_host->id, p_host->power_peak, p_host->power_scale,
570                       p_host->power_trace, p_host->state_initial,
571                       p_host->state_trace);
572     //add to routing model host list
573     surf_route_add_host((char *) p_host->id);
574   }
575
576   //add Links
577   xbt_dynar_foreach(link_list_d, i, p_link) {
578     create_link_wsL07(p_link->id, p_link->bandwidth,
579                       p_link->bandwidth_trace, p_link->latency,
580                       p_link->latency_trace, p_link->state_initial,
581                       p_link->state_trace, p_link->policy);
582   }
583   // add route
584   xbt_dynar_foreach(route_list_d, i, p_route) {
585     surf_routing_add_route((char *) p_route->src_id,
586                            (char *) p_route->dest_id, p_route->links_id);
587   }
588   /* </platform> */
589
590   // Finalize AS
591   surf_AS_finalize(AS->id);
592   // add traces
593   surf_wsL07_add_traces();
594
595   return 0;
596 }
597
598 /*
599  * surf parse bypass application for MSG Module
600  */
601 static int surf_parse_bypass_application()
602 {
603   unsigned int i;
604   p_host_attr p_host;
605   xbt_dynar_foreach(host_list_d, i, p_host) {
606     if (p_host->function)
607       MSG_set_function(p_host->id, p_host->function, p_host->args_list);
608   }
609   return 0;
610 }
611
612 /*
613  * Public Methods
614  */
615
616 int console_add_host(lua_State *L)
617 {
618         return Host_new(L);
619 }
620
621 int  console_add_link(lua_State *L)
622 {
623         return Link_new(L);
624 }
625
626 int console_add_route(lua_State *L)
627 {
628         return Route_new(L);
629 }
630
631 int console_add_AS(lua_State *L)
632 {
633         return AS_new(L);
634 }
635
636 int console_set_function(lua_State *L)
637 {
638         return Host_set_function(L);
639 }
640
641 int console_parse_platform()
642 {
643         return surf_parse_bypass_platform();
644 }
645
646 int  console_parse_application()
647 {
648         return surf_parse_bypass_application();
649 }
650
651 int console_parse_platform_wsL07()
652 {
653         return surf_wsL07_parse_bypass_platform();
654 }