Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add in DTD direction for link_ctn and sharing_policy for link and cluster
[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   network_link_GTNETS_t gtnets_link_friend;
26   int tmp_idsrc = -1;
27   int tmp_iddst = -1;
28   char *name_friend;
29
30   if (xbt_dict_get_or_null(surf_network_model->resource_set, name)) {
31     return;
32   }
33
34   DEBUG1("Scanning link name %s", name);
35   sscanf(name, "%d_%d", &tmp_idsrc, &tmp_iddst);
36   DEBUG2("Link name split into %d and %d", tmp_idsrc, tmp_iddst);
37
38   xbt_assert0((tmp_idsrc != -1)
39               && (tmp_idsrc != -1),
40               "You need to respect fullduplex convention x_y for xml link id.");
41
42   name_friend = (char *) calloc(strlen(name), sizeof(char));
43   sprintf(name_friend, "%d_%d", tmp_iddst, tmp_idsrc);
44
45   gtnets_link = xbt_new0(s_network_link_GTNETS_t, 1);
46   gtnets_link->generic_resource.name = name;
47   gtnets_link->generic_resource.properties = props;
48   gtnets_link->bw_current = bw;
49   gtnets_link->lat_current = lat;
50
51   if ((gtnets_link_friend =
52        xbt_dict_get_or_null(surf_network_model->resource_set,
53                             name_friend))) {
54     gtnets_link->id = gtnets_link_friend->id;
55   } else {
56     link_count++;
57
58     DEBUG4("Adding new link, linkid %d, name %s, latency %g, bandwidth %g",
59            link_count, name, lat, bw);
60     if (gtnets_add_link(link_count, bw, lat)) {
61       xbt_assert0(0, "Cannot create GTNetS link");
62     }
63     gtnets_link->id = link_count;
64   }
65   xbt_dict_set(surf_network_model->resource_set, name, gtnets_link,
66                surf_resource_free);
67 #ifdef HAVE_TRACING
68   TRACE_surf_link_declaration(gtnets_link, name, bw, lat);
69 #endif
70 }
71
72 static void route_new(int src_id, int dst_id, xbt_dynar_t links,
73                       int nb_link)
74 {
75   network_link_GTNETS_t link;
76   unsigned int cursor;
77   int i = 0;
78   int *gtnets_links;
79
80   XBT_IN4("(src_id=%d, dst_id=%d, links=%p, nb_link=%d)",
81           src_id, dst_id, links, nb_link);
82
83   /* Build the list of gtnets link IDs */
84   gtnets_links = (int *) calloc(nb_link, sizeof(int));
85   i = 0;
86   xbt_dynar_foreach(links, cursor, link) {
87     gtnets_links[i++] = link->id;
88   }
89
90   if (gtnets_add_route(src_id, dst_id, gtnets_links, nb_link)) {
91     xbt_assert0(0, "Cannot create GTNetS route");
92   }
93   XBT_OUT;
94 }
95
96 static void route_onehop_new(int src_id, int dst_id,
97                              network_link_GTNETS_t link)
98 {
99   if (gtnets_add_onehop_route(src_id, dst_id, link->id)) {
100     xbt_assert0(0, "Cannot create GTNetS route");
101   }
102 }
103
104 /* Parse the XML for a network link */
105 static void parse_link_init(void)
106 {
107   char *name;
108   double bw;
109   double lat;
110   e_surf_resource_state_t state;
111   name = xbt_strdup(A_surfxml_link_id);
112   surf_parse_get_double(&bw, A_surfxml_link_bandwidth);
113   surf_parse_get_double(&lat, A_surfxml_link_latency);
114   state = SURF_RESOURCE_ON;
115   DEBUG0("link_gtnets");
116   tmgr_trace_t bw_trace;
117   tmgr_trace_t state_trace;
118   tmgr_trace_t lat_trace;
119
120   bw_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
121   lat_trace = tmgr_trace_new(A_surfxml_link_latency_file);
122   state_trace = tmgr_trace_new(A_surfxml_link_state_file);
123
124   if (bw_trace)
125     INFO0
126         ("The GTNetS network model doesn't support bandwidth state traces");
127   if (lat_trace)
128     INFO0("The GTNetS network model doesn't support latency state traces");
129   if (state_trace)
130     INFO0("The GTNetS network model doesn't support link state traces");
131
132   current_property_set = xbt_dict_new();
133   if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX)
134   {
135           link_new(bprintf("%s_UP",name), bw, lat, current_property_set);
136           link_new(bprintf("%s_DOWN",name), bw, lat, current_property_set);
137
138   }
139   else  link_new(name, bw, lat, current_property_set);
140 }
141
142 /* Create the gtnets topology based on routing strategy */
143 static void create_gtnets_topology()
144 {
145 //  xbt_dict_cursor_t cursor = NULL;
146 //  char *key, *data;
147 //   xbt_dict_t onelink_routes = global_routing->get_onelink_routes();
148 //   xbt_assert0(onelink_routes, "Error onelink_routes was not initialized");
149 //
150 //   DEBUG0("Starting topology generation");
151 // À refaire plus tard. Il faut prendre la liste des hôtes/routeurs (dans routing)
152 // À partir de cette liste, on les numérote.
153 // Ensuite, on peut utiliser les id pour refaire les appels GTNets qui suivent.
154
155 //   xbt_dict_foreach(onelink_routes, cursor, key, data){
156 //      s_onelink_t link = (s_onelink_t) data;
157 //
158 //      DEBUG3("Link (#%d), src (#%s), dst (#%s)", ((network_link_GTNETS_t)(link->link_ptr))->id , link->src, link->dst);
159 //     DEBUG0("Calling one link route");
160 //     if(global_routing->is_router(link->src)){
161 //      gtnets_add_router(link->src_id);
162 //     }
163 //     if(global_routing->is_router(link->dst)){
164 //      gtnets_add_router(link->dst_id);
165 //     }
166 //     route_onehop_new(link->src_id, link->dst_id, (network_link_GTNETS_t)(link->link_ptr));
167 //   }
168 //
169 //   xbt_dict_free(&route_table);
170 //   if (XBT_LOG_ISENABLED(surf_network_gtnets, xbt_log_priority_debug)) {
171 //        gtnets_print_topology();
172 //   }
173 }
174
175 /* Main XML parsing */
176 static void define_callbacks(const char *file)
177 {
178   /* Figuring out the network links */
179   surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
180   surfxml_add_callback(ETag_surfxml_platform_cb_list,
181                        &create_gtnets_topology);
182 }
183
184 static int resource_used(void *resource_id)
185 {
186   xbt_assert0(0,
187               "The resource_used feature is not implemented in GTNets model");
188 }
189
190 static int action_unref(surf_action_t action)
191 {
192   action->refcount--;
193   if (!action->refcount) {
194     xbt_swag_remove(action, action->state_set);
195 #ifdef HAVE_TRACING
196     if (action->category)
197       xbt_free(action->category);
198 #endif
199     free(action);
200     return 1;
201   }
202   return 0;
203 }
204
205 static void action_cancel(surf_action_t action)
206 {
207   xbt_assert0(0, "Cannot cancel GTNetS flow");
208   return;
209 }
210
211 static void action_recycle(surf_action_t action)
212 {
213   xbt_assert0(0, "Cannot recycle GTNetS flow");
214   return;
215 }
216
217 static double action_get_remains(surf_action_t action)
218 {
219   return action->remains;
220 }
221
222 static void action_state_set(surf_action_t action,
223                              e_surf_action_state_t state)
224 {
225   surf_action_state_set(action, state);
226 }
227
228 static double share_resources(double now)
229 {
230   xbt_swag_t running_actions =
231       surf_network_model->states.running_action_set;
232
233   //get the first relevant value from the running_actions list
234   if (!xbt_swag_size(running_actions))
235     return -1.0;
236
237   xbt_assert0(time_to_next_flow_completion,
238               "Time to next flow completion not initialized!\n");
239
240   DEBUG0("Calling gtnets_get_time_to_next_flow_completion");
241   time_to_next_flow_completion = gtnets_get_time_to_next_flow_completion();
242   DEBUG1("gtnets_get_time_to_next_flow_completion received %lg",
243          time_to_next_flow_completion);
244
245   return time_to_next_flow_completion;
246 }
247
248 static void update_actions_state(double now, double delta)
249 {
250   surf_action_network_GTNETS_t action = NULL;
251   xbt_swag_t running_actions =
252       surf_network_model->states.running_action_set;
253
254   /* If there are no renning flows, just return */
255   if (time_to_next_flow_completion < 0.0) {
256     return;
257   }
258
259   /* if delta == time_to_next_flow_completion, too. */
260   if (time_to_next_flow_completion <= delta) {  /* run until the first flow completes */
261     void **metadata;
262     int i, num_flows;
263
264     num_flows = 0;
265
266     if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
267       xbt_assert0(0,
268                   "Cannot run GTNetS simulation until next flow completion");
269     }
270     if (num_flows < 1) {
271       xbt_assert0(0,
272                   "GTNetS simulation couldn't find a flow that would complete");
273     }
274
275     xbt_swag_foreach(action, running_actions) {
276       DEBUG2("Action (%p) remains old value: %f", action,
277              action->generic_action.remains);
278       double sent = gtnets_get_flow_rx(action);
279
280 #ifdef HAVE_TRACING
281       double trace_sent = sent;
282       if (trace_sent == 0) {
283         //if sent is equals to 0, means that gtnets sent all the bytes
284         trace_sent = action->generic_action.cost;
285       }
286       // tracing resource utilization
287 // COMMENTED BY DAVID
288 //       int src = TRACE_surf_gtnets_get_src (action);
289 //       int dst = TRACE_surf_gtnets_get_dst (action);
290 //       if (src != -1 && dst != -1){
291 //         xbt_dynar_t route = used_routing->get_route(src, dst);
292 //         network_link_GTNETS_t link;
293 //         unsigned int i;
294 //         xbt_dynar_foreach(route, i, link) {
295 //              TRACE_surf_link_set_utilization (link->generic_resource.name,
296 //             action->generic_action.data, trace_sent/delta, now-delta, delta);
297 //         }
298 //       }
299 #endif
300
301       DEBUG1("Sent value returned by GTNetS : %f", sent);
302       //need to trust this remain value
303       if (sent == 0) {
304         action->generic_action.remains = 0;
305       } else {
306         action->generic_action.remains =
307             action->generic_action.cost - sent;
308       }
309       DEBUG2("Action (%p) remains new value: %f", action,
310              action->generic_action.remains);
311     }
312
313     for (i = 0; i < num_flows; i++) {
314       action = (surf_action_network_GTNETS_t) (metadata[i]);
315
316       action->generic_action.finish = now + time_to_next_flow_completion;
317 #ifdef HAVE_TRACING
318       TRACE_surf_gtnets_destroy(action);
319 #endif
320       action_state_set((surf_action_t) action, SURF_ACTION_DONE);
321       DEBUG1("----> Action (%p) just terminated", action);
322     }
323
324
325   } else {                      /* run for a given number of seconds */
326     if (gtnets_run(delta)) {
327       xbt_assert0(0, "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_assert0(0, "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   xbt_assert0((src >= 0
353                && dst >= 0), "Either src or dst have invalid id (id<0)");
354
355   DEBUG4("Setting flow src %d \"%s\", dst %d \"%s\"", src, src_name, dst,
356          dst_name);
357
358   xbt_dynar_t links = global_routing->get_route(src_name, dst_name);
359   route_new(src, dst, links, xbt_dynar_length(links));
360
361   action =
362       surf_action_new(sizeof(s_surf_action_network_GTNETS_t), size,
363                       surf_network_model, 0);
364
365   /* Add a flow to the GTNets Simulation, associated to this action */
366   if (gtnets_create_flow(src, dst, size, (void *) action) < 0) {
367     xbt_assert2(0, "Not route between host %s and host %s", src_name,
368                 dst_name);
369   }
370 #ifdef HAVE_TRACING
371   TRACE_surf_gtnets_communicate(action, src, dst);
372 #endif
373
374   return (surf_action_t) action;
375 }
376
377 /* Suspend a flow() */
378 static void action_suspend(surf_action_t action)
379 {
380   THROW_UNIMPLEMENTED;
381 }
382
383 /* Resume a flow() */
384 static void action_resume(surf_action_t action)
385 {
386   THROW_UNIMPLEMENTED;
387 }
388
389 /* Test whether a flow is suspended */
390 static int action_is_suspended(surf_action_t action)
391 {
392   return 0;
393 }
394
395 static void finalize(void)
396 {
397   gtnets_finalize();
398 }
399
400 static void surf_network_model_init_internal(void)
401 {
402   surf_network_model = surf_model_init();
403
404   surf_network_model->name = "network GTNetS";
405   surf_network_model->action_unref = action_unref;
406   surf_network_model->action_cancel = action_cancel;
407   surf_network_model->action_recycle = action_recycle;
408   surf_network_model->action_state_set = action_state_set;
409   surf_network_model->get_remains = action_get_remains;
410
411   surf_network_model->model_private->resource_used = resource_used;
412   surf_network_model->model_private->share_resources = share_resources;
413   surf_network_model->model_private->update_actions_state =
414       update_actions_state;
415   surf_network_model->model_private->update_resource_state =
416       update_resource_state;
417   surf_network_model->model_private->finalize = finalize;
418
419   surf_network_model->suspend = action_suspend;
420   surf_network_model->resume = action_resume;
421   surf_network_model->is_suspended = action_is_suspended;
422
423   surf_network_model->extension.network.communicate = communicate;
424
425   /* Added the initialization for GTNetS interface */
426   if (gtnets_initialize(sg_tcp_gamma)) {
427     xbt_assert0(0, "Impossible to initialize GTNetS interface");
428   }
429
430   routing_model_create(sizeof(network_link_GTNETS_t), NULL);
431 }
432
433 #ifdef HAVE_LATENCY_BOUND_TRACKING
434 static int get_latency_limited(surf_action_t action)
435 {
436   return 0;
437 }
438 #endif
439
440 #ifdef HAVE_GTNETS
441 void surf_network_model_init_GTNETS(const char *filename)
442 {
443   if (surf_network_model)
444     return;
445   surf_network_model_init_internal();
446   define_callbacks(filename);
447   xbt_dynar_push(model_list, &surf_network_model);
448
449 #ifdef HAVE_LATENCY_BOUND_TRACKING
450   surf_network_model->get_latency_limited = get_latency_limited;
451 #endif
452
453   if (sg_gtnets_jitter > 0.0) {
454     gtnets_set_jitter(sg_gtnets_jitter);
455     gtnets_set_jitter_seed(sg_gtnets_jitter_seed);
456   }
457
458   update_model_description(surf_network_model_description,
459                            "GTNets", surf_network_model);
460 }
461 #endif