Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge commit '045db1657e870c721be490b411868f4181a12ced' into surf++
[simgrid.git] / src / surf / network_gtnets.c
1 /* Copyright (c) 2007-2013. 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_platf_t routing_platf;
18
19 double sg_gtnets_jitter = 0.0;
20 int sg_gtnets_jitter_seed = 10;
21
22 static void link_new(const 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 = xbt_strdup(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 = xbt_new0(int, nb_link);
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(sg_platf_link_cbarg_t link)
87 {
88   XBT_DEBUG("link_gtnets");
89
90   if (link->bandwidth_trace)
91     XBT_INFO
92         ("The GTNetS network model doesn't support bandwidth state traces");
93   if (link->latency_trace)
94     XBT_INFO("The GTNetS network model doesn't support latency state traces");
95   if (link->state_trace)
96     XBT_INFO("The GTNetS network model doesn't support link state traces");
97
98   if (link->policy == SURF_LINK_FULLDUPLEX)
99   {
100     link_new(bprintf("%s_UP",link->id), link->bandwidth, link->latency, current_property_set);
101     link_new(bprintf("%s_DOWN",link->id), link->bandwidth, link->latency, current_property_set);
102
103   }
104   else  link_new(link->id, link->bandwidth, link->latency, current_property_set);
105   current_property_set = NULL;
106 }
107
108 /* Create the gtnets topology based on routing strategy */
109 static void create_gtnets_topology(void)
110 {
111    XBT_DEBUG("Starting topology generation");
112 // FIXME: We should take the list of hosts/routers (in the routing module), number the elements of this list,
113 //   and then you can use the id to reimplement properly the following GTNets calls
114
115    //get the onelinks from the parsed platform
116    xbt_dynar_t onelink_routes = routing_platf->get_onelink_routes();
117    if (!onelink_routes)
118      return;
119
120    //save them in trace file
121    onelink_t onelink;
122    unsigned int iter;
123    xbt_dynar_foreach(onelink_routes, iter, onelink) {
124      void *link = onelink->link_ptr;
125
126      if(onelink->src->id != onelink->dst->id){
127      XBT_DEBUG("Link (#%p), src (#%s), dst (#%s), src_id = %d, dst_id = %d",
128          link,
129          onelink->src->name,
130          onelink->dst->name,
131          onelink->src->id,
132          onelink->dst->id);
133      XBT_DEBUG("Calling one link route");
134         if(onelink->src->rc_type == SURF_NETWORK_ELEMENT_ROUTER){
135           gtnets_add_router(onelink->src->id);
136         }
137         if(onelink->dst->rc_type == SURF_NETWORK_ELEMENT_ROUTER){
138          gtnets_add_router(onelink->dst->id);
139         }
140         route_onehop_new(onelink->src->id, onelink->dst->id, (network_link_GTNETS_t)(link));
141      }
142    }
143
144    if (XBT_LOG_ISENABLED(surf_network_gtnets, xbt_log_priority_debug)) {
145         gtnets_print_topology();
146    }
147 }
148
149 /* Main XML parsing */
150 static void define_callbacks(void)
151 {
152   /* Figuring out the network links */
153   sg_platf_link_add_cb (&parse_link_init);
154   sg_platf_postparse_add_cb(&create_gtnets_topology);
155 }
156
157 static int resource_used(void *resource_id)
158 {
159   xbt_die("The resource_used feature is not implemented in GTNets model");
160 }
161
162 static int action_unref(surf_action_t action)
163 {
164   action->refcount--;
165   if (!action->refcount) {
166     xbt_swag_remove(action, action->state_set);
167 #ifdef HAVE_TRACING
168     xbt_free(action->category);
169 #endif
170     surf_action_free(&action);
171     return 1;
172   }
173   return 0;
174 }
175
176 static void action_cancel(surf_action_t action)
177 {
178   xbt_die("Cannot cancel GTNetS flow");
179   return;
180 }
181
182 static void action_recycle(surf_action_t action)
183 {
184   xbt_die("Cannot recycle GTNetS flow");
185   return;
186 }
187
188 static double action_get_remains(surf_action_t action)
189 {
190   return action->remains;
191 }
192
193 static void action_state_set(surf_action_t action,
194                              e_surf_action_state_t state)
195 {
196   surf_action_state_set(action, state);
197 }
198
199 static double share_resources(double now)
200 {
201   xbt_swag_t running_actions =
202       surf_network_model->states.running_action_set;
203
204   //get the first relevant value from the running_actions list
205   if (!xbt_swag_size(running_actions))
206     return -1.0;
207
208   xbt_assert(time_to_next_flow_completion,
209               "Time to next flow completion not initialized!\n");
210
211   XBT_DEBUG("Calling gtnets_get_time_to_next_flow_completion");
212   time_to_next_flow_completion = gtnets_get_time_to_next_flow_completion();
213   XBT_DEBUG("gtnets_get_time_to_next_flow_completion received %g",
214          time_to_next_flow_completion);
215
216   return time_to_next_flow_completion;
217 }
218
219 static void update_actions_state(double now, double delta)
220 {
221   surf_action_network_GTNETS_t action = NULL;
222   xbt_swag_t running_actions =
223       surf_network_model->states.running_action_set;
224
225   /* If there are no running flows, just return */
226   if (time_to_next_flow_completion < 0.0) {
227     return;
228   }
229
230   /* if delta == time_to_next_flow_completion, too. */
231   if (time_to_next_flow_completion <= delta) {  /* run until the first flow completes */
232     void **metadata;
233     int i, num_flows;
234
235     num_flows = 0;
236
237     if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
238       xbt_die("Cannot run GTNetS simulation until next flow completion");
239     }
240     if (num_flows < 1) {
241       xbt_die("GTNetS simulation couldn't find a flow that would complete");
242     }
243
244     xbt_swag_foreach(action, running_actions) {
245       XBT_DEBUG("Action (%p) remains old value: %f", action,
246              action->generic_action.remains);
247       double sent = gtnets_get_flow_rx(action);
248
249       XBT_DEBUG("Sent value returned by GTNetS : %f", sent);
250
251 #ifdef HAVE_TRACING
252       action->last_remains = action->generic_action.remains;
253 #endif
254
255      //need to trust this remain value
256      if (sent == 0) {
257        action->generic_action.remains = 0;
258       } else {
259         action->generic_action.remains =
260             action->generic_action.cost - sent;
261       }
262
263      // verify that this action is a finishing action.
264      int found=0;
265      for (i = 0; i < num_flows; i++) {
266        if(action == (surf_action_network_GTNETS_t) (metadata[i])){
267            found = 1;
268            break;
269        }
270      }
271
272      // indeed this action have not yet started
273      // because of that we need to fix the remaining to the
274      // original total cost
275      if(found != 1 && action->generic_action.remains == 0 ){
276          action->generic_action.remains = action->generic_action.cost;
277      }
278
279      XBT_DEBUG("Action (%p) remains new value: %f", action,
280              action->generic_action.remains);
281
282 #ifdef HAVE_TRACING
283       if (TRACE_is_enabled()) {
284         double last_amount_sent = (action->generic_action.cost - action->last_remains);
285         double amount_sent = (action->generic_action.cost - action->generic_action.remains);
286
287         // tracing resource utilization
288         xbt_dynar_t route = NULL;
289
290         routing_get_route_and_latency (action->src, action->dst, &route, NULL);
291
292         unsigned int i;
293         for (i = 0; i < xbt_dynar_length (route); i++){
294           network_link_GTNETS_t *link = ((network_link_GTNETS_t*)xbt_dynar_get_ptr (route, i));
295           TRACE_surf_link_set_utilization ((*link)->generic_resource.name,
296                                             ((surf_action_t) action)->category,
297                                             (amount_sent - last_amount_sent)/(delta),
298                                             now-delta,
299                                             delta);
300       }
301       }
302 #endif
303
304
305     }
306
307     for (i = 0; i < num_flows; i++) {
308       action = (surf_action_network_GTNETS_t) (metadata[i]);
309
310
311
312       action->generic_action.finish = now + time_to_next_flow_completion;
313       action_state_set((surf_action_t) action, SURF_ACTION_DONE);
314       XBT_DEBUG("----> Action (%p) just terminated", action);
315
316     }
317
318
319   } else {                      /* run for a given number of seconds */
320     if (gtnets_run(delta)) {
321       xbt_die("Cannot run GTNetS simulation");
322     }
323   }
324
325   return;
326 }
327
328 static void update_resource_state(void *id,
329                                   tmgr_trace_event_t event_type,
330                                   double value, double date)
331 {
332   xbt_die("Cannot update model state for GTNetS simulation");
333 }
334
335 /* Max durations are not supported */
336 static surf_action_t communicate(sg_routing_edge_t src_card,
337                                  sg_routing_edge_t dst_card,
338                                  double size, double rate)
339 {
340   surf_action_network_GTNETS_t action = NULL;
341
342   int src = src_card->id;
343   int dst = dst_card->id;
344   char *src_name = src_card->name;
345   char *dst_name = dst_card->name;
346
347   xbt_assert((src >= 0
348                && dst >= 0), "Either src or dst have invalid id (id<0)");
349
350   XBT_DEBUG("Setting flow src %d \"%s\", dst %d \"%s\"", src, src_name, dst,
351          dst_name);
352
353   xbt_dynar_t route = NULL;
354
355   routing_get_route_and_latency(src_card, dst_card, &route, NULL);
356
357   route_new(src, dst, route, xbt_dynar_length(route));
358
359   action =
360       surf_action_new(sizeof(s_surf_action_network_GTNETS_t), size,
361                       surf_network_model, 0);
362
363 #ifdef HAVE_TRACING
364   action->last_remains = 0;
365 #endif
366
367   /* Add a flow to the GTNets Simulation, associated to this action */
368   if (gtnets_create_flow(src, dst, size, (void *) action) < 0) {
369     xbt_die("Not route between host %s and host %s", src_name, dst_name);
370   }
371 #ifdef HAVE_TRACING
372   TRACE_surf_gtnets_communicate(action, src_card, dst_card);
373 #endif
374
375   return (surf_action_t) action;
376 }
377
378 /* Suspend a flow() */
379 static void action_suspend(surf_action_t action)
380 {
381   THROW_UNIMPLEMENTED;
382 }
383
384 /* Resume a flow() */
385 static void action_resume(surf_action_t action)
386 {
387   THROW_UNIMPLEMENTED;
388 }
389
390 /* Test whether a flow is suspended */
391 static int action_is_suspended(surf_action_t action)
392 {
393   return 0;
394 }
395
396 #ifdef HAVE_TRACING
397 static void gtnets_action_set_category(surf_action_t action, const char *category)
398 {
399   action->category = xbt_strdup (category);
400 }
401 #endif
402
403 static void finalize(void)
404 {
405   gtnets_finalize();
406 }
407
408 static void surf_network_model_init_internal(void)
409 {
410   surf_network_model = surf_model_init();
411
412   surf_network_model->name = "network GTNetS";
413   surf_network_model->action_unref = action_unref;
414   surf_network_model->action_cancel = action_cancel;
415   surf_network_model->action_recycle = action_recycle;
416   surf_network_model->action_state_set = action_state_set;
417   surf_network_model->get_remains = action_get_remains;
418
419   surf_network_model->model_private->resource_used = resource_used;
420   surf_network_model->model_private->share_resources = share_resources;
421   surf_network_model->model_private->update_actions_state =
422       update_actions_state;
423   surf_network_model->model_private->update_resource_state =
424       update_resource_state;
425   surf_network_model->model_private->finalize = finalize;
426
427   surf_network_model->suspend = action_suspend;
428   surf_network_model->resume = action_resume;
429   surf_network_model->is_suspended = action_is_suspended;
430 #ifdef HAVE_TRACING
431   surf_network_model->set_category = gtnets_action_set_category;
432 #endif
433
434   surf_network_model->extension.network.communicate = communicate;
435
436   /* Added the initialization for GTNetS interface */
437   if (gtnets_initialize(sg_tcp_gamma)) {
438     xbt_die("Impossible to initialize GTNetS interface");
439   }
440
441   routing_model_create(NULL);
442 }
443
444 #ifdef HAVE_LATENCY_BOUND_TRACKING
445 static int get_latency_limited(surf_action_t action)
446 {
447   return 0;
448 }
449 #endif
450
451 void surf_network_model_init_GTNETS(void)
452 {
453   if (surf_network_model)
454     return;
455
456   surf_network_model_init_internal();
457   define_callbacks();
458   xbt_dynar_push(model_list, &surf_network_model);
459
460 #ifdef HAVE_LATENCY_BOUND_TRACKING
461   surf_network_model->get_latency_limited = get_latency_limited;
462 #endif
463
464   if (sg_gtnets_jitter > 0.0) {
465     gtnets_set_jitter(sg_gtnets_jitter);
466     gtnets_set_jitter_seed(sg_gtnets_jitter_seed);
467   }
468 }