Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Getting rid of lmm_variables and lmm_systems.
[simgrid.git] / src / surf / network_constant.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "surf_private.h"
9 #include "network_common.h"
10 #include "xbt/dict.h"
11 #include "xbt/str.h"
12 #include "xbt/log.h"
13
14 #define CONSTANT_VALUE 1.0
15 typedef struct network_card_Constant {
16   char *name;
17   int id;
18 } s_network_card_Constant_t, *network_card_Constant_t;
19
20 typedef struct surf_action_network_Constant {
21   s_surf_action_t generic_action;
22   double latency;
23   double lat_current;
24   double rate;
25   int suspended;
26   network_card_Constant_t src;
27   network_card_Constant_t dst;
28 } s_surf_action_network_Constant_t, *surf_action_network_Constant_t;
29
30 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
31
32 static int card_number = 0;
33 static int host_number = 0;
34
35 static void network_card_free(void *nw_card)
36 {
37   free(((network_card_Constant_t) nw_card)->name);
38   free(nw_card);
39 }
40
41 static int network_card_new(const char *card_name)
42 {
43   network_card_Constant_t card =
44       xbt_dict_get_or_null(network_card_set, card_name);
45
46   if (!card) {
47     card = xbt_new0(s_network_card_Constant_t, 1);
48     card->name = xbt_strdup(card_name);
49     card->id = card_number++;
50     xbt_dict_set(network_card_set, card_name, card, network_card_free);
51   }
52   return card->id;
53 }
54
55 static int src_id = -1;
56 static int dst_id = -1;
57
58 static void parse_route_set_endpoints(void)
59 {
60   src_id = network_card_new(A_surfxml_route_src);
61   dst_id = network_card_new(A_surfxml_route_dst);
62   route_action = A_surfxml_route_action;
63   route_link_list = xbt_dynar_new(sizeof(char *), &free_string);
64 }
65
66 static void parse_route_set_route(void)
67 {
68   char *name;
69   if (src_id != -1 && dst_id != -1) {
70     name = bprintf("%x#%x",src_id, dst_id);
71     manage_route(route_table, name, route_action, 0);
72     free(name);    
73   }
74 }
75
76 static void count_hosts(void)
77 {
78    host_number++;
79 }
80
81 static void define_callbacks(const char *file)
82 {
83   /* Figuring out the network links */
84   surfxml_add_callback(STag_surfxml_host_cb_list, &count_hosts);
85   surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties);
86   surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints);
87   surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem);
88   surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route);
89   surfxml_add_callback(STag_surfxml_platform_cb_list, &init_data);
90   surfxml_add_callback(STag_surfxml_set_cb_list, &parse_sets);
91   surfxml_add_callback(STag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_endpoints);
92   surfxml_add_callback(ETag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_route);
93   surfxml_add_callback(STag_surfxml_foreach_cb_list, &parse_foreach);
94   surfxml_add_callback(STag_surfxml_cluster_cb_list, &parse_cluster);
95   surfxml_add_callback(STag_surfxml_trace_cb_list, &parse_trace_init);
96   surfxml_add_callback(ETag_surfxml_trace_cb_list, &parse_trace_finalize);
97   surfxml_add_callback(STag_surfxml_trace_c_connect_cb_list, &parse_trace_c_connect);
98 }
99
100 static void *name_service(const char *name)
101 {
102   network_card_Constant_t card = xbt_dict_get_or_null(network_card_set, name);
103   return card;
104 }
105
106 static const char *get_resource_name(void *resource_id)
107 {
108   return ((network_card_Constant_t) resource_id)->name;
109 }
110
111 static int resource_used(void *resource_id)
112 {
113   return 0;
114 }
115
116 static int action_free(surf_action_t action)
117 {
118   action->using--;
119   if (!action->using) {
120     xbt_swag_remove(action, action->state_set);
121     free(action);
122     return 1;
123   }
124   return 0;
125 }
126
127 static void action_use(surf_action_t action)
128 {
129   action->using++;
130 }
131
132 static void action_cancel(surf_action_t action)
133 {
134   return;
135 }
136
137 static void action_recycle(surf_action_t action)
138 {
139   return;
140 }
141
142 static void action_change_state(surf_action_t action,
143                                 e_surf_action_state_t state)
144 {
145   surf_action_change_state(action, state);
146   return;
147 }
148
149 static double share_resources(double now)
150 {
151   surf_action_network_Constant_t action = NULL;
152   xbt_swag_t running_actions =
153       surf_network_model->common_public->states.running_action_set;
154   double min = -1.0;
155
156   xbt_swag_foreach(action, running_actions) {
157     if (action->latency > 0) {
158       if (min < 0)
159         min = action->latency;
160       else if (action->latency < min)
161         min = action->latency;
162     }
163   }
164
165   return min;
166 }
167
168 static void update_actions_state(double now, double delta)
169 {
170   surf_action_network_Constant_t action = NULL;
171   surf_action_network_Constant_t next_action = NULL;
172   xbt_swag_t running_actions =
173       surf_network_model->common_public->states.running_action_set;
174
175   xbt_swag_foreach_safe(action, next_action, running_actions) {
176     if (action->latency > 0) {
177       if (action->latency > delta) {
178         double_update(&(action->latency), delta);
179       } else {
180         action->latency = 0.0;
181       }
182     }
183     double_update(&(action->generic_action.remains),
184                   action->generic_action.cost * delta/ CONSTANT_VALUE);
185     if (action->generic_action.max_duration != NO_MAX_DURATION)
186       double_update(&(action->generic_action.max_duration), delta);
187
188     if (action->generic_action.remains <= 0) {
189       action->generic_action.finish = surf_get_clock();
190       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
191     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
192                (action->generic_action.max_duration <= 0)) {
193       action->generic_action.finish = surf_get_clock();
194       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
195     } 
196   }
197
198   return;
199 }
200
201 static void update_resource_state(void *id,
202                                   tmgr_trace_event_t event_type,
203                                   double value)
204 {
205   DIE_IMPOSSIBLE;
206 }
207
208 static surf_action_t communicate(void *src, void *dst, double size,
209                                  double rate)
210 {
211   surf_action_network_Constant_t action = NULL;
212   network_card_Constant_t card_src = src;
213   network_card_Constant_t card_dst = dst;
214
215   XBT_IN4("(%s,%s,%g,%g)", card_src->name, card_dst->name, size, rate);
216
217   action = xbt_new0(s_surf_action_network_Constant_t, 1);
218
219   action->generic_action.using = 1;
220   action->generic_action.cost = size;
221   action->generic_action.remains = size;
222   action->generic_action.max_duration = NO_MAX_DURATION;
223   action->generic_action.start = surf_get_clock();
224   action->generic_action.finish = -1.0;
225   action->generic_action.model_type =
226       (surf_model_t) surf_network_model;
227   action->suspended = 0;
228   action->generic_action.state_set =
229       surf_network_model->common_public->states.running_action_set;
230
231   xbt_swag_insert(action, action->generic_action.state_set);
232   action->rate = rate;
233
234   action->latency = CONSTANT_VALUE;
235   action->lat_current = action->latency;
236
237   XBT_OUT;
238
239   return (surf_action_t) action;
240 }
241
242 /* returns an array of link_Constant_t */
243 static const void **get_route(void *src, void *dst)
244 {
245   xbt_assert0(0, "Calling this function does not make any sense");
246   return (const void **) NULL;
247 }
248
249 static int get_route_size(void *src, void *dst)
250 {
251   xbt_assert0(0, "Calling this function does not make any sense");
252   return 0;
253 }
254
255 static const char *get_link_name(const void *link)
256 {
257   DIE_IMPOSSIBLE;
258 }
259
260 static double get_link_bandwidth(const void *link)
261 {
262   DIE_IMPOSSIBLE;
263 }
264
265 static double get_link_latency(const void *link)
266 {
267   DIE_IMPOSSIBLE;
268 }
269
270 static xbt_dict_t get_properties(void *link)
271 {
272   DIE_IMPOSSIBLE;
273 }
274
275 static void action_suspend(surf_action_t action)
276 {
277   ((surf_action_network_Constant_t) action)->suspended = 1;
278 }
279
280 static void action_resume(surf_action_t action)
281 {
282   if (((surf_action_network_Constant_t) action)->suspended)
283     ((surf_action_network_Constant_t) action)->suspended = 0;
284 }
285
286 static int action_is_suspended(surf_action_t action)
287 {
288   return ((surf_action_network_Constant_t) action)->suspended;
289 }
290
291 static void action_set_max_duration(surf_action_t action, double duration)
292 {
293   action->max_duration = duration;
294 }
295
296 static void finalize(void)
297 {
298   xbt_dict_free(&network_card_set);
299   xbt_dict_free(&link_set);
300   xbt_swag_free(surf_network_model->common_public->states.
301                 ready_action_set);
302   xbt_swag_free(surf_network_model->common_public->states.
303                 running_action_set);
304   xbt_swag_free(surf_network_model->common_public->states.
305                 failed_action_set);
306   xbt_swag_free(surf_network_model->common_public->states.
307                 done_action_set);
308   free(surf_network_model->common_public);
309   free(surf_network_model->common_private);
310   free(surf_network_model->extension_public);
311
312   free(surf_network_model);
313   surf_network_model = NULL;
314
315   card_number = 0;
316 }
317
318 static void surf_network_model_init_internal(void)
319 {
320   s_surf_action_t action;
321
322   surf_network_model = xbt_new0(s_surf_network_model_t, 1);
323
324   surf_network_model->common_private =
325       xbt_new0(s_surf_model_private_t, 1);
326   surf_network_model->common_public =
327       xbt_new0(s_surf_model_public_t, 1);
328   surf_network_model->extension_public =
329       xbt_new0(s_surf_network_model_extension_public_t, 1);
330
331   surf_network_model->common_public->states.ready_action_set =
332       xbt_swag_new(xbt_swag_offset(action, state_hookup));
333   surf_network_model->common_public->states.running_action_set =
334       xbt_swag_new(xbt_swag_offset(action, state_hookup));
335   surf_network_model->common_public->states.failed_action_set =
336       xbt_swag_new(xbt_swag_offset(action, state_hookup));
337   surf_network_model->common_public->states.done_action_set =
338       xbt_swag_new(xbt_swag_offset(action, state_hookup));
339
340   surf_network_model->common_public->name_service = name_service;
341   surf_network_model->common_public->get_resource_name =
342       get_resource_name;
343   surf_network_model->common_public->action_get_state =
344       surf_action_get_state;
345   surf_network_model->common_public->action_get_start_time =
346       surf_action_get_start_time;
347   surf_network_model->common_public->action_get_finish_time =
348       surf_action_get_finish_time;
349   surf_network_model->common_public->action_free = action_free;
350   surf_network_model->common_public->action_use = action_use;
351   surf_network_model->common_public->action_cancel = action_cancel;
352   surf_network_model->common_public->action_recycle = action_recycle;
353   surf_network_model->common_public->action_change_state =
354       action_change_state;
355   surf_network_model->common_public->action_set_data =
356       surf_action_set_data;
357   surf_network_model->common_public->name = "network";
358
359   surf_network_model->common_private->resource_used = resource_used;
360   surf_network_model->common_private->share_resources = share_resources;
361   surf_network_model->common_private->update_actions_state =
362       update_actions_state;
363   surf_network_model->common_private->update_resource_state =
364       update_resource_state;
365   surf_network_model->common_private->finalize = finalize;
366
367   surf_network_model->common_public->suspend = action_suspend;
368   surf_network_model->common_public->resume = action_resume;
369   surf_network_model->common_public->is_suspended = action_is_suspended;
370   surf_cpu_model->common_public->set_max_duration =
371       action_set_max_duration;
372
373   surf_network_model->extension_public->communicate = communicate;
374   surf_network_model->extension_public->get_route = get_route;
375   surf_network_model->extension_public->get_route_size = get_route_size;
376   surf_network_model->extension_public->get_link_name = get_link_name;
377   surf_network_model->extension_public->get_link_bandwidth =
378       get_link_bandwidth;
379   surf_network_model->extension_public->get_link_latency =
380       get_link_latency;
381
382   surf_network_model->common_public->get_properties =  get_properties;
383
384   link_set = xbt_dict_new();
385   network_card_set = xbt_dict_new();
386
387 }
388
389 void surf_network_model_init_Constant(const char *filename)
390 {
391
392   if (surf_network_model)
393     return;
394   surf_network_model_init_internal();
395   define_callbacks(filename);
396   xbt_dynar_push(model_list, &surf_network_model);
397
398   update_model_description(surf_network_model_description,
399                               surf_network_model_description_size,
400                               "Constant",
401                               (surf_model_t) surf_network_model);
402 }