Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
various cleanups
[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 #include "gtnets/gtnets_interface.h"
10 #include "xbt/str.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_gtnets, surf,
13                                 "Logging specific to the SURF network module");
14
15 /** QUESTIONS for GTNetS integration
16  **   1. Check that we did the right thing with name_service and get_resource_name
17  **   2. Right now there is no "kill flow" in our GTNetS implementation. Do we
18  **      need to do something about this?
19  **   3. We ignore the fact there is some max_duration on flows (see #2 above)
20  **   4. share_resources() returns a duration, not a date, right?
21  **   5. We don't suppoer "rates"
22  **   6. We don't update "remaining" for ongoing flows. Is it bad?
23  **/
24
25 static int src_id = -1;
26 static int dst_id = -1;
27
28 /* Free memory for a network link */
29 static void link_free(void *nw_link)
30 {
31   free(((network_link_GTNETS_t) nw_link)->name);
32   xbt_dict_free(&(((network_link_GTNETS_t)nw_link)->properties));
33   free(nw_link);
34 }
35
36 /* Instantiate a new network link */
37 /* name: some name for the link, from the XML */
38 /* bw: The bandwidth value            */
39 /* lat: The latency value             */
40 static void link_new(char *name, double bw, double lat, xbt_dict_t props)
41 {
42   static int link_count = -1;
43   network_link_GTNETS_t gtnets_link;
44
45   /* If link already exists, nothing to do (FIXME: check that multiple definition match?) */
46   if (xbt_dict_get_or_null(link_set, name)) {
47     return;
48   }
49
50   /* KF: Increment the link counter for GTNetS */
51   link_count++;
52
53 /*
54   nw_link->model = (surf_model_t) surf_network_model;
55   nw_link->name = name;
56   nw_link->bw_current = bw_initial;
57   if (bw_trace)
58     nw_link->bw_event =
59         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
60   nw_link->lat_current = lat_initial;
61   if (lat_trace)
62     nw_link->lat_event =
63         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
64   nw_link->state_current = state_initial;
65   if (state_trace)
66     nw_link->state_event =
67         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
68 */
69
70   /* KF: Add the link to the GTNetS simulation */
71   if (gtnets_add_link(link_count, bw, lat)) {
72     xbt_assert0(0, "Cannot create GTNetS link");
73   }
74
75   /* KF: Insert entry in the dictionary */
76   gtnets_link = xbt_new0(s_network_link_GTNETS_t, 1);
77   gtnets_link->name = name;
78   gtnets_link->bw_current = bw;
79   gtnets_link->lat_current = lat;
80   gtnets_link->id = link_count;
81   /* Add the properties */
82   gtnets_link->properties = props;
83
84   xbt_dict_set(link_set, name, gtnets_link, link_free);
85
86   return;
87 }
88
89 /* free the network card */
90 static void network_card_free(void *nw_card)
91 {
92   free(((network_card_GTNETS_t) nw_card)->name);
93   free(nw_card);
94 }
95
96 /* Instantiate a new network card: MODIFYED BY KF */
97 static int network_card_new(const char *name)
98 {
99   static int card_count = -1;
100
101   /* KF: Check that we haven't seen the network card before */
102   network_card_GTNETS_t card =
103       xbt_dict_get_or_null(network_card_set, name);
104
105   if (!card) {
106     /* KF: Increment the card counter for GTNetS */
107     card_count++;
108
109     /* KF: just use the dictionary to map link names to link indices */
110     card = xbt_new0(s_network_card_GTNETS_t, 1);
111     card->name = xbt_strdup(name);
112     card->id = card_count;
113     xbt_dict_set(network_card_set, name, card, network_card_free);
114   }
115
116   /* KF: just return the GTNetS ID as the SURF ID */
117   return card->id;
118 }
119
120 /* Instantiate a new route: MODIFY BY KF */
121 static void route_new(int src_id, int dst_id, network_link_GTNETS_t *links, int nb_link)
122 {
123   int i;
124   int *gtnets_links;
125
126   /* KF: Build the list of gtnets link IDs */
127   gtnets_links = (int *) calloc(nb_link, sizeof(int));
128   for (i = 0; i < nb_link; i++) {
129     gtnets_links[i] = links[i]->id;
130   }
131
132   /* KF: Create the GTNets route */
133   if (gtnets_add_route(src_id, dst_id, gtnets_links, nb_link)) {
134     xbt_assert0(0, "Cannot create GTNetS route");
135   }
136 }
137
138 /* Instantiate a new route: MODIFY BY KF */
139 static void route_onehop_new(int src_id, int dst_id, network_link_GTNETS_t *links, int nb_link)
140 {
141   int linkid;
142
143   if (nb_link != 1) {
144     xbt_assert0(0, "In onehop_new, nb_link should be 1");
145   }
146
147   /* KF: Build the linbst of gtnets link IDs */
148   linkid = links[0]->id;
149
150   /* KF: Create the GTNets route */
151   if (gtnets_add_onehop_route(src_id, dst_id, linkid)) {
152     xbt_assert0(0, "Cannot create GTNetS route");
153   }
154 }
155
156
157
158 /* Parse the XML for a network link */
159 static void parse_link_init(void)
160 {
161   char *name;
162   double bw;
163   double lat;
164   e_surf_link_state_t state;
165
166   name = xbt_strdup(A_surfxml_link_id);
167   surf_parse_get_double(&bw, A_surfxml_link_bandwidth);
168   surf_parse_get_double(&lat, A_surfxml_link_latency);
169   state = SURF_LINK_ON;
170
171   tmgr_trace_t bw_trace;
172   tmgr_trace_t state_trace;
173   tmgr_trace_t lat_trace;
174   
175   surf_parse_get_trace(&bw_trace, A_surfxml_link_bandwidth_file);
176   surf_parse_get_trace(&lat_trace, A_surfxml_link_latency_file);
177   surf_parse_get_trace(&state_trace, A_surfxml_link_state_file);
178
179   if (bw_trace)
180     INFO0("The GTNetS network model doesn't support bandwidth state traces");
181   if (lat_trace)
182     INFO0("The GTNetS network model doesn't support latency state traces");
183   if (state_trace)
184     INFO0("The GTNetS network model doesn't support link state traces");
185
186   current_property_set = xbt_dict_new();
187   link_new(name, bw, lat, current_property_set);
188 }
189
190 /* Parses a route from the XML: UNMODIFIED BY HC */
191 static void parse_route_set_endpoints(void)
192 {
193   src_id = network_card_new(A_surfxml_route_src);
194   dst_id = network_card_new(A_surfxml_route_dst);
195   route_action = A_surfxml_route_action;
196 }
197
198 /* KF*/
199 static void parse_route_set_routers(void)
200 {
201   int id = network_card_new(A_surfxml_router_id);
202
203   /* KF: Create the GTNets router */
204   if (gtnets_add_router(id)) {
205     xbt_assert0(0, "Cannot add GTNetS router");
206   }
207 }
208
209 /* Create the route (more than one hops): MODIFIED BY KF */
210 static void parse_route_set_route(void)
211 {
212   char *name;
213   if (src_id != -1 && dst_id != -1) {
214     name = bprintf("%x#%x",src_id, dst_id);
215     manage_route(route_table, name, route_action, 0);
216     free(name);    
217   }
218 }
219
220 static void add_route()
221 {
222   xbt_ex_t e;
223   unsigned int cpt = 0;    
224   int link_list_capacity = 0;
225   int nb_link = 0;
226   xbt_dict_cursor_t cursor = NULL;
227   char *key,*data, *end;
228   const char *sep = "#";
229   xbt_dynar_t links, keys;
230   static network_link_GTNETS_t *link_list = NULL;
231
232
233   DEBUG0("Entering add_route()");
234   xbt_dict_foreach(route_table, cursor, key, data) {
235     char *link = NULL;
236     nb_link = 0;
237     links = (xbt_dynar_t)data;
238     keys = xbt_str_split_str(key, sep);
239
240     link_list_capacity = xbt_dynar_length(links);
241     link_list = xbt_new(network_link_GTNETS_t, link_list_capacity);
242
243     src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
244     dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
245     xbt_dynar_free(&keys);
246
247     xbt_dynar_foreach (links, cpt, link) {
248       TRY {
249         link_list[nb_link++] = xbt_dict_get(link_set, link);
250       }
251       CATCH(e) {
252         RETHROW1("Link %s not found (dict raised this exception: %s)", link);
253       }     
254     }
255     if (nb_link > 1)
256       route_new(src_id, dst_id, link_list, nb_link);
257     if (nb_link == 1)
258       route_onehop_new(src_id, dst_id, link_list, nb_link);
259    }
260
261   xbt_dict_free(&route_table);
262   DEBUG0("Bailling add_route()");
263 }
264
265 /* Main XML parsing */
266 static void define_callbacks(const char *file)
267 {
268   surfxml_add_callback(STag_surfxml_router_cb_list,   &parse_route_set_routers);
269   surfxml_add_callback(STag_surfxml_link_cb_list,     &parse_link_init);
270   surfxml_add_callback(STag_surfxml_route_cb_list,    &parse_route_set_endpoints);
271   surfxml_add_callback(ETag_surfxml_route_cb_list,      &parse_route_set_route);
272   surfxml_add_callback(ETag_surfxml_platform_cb_list,   &add_route);
273 }
274
275 static void *name_service(const char *name)
276 {
277   return xbt_dict_get_or_null(network_card_set, name);
278 }
279
280 static const char *get_resource_name(void *resource_id)
281 {
282   return ((network_card_GTNETS_t) resource_id)->name;
283 }
284
285 static xbt_dict_t get_properties(void *link)
286 {
287   return ((network_card_GTNETS_t) link)->properties;
288 }
289
290
291 /* We do not care about this: only used for traces */
292 static int resource_used(void *resource_id)
293 {
294   return 0;                     /* We don't care */
295 }
296
297 static int action_free(surf_action_t action)
298 {
299   action->using--;
300   if (!action->using) {
301     xbt_swag_remove(action, action->state_set);
302     /* KF: No explicit freeing needed for GTNeTS here */
303     free(action);
304     return 1;
305   }
306   return 0;
307 }
308
309 static void action_use(surf_action_t action)
310 {
311   action->using++;
312 }
313
314 static void action_cancel(surf_action_t action)
315 {
316   xbt_assert0(0, "Cannot cancel GTNetS flow");
317   return;
318 }
319
320 static void action_recycle(surf_action_t action)
321 {
322   xbt_assert0(0, "Cannot recycle GTNetS flow");
323   return;
324 }
325
326 static void action_change_state(surf_action_t action,
327                                 e_surf_action_state_t state)
328 {
329 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
330 /*     if(((surf_action_network_GTNETS_t)action)->variable) { */
331 /*       lmm_variable_disable(maxmin_system, ((surf_action_network_GTNETS_t)action)->variable); */
332 /*       ((surf_action_network_GTNETS_t)action)->variable = NULL; */
333 /*     } */
334
335   surf_action_change_state(action, state);
336   return;
337 }
338
339
340 /* share_resources() */
341 static double share_resources(double now)
342 {
343   xbt_swag_t running_actions = surf_network_model->common_public->states.running_action_set;
344
345   //get the first relevant value from the running_actions list
346   if (!xbt_swag_size(running_actions))
347     return -1.0;
348
349   return gtnets_get_time_to_next_flow_completion();
350 }
351
352 /* delta: by how many time units the simulation must advance */
353 /* In this function: change the state of actions that terminate */
354 /* The delta may not come from the network, and thus may be different (smaller) 
355    than the one returned by the function above */
356 /* If the delta is a network-caused min, then do not emulate any timer in the
357    network simulation, otherwise fake a timer somehow to advance the simulation of min seconds */
358
359 static void update_actions_state(double now, double delta)
360 {
361   surf_action_network_GTNETS_t action = NULL;
362   //  surf_action_network_GTNETS_t next_action = NULL;
363   xbt_swag_t running_actions =
364       surf_network_model->common_public->states.running_action_set;
365
366   double time_to_next_flow_completion =
367       gtnets_get_time_to_next_flow_completion();
368
369   /* If there are no renning flows, just return */
370   if (time_to_next_flow_completion < 0.0) {
371     return;
372   }
373
374   /*KF: if delta == time_to_next_flow_completion, too. */
375   if (time_to_next_flow_completion <= delta) {  /* run until the first flow completes */
376     void **metadata;
377     int i, num_flows;
378
379     num_flows = 0;
380
381     if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
382       xbt_assert0(0,
383                   "Cannot run GTNetS simulation until next flow completion");
384     }
385     if (num_flows < 1) {
386       xbt_assert0(0,
387                   "GTNetS simulation couldn't find a flow that would complete");
388     }
389
390
391
392     xbt_swag_foreach(action, running_actions) {
393       DEBUG1("]]]]]]]]] Action remains old value: %f", action->generic_action.remains);
394       double remain = gtnets_get_flow_rx(action);
395       //need to trust this remain value
396       if(remain == 0){
397         action->generic_action.remains=0;
398       }else {
399         action->generic_action.remains-=remain;
400       }
401       DEBUG1("[[[[[[[[[ Action remains new value: %f", action->generic_action.remains);
402     }
403
404     for (i = 0; i < num_flows; i++) {
405       action =  (surf_action_network_GTNETS_t) (metadata[i]);
406       
407       action->generic_action.finish = now + time_to_next_flow_completion;
408       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
409       /* TODO: Anything else here? */
410
411       //need to map this action to the gtnets engine
412       DEBUG1("]]]]]]]]] Action remains old value: %f", action->generic_action.remains);
413       action->generic_action.remains -= gtnets_get_flow_rx(metadata[i]);
414       DEBUG1("[[[[[[[[[ Action remains new value: %f", action->generic_action.remains);
415     }
416
417
418   } else {                      /* run for a given number of seconds */
419     if (gtnets_run(delta)) {
420       xbt_assert0(0, "Cannot run GTNetS simulation");
421     }
422   }
423
424   return;
425 }
426
427 /* UNUSED HERE: no traces */
428 static void update_resource_state(void *id,
429                                   tmgr_trace_event_t event_type,
430                                   double value, double date)
431 {
432   xbt_assert0(0, "Cannot update model state for GTNetS simulation");
433   return;
434 }
435
436 /* KF: Rate not supported */
437 static surf_action_t communicate(void *src, void *dst, double size,
438                                  double rate)
439 {
440   surf_action_network_GTNETS_t action = NULL;
441   network_card_GTNETS_t card_src = src;
442   network_card_GTNETS_t card_dst = dst;
443 /*
444   int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
445   network_link_GTNETS_t *route = ROUTE(card_src->id, card_dst->id);
446 */
447
448 /*
449   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);
450 */
451
452   action = xbt_new0(s_surf_action_network_GTNETS_t, 1);
453
454   action->generic_action.using = 1;
455   action->generic_action.cost = size;
456   action->generic_action.remains = size;
457   /* Max durations are not supported */
458   action->generic_action.max_duration = NO_MAX_DURATION;
459   action->generic_action.start = surf_get_clock();
460   action->generic_action.finish = -1.0;
461   action->generic_action.model_type =
462       (surf_model_t) surf_network_model;
463
464   action->generic_action.state_set =
465       surf_network_model->common_public->states.running_action_set;
466
467   xbt_swag_insert(action, action->generic_action.state_set);
468
469   /* KF: Add a flow to the GTNets Simulation, associated to this action */
470   if (gtnets_create_flow(card_src->id, card_dst->id, size, (void *) action)
471       < 0) {
472     xbt_assert2(0, "Not route between host %s and host %s", card_src->name,
473                 card_dst->name);
474   }
475
476   return (surf_action_t) action;
477 }
478
479 /* Suspend a flow() */
480 static void action_suspend(surf_action_t action)
481 {
482   xbt_assert0(0,
483               "action_suspend() not supported for the GTNets network model");
484 }
485
486 /* Resume a flow() */
487 static void action_resume(surf_action_t action)
488 {
489   xbt_assert0(0,
490               "action_resume() not supported for the GTNets network model");
491 }
492
493 /* Test whether a flow is suspended */
494 static int action_is_suspended(surf_action_t action)
495 {
496   return 0;
497 }
498
499 static void finalize(void)
500 {
501 #if 0
502   int i, j;
503 #endif
504   xbt_dict_free(&network_card_set);
505   xbt_dict_free(&link_set);
506   xbt_swag_free(surf_network_model->common_public->states.
507                 ready_action_set);
508   xbt_swag_free(surf_network_model->common_public->states.
509                 running_action_set);
510   xbt_swag_free(surf_network_model->common_public->states.
511                 failed_action_set);
512   xbt_swag_free(surf_network_model->common_public->states.
513                 done_action_set);
514   free(surf_network_model->common_public);
515   free(surf_network_model->common_private);
516   free(surf_network_model->extension_public);
517
518   free(surf_network_model);
519   surf_network_model = NULL;
520
521   gtnets_finalize();
522 }
523
524 static void surf_network_model_init_internal(void)
525 {
526   s_surf_action_t action;
527
528   surf_network_model = xbt_new0(s_surf_network_model_t, 1);
529
530   surf_network_model->common_private =
531       xbt_new0(s_surf_model_private_t, 1);
532   surf_network_model->common_public =
533       xbt_new0(s_surf_model_public_t, 1);
534   surf_network_model->extension_public =
535       xbt_new0(s_surf_network_model_extension_public_t, 1);
536
537   surf_network_model->common_public->states.ready_action_set =
538       xbt_swag_new(xbt_swag_offset(action, state_hookup));
539   surf_network_model->common_public->states.running_action_set =
540       xbt_swag_new(xbt_swag_offset(action, state_hookup));
541   surf_network_model->common_public->states.failed_action_set =
542       xbt_swag_new(xbt_swag_offset(action, state_hookup));
543   surf_network_model->common_public->states.done_action_set =
544       xbt_swag_new(xbt_swag_offset(action, state_hookup));
545
546   surf_network_model->common_public->name_service = name_service;
547   surf_network_model->common_public->get_resource_name =
548       get_resource_name;
549   surf_network_model->common_public->action_get_state =
550       surf_action_get_state;
551   surf_network_model->common_public->action_use = action_use;
552   surf_network_model->common_public->action_free = action_free;
553   surf_network_model->common_public->action_cancel = action_cancel;
554   surf_network_model->common_public->action_recycle = action_recycle;
555   surf_network_model->common_public->action_change_state =
556       action_change_state;
557   surf_network_model->common_public->action_set_data =
558       surf_action_set_data;
559   surf_network_model->common_public->name = "network";
560
561   surf_network_model->common_private->resource_used = resource_used;
562   surf_network_model->common_private->share_resources = share_resources;
563   surf_network_model->common_private->update_actions_state =
564       update_actions_state;
565   surf_network_model->common_private->update_resource_state = update_resource_state;
566   surf_network_model->common_private->finalize = finalize;
567
568   surf_network_model->common_public->suspend = action_suspend;
569   surf_network_model->common_public->resume = action_resume;
570   surf_network_model->common_public->is_suspended = action_is_suspended;
571
572   surf_network_model->extension_public->communicate = communicate;
573
574   /*for the props of the link*/
575   surf_network_model->common_public->get_properties =  get_properties;
576
577   link_set = xbt_dict_new();
578   network_card_set = xbt_dict_new();
579
580   /* KF: Added the initialization for GTNetS interface */
581   if (gtnets_initialize()) {
582     xbt_assert0(0, "impossible to initialize GTNetS interface");
583   }
584 }
585
586 #ifdef HAVE_GTNETS
587 void surf_network_model_init_GTNETS(const char *filename)
588 {
589   if (surf_network_model)
590     return;
591   surf_network_model_init_internal();
592   define_callbacks(filename);
593   xbt_dynar_push(model_list, &surf_network_model);
594
595   update_model_description(surf_network_model_description,
596                            "GTNets",
597                            (surf_model_t) surf_network_model);
598 }
599 #endif