Logo AND Algorithmique Numérique Distribuée

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