Logo AND Algorithmique Numérique Distribuée

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