Logo AND Algorithmique Numérique Distribuée

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