Logo AND Algorithmique Numérique Distribuée

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