Logo AND Algorithmique Numérique Distribuée

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