Logo AND Algorithmique Numérique Distribuée

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