Logo AND Algorithmique Numérique Distribuée

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