Logo AND Algorithmique Numérique Distribuée

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