Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix on the name of the variable
[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       // tracing resource utilization
274       int src = TRACE_surf_gtnets_get_src (action);
275       int dst = TRACE_surf_gtnets_get_dst (action);
276       if (src != -1 && dst != -1){
277         xbt_dynar_t route = used_routing->get_route(src, dst);
278         network_link_GTNETS_t link;
279         unsigned int i;
280         xbt_dynar_foreach(route, i, link) {
281
282                 TRACE_surf_link_set_utilization (link->generic_resource.name,
283             action->generic_action.data, (action->generic_action.remains-remain)/delta, now-delta, delta);
284         }
285       }
286 #endif
287
288       DEBUG1("Sent value returned by GTNetS : %f", sent);
289       //need to trust this remain value
290       if (sent == 0) {
291         action->generic_action.remains = 0;
292       } else {
293         action->generic_action.remains = action->generic_action.cost - sent;
294       }
295       DEBUG2("Action (%p) remains new value: %f", action,
296              action->generic_action.remains);
297     }
298
299     for (i = 0; i < num_flows; i++) {
300       action = (surf_action_network_GTNETS_t) (metadata[i]);
301
302       action->generic_action.finish = now + time_to_next_flow_completion;
303 #ifdef HAVE_TRACING
304       TRACE_surf_gtnets_destroy (action);
305 #endif
306       action_state_set((surf_action_t) action, SURF_ACTION_DONE);
307       DEBUG1("----> Action (%p) just terminated", action);
308     }
309
310
311   } else {                      /* run for a given number of seconds */
312     if (gtnets_run(delta)) {
313       xbt_assert0(0, "Cannot run GTNetS simulation");
314     }
315   }
316
317   return;
318 }
319
320 static void update_resource_state(void *id,
321                                   tmgr_trace_event_t event_type,
322                                   double value, double date)
323 {
324   xbt_assert0(0, "Cannot update model state for GTNetS simulation");
325 }
326
327 /* Max durations are not supported */
328 static surf_action_t communicate(const char *src_name, const char *dst_name,
329                                  int src, int dst, double size, double rate)
330 {
331   surf_action_network_GTNETS_t action = NULL;
332
333   xbt_assert0((src >= 0 && dst >= 0), "Either src or dst have invalid id (id<0)");
334
335   DEBUG4("Setting flow src %d \"%s\", dst %d \"%s\"", src, src_name, dst, dst_name);
336
337   xbt_dynar_t links = used_routing->get_route(src, dst);
338   route_new(src, dst, links, xbt_dynar_length(links));
339
340   action =  surf_action_new(sizeof(s_surf_action_network_GTNETS_t), size, surf_network_model, 0);
341
342   /* Add a flow to the GTNets Simulation, associated to this action */
343   if (gtnets_create_flow(src, dst, size, (void *) action) < 0) {
344     xbt_assert2(0, "Not route between host %s and host %s", src_name,
345                 dst_name);
346   }
347 #ifdef HAVE_TRACING
348   TRACE_surf_gtnets_communicate (action, src, dst);
349 #endif
350
351   return (surf_action_t) action;
352 }
353
354 /* Suspend a flow() */
355 static void action_suspend(surf_action_t action)
356 {
357   THROW_UNIMPLEMENTED;
358 }
359
360 /* Resume a flow() */
361 static void action_resume(surf_action_t action)
362 {
363   THROW_UNIMPLEMENTED;
364 }
365
366 /* Test whether a flow is suspended */
367 static int action_is_suspended(surf_action_t action)
368 {
369   return 0;
370 }
371
372 static void finalize(void)
373 {
374   gtnets_finalize();
375 }
376
377 static void surf_network_model_init_internal(void)
378 {
379   surf_network_model = surf_model_init();
380
381   surf_network_model->name = "network GTNetS";
382   surf_network_model->action_unref = action_unref;
383   surf_network_model->action_cancel = action_cancel;
384   surf_network_model->action_recycle = action_recycle;
385   surf_network_model->action_state_set = action_state_set;
386   surf_network_model->get_remains = action_get_remains;
387
388   surf_network_model->model_private->resource_used = resource_used;
389   surf_network_model->model_private->share_resources = share_resources;
390   surf_network_model->model_private->update_actions_state =
391     update_actions_state;
392   surf_network_model->model_private->update_resource_state =
393     update_resource_state;
394   surf_network_model->model_private->finalize = finalize;
395
396   surf_network_model->suspend = action_suspend;
397   surf_network_model->resume = action_resume;
398   surf_network_model->is_suspended = action_is_suspended;
399
400   surf_network_model->extension.network.communicate = communicate;
401
402   /* Added the initialization for GTNetS interface */
403   if (gtnets_initialize(sg_tcp_gamma)) {
404     xbt_assert0(0, "Impossible to initialize GTNetS interface");
405   }
406
407   routing_model_create(sizeof(network_link_GTNETS_t), NULL);
408 }
409
410 static int get_latency_limited(surf_action_t action){
411         return 0;
412 }
413
414 #ifdef HAVE_GTNETS
415 void surf_network_model_init_GTNETS(const char *filename)
416 {
417   if (surf_network_model)
418     return;
419   surf_network_model_init_internal();
420   define_callbacks(filename);
421   xbt_dynar_push(model_list, &surf_network_model);
422
423   surf_network_model->get_latency_limited = get_latency_limited;
424
425   if(sg_gtnets_jitter > 0.0){
426           gtnets_set_jitter(sg_gtnets_jitter);
427           gtnets_set_jitter_seed(sg_gtnets_jitter_seed);
428   }
429
430   update_model_description(surf_network_model_description,
431                            "GTNets", surf_network_model);
432 }
433 #endif