Logo AND Algorithmique Numérique Distribuée

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