Logo AND Algorithmique Numérique Distribuée

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