Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
gtnets tracing possible again, updated to latest routing
[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 renning 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 #ifdef HAVE_TRACING
268       double trace_sent = sent;
269       if (trace_sent == 0) {
270         //if sent is equals to 0, means that gtnets sent all the bytes
271         trace_sent = action->generic_action.cost;
272       }
273       // tracing resource utilization
274       xbt_dynar_t route = global_routing->get_route(action->src_name, action->dst_name);
275       network_link_GTNETS_t link;
276       unsigned int i;
277       xbt_dynar_foreach(route, i, link) {
278         TRACE_surf_link_set_utilization (link->generic_resource.name,
279             action->generic_action.data,
280             (surf_action_t) action,
281             trace_sent/delta,
282             now-delta,
283             delta);
284       }
285 #endif
286
287       XBT_DEBUG("Sent value returned by GTNetS : %f", sent);
288       //need to trust this remain value
289       if (sent == 0) {
290         action->generic_action.remains = 0;
291       } else {
292         action->generic_action.remains =
293             action->generic_action.cost - sent;
294       }
295       XBT_DEBUG("Action (%p) remains new value: %f", action,
296              action->generic_action.remains);
297     }
298
299     for (i = 0; i < num_flows; i++) {
300       action = (surf_action_network_GTNETS_t) (metadata[i]);
301
302       action->generic_action.finish = now + time_to_next_flow_completion;
303 #ifdef HAVE_TRACING
304       TRACE_surf_gtnets_destroy(action);
305 #endif
306       action_state_set((surf_action_t) action, SURF_ACTION_DONE);
307       XBT_DEBUG("----> Action (%p) just terminated", action);
308     }
309
310
311   } else {                      /* run for a given number of seconds */
312     if (gtnets_run(delta)) {
313       xbt_die("Cannot run GTNetS simulation");
314     }
315   }
316
317   return;
318 }
319
320 static void update_resource_state(void *id,
321                                   tmgr_trace_event_t event_type,
322                                   double value, double date)
323 {
324   xbt_die("Cannot update model state for GTNetS simulation");
325 }
326
327 /* Max durations are not supported */
328 static surf_action_t communicate(const char *src_name,
329                                  const char *dst_name, double size,
330                                  double rate)
331 {
332   int src, dst;
333
334   // Utiliser le dictionnaire définit dans create_gtnets_topology pour initialiser correctement src et dst
335   src = dst = -1;
336   surf_action_network_GTNETS_t action = NULL;
337
338   src = *((int *) xbt_dict_get_or_null(global_routing->root->to_index,src_name));
339   dst = *((int *) xbt_dict_get_or_null(global_routing->root->to_index,dst_name));
340   xbt_assert((src >= 0
341                && dst >= 0), "Either src or dst have invalid id (id<0)");
342
343   XBT_DEBUG("Setting flow src %d \"%s\", dst %d \"%s\"", src, src_name, dst,
344          dst_name);
345
346   xbt_dynar_t links = global_routing->get_route(src_name, dst_name);
347   route_new(src, dst, links, xbt_dynar_length(links));
348
349   action =
350       surf_action_new(sizeof(s_surf_action_network_GTNETS_t), size,
351                       surf_network_model, 0);
352
353   /* Add a flow to the GTNets Simulation, associated to this action */
354   if (gtnets_create_flow(src, dst, size, (void *) action) < 0) {
355     xbt_die("Not route between host %s and host %s", src_name, dst_name);
356   }
357 #ifdef HAVE_TRACING
358   TRACE_surf_gtnets_communicate(action, src_name, dst_name);
359 #endif
360
361   return (surf_action_t) action;
362 }
363
364 /* Suspend a flow() */
365 static void action_suspend(surf_action_t action)
366 {
367   THROW_UNIMPLEMENTED;
368 }
369
370 /* Resume a flow() */
371 static void action_resume(surf_action_t action)
372 {
373   THROW_UNIMPLEMENTED;
374 }
375
376 /* Test whether a flow is suspended */
377 static int action_is_suspended(surf_action_t action)
378 {
379   return 0;
380 }
381
382 #ifdef HAVE_TRACING
383 static void gtnets_action_set_category(surf_action_t action, const char *category)
384 {
385   action->category = xbt_strdup (category);
386 }
387 #endif
388
389 static void finalize(void)
390 {
391   gtnets_finalize();
392 }
393
394 static void surf_network_model_init_internal(void)
395 {
396   surf_network_model = surf_model_init();
397
398   surf_network_model->name = "network GTNetS";
399   surf_network_model->action_unref = action_unref;
400   surf_network_model->action_cancel = action_cancel;
401   surf_network_model->action_recycle = action_recycle;
402   surf_network_model->action_state_set = action_state_set;
403   surf_network_model->get_remains = action_get_remains;
404
405   surf_network_model->model_private->resource_used = resource_used;
406   surf_network_model->model_private->share_resources = share_resources;
407   surf_network_model->model_private->update_actions_state =
408       update_actions_state;
409   surf_network_model->model_private->update_resource_state =
410       update_resource_state;
411   surf_network_model->model_private->finalize = finalize;
412
413   surf_network_model->suspend = action_suspend;
414   surf_network_model->resume = action_resume;
415   surf_network_model->is_suspended = action_is_suspended;
416 #ifdef HAVE_TRACING
417   surf_network_model->set_category = gtnets_action_set_category;
418 #endif
419
420   surf_network_model->extension.network.communicate = communicate;
421
422   /* Added the initialization for GTNetS interface */
423   if (gtnets_initialize(sg_tcp_gamma)) {
424     xbt_die("Impossible to initialize GTNetS interface");
425   }
426
427   routing_model_create(sizeof(network_link_GTNETS_t), NULL, NULL);
428 }
429
430 #ifdef HAVE_LATENCY_BOUND_TRACKING
431 static int get_latency_limited(surf_action_t action)
432 {
433   return 0;
434 }
435 #endif
436
437 #ifdef HAVE_GTNETS
438 void surf_network_model_init_GTNETS(const char *filename)
439 {
440   if (surf_network_model)
441     return;
442   surf_network_model_init_internal();
443   define_callbacks(filename);
444   xbt_dynar_push(model_list, &surf_network_model);
445
446 #ifdef HAVE_LATENCY_BOUND_TRACKING
447   surf_network_model->get_latency_limited = get_latency_limited;
448 #endif
449
450   if (sg_gtnets_jitter > 0.0) {
451     gtnets_set_jitter(sg_gtnets_jitter);
452     gtnets_set_jitter_seed(sg_gtnets_jitter_seed);
453   }
454
455   update_model_description(surf_network_model_description,
456                            "GTNets", surf_network_model);
457 }
458 #endif