Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Replace xbt_die(bprintf(...)) with xbt_die(...).
[simgrid.git] / src / surf / network_gtnets.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "network_gtnets_private.h"
8 #include "gtnets/gtnets_interface.h"
9 #include "xbt/str.h"
10
11 static double time_to_next_flow_completion = -1;
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_gtnets, surf,
14                                 "Logging specific to the SURF network GTNetS module");
15
16 extern routing_global_t global_routing;
17
18 double sg_gtnets_jitter = 0.0;
19 int sg_gtnets_jitter_seed = 10;
20
21 static void link_new(char *name, double bw, double lat, xbt_dict_t props)
22 {
23   static int link_count = -1;
24   network_link_GTNETS_t gtnets_link;
25
26   if (xbt_dict_get_or_null(surf_network_model->resource_set, name)) {
27     return;
28   }
29
30   XBT_DEBUG("Scanning link name %s", name);
31
32
33   gtnets_link = xbt_new0(s_network_link_GTNETS_t, 1);
34   gtnets_link->generic_resource.name = name;
35   gtnets_link->generic_resource.properties = props;
36   gtnets_link->bw_current = bw;
37   gtnets_link->lat_current = lat;
38
39   link_count++;
40
41   XBT_DEBUG("Adding new link, linkid %d, name %s, latency %g, bandwidth %g",
42            link_count, name, lat, bw);
43
44   if (gtnets_add_link(link_count, bw, lat)) {
45           xbt_die("Cannot create GTNetS link");
46   }
47   gtnets_link->id = link_count;
48
49   xbt_dict_set(surf_network_model->resource_set, name, gtnets_link,
50                surf_resource_free);
51 }
52
53 static void route_new(int src_id, int dst_id, xbt_dynar_t links,
54                       int nb_link)
55 {
56   network_link_GTNETS_t link;
57   unsigned int cursor;
58   int i = 0;
59   int *gtnets_links;
60
61   XBT_IN("(src_id=%d, dst_id=%d, links=%p, nb_link=%d)",
62           src_id, dst_id, links, nb_link);
63
64   /* Build the list of gtnets link IDs */
65   gtnets_links = (int *) calloc(nb_link, sizeof(int));
66   i = 0;
67   xbt_dynar_foreach(links, cursor, link) {
68     gtnets_links[i++] = link->id;
69   }
70
71   if (gtnets_add_route(src_id, dst_id, gtnets_links, nb_link)) {
72     xbt_die("Cannot create GTNetS route");
73   }
74   XBT_OUT();
75 }
76
77 static void route_onehop_new(int src_id, int dst_id,
78                              network_link_GTNETS_t link)
79 {
80   if (gtnets_add_onehop_route(src_id, dst_id, link->id)) {
81     xbt_die("Cannot create GTNetS route");
82   }
83 }
84
85 /* Parse the XML for a network link */
86 static void parse_link_init(void)
87 {
88   char *name;
89   double bw;
90   double lat;
91   e_surf_resource_state_t state;
92   name = xbt_strdup(A_surfxml_link_id);
93   surf_parse_get_double(&bw, A_surfxml_link_bandwidth);
94   surf_parse_get_double(&lat, A_surfxml_link_latency);
95   state = SURF_RESOURCE_ON;
96   XBT_DEBUG("link_gtnets");
97   tmgr_trace_t bw_trace;
98   tmgr_trace_t state_trace;
99   tmgr_trace_t lat_trace;
100
101   bw_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
102   lat_trace = tmgr_trace_new(A_surfxml_link_latency_file);
103   state_trace = tmgr_trace_new(A_surfxml_link_state_file);
104
105   if (bw_trace)
106     XBT_INFO
107         ("The GTNetS network model doesn't support bandwidth state traces");
108   if (lat_trace)
109     XBT_INFO("The GTNetS network model doesn't support latency state traces");
110   if (state_trace)
111     XBT_INFO("The GTNetS network model doesn't support link state traces");
112
113   current_property_set = xbt_dict_new();
114   if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX)
115   {
116           link_new(bprintf("%s_UP",name), bw, lat, current_property_set);
117           link_new(bprintf("%s_DOWN",name), bw, lat, current_property_set);
118
119   }
120   else  link_new(name, bw, lat, current_property_set);
121 }
122
123 /* Create the gtnets topology based on routing strategy */
124 static void create_gtnets_topology()
125 {
126   int src_id,dst_id;
127
128    XBT_DEBUG("Starting topology generation");
129 // À refaire plus tard. Il faut prendre la liste des hôtes/routeurs (dans routing)
130 // À partir de cette liste, on les numérote.
131 // Ensuite, on peut utiliser les id pour refaire les appels GTNets qui suivent.
132
133    //get the onelinks from the parsed platform
134    xbt_dynar_t onelink_routes = global_routing->get_onelink_routes();
135    if (!onelink_routes)
136      return;
137
138    //save them in trace file
139    onelink_t onelink;
140    unsigned int iter;
141    xbt_dynar_foreach(onelink_routes, iter, onelink) {
142      char *src = onelink->src;
143      char *dst = onelink->dst;
144      void *link = onelink->link_ptr;
145      src_id = *((int *) xbt_dict_get_or_null(global_routing->root->to_index,src));
146      dst_id = *((int *) xbt_dict_get_or_null(global_routing->root->to_index,dst));
147
148      if(src_id != dst_id){
149      XBT_DEBUG("Link (#%p), src (#%s), dst (#%s), src_id = %d, dst_id = %d", link,src,dst, src_id, dst_id);
150      XBT_DEBUG("Calling one link route");
151         if(global_routing->get_network_element_type(src) == SURF_NETWORK_ELEMENT_ROUTER){
152                 gtnets_add_router(src_id);
153         }
154         if(global_routing->get_network_element_type(dst) == SURF_NETWORK_ELEMENT_ROUTER){
155          gtnets_add_router(dst_id);
156         }
157         route_onehop_new(src_id, dst_id, (network_link_GTNETS_t)(link));
158      }
159    }
160
161    if (XBT_LOG_ISENABLED(surf_network_gtnets, xbt_log_priority_debug)) {
162         gtnets_print_topology();
163    }
164 }
165
166 /* Main XML parsing */
167 static void define_callbacks(const char *file)
168 {
169   /* Figuring out the network links */
170   surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
171   surfxml_add_callback(ETag_surfxml_platform_cb_list,
172                        &create_gtnets_topology);
173 }
174
175 static int resource_used(void *resource_id)
176 {
177   xbt_die("The resource_used feature is not implemented in GTNets model");
178 }
179
180 static int action_unref(surf_action_t action)
181 {
182   action->refcount--;
183   if (!action->refcount) {
184     xbt_swag_remove(action, action->state_set);
185 #ifdef HAVE_TRACING
186     if (action->category)
187       xbt_free(action->category);
188 #endif
189     surf_action_free(&action);
190     return 1;
191   }
192   return 0;
193 }
194
195 static void action_cancel(surf_action_t action)
196 {
197   xbt_die("Cannot cancel GTNetS flow");
198   return;
199 }
200
201 static void action_recycle(surf_action_t action)
202 {
203   xbt_die("Cannot recycle GTNetS flow");
204   return;
205 }
206
207 static double action_get_remains(surf_action_t action)
208 {
209   return action->remains;
210 }
211
212 static void action_state_set(surf_action_t action,
213                              e_surf_action_state_t state)
214 {
215   surf_action_state_set(action, state);
216 }
217
218 static double share_resources(double now)
219 {
220   xbt_swag_t running_actions =
221       surf_network_model->states.running_action_set;
222
223   //get the first relevant value from the running_actions list
224   if (!xbt_swag_size(running_actions))
225     return -1.0;
226
227   xbt_assert0(time_to_next_flow_completion,
228               "Time to next flow completion not initialized!\n");
229
230   XBT_DEBUG("Calling gtnets_get_time_to_next_flow_completion");
231   time_to_next_flow_completion = gtnets_get_time_to_next_flow_completion();
232   XBT_DEBUG("gtnets_get_time_to_next_flow_completion received %lg",
233          time_to_next_flow_completion);
234
235   return time_to_next_flow_completion;
236 }
237
238 static void update_actions_state(double now, double delta)
239 {
240   surf_action_network_GTNETS_t action = NULL;
241   xbt_swag_t running_actions =
242       surf_network_model->states.running_action_set;
243
244   /* If there are no renning flows, just return */
245   if (time_to_next_flow_completion < 0.0) {
246     return;
247   }
248
249   /* if delta == time_to_next_flow_completion, too. */
250   if (time_to_next_flow_completion <= delta) {  /* run until the first flow completes */
251     void **metadata;
252     int i, num_flows;
253
254     num_flows = 0;
255
256     if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
257       xbt_die("Cannot run GTNetS simulation until next flow completion");
258     }
259     if (num_flows < 1) {
260       xbt_die("GTNetS simulation couldn't find a flow that would complete");
261     }
262
263     xbt_swag_foreach(action, running_actions) {
264       XBT_DEBUG("Action (%p) remains old value: %f", action,
265              action->generic_action.remains);
266       double sent = gtnets_get_flow_rx(action);
267
268 #ifdef HAVE_TRACING
269       double trace_sent = sent;
270       if (trace_sent == 0) {
271         //if sent is equals to 0, means that gtnets sent all the bytes
272         trace_sent = action->generic_action.cost;
273       }
274       // tracing resource utilization
275 // COMMENTED BY DAVID
276 //       int src = TRACE_surf_gtnets_get_src (action);
277 //       int dst = TRACE_surf_gtnets_get_dst (action);
278 //       if (src != -1 && dst != -1){
279 //         xbt_dynar_t route = used_routing->get_route(src, dst);
280 //         network_link_GTNETS_t link;
281 //         unsigned int i;
282 //         xbt_dynar_foreach(route, i, link) {
283 //              TRACE_surf_link_set_utilization (link->generic_resource.name,
284 //             action->generic_action.data, trace_sent/delta, now-delta, delta);
285 //         }
286 //       }
287 #endif
288
289       XBT_DEBUG("Sent value returned by GTNetS : %f", sent);
290       //need to trust this remain value
291       if (sent == 0) {
292         action->generic_action.remains = 0;
293       } else {
294         action->generic_action.remains =
295             action->generic_action.cost - sent;
296       }
297       XBT_DEBUG("Action (%p) remains new value: %f", action,
298              action->generic_action.remains);
299     }
300
301     for (i = 0; i < num_flows; i++) {
302       action = (surf_action_network_GTNETS_t) (metadata[i]);
303
304       action->generic_action.finish = now + time_to_next_flow_completion;
305 #ifdef HAVE_TRACING
306       TRACE_surf_gtnets_destroy(action);
307 #endif
308       action_state_set((surf_action_t) action, SURF_ACTION_DONE);
309       XBT_DEBUG("----> Action (%p) just terminated", action);
310     }
311
312
313   } else {                      /* run for a given number of seconds */
314     if (gtnets_run(delta)) {
315       xbt_die("Cannot run GTNetS simulation");
316     }
317   }
318
319   return;
320 }
321
322 static void update_resource_state(void *id,
323                                   tmgr_trace_event_t event_type,
324                                   double value, double date)
325 {
326   xbt_die("Cannot update model state for GTNetS simulation");
327 }
328
329 /* Max durations are not supported */
330 static surf_action_t communicate(const char *src_name,
331                                  const char *dst_name, double size,
332                                  double rate)
333 {
334   int src, dst;
335
336   // Utiliser le dictionnaire définit dans create_gtnets_topology pour initialiser correctement src et dst
337   src = dst = -1;
338   surf_action_network_GTNETS_t action = NULL;
339
340   src = *((int *) xbt_dict_get_or_null(global_routing->root->to_index,src_name));
341   dst = *((int *) xbt_dict_get_or_null(global_routing->root->to_index,dst_name));
342   xbt_assert0((src >= 0
343                && dst >= 0), "Either src or dst have invalid id (id<0)");
344
345   XBT_DEBUG("Setting flow src %d \"%s\", dst %d \"%s\"", src, src_name, dst,
346          dst_name);
347
348   xbt_dynar_t links = global_routing->get_route(src_name, dst_name);
349   route_new(src, dst, links, xbt_dynar_length(links));
350
351   action =
352       surf_action_new(sizeof(s_surf_action_network_GTNETS_t), size,
353                       surf_network_model, 0);
354
355   /* Add a flow to the GTNets Simulation, associated to this action */
356   if (gtnets_create_flow(src, dst, size, (void *) action) < 0) {
357     xbt_die("Not route between host %s and host %s", src_name, dst_name);
358   }
359 #ifdef HAVE_TRACING
360   TRACE_surf_gtnets_communicate(action, src, dst);
361 #endif
362
363   return (surf_action_t) action;
364 }
365
366 /* Suspend a flow() */
367 static void action_suspend(surf_action_t action)
368 {
369   THROW_UNIMPLEMENTED;
370 }
371
372 /* Resume a flow() */
373 static void action_resume(surf_action_t action)
374 {
375   THROW_UNIMPLEMENTED;
376 }
377
378 /* Test whether a flow is suspended */
379 static int action_is_suspended(surf_action_t action)
380 {
381   return 0;
382 }
383
384 static void finalize(void)
385 {
386   gtnets_finalize();
387 }
388
389 static void surf_network_model_init_internal(void)
390 {
391   surf_network_model = surf_model_init();
392
393   surf_network_model->name = "network GTNetS";
394   surf_network_model->action_unref = action_unref;
395   surf_network_model->action_cancel = action_cancel;
396   surf_network_model->action_recycle = action_recycle;
397   surf_network_model->action_state_set = action_state_set;
398   surf_network_model->get_remains = action_get_remains;
399
400   surf_network_model->model_private->resource_used = resource_used;
401   surf_network_model->model_private->share_resources = share_resources;
402   surf_network_model->model_private->update_actions_state =
403       update_actions_state;
404   surf_network_model->model_private->update_resource_state =
405       update_resource_state;
406   surf_network_model->model_private->finalize = finalize;
407
408   surf_network_model->suspend = action_suspend;
409   surf_network_model->resume = action_resume;
410   surf_network_model->is_suspended = action_is_suspended;
411
412   surf_network_model->extension.network.communicate = communicate;
413
414   /* Added the initialization for GTNetS interface */
415   if (gtnets_initialize(sg_tcp_gamma)) {
416     xbt_die("Impossible to initialize GTNetS interface");
417   }
418
419   routing_model_create(sizeof(network_link_GTNETS_t), NULL, NULL);
420 }
421
422 #ifdef HAVE_LATENCY_BOUND_TRACKING
423 static int get_latency_limited(surf_action_t action)
424 {
425   return 0;
426 }
427 #endif
428
429 #ifdef HAVE_GTNETS
430 void surf_network_model_init_GTNETS(const char *filename)
431 {
432   if (surf_network_model)
433     return;
434   surf_network_model_init_internal();
435   define_callbacks(filename);
436   xbt_dynar_push(model_list, &surf_network_model);
437
438 #ifdef HAVE_LATENCY_BOUND_TRACKING
439   surf_network_model->get_latency_limited = get_latency_limited;
440 #endif
441
442   if (sg_gtnets_jitter > 0.0) {
443     gtnets_set_jitter(sg_gtnets_jitter);
444     gtnets_set_jitter_seed(sg_gtnets_jitter_seed);
445   }
446
447   update_model_description(surf_network_model_description,
448                            "GTNets", surf_network_model);
449 }
450 #endif