Logo AND Algorithmique Numérique Distribuée

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