Logo AND Algorithmique Numérique Distribuée

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