Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
First draft integration of SURF and GTNetS. The code isn't even close to compile...
[simgrid.git] / src / surf / network_gtnets.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2005 Henri Casanova. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "network_gtnets_private.h"
9
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network_gtnets);
11
12 /* surf_network_resource_t surf_network_resource = NULL; */
13 static xbt_dict_t network_link_set = NULL;
14
15 /* xbt_dict_t network_card_set = NULL; */
16
17 #if 0
18 static int card_number = 0;
19 static network_link_GTNETS_t **routing_table = NULL;
20 static int *routing_table_size = NULL;
21
22 #define ROUTE(i,j) routing_table[(i)+(j)*card_number]
23 #define ROUTE_SIZE(i,j) routing_table_size[(i)+(j)*card_number]
24 #endif
25
26 /** QUESTIONS for GTNetS integration
27  **   1. What's the deal with name_service and get_resource_name
28  **      Right now our local dictionaries contain only integers
29  **      and not structures, but it seems that surf_network_resource->common_public
30  **      needs something in it, for the above.
31  **   2. Right now there is no "kill flow" in our GTNetS implementation. Do we
32  **      need to do something about this?
33  **   3. We didn't reall do anything with the "action" stuff, which we assume
34  **      is fine.
35  **   4. We ignore the fact there is some max_duration on flows (see #2 above)
36  **   5. share_resources() returns a duration, not a date, right?
37  **   6. How do we tell the user that "rates" are not supported?
38  **   7. We don't update "remaining" for ongoing flows. Is it bad?
39  **/
40
41 /* Free memory for a network link: REMOVED BY KF */
42 static void network_link_free(void *nw_link)
43 {
44   free(((network_link_GTNETS_t)nw_link)->name);
45   free(nw_link);
46 }
47
48 /* Instantiate a new network link: UNMODIFIED BY HC */
49 /* name: some name for the link, from the XML */
50 /* bw_initial: The bandwidth value            */
51 /* bw_trace: unused here                      */
52 /* lat_initial: The latency value             */
53 /* lat_trace: not used here                   */
54 /* state_initial: SURF_NETWORK_LINK_ON        */
55 /* state_trace: not used here                 */
56 static void network_link_new(char *name,
57                              double bw,
58                              double lat)
59 {
60   static int link_count=-1;
61   network_link_GTNETS_t gtnets_link;
62
63   /* KF: Check that the link wasn't added before */
64   if (xbt_dict_get_or_null(network_link_set, name)) {
65     return;
66   }
67
68   /* KF: Increment the link counter for GTNetS */
69   link_count++;
70
71 /*
72   nw_link->resource = (surf_resource_t) surf_network_resource;
73   nw_link->name = name;
74   nw_link->bw_current = bw_initial;
75   if (bw_trace)
76     nw_link->bw_event =
77         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
78   nw_link->lat_current = lat_initial;
79   if (lat_trace)
80     nw_link->lat_event =
81         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
82   nw_link->state_current = state_initial;
83   if (state_trace)
84     nw_link->state_event =
85         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
86 */
87
88   /* KF: Add the link to the GTNetS simulation */
89   if (GTNetS_add_link(link_count, bw, lat)) {
90     xbt_assert0(0,"Cannot create GTNetS link");
91   }
92
93   /* KF: Insert entry in the dictionary */
94   gtnets_link = xbt_new0(s_network_link_GTNETS_t,1);
95   gtnets_link->name = strcpy(name);
96   gtnets_link->bw_current = bw;
97   gtnets_link->lat_current = lat;
98   gtnets_link->id = link_count;
99   xbt_dict_set(network_link_set, name, gtnets_link, network_link_free);
100
101   return;
102 }
103
104 /* free the network card: REMOVED by KF */
105 static void network_card_free(void *nw_card)
106 {
107   free(((network_card_GTNETS_t)nw_card)->name);
108   free(nw_card);
109 }
110
111 /* Instantiate a new network card: MODIFYED BY KF */
112 static int network_card_new(const char *name)
113 {
114   static int card_count=-1;
115
116   /* KF: Check that we haven't seen the network card before */ 
117   if (xbt_dict_get_or_null(network_card_set, name)) 
118     return;
119
120   /* KF: Increment the card counter for GTNetS */
121   card_count++;
122
123   /* KF: just use the dictionary to map link names to link indices */
124   gtnets_network_card = xbt_new0(s_network_card_GTNETS_t,1);
125   gtnets_network_card->name = strcpy(name);
126   gtnets_network_card->id = card_count;
127   xbt_dict_set(network_card_set, name, gtnets_network_card, network_card_free);
128
129   /* KF: just return the GTNetS ID as the SURF ID */
130   return card_count;
131 }
132
133 /* Instantiate a new route: MODIFY BY KF */
134 static void route_new(int src_id, int dst_id, char **links, int nb_link)
135 {
136 #if 0
137   network_link_GTNETS_t *link_list = NULL;
138   int i;
139
140   ROUTE_SIZE(src_id, dst_id) = nb_link;
141   link_list = (ROUTE(src_id, dst_id) = xbt_new0(network_link_GTNETS_t, nb_link));
142   for (i = 0; i < nb_link; i++) {
143     link_list[i] = xbt_dict_get_or_null(network_link_set, links[i]);
144     free(links[i]);
145   }
146   free(links);
147 #endif
148   int i;
149   int *gtnets_links;
150  
151   /* KF: Build the list of gtnets link IDs */
152   gtnets_links = (int *)calloc(nb_link, sizeof(int));
153   for (i=0; i<nb_link; i++) {
154     gtnets_links[i]=(int)(xbt_dict_get(network_link_set, links[i]))
155   }
156
157   /* KF: Create the GTNets route */
158   if (GTNetS_add_route(src_id, dst_id, gtnets_links, nb_link)) {
159     xbt_assert0(0,"Cannot create GTNetS route");
160   }
161 }
162
163 /* Parse the XML for a network link */
164 static void parse_network_link(void)
165 {
166   char *name;
167   double bw;
168   double lat;
169   e_surf_network_link_state_t state;
170
171   name = xbt_strdup(A_surfxml_network_link_name);
172   surf_parse_get_double(&bw,A_surfxml_network_link_bandwidth);
173   surf_parse_get_double(&lat,A_surfxml_network_link_latency);
174   state = SURF_NETWORK_LINK_ON;
175
176   /* Print values when no traces are specified */
177   {
178     tmgr_trace_t bw_trace;
179     tmgr_trace_t state;
180     tmgr_trace_t lat_trace;
181
182     surf_parse_get_trace(&bw_trace, A_surfxml_network_link_bandwidth_file);
183     surf_parse_get_trace(&lat_trace, A_surfxml_network_link_latency_file);
184     surf_parse_get_trace(&state_trace,A_surfxml_network_link_state_file);
185   
186     if (bw_trace) 
187       WARNING0("The GTNetS network model doesn't support bandwidth state traces");
188     if (lat_trace)
189       WARNING0("The GTNetS network model doesn't support latency state traces");
190     if (state_trace)
191       WARNING0("The GTNetS network model doesn't support link state traces");
192   }
193
194   /* KF: remove several arguments to network_link_new */
195   network_link_new(name, bw, lat);
196 }
197
198 static int nb_link = 0;
199 static char **link_name = NULL;
200 static int src_id = -1;
201 static int dst_id = -1;
202
203 /* Parses a route from the XML: UNMODIFIED BY HC */
204 static void parse_route_set_endpoints(void)
205 {
206   src_id = network_card_new(A_surfxml_route_src);
207   dst_id = network_card_new(A_surfxml_route_dst);
208   nb_link = 0;
209   link_name = NULL;
210 }
211
212 /* Parses a route element from the XML: UNMODIFIED BY HC */
213 static void parse_route_elem(void)
214 {
215   nb_link++;
216   link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *));
217   link_name[(nb_link) - 1] = xbt_strdup(A_surfxml_route_element_name);
218 }
219
220 /* Create the route: UNMODIFIED BY HC */
221 static void parse_route_set_route(void)
222 {
223   route_new(src_id, dst_id, link_name, nb_link);
224 }
225
226 /* Main XML parsing */
227 static void parse_file(const char *file)
228 {
229   /* Figuring out the network links */
230   surf_parse_reset_parser();
231   ETag_surfxml_network_link_fun=parse_network_link;
232   surf_parse_open(file);
233   xbt_assert1((!surf_parse()),"Parse error in %s",file);
234   surf_parse_close();
235
236   /* Figuring out the network cards used */
237   surf_parse_reset_parser();
238   STag_surfxml_route_fun=parse_route_set_endpoints;
239   surf_parse_open(file);
240   xbt_assert1((!surf_parse()),"Parse error in %s",file);
241   surf_parse_close();
242
243   /* Building the routes */
244   surf_parse_reset_parser();
245   STag_surfxml_route_fun=parse_route_set_endpoints;
246   ETag_surfxml_route_element_fun=parse_route_elem;
247   ETag_surfxml_route_fun=parse_route_set_route;
248   surf_parse_open(file);
249   xbt_assert1((!surf_parse()),"Parse error in %s",file);
250   surf_parse_close();
251 }
252
253 static void *name_service(const char *name)
254 {
255   return xbt_dict_get_or_null(network_card_set, name);
256 }
257
258 static const char *get_resource_name(void *resource_id)
259 {
260   return ((network_card_GTNETS_t) resource_id)->name;
261 }
262
263 /* We do not care about this: only used for traces */
264 static int resource_used(void *resource_id)
265 {
266   return 0; /* We don't care */
267 }
268
269 static int action_free(surf_action_t action)
270 {
271   action->using--;
272   if(!action->using) {
273     xbt_swag_remove(action, action->state_set);
274     /* KF: No explicit freeing needed for GTNeTS here */
275     free(action);
276   }
277   return 1;
278 }
279
280 static void action_use(surf_action_t action)
281 {
282   action->using++;
283 }
284
285 static void action_cancel(surf_action_t action)
286 {
287   return;
288 }
289
290 static void action_recycle(surf_action_t action)
291 {
292   return;
293 }
294
295 static void action_change_state(surf_action_t action,
296                                 e_surf_action_state_t state)
297 {
298 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
299 /*     if(((surf_action_network_GTNETS_t)action)->variable) { */
300 /*       lmm_variable_disable(maxmin_system, ((surf_action_network_GTNETS_t)action)->variable); */
301 /*       ((surf_action_network_GTNETS_t)action)->variable = NULL; */
302 /*     } */
303
304   surf_action_change_state(action, state);
305   return;
306 }
307
308
309 /* share_resources() */
310 static double share_resources(double now)
311 {
312 #if 0
313   s_surf_action_network_GTNETS_t s_action;
314   surf_action_network_GTNETS_t action = NULL;
315   xbt_swag_t running_actions = surf_network_resource->common_public->states.running_action_set;
316 #endif
317
318   return GTNetS_get_time_to_next_flow_completion();
319 }
320
321 /* delta: by how many time units the simulation must advance */
322 /* In this function: change the state of actions that terminate */
323 /* The delta may not come from the network, and thus may be different (smaller) 
324    than the one returned by the function above */
325 /* If the delta is a network-caused min, then do not emulate any timer in the
326    network simulation, otherwise fake a timer somehow to advance the simulation of min seconds */
327
328 static void update_actions_state(double now, double delta)
329 {
330   surf_action_network_GTNETS_t action = NULL;
331   surf_action_network_GTNETS_t next_action = NULL;
332   xbt_swag_t running_actions =
333       surf_network_resource->common_public->states.running_action_set;
334
335   double time_to_next_flow_completion =  GTNetS_get_time_to_next_flow_completion();
336   
337   if (time_to_next_flow_completion < delta) {
338     void *metadata; 
339     surf_action_t action;
340
341     if (GTNetS_run_until_next_flow_completion(&metadata)) {
342       xbt_assert0(0,"Cannot run GTNetS simulation until next flow completion");
343     }
344
345     action = (surf_action_t)metadata;
346
347     action->generic_action.remains = 0;
348     action->generic_action.finish =  now + time_to_next_flow_completion;
349     action->generic_action.finish =  SURF_ACTION_DONE;
350     /* TODO: Anything else here? */
351   } else {
352     if (GTNetS_run(delta)) {
353       xbt_assert0(0,"Cannot run GTNetS simulation");
354     }
355   }
356   
357   return;
358 }
359
360 /* UNUSED HERE: no traces */
361 static void update_resource_state(void *id,
362                                   tmgr_trace_event_t event_type,
363                                   double value)
364 {
365   return;
366 }
367
368 /* KF: Rate not supported */
369 static surf_action_t communicate(void *src, void *dst, double size, double rate)
370 {
371   surf_action_network_GTNETS_t action = NULL;
372   network_card_GTNETS_t card_src = src;
373   network_card_GTNETS_t card_dst = dst;
374   int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
375   network_link_GTNETS_t *route = ROUTE(card_src->id, card_dst->id);
376   int i;
377
378   xbt_assert2(route_size,"You're trying to send data from %s to %s but there is no connexion between these two cards.", card_src->name, card_dst->name);
379
380   action = xbt_new0(s_surf_action_network_GTNETS_t, 1);
381
382   action->generic_action.using = 1; 
383   action->generic_action.cost = size;
384   action->generic_action.remains = size;
385   action->generic_action.max_duration = NO_MAX_DURATION;
386   action->generic_action.start = surf_get_clock(); 
387   action->generic_action.finish = -1.0; 
388   action->generic_action.resource_type =
389       (surf_resource_t) surf_network_resource;
390
391   action->generic_action.state_set =
392       surf_network_resource->common_public->states.running_action_set;
393
394   xbt_swag_insert(action, action->generic_action.state_set);
395
396   /* KF: Add a flow to the GTNets Simulation, associated to this action */
397   GTNetS_create_flow(src->id, dst->id, size, (void *)action);
398
399   return (surf_action_t) action;
400 }
401
402 /* Suspend a flow() */
403 static void action_suspend(surf_action_t action)
404 {
405   xbt_assert0(0,"action_suspend() not supported for the GTNets network model");
406 }
407
408 /* Resume a flow() */
409 static void action_resume(surf_action_t action)
410 {
411   xbt_assert0(0,"action_resume() not supported for the GTNets network model");
412 }
413
414 /* Test whether a flow is suspended */
415 static int action_is_suspended(surf_action_t action)
416 {
417   return 0;
418 }
419
420 static void finalize(void)
421 {
422   int i,j;
423
424   xbt_dict_free(&network_card_set);
425   xbt_dict_free(&network_link_set);
426   xbt_swag_free(surf_network_resource->common_public->states.
427                 ready_action_set);
428   xbt_swag_free(surf_network_resource->common_public->states.
429                 running_action_set);
430   xbt_swag_free(surf_network_resource->common_public->states.
431                 failed_action_set);
432   xbt_swag_free(surf_network_resource->common_public->states.
433                 done_action_set);
434   free(surf_network_resource->common_public);
435   free(surf_network_resource->common_private);
436   free(surf_network_resource->extension_public);
437
438   free(surf_network_resource);
439   surf_network_resource = NULL;
440
441 #if 0
442   for (i = 0; i < card_number; i++) 
443     for (j = 0; j < card_number; j++) 
444       free(ROUTE(i,j));
445   free(routing_table);
446   routing_table = NULL;
447   free(routing_table_size);
448   routing_table_size = NULL;
449   card_number = 0;
450 #endif
451
452   /* ADDED BY KF */
453   GTNetS_finalize();
454   /* END ADDITION */
455 }
456
457 static void surf_network_resource_init_internal(void)
458 {
459   s_surf_action_t action;
460
461   surf_network_resource = xbt_new0(s_surf_network_resource_t, 1);
462
463   surf_network_resource->common_private =
464       xbt_new0(s_surf_resource_private_t, 1);
465   surf_network_resource->common_public =
466       xbt_new0(s_surf_resource_public_t, 1);
467   surf_network_resource->extension_public =
468       xbt_new0(s_surf_network_resource_extension_public_t, 1);
469
470   surf_network_resource->common_public->states.ready_action_set =
471       xbt_swag_new(xbt_swag_offset(action, state_hookup));
472   surf_network_resource->common_public->states.running_action_set =
473       xbt_swag_new(xbt_swag_offset(action, state_hookup));
474   surf_network_resource->common_public->states.failed_action_set =
475       xbt_swag_new(xbt_swag_offset(action, state_hookup));
476   surf_network_resource->common_public->states.done_action_set =
477       xbt_swag_new(xbt_swag_offset(action, state_hookup));
478
479   surf_network_resource->common_public->name_service = name_service;
480   surf_network_resource->common_public->get_resource_name =
481       get_resource_name;
482   surf_network_resource->common_public->action_get_state =
483       surf_action_get_state;
484   surf_network_resource->common_public->action_use = action_use;
485   surf_network_resource->common_public->action_free = action_free;
486   surf_network_resource->common_public->action_cancel = action_cancel;
487   surf_network_resource->common_public->action_recycle = action_recycle;
488   surf_network_resource->common_public->action_change_state =
489       action_change_state;
490   surf_network_resource->common_public->action_set_data = surf_action_set_data;
491   surf_network_resource->common_public->name = "network";
492
493   surf_network_resource->common_private->resource_used = resource_used;
494   surf_network_resource->common_private->share_resources = share_resources;
495   surf_network_resource->common_private->update_actions_state =
496       update_actions_state;
497   surf_network_resource->common_private->update_resource_state =
498       update_resource_state;
499   surf_network_resource->common_private->finalize = finalize;
500
501   surf_network_resource->common_public->suspend = action_suspend;
502   surf_network_resource->common_public->resume = action_resume;
503   surf_network_resource->common_public->is_suspended = action_is_suspended;
504
505   surf_network_resource->extension_public->communicate = communicate;
506
507   network_link_set = xbt_dict_new();
508   network_card_set = xbt_dict_new();
509
510   /* HC: I am assuming that this stays in for simulation of compute tasks */
511   xbt_assert0(maxmin_system, "surf_init has to be called first!");
512
513   /* KF: Added the initialization for GTNetS interface */
514   if (GTNetS_initialize()) {
515     xbt_assert0(0, "impossible to initialize GTNetS interface");
516   }
517 }
518
519 /* HC: I put this prototype here for now but it will have to go in 
520    src/include/surf.h when it is functionnal. */
521 void surf_network_resource_init_GTNETS(const char *filename);
522
523 void surf_network_resource_init_GTNETS(const char *filename)
524 {
525   if (surf_network_resource)
526     return;
527   surf_network_resource_init_internal();
528   parse_file(filename);
529   xbt_dynar_push(resource_list, &surf_network_resource);
530 }