Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Removed double free on GTNetS code.
[simgrid.git] / src / surf / network_gtnets.c
1 /* Copyright (c) 2005 Henri Casanova. All rights reserved.                  */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "network_gtnets_private.h"
7 #include "gtnets/gtnets_interface.h"
8 #include "xbt/str.h"
9
10 static double time_to_next_flow_completion = -1;
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_gtnets, surf,
13                                 "Logging specific to the SURF network GTNetS module");
14
15 extern routing_t used_routing;
16 double sg_gtnets_jitter=0.0;
17 int sg_gtnets_jitter_seed=10;
18
19 static void link_new(char *name, double bw, double lat, xbt_dict_t props)
20 {
21   static int link_count = -1;
22   network_link_GTNETS_t gtnets_link;
23
24   if (xbt_dict_get_or_null(surf_network_model->resource_set, name)) {
25     return;
26   }
27
28   link_count++;
29
30   if (gtnets_add_link(link_count, bw, lat)) {
31     xbt_assert0(0, "Cannot create GTNetS link");
32   }
33
34   /* KF: Insert entry in the dictionary */
35   gtnets_link = xbt_new0(s_network_link_GTNETS_t, 1);
36   gtnets_link->generic_resource.name = name;
37   gtnets_link->generic_resource.properties = props;
38   gtnets_link->bw_current = bw;
39   gtnets_link->lat_current = lat;
40   gtnets_link->id = link_count;
41 #ifdef HAVE_TRACING
42   TRACE_surf_net_link_new (name, bw, lat);
43 #endif
44   xbt_dict_set(surf_network_model->resource_set, name, gtnets_link,
45                surf_resource_free);
46 }
47
48 static void route_new(int src_id, int dst_id, xbt_dynar_t links,int nb_link)
49 {
50   network_link_GTNETS_t link;
51   unsigned int cursor;
52   int i=0;
53   int *gtnets_links;
54
55   XBT_IN4("(src_id=%d, dst_id=%d, links=%p, nb_link=%d)",
56           src_id, dst_id, links, nb_link);
57
58   /* Build the list of gtnets link IDs */
59   gtnets_links = (int *) calloc(nb_link, sizeof(int));
60   i = 0;
61   xbt_dynar_foreach(links, cursor, link) {
62     gtnets_links[i++] = link->id;
63   }
64
65   if (gtnets_add_route(src_id, dst_id, gtnets_links, nb_link)) {
66     xbt_assert0(0, "Cannot create GTNetS route");
67   }
68   XBT_OUT;
69 }
70
71 static void route_onehop_new(int src_id, int dst_id,
72                              network_link_GTNETS_t link)
73 {
74   if (gtnets_add_onehop_route(src_id, dst_id, link->id)) {
75     xbt_assert0(0, "Cannot create GTNetS route");
76   }
77 }
78
79 /* Parse the XML for a network link */
80 static void parse_link_init(void)
81 {
82   char *name;
83   double bw;
84   double lat;
85   e_surf_resource_state_t state;
86
87   name = xbt_strdup(A_surfxml_link_id);
88   surf_parse_get_double(&bw, A_surfxml_link_bandwidth);
89   surf_parse_get_double(&lat, A_surfxml_link_latency);
90   state = SURF_RESOURCE_ON;
91
92   tmgr_trace_t bw_trace;
93   tmgr_trace_t state_trace;
94   tmgr_trace_t lat_trace;
95
96   bw_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
97   lat_trace = tmgr_trace_new(A_surfxml_link_latency_file);
98   state_trace = tmgr_trace_new(A_surfxml_link_state_file);
99
100   if (bw_trace)
101     INFO0("The GTNetS network model doesn't support bandwidth state traces");
102   if (lat_trace)
103     INFO0("The GTNetS network model doesn't support latency state traces");
104   if (state_trace)
105     INFO0("The GTNetS network model doesn't support link state traces");
106
107   current_property_set = xbt_dict_new();
108   link_new(name, bw, lat, current_property_set);
109 }
110
111 /* Create the gtnets topology based on routing strategy */
112 static void create_gtnets_topology()
113 {
114   xbt_dict_cursor_t cursor = NULL;
115   char *key, *data;
116
117   xbt_dict_t onelink_routes = used_routing->get_onelink_routes();
118   xbt_assert0(onelink_routes, "Error onelink_routes was not initialized");
119
120   DEBUG0("Starting topology generation");
121
122   xbt_dict_foreach(onelink_routes, cursor, key, data){
123         s_onelink_t link = (s_onelink_t) data;
124         DEBUG3("Link (#%d), src (#%d), dst (#%d)", ((network_link_GTNETS_t)(link->link_ptr))->id , link->src_id, link->dst_id);
125     DEBUG0("Calling one link route");
126     if(used_routing->is_router(link->src_id)){
127         gtnets_add_router(link->src_id);
128     }
129     if(used_routing->is_router(link->dst_id)){
130         gtnets_add_router(link->dst_id);
131     }
132     route_onehop_new(link->src_id, link->dst_id, (network_link_GTNETS_t)(link->link_ptr));
133   }
134
135   xbt_dict_free(&route_table);
136   if (XBT_LOG_ISENABLED(surf_network_gtnets, xbt_log_priority_debug)) {
137           gtnets_print_topology();
138   }
139 }
140
141 /* Main XML parsing */
142 static void define_callbacks(const char *file)
143 {
144   /* Figuring out the network links */
145   surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
146   surfxml_add_callback(ETag_surfxml_platform_cb_list, &create_gtnets_topology);
147 }
148
149 static int resource_used(void *resource_id)
150 {
151   xbt_assert0(0, "The resource_used feature is not implemented in GTNets model");
152 }
153
154 static int action_unref(surf_action_t action)
155 {
156   action->refcount--;
157   if (!action->refcount) {
158     xbt_swag_remove(action, action->state_set);
159     free(action);
160     return 1;
161   }
162   return 0;
163 }
164
165 static void action_cancel(surf_action_t action)
166 {
167   xbt_assert0(0,"Cannot cancel GTNetS flow");
168   return;
169 }
170
171 static void action_recycle(surf_action_t action)
172 {
173   xbt_assert0(0,"Cannot recycle GTNetS flow");
174   return;
175 }
176
177 static double action_get_remains(surf_action_t action)
178 {
179   return action->remains;
180 }
181
182 static void action_state_set(surf_action_t action,
183                              e_surf_action_state_t state)
184 {
185   surf_action_state_set(action, state);
186 }
187
188 static double share_resources(double now)
189 {
190   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
191
192   //get the first relevant value from the running_actions list
193   if (!xbt_swag_size(running_actions))
194     return -1.0;
195
196   xbt_assert0(time_to_next_flow_completion,
197               "Time to next flow completion not initialized!\n");
198
199   DEBUG0("Calling gtnets_get_time_to_next_flow_completion");
200   time_to_next_flow_completion = gtnets_get_time_to_next_flow_completion();
201   DEBUG1("gtnets_get_time_to_next_flow_completion received %lg", time_to_next_flow_completion);
202
203   return time_to_next_flow_completion;
204 }
205
206 static void update_actions_state(double now, double delta)
207 {
208   surf_action_network_GTNETS_t action = NULL;
209   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
210
211   /* If there are no renning flows, just return */
212   if (time_to_next_flow_completion < 0.0) {
213     return;
214   }
215
216   /* if delta == time_to_next_flow_completion, too. */
217   if (time_to_next_flow_completion <= delta) {  /* run until the first flow completes */
218     void **metadata;
219     int i, num_flows;
220
221     num_flows = 0;
222
223     if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
224       xbt_assert0(0,
225                   "Cannot run GTNetS simulation until next flow completion");
226     }
227     if (num_flows < 1) {
228       xbt_assert0(0,
229                   "GTNetS simulation couldn't find a flow that would complete");
230     }
231
232     xbt_swag_foreach(action, running_actions) {
233       DEBUG2("Action (%p) remains old value: %f", action,
234              action->generic_action.remains);
235       double remain = gtnets_get_flow_rx(action);
236
237 #ifdef HAVE_TRACING
238       // tracing surf action
239       TRACE_surf_update_action_state (action, action->generic_action.data,
240                   (action->generic_action.remains-remain)/delta, "BandwidthUsed", now-delta, delta);
241
242       // tracing resource utilization
243       int src = TRACE_surf_gtnets_get_src (action);
244       int dst = TRACE_surf_gtnets_get_dst (action);
245       if (src != -1 && dst != -1){
246         xbt_dynar_t route = used_routing->get_route(src, dst);
247         network_link_GTNETS_t link;
248         unsigned int i;
249         xbt_dynar_foreach(route, i, link) {
250
251           TRACE_surf_update_action_state_net_resource (link->generic_resource.name,
252             action->generic_action.data, (action->generic_action.remains-remain)/delta, now-delta, delta);
253         }
254       }
255 #endif
256
257       DEBUG1("Remain value returned by GTNetS : %f", remain);
258       //need to trust this remain value
259       if (remain == 0) {
260         action->generic_action.remains = 0;
261       } else {
262         action->generic_action.remains = action->generic_action.cost - remain;
263       }
264       DEBUG2("Action (%p) remains new value: %f", action,
265              action->generic_action.remains);
266     }
267
268     for (i = 0; i < num_flows; i++) {
269       action = (surf_action_network_GTNETS_t) (metadata[i]);
270
271       action->generic_action.finish = now + time_to_next_flow_completion;
272 #ifdef HAVE_TRACING
273       TRACE_surf_gtnets_destroy (action);
274 #endif
275       action_state_set((surf_action_t) action, SURF_ACTION_DONE);
276       DEBUG1("----> Action (%p) just terminated", action);
277     }
278
279
280   } else {                      /* run for a given number of seconds */
281     if (gtnets_run(delta)) {
282       xbt_assert0(0, "Cannot run GTNetS simulation");
283     }
284   }
285
286   return;
287 }
288
289 static void update_resource_state(void *id,
290                                   tmgr_trace_event_t event_type,
291                                   double value, double date)
292 {
293   xbt_assert0(0, "Cannot update model state for GTNetS simulation");
294 }
295
296 /* Max durations are not supported */
297 static surf_action_t communicate(const char *src_name, const char *dst_name,
298                                  int src, int dst, double size, double rate)
299 {
300   surf_action_network_GTNETS_t action = NULL;
301
302   xbt_assert0((src >= 0 && dst >= 0), "Either src or dst have invalid id (id<0)");
303
304   DEBUG4("Setting flow src %d \"%s\", dst %d \"%s\"", src, src_name, dst, dst_name);
305
306   xbt_dynar_t links = used_routing->get_route(src, dst);
307   route_new(src, dst, links, xbt_dynar_length(links));
308
309   action =  surf_action_new(sizeof(s_surf_action_network_GTNETS_t), size, surf_network_model, 0);
310
311   /* Add a flow to the GTNets Simulation, associated to this action */
312   if (gtnets_create_flow(src, dst, size, (void *) action) < 0) {
313     xbt_assert2(0, "Not route between host %s and host %s", src_name,
314                 dst_name);
315   }
316 #ifdef HAVE_TRACING
317   TRACE_surf_gtnets_communicate (action, src, dst);
318 #endif
319
320   return (surf_action_t) action;
321 }
322
323 /* Suspend a flow() */
324 static void action_suspend(surf_action_t action)
325 {
326   THROW_UNIMPLEMENTED;
327 }
328
329 /* Resume a flow() */
330 static void action_resume(surf_action_t action)
331 {
332   THROW_UNIMPLEMENTED;
333 }
334
335 /* Test whether a flow is suspended */
336 static int action_is_suspended(surf_action_t action)
337 {
338   return 0;
339 }
340
341 static void finalize(void)
342 {
343   xbt_dict_free(&surf_network_model->resource_set);
344
345   surf_model_exit(surf_network_model);
346   surf_network_model = NULL;
347
348   gtnets_finalize();
349 }
350
351 static void surf_network_model_init_internal(void)
352 {
353   surf_network_model = surf_model_init();
354
355   surf_network_model->name = "network GTNetS";
356   surf_network_model->action_unref = action_unref;
357   surf_network_model->action_cancel = action_cancel;
358   surf_network_model->action_recycle = action_recycle;
359   surf_network_model->action_state_set = action_state_set;
360   surf_network_model->get_remains = action_get_remains;
361
362   surf_network_model->model_private->resource_used = resource_used;
363   surf_network_model->model_private->share_resources = share_resources;
364   surf_network_model->model_private->update_actions_state =
365     update_actions_state;
366   surf_network_model->model_private->update_resource_state =
367     update_resource_state;
368   surf_network_model->model_private->finalize = finalize;
369
370   surf_network_model->suspend = action_suspend;
371   surf_network_model->resume = action_resume;
372   surf_network_model->is_suspended = action_is_suspended;
373
374   surf_network_model->extension.network.communicate = communicate;
375
376   /* Added the initialization for GTNetS interface */
377   if (gtnets_initialize()) {
378     xbt_assert0(0, "Impossible to initialize GTNetS interface");
379   }
380
381   routing_model_create(sizeof(network_link_GTNETS_t), NULL);
382 }
383
384 #ifdef HAVE_GTNETS
385 void surf_network_model_init_GTNETS(const char *filename)
386 {
387   if (surf_network_model)
388     return;
389   surf_network_model_init_internal();
390   define_callbacks(filename);
391   xbt_dynar_push(model_list, &surf_network_model);
392
393   if(sg_gtnets_jitter > 0.0){
394           gtnets_set_jitter(sg_gtnets_jitter);
395           gtnets_set_jitter_seed(sg_gtnets_jitter_seed);
396   }
397
398   update_model_description(surf_network_model_description,
399                            "GTNets", surf_network_model);
400 }
401 #endif