Logo AND Algorithmique Numérique Distribuée

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