Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b73385b1cc8aea4d9b082856ebe3f9c8eef5b799
[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_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   XBT_IN1("(%s)", name);
105   /* KF: Check that we haven't seen the network card before */
106   network_card_GTNETS_t card =
107     surf_model_resource_by_name(surf_network_model, name);
108
109   if (!card) {
110     /* KF: Increment the card counter for GTNetS */
111     card_count++;
112
113     /* KF: just use the dictionary to map link names to link indices */
114     card = xbt_new0(s_network_card_GTNETS_t, 1);
115     card->name = xbt_strdup(name);
116     card->id = card_count;
117     xbt_dict_set(surf_model_resource_set(surf_network_model), name, card,
118                  network_card_free);
119   }
120
121   LOG1(xbt_log_priority_trace, "   return %d", card->id);
122   XBT_OUT;
123   /* KF: just return the GTNetS ID as the SURF ID */
124   return card->id;
125 }
126
127 /* Instantiate a new route: MODIFY BY KF */
128 static void route_new(int src_id, int dst_id, network_link_GTNETS_t * links,
129                       int nb_link)
130 {
131   int i;
132   int *gtnets_links;
133   XBT_IN4("(src_id=%d, dst_id=%d, links=%p, nb_link=%d)",
134           src_id, dst_id, links, nb_link);
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] = 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   XBT_OUT;
147 }
148
149 /* Instantiate a new route: MODIFY BY KF */
150 static void route_onehop_new(int src_id, int dst_id,
151                              network_link_GTNETS_t * links, 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 linbst of gtnets link IDs */
160   linkid = links[0]->id;
161
162   /* KF: Create the GTNets route */
163   if (gtnets_add_onehop_route(src_id, dst_id, linkid)) {
164     xbt_assert0(0, "Cannot create GTNetS route");
165   }
166 }
167
168
169
170 /* Parse the XML for a network link */
171 static void parse_link_init(void)
172 {
173   char *name;
174   double bw;
175   double lat;
176   e_surf_link_state_t state;
177
178   name = xbt_strdup(A_surfxml_link_id);
179   surf_parse_get_double(&bw, A_surfxml_link_bandwidth);
180   surf_parse_get_double(&lat, A_surfxml_link_latency);
181   state = SURF_LINK_ON;
182
183   tmgr_trace_t bw_trace;
184   tmgr_trace_t state_trace;
185   tmgr_trace_t lat_trace;
186
187   surf_parse_get_trace(&bw_trace, A_surfxml_link_bandwidth_file);
188   surf_parse_get_trace(&lat_trace, A_surfxml_link_latency_file);
189   surf_parse_get_trace(&state_trace, A_surfxml_link_state_file);
190
191   if (bw_trace)
192     INFO0("The GTNetS network model doesn't support bandwidth state traces");
193   if (lat_trace)
194     INFO0("The GTNetS network model doesn't support latency state traces");
195   if (state_trace)
196     INFO0("The GTNetS network model doesn't support link state traces");
197
198   current_property_set = xbt_dict_new();
199   link_new(name, bw, lat, current_property_set);
200 }
201
202 /* Parses a route from the XML: UNMODIFIED BY HC */
203 static void parse_route_set_endpoints(void)
204 {
205   src_id = network_card_new(A_surfxml_route_src);
206   dst_id = network_card_new(A_surfxml_route_dst);
207   route_action = A_surfxml_route_action;
208 }
209
210 /* KF*/
211 static void parse_route_set_routers(void)
212 {
213   int id = network_card_new(A_surfxml_router_id);
214
215   /* KF: Create the GTNets router */
216   if (gtnets_add_router(id)) {
217     xbt_assert0(0, "Cannot add GTNetS router");
218   }
219 }
220
221 /* Create the route (more than one hops): MODIFIED BY KF */
222 static void parse_route_set_route(void)
223 {
224   char *name;
225   if (src_id != -1 && dst_id != -1) {
226     name = bprintf("%x#%x", src_id, dst_id);
227     manage_route(route_table, name, route_action, 0);
228     free(name);
229   }
230 }
231
232 static void add_route()
233 {
234   xbt_ex_t e;
235   unsigned int cpt = 0;
236   int link_list_capacity = 0;
237   int nb_link = 0;
238   xbt_dict_cursor_t cursor = NULL;
239   char *key, *data, *end;
240   const char *sep = "#";
241   xbt_dynar_t links, keys;
242   static network_link_GTNETS_t *link_list = NULL;
243
244
245   XBT_IN;
246   xbt_dict_foreach(route_table, cursor, key, data) {
247     char *link = NULL;
248     nb_link = 0;
249     links = (xbt_dynar_t) data;
250     keys = xbt_str_split_str(key, sep);
251
252     link_list_capacity = xbt_dynar_length(links);
253     link_list = xbt_new(network_link_GTNETS_t, link_list_capacity);
254
255     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
256     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
257     xbt_dynar_free(&keys);
258
259     xbt_dynar_foreach(links, cpt, link) {
260       TRY {
261         link_list[nb_link++] = xbt_dict_get(link_set, link);
262       }
263       CATCH(e) {
264         RETHROW1("Link %s not found (dict raised this exception: %s)", link);
265       }
266     }
267     if (nb_link == 1)
268       route_onehop_new(src_id, dst_id, link_list, nb_link);
269   }
270
271   xbt_dict_foreach(route_table, cursor, key, data) {
272     char *link = NULL;
273     nb_link = 0;
274     links = (xbt_dynar_t) data;
275     keys = xbt_str_split_str(key, sep);
276
277     link_list_capacity = xbt_dynar_length(links);
278     link_list = xbt_new(network_link_GTNETS_t, link_list_capacity);
279
280     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
281     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
282     xbt_dynar_free(&keys);
283
284     xbt_dynar_foreach(links, cpt, link) {
285       TRY {
286         link_list[nb_link++] = xbt_dict_get(link_set, link);
287       }
288       CATCH(e) {
289         RETHROW1("Link %s not found (dict raised this exception: %s)", link);
290       }
291     }
292     if (nb_link >= 1)
293       route_new(src_id, dst_id, link_list, nb_link);
294   }
295
296   xbt_dict_free(&route_table);
297   gtnets_print_topology();
298   XBT_OUT;
299 }
300
301 /* Main XML parsing */
302 static void define_callbacks(const char *file)
303 {
304   surfxml_add_callback(STag_surfxml_router_cb_list, &parse_route_set_routers);
305   surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
306   surfxml_add_callback(STag_surfxml_route_cb_list,
307                        &parse_route_set_endpoints);
308   surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route);
309   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_route);
310 }
311
312 static const char *get_resource_name(void *resource_id)
313 {
314   return ((network_card_GTNETS_t) resource_id)->name;
315 }
316
317 static xbt_dict_t get_properties(void *link)
318 {
319   return ((network_card_GTNETS_t) link)->properties;
320 }
321
322
323 /* We do not care about this: only used for traces */
324 static int resource_used(void *resource_id)
325 {
326   return 0;                     /* We don't care */
327 }
328
329 static int action_free(surf_action_t action)
330 {
331   action->refcount--;
332   if (!action->refcount) {
333     xbt_swag_remove(action, action->state_set);
334     /* KF: No explicit freeing needed for GTNeTS here */
335     free(action);
336     return 1;
337   }
338   return 0;
339 }
340
341 static void action_use(surf_action_t action)
342 {
343   action->refcount++;
344 }
345
346 static void action_cancel(surf_action_t action)
347 {
348   xbt_assert0(0, "Cannot cancel GTNetS flow");
349   return;
350 }
351
352 static void action_recycle(surf_action_t action)
353 {
354   xbt_assert0(0, "Cannot recycle GTNetS flow");
355   return;
356 }
357
358 static void action_change_state(surf_action_t action,
359                                 e_surf_action_state_t state)
360 {
361 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
362 /*     if(((surf_action_network_GTNETS_t)action)->variable) { */
363 /*       lmm_variable_disable(maxmin_system, ((surf_action_network_GTNETS_t)action)->variable); */
364 /*       ((surf_action_network_GTNETS_t)action)->variable = NULL; */
365 /*     } */
366
367   surf_action_change_state(action, state);
368   return;
369 }
370
371
372 /* share_resources() */
373 static double share_resources(double now)
374 {
375   xbt_swag_t running_actions =
376     surf_network_model->common_public.states.running_action_set;
377
378   //get the first relevant value from the running_actions list
379   if (!xbt_swag_size(running_actions))
380     return -1.0;
381
382   xbt_assert0(time_to_next_flow_completion,
383               "Time to next flow completion not initialized!\n");
384
385   time_to_next_flow_completion = gtnets_get_time_to_next_flow_completion();
386
387   return time_to_next_flow_completion;
388 }
389
390 /* delta: by how many time units the simulation must advance */
391 /* In this function: change the state of actions that terminate */
392 /* The delta may not come from the network, and thus may be different (smaller)
393    than the one returned by the function above */
394 /* If the delta is a network-caused min, then do not emulate any timer in the
395    network simulation, otherwise fake a timer somehow to advance the simulation of min seconds */
396
397 static void update_actions_state(double now, double delta)
398 {
399   surf_action_network_GTNETS_t action = NULL;
400   //  surf_action_network_GTNETS_t next_action = NULL;
401   xbt_swag_t running_actions =
402     surf_network_model->common_public.states.running_action_set;
403
404   /* If there are no renning flows, just return */
405   if (time_to_next_flow_completion < 0.0) {
406     return;
407   }
408
409   /*KF: if delta == time_to_next_flow_completion, too. */
410   if (time_to_next_flow_completion <= delta) {  /* run until the first flow completes */
411     void **metadata;
412     int i, num_flows;
413
414     num_flows = 0;
415
416     if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
417       xbt_assert0(0,
418                   "Cannot run GTNetS simulation until next flow completion");
419     }
420     if (num_flows < 1) {
421       xbt_assert0(0,
422                   "GTNetS simulation couldn't find a flow that would complete");
423     }
424
425     xbt_swag_foreach(action, running_actions) {
426       DEBUG2("Action (%p) remains old value: %f", action,
427              action->generic_action.remains);
428       double remain = gtnets_get_flow_rx(action);
429       DEBUG1("Remain value returned by GTNetS : %f", remain);
430       //need to trust this remain value
431       if (remain == 0) {
432         action->generic_action.remains = 0;
433       } else {
434         action->generic_action.remains = action->generic_action.cost - remain;
435       }
436       DEBUG2("Action (%p) remains new value: %f", action,
437              action->generic_action.remains);
438     }
439
440     for (i = 0; i < num_flows; i++) {
441       action = (surf_action_network_GTNETS_t) (metadata[i]);
442
443       action->generic_action.finish = now + time_to_next_flow_completion;
444       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
445       DEBUG1("----> Action (%p) just terminated", action);
446     }
447
448
449   } else {                      /* run for a given number of seconds */
450     if (gtnets_run(delta)) {
451       xbt_assert0(0, "Cannot run GTNetS simulation");
452     }
453   }
454
455   return;
456 }
457
458 /* UNUSED HERE: no traces */
459 static void update_resource_state(void *id,
460                                   tmgr_trace_event_t event_type,
461                                   double value, double date)
462 {
463   xbt_assert0(0, "Cannot update model state for GTNetS simulation");
464   return;
465 }
466
467 /* KF: Rate not supported */
468 static surf_action_t communicate(void *src, void *dst, double size,
469                                  double rate)
470 {
471   surf_action_network_GTNETS_t action = NULL;
472   network_card_GTNETS_t card_src = src;
473   network_card_GTNETS_t card_dst = dst;
474 /*
475   int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
476   network_link_GTNETS_t *route = ROUTE(card_src->id, card_dst->id);
477 */
478
479 /*
480   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);
481 */
482
483   action = xbt_new0(s_surf_action_network_GTNETS_t, 1);
484
485   action->generic_action.refcount = 1;
486   action->generic_action.cost = size;
487   action->generic_action.remains = size;
488   /* Max durations are not supported */
489   action->generic_action.max_duration = NO_MAX_DURATION;
490   action->generic_action.start = surf_get_clock();
491   action->generic_action.finish = -1.0;
492   action->generic_action.model_type = surf_network_model;
493
494   action->generic_action.state_set =
495     surf_network_model->common_public.states.running_action_set;
496
497   xbt_swag_insert(action, action->generic_action.state_set);
498
499   /* KF: Add a flow to the GTNets Simulation, associated to this action */
500   if (gtnets_create_flow(card_src->id, card_dst->id, size, (void *) action)
501       < 0) {
502     xbt_assert2(0, "Not route between host %s and host %s", card_src->name,
503                 card_dst->name);
504   }
505
506   return (surf_action_t) action;
507 }
508
509 /* Suspend a flow() */
510 static void action_suspend(surf_action_t action)
511 {
512   xbt_assert0(0,
513               "action_suspend() not supported for the GTNets network model");
514 }
515
516 /* Resume a flow() */
517 static void action_resume(surf_action_t action)
518 {
519   xbt_assert0(0,
520               "action_resume() not supported for the GTNets network model");
521 }
522
523 /* Test whether a flow is suspended */
524 static int action_is_suspended(surf_action_t action)
525 {
526   return 0;
527 }
528
529 static void finalize(void)
530 {
531   xbt_dict_free(&link_set);
532
533   surf_model_exit(surf_network_model);
534
535   free(surf_network_model);
536   surf_network_model = NULL;
537
538   gtnets_finalize();
539 }
540
541 static void surf_network_model_init_internal(void)
542 {
543   s_surf_action_t action;
544
545   surf_network_model = xbt_new0(s_surf_network_model_t, 1);
546
547   surf_model_init(surf_network_model);
548
549   surf_network_model->common_public.name_service = name_service;
550   surf_network_model->common_public.get_resource_name = get_resource_name;
551   surf_network_model->common_public.action_get_state = surf_action_get_state;
552   surf_network_model->common_public.action_use = action_use;
553   surf_network_model->common_public.action_free = action_free;
554   surf_network_model->common_public.action_cancel = action_cancel;
555   surf_network_model->common_public.action_recycle = action_recycle;
556   surf_network_model->common_public.action_change_state = action_change_state;
557   surf_network_model->common_public.action_set_data = surf_action_set_data;
558   surf_network_model->common_public.name = "network";
559
560   surf_network_model->model_private->resource_used = resource_used;
561   surf_network_model->model_private->share_resources = share_resources;
562   surf_network_model->model_private->update_actions_state =
563     update_actions_state;
564   surf_network_model->model_private->update_resource_state =
565     update_resource_state;
566   surf_network_model->model_private->finalize = finalize;
567
568   surf_network_model->common_public.suspend = action_suspend;
569   surf_network_model->common_public.resume = action_resume;
570   surf_network_model->common_public.is_suspended = action_is_suspended;
571
572   surf_network_model->extension.network.communicate = communicate;
573
574   /*for the props of the link */
575   surf_network_model->common_public.get_properties = get_properties;
576
577   link_set = xbt_dict_new();
578
579   /* KF: Added the initialization for GTNetS interface */
580   if (gtnets_initialize()) {
581     xbt_assert0(0, "impossible to initialize GTNetS interface");
582   }
583 }
584
585 #ifdef HAVE_GTNETS
586 void surf_network_model_init_GTNETS(const char *filename)
587 {
588   if (surf_network_model)
589     return;
590   surf_network_model_init_internal();
591   define_callbacks(filename);
592   xbt_dynar_push(model_list, &surf_network_model);
593
594   update_model_description(surf_network_model_description,
595                            "GTNets", surf_network_model);
596 }
597 #endif