Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] removing obsolete code (host and link declaration is in instr_routing.c)
[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   DEBUG1("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   DEBUG4("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_assert0(0, "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_IN4("(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_assert0(0, "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_assert0(0, "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   DEBUG0("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     INFO0
107         ("The GTNetS network model doesn't support bandwidth state traces");
108   if (lat_trace)
109     INFO0("The GTNetS network model doesn't support latency state traces");
110   if (state_trace)
111     INFO0("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    DEBUG0("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      DEBUG5("Link (#%p), src (#%s), dst (#%s), src_id = %d, dst_id = %d", link,src,dst, src_id, dst_id);
150      DEBUG0("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_assert0(0,
178               "The resource_used feature is not implemented in GTNets model");
179 }
180
181 static int action_unref(surf_action_t action)
182 {
183   action->refcount--;
184   if (!action->refcount) {
185     xbt_swag_remove(action, action->state_set);
186 #ifdef HAVE_TRACING
187     if (action->category)
188       xbt_free(action->category);
189 #endif
190     free(action);
191     return 1;
192   }
193   return 0;
194 }
195
196 static void action_cancel(surf_action_t action)
197 {
198   xbt_assert0(0, "Cannot cancel GTNetS flow");
199   return;
200 }
201
202 static void action_recycle(surf_action_t action)
203 {
204   xbt_assert0(0, "Cannot recycle GTNetS flow");
205   return;
206 }
207
208 static double action_get_remains(surf_action_t action)
209 {
210   return action->remains;
211 }
212
213 static void action_state_set(surf_action_t action,
214                              e_surf_action_state_t state)
215 {
216   surf_action_state_set(action, state);
217 }
218
219 static double share_resources(double now)
220 {
221   xbt_swag_t running_actions =
222       surf_network_model->states.running_action_set;
223
224   //get the first relevant value from the running_actions list
225   if (!xbt_swag_size(running_actions))
226     return -1.0;
227
228   xbt_assert0(time_to_next_flow_completion,
229               "Time to next flow completion not initialized!\n");
230
231   DEBUG0("Calling gtnets_get_time_to_next_flow_completion");
232   time_to_next_flow_completion = gtnets_get_time_to_next_flow_completion();
233   DEBUG1("gtnets_get_time_to_next_flow_completion received %lg",
234          time_to_next_flow_completion);
235
236   return time_to_next_flow_completion;
237 }
238
239 static void update_actions_state(double now, double delta)
240 {
241   surf_action_network_GTNETS_t action = NULL;
242   xbt_swag_t running_actions =
243       surf_network_model->states.running_action_set;
244
245   /* If there are no renning flows, just return */
246   if (time_to_next_flow_completion < 0.0) {
247     return;
248   }
249
250   /* if delta == time_to_next_flow_completion, too. */
251   if (time_to_next_flow_completion <= delta) {  /* run until the first flow completes */
252     void **metadata;
253     int i, num_flows;
254
255     num_flows = 0;
256
257     if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
258       xbt_assert0(0,
259                   "Cannot run GTNetS simulation until next flow completion");
260     }
261     if (num_flows < 1) {
262       xbt_assert0(0,
263                   "GTNetS simulation couldn't find a flow that would complete");
264     }
265
266     xbt_swag_foreach(action, running_actions) {
267       DEBUG2("Action (%p) remains old value: %f", action,
268              action->generic_action.remains);
269       double sent = gtnets_get_flow_rx(action);
270
271 #ifdef HAVE_TRACING
272       double trace_sent = sent;
273       if (trace_sent == 0) {
274         //if sent is equals to 0, means that gtnets sent all the bytes
275         trace_sent = action->generic_action.cost;
276       }
277       // tracing resource utilization
278 // COMMENTED BY DAVID
279 //       int src = TRACE_surf_gtnets_get_src (action);
280 //       int dst = TRACE_surf_gtnets_get_dst (action);
281 //       if (src != -1 && dst != -1){
282 //         xbt_dynar_t route = used_routing->get_route(src, dst);
283 //         network_link_GTNETS_t link;
284 //         unsigned int i;
285 //         xbt_dynar_foreach(route, i, link) {
286 //              TRACE_surf_link_set_utilization (link->generic_resource.name,
287 //             action->generic_action.data, trace_sent/delta, now-delta, delta);
288 //         }
289 //       }
290 #endif
291
292       DEBUG1("Sent value returned by GTNetS : %f", sent);
293       //need to trust this remain value
294       if (sent == 0) {
295         action->generic_action.remains = 0;
296       } else {
297         action->generic_action.remains =
298             action->generic_action.cost - sent;
299       }
300       DEBUG2("Action (%p) remains new value: %f", action,
301              action->generic_action.remains);
302     }
303
304     for (i = 0; i < num_flows; i++) {
305       action = (surf_action_network_GTNETS_t) (metadata[i]);
306
307       action->generic_action.finish = now + time_to_next_flow_completion;
308 #ifdef HAVE_TRACING
309       TRACE_surf_gtnets_destroy(action);
310 #endif
311       action_state_set((surf_action_t) action, SURF_ACTION_DONE);
312       DEBUG1("----> Action (%p) just terminated", action);
313     }
314
315
316   } else {                      /* run for a given number of seconds */
317     if (gtnets_run(delta)) {
318       xbt_assert0(0, "Cannot run GTNetS simulation");
319     }
320   }
321
322   return;
323 }
324
325 static void update_resource_state(void *id,
326                                   tmgr_trace_event_t event_type,
327                                   double value, double date)
328 {
329   xbt_assert0(0, "Cannot update model state for GTNetS simulation");
330 }
331
332 /* Max durations are not supported */
333 static surf_action_t communicate(const char *src_name,
334                                  const char *dst_name, double size,
335                                  double rate)
336 {
337   int src, dst;
338
339   // Utiliser le dictionnaire définit dans create_gtnets_topology pour initialiser correctement src et dst
340   src = dst = -1;
341   surf_action_network_GTNETS_t action = NULL;
342
343   src = *((int *) xbt_dict_get_or_null(global_routing->root->to_index,src_name));
344   dst = *((int *) xbt_dict_get_or_null(global_routing->root->to_index,dst_name));
345   xbt_assert0((src >= 0
346                && dst >= 0), "Either src or dst have invalid id (id<0)");
347
348   DEBUG4("Setting flow src %d \"%s\", dst %d \"%s\"", src, src_name, dst,
349          dst_name);
350
351   xbt_dynar_t links = global_routing->get_route(src_name, dst_name);
352   route_new(src, dst, links, xbt_dynar_length(links));
353
354   action =
355       surf_action_new(sizeof(s_surf_action_network_GTNETS_t), size,
356                       surf_network_model, 0);
357
358   /* Add a flow to the GTNets Simulation, associated to this action */
359   if (gtnets_create_flow(src, dst, size, (void *) action) < 0) {
360     xbt_assert2(0, "Not route between host %s and host %s", src_name,
361                 dst_name);
362   }
363 #ifdef HAVE_TRACING
364   TRACE_surf_gtnets_communicate(action, src, dst);
365 #endif
366
367   return (surf_action_t) action;
368 }
369
370 /* Suspend a flow() */
371 static void action_suspend(surf_action_t action)
372 {
373   THROW_UNIMPLEMENTED;
374 }
375
376 /* Resume a flow() */
377 static void action_resume(surf_action_t action)
378 {
379   THROW_UNIMPLEMENTED;
380 }
381
382 /* Test whether a flow is suspended */
383 static int action_is_suspended(surf_action_t action)
384 {
385   return 0;
386 }
387
388 static void finalize(void)
389 {
390   gtnets_finalize();
391 }
392
393 static void surf_network_model_init_internal(void)
394 {
395   surf_network_model = surf_model_init();
396
397   surf_network_model->name = "network GTNetS";
398   surf_network_model->action_unref = action_unref;
399   surf_network_model->action_cancel = action_cancel;
400   surf_network_model->action_recycle = action_recycle;
401   surf_network_model->action_state_set = action_state_set;
402   surf_network_model->get_remains = action_get_remains;
403
404   surf_network_model->model_private->resource_used = resource_used;
405   surf_network_model->model_private->share_resources = share_resources;
406   surf_network_model->model_private->update_actions_state =
407       update_actions_state;
408   surf_network_model->model_private->update_resource_state =
409       update_resource_state;
410   surf_network_model->model_private->finalize = finalize;
411
412   surf_network_model->suspend = action_suspend;
413   surf_network_model->resume = action_resume;
414   surf_network_model->is_suspended = action_is_suspended;
415
416   surf_network_model->extension.network.communicate = communicate;
417
418   /* Added the initialization for GTNetS interface */
419   if (gtnets_initialize(sg_tcp_gamma)) {
420     xbt_assert0(0, "Impossible to initialize GTNetS interface");
421   }
422
423   routing_model_create(sizeof(network_link_GTNETS_t), NULL, NULL);
424 }
425
426 #ifdef HAVE_LATENCY_BOUND_TRACKING
427 static int get_latency_limited(surf_action_t action)
428 {
429   return 0;
430 }
431 #endif
432
433 #ifdef HAVE_GTNETS
434 void surf_network_model_init_GTNETS(const char *filename)
435 {
436   if (surf_network_model)
437     return;
438   surf_network_model_init_internal();
439   define_callbacks(filename);
440   xbt_dynar_push(model_list, &surf_network_model);
441
442 #ifdef HAVE_LATENCY_BOUND_TRACKING
443   surf_network_model->get_latency_limited = get_latency_limited;
444 #endif
445
446   if (sg_gtnets_jitter > 0.0) {
447     gtnets_set_jitter(sg_gtnets_jitter);
448     gtnets_set_jitter_seed(sg_gtnets_jitter_seed);
449   }
450
451   update_model_description(surf_network_model_description,
452                            "GTNets", surf_network_model);
453 }
454 #endif