Logo AND Algorithmique Numérique Distribuée

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