Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
eba71de24164375c80cc23e92a8bbb0bdf672637
[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 "surf/random_mgr.h"
11 #include "xbt/dict.h"
12 #include "xbt/str.h"
13 #include "xbt/log.h"
14
15 typedef struct network_card_Constant {
16   s_surf_resource_t generic_resource;
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_init;
24   int suspended;
25   network_card_Constant_t src;
26   network_card_Constant_t dst;
27 } s_surf_action_network_Constant_t, *surf_action_network_Constant_t;
28
29 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
30 static random_data_t random_latency = NULL;
31 static int card_number = 0;
32 static int host_number = 0;
33
34 static int network_card_new(const char *card_name)
35 {
36   network_card_Constant_t card =
37     surf_model_resource_by_name(surf_network_model, card_name);
38
39   if (!card) {
40     card = xbt_new0(s_network_card_Constant_t, 1);
41     card->generic_resource.name = xbt_strdup(card_name);
42     card->id = card_number++;
43     xbt_dict_set(surf_model_resource_set(surf_network_model), card_name, card,
44                  surf_resource_free);
45   }
46   return card->id;
47 }
48
49 static int src_id = -1;
50 static int dst_id = -1;
51
52 static void parse_route_set_endpoints(void)
53 {
54   src_id = network_card_new(A_surfxml_route_src);
55   dst_id = network_card_new(A_surfxml_route_dst);
56   route_action = A_surfxml_route_action;
57 }
58
59 static void parse_route_set_route(void)
60 {
61   char *name;
62   if (src_id != -1 && dst_id != -1) {
63     name = bprintf("%x#%x", src_id, dst_id);
64     manage_route(route_table, name, route_action, 0);
65     free(name);
66   }
67 }
68
69 static void count_hosts(void)
70 {
71   host_number++;
72 }
73
74 static void define_callbacks(const char *file)
75 {
76   /* Figuring out the network links */
77   surfxml_add_callback(STag_surfxml_host_cb_list, &count_hosts);
78   surfxml_add_callback(STag_surfxml_route_cb_list,
79                        &parse_route_set_endpoints);
80   surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route);
81 }
82
83 static int resource_used(void *resource_id)
84 {
85   return 0;
86 }
87
88 static int action_unref(surf_action_t action)
89 {
90   action->refcount--;
91   if (!action->refcount) {
92     xbt_swag_remove(action, action->state_set);
93     free(action);
94     return 1;
95   }
96   return 0;
97 }
98
99 static void action_cancel(surf_action_t action)
100 {
101   return;
102 }
103
104 static void action_recycle(surf_action_t action)
105 {
106   return;
107 }
108
109 static double share_resources(double now)
110 {
111   surf_action_network_Constant_t action = NULL;
112   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
113   double min = -1.0;
114
115   xbt_swag_foreach(action, running_actions) {
116     if (action->latency > 0) {
117       if (min < 0)
118         min = action->latency;
119       else if (action->latency < min)
120         min = action->latency;
121     }
122   }
123
124   return min;
125 }
126
127 static void update_actions_state(double now, double delta)
128 {
129   surf_action_network_Constant_t action = NULL;
130   surf_action_network_Constant_t next_action = NULL;
131   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
132
133   xbt_swag_foreach_safe(action, next_action, running_actions) {
134     if (action->latency > 0) {
135       if (action->latency > delta) {
136         double_update(&(action->latency), delta);
137       } else {
138         action->latency = 0.0;
139       }
140     }
141     double_update(&(action->generic_action.remains),
142                   action->generic_action.cost * delta / action->lat_init);
143     if (action->generic_action.max_duration != NO_MAX_DURATION)
144       double_update(&(action->generic_action.max_duration), delta);
145
146     if (action->generic_action.remains <= 0) {
147       action->generic_action.finish = surf_get_clock();
148       surf_network_model->action_state_set((surf_action_t) action, SURF_ACTION_DONE);
149     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
150                (action->generic_action.max_duration <= 0)) {
151       action->generic_action.finish = surf_get_clock();
152       surf_network_model->action_state_set((surf_action_t) action, SURF_ACTION_DONE);
153     }
154   }
155
156   return;
157 }
158
159 static void update_resource_state(void *id,
160                                   tmgr_trace_event_t event_type,
161                                   double value, double time)
162 {
163   DIE_IMPOSSIBLE;
164 }
165
166 static surf_action_t communicate(void *src, void *dst, double size,
167                                  double rate)
168 {
169   surf_action_network_Constant_t action = NULL;
170   network_card_Constant_t card_src = src;
171   network_card_Constant_t card_dst = dst;
172
173   XBT_IN4("(%s,%s,%g,%g)", card_src->generic_resource.name, card_dst->generic_resource.name, size, rate);
174
175   action = xbt_new0(s_surf_action_network_Constant_t, 1);
176
177   action->generic_action.refcount = 1;
178   action->generic_action.cost = size;
179   action->generic_action.remains = size;
180   action->generic_action.max_duration = NO_MAX_DURATION;
181   action->generic_action.start = surf_get_clock();
182   action->generic_action.finish = -1.0;
183   action->generic_action.model_type = surf_network_model;
184   action->suspended = 0;
185
186   action->latency = random_generate(random_latency);
187   action->lat_init = action->latency;
188
189   if (action->latency <= 0.0)
190     action->generic_action.state_set =
191       surf_network_model->states.done_action_set;
192   else
193     action->generic_action.state_set =
194       surf_network_model->states.running_action_set;
195
196   xbt_swag_insert(action, action->generic_action.state_set);
197
198
199   XBT_OUT;
200
201   return (surf_action_t) action;
202 }
203
204 /* returns an array of link_Constant_t */
205 static xbt_dynar_t get_route(void *src, void *dst)
206 {
207   xbt_die("Calling this function does not make any sense");
208 }
209
210 static double get_link_bandwidth(const void *link)
211 {
212   DIE_IMPOSSIBLE;
213 }
214
215 static double get_link_latency(const void *link)
216 {
217   DIE_IMPOSSIBLE;
218 }
219
220 static int link_shared(const void *link)
221 {
222   DIE_IMPOSSIBLE;
223 }
224
225 static xbt_dict_t get_properties(void *link)
226 {
227   DIE_IMPOSSIBLE;
228 }
229
230 static void action_suspend(surf_action_t action)
231 {
232   ((surf_action_network_Constant_t) action)->suspended = 1;
233 }
234
235 static void action_resume(surf_action_t action)
236 {
237   if (((surf_action_network_Constant_t) action)->suspended)
238     ((surf_action_network_Constant_t) action)->suspended = 0;
239 }
240
241 static int action_is_suspended(surf_action_t action)
242 {
243   return ((surf_action_network_Constant_t) action)->suspended;
244 }
245
246 static void action_set_max_duration(surf_action_t action, double duration)
247 {
248   action->max_duration = duration;
249 }
250
251 static void finalize(void)
252 {
253   surf_model_exit(surf_network_model);
254   surf_network_model = NULL;
255
256   card_number = 0;
257 }
258
259 static void surf_network_model_init_internal(void)
260 {
261   surf_network_model = surf_model_init();
262
263   surf_network_model->name = "network constant";
264   surf_network_model->action_unref = action_unref;
265   surf_network_model->action_cancel = action_cancel;
266   surf_network_model->action_recycle = action_recycle;
267
268   surf_network_model->model_private->resource_used = resource_used;
269   surf_network_model->model_private->share_resources = share_resources;
270   surf_network_model->model_private->update_actions_state =
271     update_actions_state;
272   surf_network_model->model_private->update_resource_state =
273     update_resource_state;
274   surf_network_model->model_private->finalize = finalize;
275
276   surf_network_model->suspend = action_suspend;
277   surf_network_model->resume = action_resume;
278   surf_network_model->is_suspended = action_is_suspended;
279   surf_cpu_model->set_max_duration = action_set_max_duration;
280
281   surf_network_model->extension.network.communicate = communicate;
282   surf_network_model->extension.network.get_route = get_route;
283   surf_network_model->extension.network.get_link_bandwidth =
284     get_link_bandwidth;
285   surf_network_model->extension.network.get_link_latency = get_link_latency;
286   surf_network_model->extension.network.link_shared = link_shared;
287
288   surf_network_model->get_properties = get_properties;
289
290   if (!random_latency)
291     random_latency = random_new(RAND, 100, 0.0, 1.0, .125, .034);
292 }
293
294 void surf_network_model_init_Constant(const char *filename)
295 {
296
297   if (surf_network_model)
298     return;
299   surf_network_model_init_internal();
300   define_callbacks(filename);
301   xbt_dynar_push(model_list, &surf_network_model);
302
303   update_model_description(surf_network_model_description,
304                            "Constant", surf_network_model);
305 }