Logo AND Algorithmique Numérique Distribuée

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