Logo AND Algorithmique Numérique Distribuée

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