Logo AND Algorithmique Numérique Distribuée

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