Logo AND Algorithmique Numérique Distribuée

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