Logo AND Algorithmique Numérique Distribuée

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