Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Factorize the code of model->action_state_set where possible
[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_free(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_use(surf_action_t action)
100 {
101   action->refcount++;
102 }
103
104 static void action_cancel(surf_action_t action)
105 {
106   return;
107 }
108
109 static void action_recycle(surf_action_t action)
110 {
111   return;
112 }
113
114 static double share_resources(double now)
115 {
116   surf_action_network_Constant_t action = NULL;
117   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
118   double min = -1.0;
119
120   xbt_swag_foreach(action, running_actions) {
121     if (action->latency > 0) {
122       if (min < 0)
123         min = action->latency;
124       else if (action->latency < min)
125         min = action->latency;
126     }
127   }
128
129   return min;
130 }
131
132 static void update_actions_state(double now, double delta)
133 {
134   surf_action_network_Constant_t action = NULL;
135   surf_action_network_Constant_t next_action = NULL;
136   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
137
138   xbt_swag_foreach_safe(action, next_action, running_actions) {
139     if (action->latency > 0) {
140       if (action->latency > delta) {
141         double_update(&(action->latency), delta);
142       } else {
143         action->latency = 0.0;
144       }
145     }
146     double_update(&(action->generic_action.remains),
147                   action->generic_action.cost * delta / action->lat_init);
148     if (action->generic_action.max_duration != NO_MAX_DURATION)
149       double_update(&(action->generic_action.max_duration), delta);
150
151     if (action->generic_action.remains <= 0) {
152       action->generic_action.finish = surf_get_clock();
153       surf_network_model->action_state_set((surf_action_t) action, SURF_ACTION_DONE);
154     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
155                (action->generic_action.max_duration <= 0)) {
156       action->generic_action.finish = surf_get_clock();
157       surf_network_model->action_state_set((surf_action_t) action, SURF_ACTION_DONE);
158     }
159   }
160
161   return;
162 }
163
164 static void update_resource_state(void *id,
165                                   tmgr_trace_event_t event_type,
166                                   double value, double time)
167 {
168   DIE_IMPOSSIBLE;
169 }
170
171 static surf_action_t communicate(void *src, void *dst, double size,
172                                  double rate)
173 {
174   surf_action_network_Constant_t action = NULL;
175   network_card_Constant_t card_src = src;
176   network_card_Constant_t card_dst = dst;
177
178   XBT_IN4("(%s,%s,%g,%g)", card_src->generic_resource.name, card_dst->generic_resource.name, size, rate);
179
180   action = xbt_new0(s_surf_action_network_Constant_t, 1);
181
182   action->generic_action.refcount = 1;
183   action->generic_action.cost = size;
184   action->generic_action.remains = size;
185   action->generic_action.max_duration = NO_MAX_DURATION;
186   action->generic_action.start = surf_get_clock();
187   action->generic_action.finish = -1.0;
188   action->generic_action.model_type = surf_network_model;
189   action->suspended = 0;
190
191   action->latency = random_generate(random_latency);
192   action->lat_init = action->latency;
193
194   if (action->latency <= 0.0)
195     action->generic_action.state_set =
196       surf_network_model->states.done_action_set;
197   else
198     action->generic_action.state_set =
199       surf_network_model->states.running_action_set;
200
201   xbt_swag_insert(action, action->generic_action.state_set);
202
203
204   XBT_OUT;
205
206   return (surf_action_t) action;
207 }
208
209 /* returns an array of link_Constant_t */
210 static const void **get_route(void *src, void *dst)
211 {
212   xbt_assert0(0, "Calling this function does not make any sense");
213   return (const void **) NULL;
214 }
215
216 static int get_route_size(void *src, void *dst)
217 {
218   xbt_assert0(0, "Calling this function does not make any sense");
219   return 0;
220 }
221
222 static double get_link_bandwidth(const void *link)
223 {
224   DIE_IMPOSSIBLE;
225 }
226
227 static double get_link_latency(const void *link)
228 {
229   DIE_IMPOSSIBLE;
230 }
231
232 static int link_shared(const void *link)
233 {
234   DIE_IMPOSSIBLE;
235 }
236
237 static xbt_dict_t get_properties(void *link)
238 {
239   DIE_IMPOSSIBLE;
240 }
241
242 static void action_suspend(surf_action_t action)
243 {
244   ((surf_action_network_Constant_t) action)->suspended = 1;
245 }
246
247 static void action_resume(surf_action_t action)
248 {
249   if (((surf_action_network_Constant_t) action)->suspended)
250     ((surf_action_network_Constant_t) action)->suspended = 0;
251 }
252
253 static int action_is_suspended(surf_action_t action)
254 {
255   return ((surf_action_network_Constant_t) action)->suspended;
256 }
257
258 static void action_set_max_duration(surf_action_t action, double duration)
259 {
260   action->max_duration = duration;
261 }
262
263 static void finalize(void)
264 {
265   surf_model_exit(surf_network_model);
266   surf_network_model = NULL;
267
268   card_number = 0;
269 }
270
271 static void surf_network_model_init_internal(void)
272 {
273   surf_network_model = surf_model_init();
274
275   surf_network_model->name = "network constant";
276   surf_network_model->action_free = action_free;
277   surf_network_model->action_use = action_use;
278   surf_network_model->action_cancel = action_cancel;
279   surf_network_model->action_recycle = action_recycle;
280
281   surf_network_model->model_private->resource_used = resource_used;
282   surf_network_model->model_private->share_resources = share_resources;
283   surf_network_model->model_private->update_actions_state =
284     update_actions_state;
285   surf_network_model->model_private->update_resource_state =
286     update_resource_state;
287   surf_network_model->model_private->finalize = finalize;
288
289   surf_network_model->suspend = action_suspend;
290   surf_network_model->resume = action_resume;
291   surf_network_model->is_suspended = action_is_suspended;
292   surf_cpu_model->set_max_duration = action_set_max_duration;
293
294   surf_network_model->extension.network.communicate = communicate;
295   surf_network_model->extension.network.get_route = get_route;
296   surf_network_model->extension.network.get_route_size = get_route_size;
297   surf_network_model->extension.network.get_link_bandwidth =
298     get_link_bandwidth;
299   surf_network_model->extension.network.get_link_latency = get_link_latency;
300   surf_network_model->extension.network.link_shared = link_shared;
301
302   surf_network_model->get_properties = get_properties;
303
304   if (!random_latency)
305     random_latency = random_new(RAND, 100, 0.0, 1.0, .125, .034);
306 }
307
308 void surf_network_model_init_Constant(const char *filename)
309 {
310
311   if (surf_network_model)
312     return;
313   surf_network_model_init_internal();
314   define_callbacks(filename);
315   xbt_dynar_push(model_list, &surf_network_model);
316
317   update_model_description(surf_network_model_description,
318                            "Constant", surf_network_model);
319 }