Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix bug introduced by the new routing mechanism, added more debug and error checking.
[simgrid.git] / src / surf / network_gtnets.c
1 /* Copyright (c) 2005 Henri Casanova. All rights reserved.                  */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "network_gtnets_private.h"
7 #include "gtnets/gtnets_interface.h"
8 #include "xbt/str.h"
9 #include "surf_private.h"
10 #include "surf/datatypes.h"
11
12 /* ************************************************************************** */
13 /* *************************** FULL ROUTING ********************************* */
14 typedef struct {
15   s_routing_t generic_routing;
16   xbt_dynar_t *routing_table;
17   void *loopback;
18   size_t size_of_link;
19 } s_routing_full_t,*routing_full_t;
20
21
22 static double time_to_next_flow_completion = -1;
23
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_gtnets, surf,
25                                 "Logging specific to the SURF network GTNetS module");
26
27 extern routing_t used_routing;
28
29 /** QUESTIONS for GTNetS integration
30  **   1. Check that we did the right thing with name_service and get_resource_name
31  **   2. Right now there is no "kill flow" in our GTNetS implementation. Do we
32  **      need to do something about this?
33  **   3. We ignore the fact there is some max_duration on flows (see #2 above)
34  **   4. share_resources() returns a duration, not a date, right?
35  **   5. We don't suppoer "rates"
36  **   6. We don't update "remaining" for ongoing flows. Is it bad?
37  **/
38
39 static int src_id = -1;
40 static int dst_id = -1;
41
42 /* Instantiate a new network link */
43 /* name: some name for the link, from the XML */
44 /* bw: The bandwidth value            */
45 /* lat: The latency value             */
46 static void link_new(char *name, double bw, double lat, xbt_dict_t props)
47 {
48   static int link_count = -1;
49   network_link_GTNETS_t gtnets_link;
50
51   /* If link already exists, nothing to do (FIXME: check that multiple definition match?) */
52   if (xbt_dict_get_or_null(surf_network_model->resource_set, name)) {
53     return;
54   }
55
56   /* KF: Increment the link counter for GTNetS */
57   link_count++;
58
59 /*
60   nw_link->model =  surf_network_model;
61   nw_link->name = name;
62   nw_link->bw_current = bw_initial;
63   if (bw_trace)
64     nw_link->bw_event =
65         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
66   nw_link->lat_current = lat_initial;
67   if (lat_trace)
68     nw_link->lat_event =
69         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
70   nw_link->state_current = state_initial;
71   if (state_trace)
72     nw_link->state_event =
73         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
74 */
75
76   /* KF: Add the link to the GTNetS simulation */
77   if (gtnets_add_link(link_count, bw, lat)) {
78     xbt_assert0(0, "Cannot create GTNetS link");
79   }
80
81   /* KF: Insert entry in the dictionary */
82   gtnets_link = xbt_new0(s_network_link_GTNETS_t, 1);
83   gtnets_link->generic_resource.name = name;
84   gtnets_link->generic_resource.properties = props;
85   gtnets_link->bw_current = bw;
86   gtnets_link->lat_current = lat;
87   gtnets_link->id = link_count;
88
89   xbt_dict_set(surf_network_model->resource_set, name, gtnets_link,
90                surf_resource_free);
91
92   return;
93 }
94
95 /* Instantiate a new network card: MODIFYED BY KF */
96 static int network_card_new(const char *name)
97 {
98   static int card_count = -1;
99
100   XBT_IN1("(%s)", name);
101   /* KF: Check that we haven't seen the network card before */
102   network_card_GTNETS_t card =
103     surf_model_resource_by_name(surf_network_model, 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(surf_model_resource_set(surf_network_model), name, card,
114                  surf_resource_free);
115   }
116
117   LOG1(xbt_log_priority_trace, "   return %d", card->id);
118   XBT_OUT;
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,
125                       int nb_link)
126 {
127   int i;
128   int *gtnets_links;
129   XBT_IN4("(src_id=%d, dst_id=%d, links=%p, nb_link=%d)",
130           src_id, dst_id, links, nb_link);
131
132   /* KF: Build the list of gtnets link IDs */
133   gtnets_links = (int *) calloc(nb_link, sizeof(int));
134   for (i = 0; i < nb_link; i++) {
135     gtnets_links[i] = links[i]->id;
136   }
137
138   /* KF: Create the GTNets route */
139   if (gtnets_add_route(src_id, dst_id, gtnets_links, nb_link)) {
140     xbt_assert0(0, "Cannot create GTNetS route");
141   }
142   XBT_OUT;
143 }
144
145 /* Instantiate a new route: MODIFY BY KF */
146 static void route_onehop_new(int src_id, int dst_id,
147                              network_link_GTNETS_t * links, int nb_link)
148 {
149   int linkid;
150
151   if (nb_link != 1) {
152     xbt_assert0(0, "In onehop_new, nb_link should be 1");
153   }
154
155   /* KF: Build the linbst of gtnets link IDs */
156   linkid = links[0]->id;
157
158   /* KF: Create the GTNets route */
159   if (gtnets_add_onehop_route(src_id, dst_id, linkid)) {
160     xbt_assert0(0, "Cannot create GTNetS route");
161   }
162 }
163
164 /* Read hosts using a full-fledged topollogy */
165 static void routing_full_parse_Shost(void) {
166   int *val = xbt_malloc(sizeof(int));
167   DEBUG2("Seen host %s (#%d)",A_surfxml_host_id,used_routing->host_count);
168   *val = used_routing->host_count++;
169   xbt_dict_set(used_routing->host_id,A_surfxml_host_id,val,xbt_free);
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_resource_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_RESOURCE_ON;
185
186   tmgr_trace_t bw_trace;
187   tmgr_trace_t state_trace;
188   tmgr_trace_t lat_trace;
189
190   bw_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
191   lat_trace = tmgr_trace_new(A_surfxml_link_latency_file);
192   state_trace = tmgr_trace_new(A_surfxml_link_state_file);
193
194   if (bw_trace)
195     INFO0("The GTNetS network model doesn't support bandwidth state traces");
196   if (lat_trace)
197     INFO0("The GTNetS network model doesn't support latency state traces");
198   if (state_trace)
199     INFO0("The GTNetS network model doesn't support link state traces");
200
201   current_property_set = xbt_dict_new();
202   link_new(name, bw, lat, current_property_set);
203 }
204
205 /* Parses a route from the XML: UNMODIFIED BY HC */
206 static void parse_route_set_endpoints(void)
207 {
208   src_id = network_card_new(A_surfxml_route_src);
209   dst_id = network_card_new(A_surfxml_route_dst);
210   route_action = A_surfxml_route_action;
211 }
212
213 /* KF*/
214 static void parse_route_set_routers(void)
215 {
216   int id = network_card_new(A_surfxml_router_id);
217
218   /* KF: Create the GTNets router */
219   if (gtnets_add_router(id)) {
220     xbt_assert0(0, "Cannot add GTNetS router");
221   }
222 }
223
224 /* Create the route (more than one hops): MODIFIED BY KF */
225 static void parse_route_set_route(void)
226 {
227   char *name;
228   if (src_id != -1 && dst_id != -1) {
229     name = bprintf("%x#%x", src_id, dst_id);
230     DEBUG0("Calling manage route with route_action=");
231     switch (route_action) {
232            case A_surfxml_route_action_PREPEND:
233                                    DEBUG0("PREPEND");
234                                    break;
235            case A_surfxml_route_action_POSTPEND:
236                                    DEBUG0("POSTPEND");
237                                    break;
238            case A_surfxml_route_action_OVERRIDE:
239                                    DEBUG0("OVERRIDE");
240                    break;
241            default:break;
242     }
243     manage_route(route_table, name, route_action, 0);
244
245     DEBUG2("Setting a route from NIC %d to NIC %d", src_id, dst_id);
246     free(name);
247   }
248 }
249
250 static void add_route()
251 {
252   xbt_ex_t e;
253   unsigned int cpt = 0;
254   int link_list_capacity = 0;
255   int nb_link = 0;
256   xbt_dict_cursor_t cursor = NULL;
257   char *key, *data, *end;
258   const char *sep = "#";
259   xbt_dynar_t links, keys;
260   static network_link_GTNETS_t *link_list = NULL;
261
262
263   XBT_IN;
264   xbt_dict_foreach(route_table, cursor, key, data) {
265     char *link = NULL;
266     nb_link = 0;
267     links = (xbt_dynar_t) data;
268     keys = xbt_str_split_str(key, sep);
269
270     link_list_capacity = xbt_dynar_length(links);
271     link_list = xbt_new(network_link_GTNETS_t, link_list_capacity);
272
273     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
274     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
275     xbt_dynar_free(&keys);
276
277     xbt_dynar_foreach(links, cpt, link) {
278       TRY {
279         link_list[nb_link++] =
280           xbt_dict_get(surf_network_model->resource_set, link);
281       }
282       CATCH(e) {
283         RETHROW1("Link %s not found (dict raised this exception: %s)", link);
284       }
285     }
286     if (nb_link == 1){
287       DEBUG0("Calling a one link route");
288       route_onehop_new(src_id, dst_id, link_list, nb_link);
289     }
290   }
291
292   xbt_dict_foreach(route_table, cursor, key, data) {
293     char *link = NULL;
294     nb_link = 0;
295     links = (xbt_dynar_t) data;
296     keys = xbt_str_split_str(key, sep);
297
298     link_list_capacity = xbt_dynar_length(links);
299     link_list = xbt_new(network_link_GTNETS_t, link_list_capacity);
300
301     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
302     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
303     xbt_dynar_free(&keys);
304
305     xbt_dynar_foreach(links, cpt, link) {
306       TRY {
307         link_list[nb_link++] =
308           xbt_dict_get(surf_network_model->resource_set, link);
309       }
310       CATCH(e) {
311         RETHROW1("Link %s not found (dict raised this exception: %s)", link);
312       }
313     }
314     if (nb_link >= 1)
315       route_new(src_id, dst_id, link_list, nb_link);
316   }
317
318   xbt_dict_free(&route_table);
319   if (XBT_LOG_ISENABLED(surf_network_gtnets, xbt_log_priority_debug)) {
320           gtnets_print_topology();
321   }
322   XBT_OUT;
323 }
324
325 /* Main XML parsing */
326 static void define_callbacks(const char *file)
327 {
328   routing_full_t routing = xbt_new0(s_routing_full_t,1);
329   routing->generic_routing.name = "Full";
330   routing->generic_routing.host_count = 0;
331   routing->generic_routing.host_id = xbt_dict_new();
332   used_routing = (routing_t) routing;
333   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
334
335   surfxml_add_callback(STag_surfxml_router_cb_list, &parse_route_set_routers);
336   surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
337   surfxml_add_callback(STag_surfxml_route_cb_list,
338                        &parse_route_set_endpoints);
339   surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route);
340   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_route);
341
342 }
343
344 /* We do not care about this: only used for traces */
345 static int resource_used(void *resource_id)
346 {
347   return 0;                     /* We don't care */
348 }
349
350 static int action_unref(surf_action_t action)
351 {
352   action->refcount--;
353   if (!action->refcount) {
354     xbt_swag_remove(action, action->state_set);
355     /* KF: No explicit freeing needed for GTNeTS here */
356     free(action);
357     return 1;
358   }
359   return 0;
360 }
361
362 static void action_cancel(surf_action_t action)
363 {
364   xbt_die("Cannot cancel GTNetS flow");
365   return;
366 }
367
368 static void action_recycle(surf_action_t action)
369 {
370   xbt_die("Cannot recycle GTNetS flow");
371   return;
372 }
373
374 static double action_get_remains(surf_action_t action)
375 {
376   return action->remains;
377 }
378
379 static void action_state_set(surf_action_t action,
380                              e_surf_action_state_t state)
381 {
382 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
383 /*     if(((surf_action_network_GTNETS_t)action)->variable) { */
384 /*       lmm_variable_disable(maxmin_system, ((surf_action_network_GTNETS_t)action)->variable); */
385 /*       ((surf_action_network_GTNETS_t)action)->variable = NULL; */
386 /*     } */
387
388   surf_action_state_set(action, state);
389   return;
390 }
391
392
393 /* share_resources() */
394 static double share_resources(double now)
395 {
396   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
397
398   //get the first relevant value from the running_actions list
399   if (!xbt_swag_size(running_actions))
400     return -1.0;
401
402   xbt_assert0(time_to_next_flow_completion,
403               "Time to next flow completion not initialized!\n");
404
405   DEBUG0("Calling gtnets_get_time_to_next_flow_completion");
406   time_to_next_flow_completion = gtnets_get_time_to_next_flow_completion();
407   DEBUG1("gtnets_get_time_to_next_flow_completion received %lg", time_to_next_flow_completion);
408
409   return time_to_next_flow_completion;
410 }
411
412 /* delta: by how many time units the simulation must advance */
413 /* In this function: change the state of actions that terminate */
414 /* The delta may not come from the network, and thus may be different (smaller)
415    than the one returned by the function above */
416 /* If the delta is a network-caused min, then do not emulate any timer in the
417    network simulation, otherwise fake a timer somehow to advance the simulation of min seconds */
418
419 static void update_actions_state(double now, double delta)
420 {
421   surf_action_network_GTNETS_t action = NULL;
422   //  surf_action_network_GTNETS_t next_action = NULL;
423   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
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     xbt_swag_foreach(action, running_actions) {
447       DEBUG2("Action (%p) remains old value: %f", action,
448              action->generic_action.remains);
449       double remain = gtnets_get_flow_rx(action);
450       DEBUG1("Remain value returned by GTNetS : %f", remain);
451       //need to trust this remain value
452       if (remain == 0) {
453         action->generic_action.remains = 0;
454       } else {
455         action->generic_action.remains = action->generic_action.cost - remain;
456       }
457       DEBUG2("Action (%p) remains new value: %f", action,
458              action->generic_action.remains);
459     }
460
461     for (i = 0; i < num_flows; i++) {
462       action = (surf_action_network_GTNETS_t) (metadata[i]);
463
464       action->generic_action.finish = now + time_to_next_flow_completion;
465       action_state_set((surf_action_t) action, SURF_ACTION_DONE);
466       DEBUG1("----> Action (%p) just terminated", action);
467     }
468
469
470   } else {                      /* run for a given number of seconds */
471     if (gtnets_run(delta)) {
472       xbt_assert0(0, "Cannot run GTNetS simulation");
473     }
474   }
475
476   return;
477 }
478
479 /* UNUSED HERE: no traces */
480 static void update_resource_state(void *id,
481                                   tmgr_trace_event_t event_type,
482                                   double value, double date)
483 {
484   xbt_assert0(0, "Cannot update model state for GTNetS simulation");
485   return;
486 }
487
488 /* KF: Rate not supported */
489 /* Max durations are not supported */
490 static surf_action_t communicate(const char *src_name, const char *dst_name,
491                                  int src, int dst, double size, double rate)
492 {
493   surf_action_network_GTNETS_t action = NULL;
494
495   DEBUG4("Setting flow src %d \"%s\", dst %d \"%s\"", src, src_name, dst, dst_name);
496
497   action =
498     surf_action_new(sizeof(s_surf_action_network_GTNETS_t), size,
499                     surf_network_model, 0);
500
501   /* KF: Add a flow to the GTNets Simulation, associated to this action */
502   if (gtnets_create_flow(src, dst, size, (void *) action) < 0) {
503     xbt_assert2(0, "Not route between host %s and host %s", src_name,
504                 dst_name);
505   }
506
507   return (surf_action_t) action;
508 }
509
510 /* Suspend a flow() */
511 static void action_suspend(surf_action_t action)
512 {
513   THROW_UNIMPLEMENTED;
514 }
515
516 /* Resume a flow() */
517 static void action_resume(surf_action_t action)
518 {
519   THROW_UNIMPLEMENTED;
520 }
521
522 /* Test whether a flow is suspended */
523 static int action_is_suspended(surf_action_t action)
524 {
525   return 0;
526 }
527
528 static void finalize(void)
529 {
530   xbt_dict_free(&surf_network_model->resource_set);
531
532   surf_model_exit(surf_network_model);
533
534   free(surf_network_model);
535   surf_network_model = NULL;
536
537   gtnets_finalize();
538 }
539
540 static void surf_network_model_init_internal(void)
541 {
542   surf_network_model = surf_model_init();
543
544   surf_network_model->name = "network GTNetS";
545   surf_network_model->action_unref = action_unref;
546   surf_network_model->action_cancel = action_cancel;
547   surf_network_model->action_recycle = action_recycle;
548   surf_network_model->action_state_set = action_state_set;
549   surf_network_model->get_remains = action_get_remains;
550
551   surf_network_model->model_private->resource_used = resource_used;
552   surf_network_model->model_private->share_resources = share_resources;
553   surf_network_model->model_private->update_actions_state =
554     update_actions_state;
555   surf_network_model->model_private->update_resource_state =
556     update_resource_state;
557   surf_network_model->model_private->finalize = finalize;
558
559   surf_network_model->suspend = action_suspend;
560   surf_network_model->resume = action_resume;
561   surf_network_model->is_suspended = action_is_suspended;
562
563   surf_network_model->extension.network.communicate = communicate;
564
565   /* KF: Added the initialization for GTNetS interface */
566   if (gtnets_initialize()) {
567     xbt_assert0(0, "impossible to initialize GTNetS interface");
568   }
569
570 }
571
572 #ifdef HAVE_GTNETS
573 void surf_network_model_init_GTNETS(const char *filename)
574 {
575   if (surf_network_model)
576     return;
577   surf_network_model_init_internal();
578   define_callbacks(filename);
579   xbt_dynar_push(model_list, &surf_network_model);
580
581   update_model_description(surf_network_model_description,
582                            "GTNets", surf_network_model);
583 }
584 #endif