Logo AND Algorithmique Numérique Distribuée

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