Logo AND Algorithmique Numérique Distribuée

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