Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c01d32e08652357a085874732a2305c3db3ff95d
[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
112   name = xbt_strdup(A_surfxml_link_id);
113   surf_parse_get_double(&bw, A_surfxml_link_bandwidth);
114   surf_parse_get_double(&lat, A_surfxml_link_latency);
115   state = SURF_RESOURCE_ON;
116
117   tmgr_trace_t bw_trace;
118   tmgr_trace_t state_trace;
119   tmgr_trace_t lat_trace;
120
121   bw_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
122   lat_trace = tmgr_trace_new(A_surfxml_link_latency_file);
123   state_trace = tmgr_trace_new(A_surfxml_link_state_file);
124
125   if (bw_trace)
126     INFO0
127         ("The GTNetS network model doesn't support bandwidth state traces");
128   if (lat_trace)
129     INFO0("The GTNetS network model doesn't support latency state traces");
130   if (state_trace)
131     INFO0("The GTNetS network model doesn't support link state traces");
132
133   current_property_set = xbt_dict_new();
134   link_new(name, bw, lat, current_property_set);
135 }
136
137 /* Create the gtnets topology based on routing strategy */
138 static void create_gtnets_topology()
139 {
140 //  xbt_dict_cursor_t cursor = NULL;
141 //  char *key, *data;
142 //   xbt_dict_t onelink_routes = global_routing->get_onelink_routes();
143 //   xbt_assert0(onelink_routes, "Error onelink_routes was not initialized");
144 //
145 //   DEBUG0("Starting topology generation");
146 // À refaire plus tard. Il faut prendre la liste des hôtes/routeurs (dans routing)
147 // À partir de cette liste, on les numérote.
148 // Ensuite, on peut utiliser les id pour refaire les appels GTNets qui suivent.
149
150 //   xbt_dict_foreach(onelink_routes, cursor, key, data){
151 //      s_onelink_t link = (s_onelink_t) data;
152 //
153 //      DEBUG3("Link (#%d), src (#%s), dst (#%s)", ((network_link_GTNETS_t)(link->link_ptr))->id , link->src, link->dst);
154 //     DEBUG0("Calling one link route");
155 //     if(global_routing->is_router(link->src)){
156 //      gtnets_add_router(link->src_id);
157 //     }
158 //     if(global_routing->is_router(link->dst)){
159 //      gtnets_add_router(link->dst_id);
160 //     }
161 //     route_onehop_new(link->src_id, link->dst_id, (network_link_GTNETS_t)(link->link_ptr));
162 //   }
163 //
164 //   xbt_dict_free(&route_table);
165 //   if (XBT_LOG_ISENABLED(surf_network_gtnets, xbt_log_priority_debug)) {
166 //        gtnets_print_topology();
167 //   }
168 }
169
170 /* Main XML parsing */
171 static void define_callbacks(const char *file)
172 {
173   /* Figuring out the network links */
174   surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
175   surfxml_add_callback(ETag_surfxml_platform_cb_list,
176                        &create_gtnets_topology);
177 }
178
179 static int resource_used(void *resource_id)
180 {
181   xbt_assert0(0,
182               "The resource_used feature is not implemented in GTNets model");
183 }
184
185 static int action_unref(surf_action_t action)
186 {
187   action->refcount--;
188   if (!action->refcount) {
189     xbt_swag_remove(action, action->state_set);
190 #ifdef HAVE_TRACING
191     if (action->category)
192       xbt_free(action->category);
193 #endif
194     free(action);
195     return 1;
196   }
197   return 0;
198 }
199
200 static void action_cancel(surf_action_t action)
201 {
202   xbt_assert0(0, "Cannot cancel GTNetS flow");
203   return;
204 }
205
206 static void action_recycle(surf_action_t action)
207 {
208   xbt_assert0(0, "Cannot recycle GTNetS flow");
209   return;
210 }
211
212 static double action_get_remains(surf_action_t action)
213 {
214   return action->remains;
215 }
216
217 static void action_state_set(surf_action_t action,
218                              e_surf_action_state_t state)
219 {
220   surf_action_state_set(action, state);
221 }
222
223 static double share_resources(double now)
224 {
225   xbt_swag_t running_actions =
226       surf_network_model->states.running_action_set;
227
228   //get the first relevant value from the running_actions list
229   if (!xbt_swag_size(running_actions))
230     return -1.0;
231
232   xbt_assert0(time_to_next_flow_completion,
233               "Time to next flow completion not initialized!\n");
234
235   DEBUG0("Calling gtnets_get_time_to_next_flow_completion");
236   time_to_next_flow_completion = gtnets_get_time_to_next_flow_completion();
237   DEBUG1("gtnets_get_time_to_next_flow_completion received %lg",
238          time_to_next_flow_completion);
239
240   return time_to_next_flow_completion;
241 }
242
243 static void update_actions_state(double now, double delta)
244 {
245   surf_action_network_GTNETS_t action = NULL;
246   xbt_swag_t running_actions =
247       surf_network_model->states.running_action_set;
248
249   /* If there are no renning flows, just return */
250   if (time_to_next_flow_completion < 0.0) {
251     return;
252   }
253
254   /* if delta == time_to_next_flow_completion, too. */
255   if (time_to_next_flow_completion <= delta) {  /* run until the first flow completes */
256     void **metadata;
257     int i, num_flows;
258
259     num_flows = 0;
260
261     if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
262       xbt_assert0(0,
263                   "Cannot run GTNetS simulation until next flow completion");
264     }
265     if (num_flows < 1) {
266       xbt_assert0(0,
267                   "GTNetS simulation couldn't find a flow that would complete");
268     }
269
270     xbt_swag_foreach(action, running_actions) {
271       DEBUG2("Action (%p) remains old value: %f", action,
272              action->generic_action.remains);
273       double sent = gtnets_get_flow_rx(action);
274
275 #ifdef HAVE_TRACING
276       double trace_sent = sent;
277       if (trace_sent == 0) {
278         //if sent is equals to 0, means that gtnets sent all the bytes
279         trace_sent = action->generic_action.cost;
280       }
281       // tracing resource utilization
282 // COMMENTED BY DAVID
283 //       int src = TRACE_surf_gtnets_get_src (action);
284 //       int dst = TRACE_surf_gtnets_get_dst (action);
285 //       if (src != -1 && dst != -1){
286 //         xbt_dynar_t route = used_routing->get_route(src, dst);
287 //         network_link_GTNETS_t link;
288 //         unsigned int i;
289 //         xbt_dynar_foreach(route, i, link) {
290 //              TRACE_surf_link_set_utilization (link->generic_resource.name,
291 //             action->generic_action.data, trace_sent/delta, now-delta, delta);
292 //         }
293 //       }
294 #endif
295
296       DEBUG1("Sent value returned by GTNetS : %f", sent);
297       //need to trust this remain value
298       if (sent == 0) {
299         action->generic_action.remains = 0;
300       } else {
301         action->generic_action.remains =
302             action->generic_action.cost - sent;
303       }
304       DEBUG2("Action (%p) remains new value: %f", action,
305              action->generic_action.remains);
306     }
307
308     for (i = 0; i < num_flows; i++) {
309       action = (surf_action_network_GTNETS_t) (metadata[i]);
310
311       action->generic_action.finish = now + time_to_next_flow_completion;
312 #ifdef HAVE_TRACING
313       TRACE_surf_gtnets_destroy(action);
314 #endif
315       action_state_set((surf_action_t) action, SURF_ACTION_DONE);
316       DEBUG1("----> Action (%p) just terminated", action);
317     }
318
319
320   } else {                      /* run for a given number of seconds */
321     if (gtnets_run(delta)) {
322       xbt_assert0(0, "Cannot run GTNetS simulation");
323     }
324   }
325
326   return;
327 }
328
329 static void update_resource_state(void *id,
330                                   tmgr_trace_event_t event_type,
331                                   double value, double date)
332 {
333   xbt_assert0(0, "Cannot update model state for GTNetS simulation");
334 }
335
336 /* Max durations are not supported */
337 static surf_action_t communicate(const char *src_name,
338                                  const char *dst_name, double size,
339                                  double rate)
340 {
341   int src, dst;
342
343   // Utiliser le dictionnaire définit dans create_gtnets_topology pour initialiser correctement src et dst
344   src = dst = -1;
345   surf_action_network_GTNETS_t action = NULL;
346
347   xbt_assert0((src >= 0
348                && dst >= 0), "Either src or dst have invalid id (id<0)");
349
350   DEBUG4("Setting flow src %d \"%s\", dst %d \"%s\"", src, src_name, dst,
351          dst_name);
352
353   xbt_dynar_t links = global_routing->get_route(src_name, dst_name);
354   route_new(src, dst, links, xbt_dynar_length(links));
355
356   action =
357       surf_action_new(sizeof(s_surf_action_network_GTNETS_t), size,
358                       surf_network_model, 0);
359
360   /* Add a flow to the GTNets Simulation, associated to this action */
361   if (gtnets_create_flow(src, dst, size, (void *) action) < 0) {
362     xbt_assert2(0, "Not route between host %s and host %s", src_name,
363                 dst_name);
364   }
365 #ifdef HAVE_TRACING
366   TRACE_surf_gtnets_communicate(action, src, dst);
367 #endif
368
369   return (surf_action_t) action;
370 }
371
372 /* Suspend a flow() */
373 static void action_suspend(surf_action_t action)
374 {
375   THROW_UNIMPLEMENTED;
376 }
377
378 /* Resume a flow() */
379 static void action_resume(surf_action_t action)
380 {
381   THROW_UNIMPLEMENTED;
382 }
383
384 /* Test whether a flow is suspended */
385 static int action_is_suspended(surf_action_t action)
386 {
387   return 0;
388 }
389
390 static void finalize(void)
391 {
392   gtnets_finalize();
393 }
394
395 static void surf_network_model_init_internal(void)
396 {
397   surf_network_model = surf_model_init();
398
399   surf_network_model->name = "network GTNetS";
400   surf_network_model->action_unref = action_unref;
401   surf_network_model->action_cancel = action_cancel;
402   surf_network_model->action_recycle = action_recycle;
403   surf_network_model->action_state_set = action_state_set;
404   surf_network_model->get_remains = action_get_remains;
405
406   surf_network_model->model_private->resource_used = resource_used;
407   surf_network_model->model_private->share_resources = share_resources;
408   surf_network_model->model_private->update_actions_state =
409       update_actions_state;
410   surf_network_model->model_private->update_resource_state =
411       update_resource_state;
412   surf_network_model->model_private->finalize = finalize;
413
414   surf_network_model->suspend = action_suspend;
415   surf_network_model->resume = action_resume;
416   surf_network_model->is_suspended = action_is_suspended;
417
418   surf_network_model->extension.network.communicate = communicate;
419
420   /* Added the initialization for GTNetS interface */
421   if (gtnets_initialize(sg_tcp_gamma)) {
422     xbt_assert0(0, "Impossible to initialize GTNetS interface");
423   }
424
425   routing_model_create(sizeof(network_link_GTNETS_t), NULL);
426 }
427
428 #ifdef HAVE_LATENCY_BOUND_TRACKING
429 static int get_latency_limited(surf_action_t action)
430 {
431   return 0;
432 }
433 #endif
434
435 #ifdef HAVE_GTNETS
436 void surf_network_model_init_GTNETS(const char *filename)
437 {
438   if (surf_network_model)
439     return;
440   surf_network_model_init_internal();
441   define_callbacks(filename);
442   xbt_dynar_push(model_list, &surf_network_model);
443
444 #ifdef HAVE_LATENCY_BOUND_TRACKING
445   surf_network_model->get_latency_limited = get_latency_limited;
446 #endif
447
448   if (sg_gtnets_jitter > 0.0) {
449     gtnets_set_jitter(sg_gtnets_jitter);
450     gtnets_set_jitter_seed(sg_gtnets_jitter_seed);
451   }
452
453   update_model_description(surf_network_model_description,
454                            "GTNets", surf_network_model);
455 }
456 #endif