Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bc6f1bcb5a655db3618908ba337dba0a50b1bd66
[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(const char *src_name,const char *dst_name,int src, int dst, double size,
167     double rate)
168 {
169   surf_action_network_Constant_t action = NULL;
170
171   XBT_IN4("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
172
173   action = xbt_new0(s_surf_action_network_Constant_t, 1);
174
175   action->generic_action.refcount = 1;
176   action->generic_action.cost = size;
177   action->generic_action.remains = size;
178   action->generic_action.max_duration = NO_MAX_DURATION;
179   action->generic_action.start = surf_get_clock();
180   action->generic_action.finish = -1.0;
181   action->generic_action.model_type = surf_network_model;
182   action->suspended = 0;
183
184   action->latency = random_generate(random_latency);
185   action->lat_init = action->latency;
186
187   if (action->latency <= 0.0)
188     action->generic_action.state_set =
189       surf_network_model->states.done_action_set;
190   else
191     action->generic_action.state_set =
192       surf_network_model->states.running_action_set;
193
194   xbt_swag_insert(action, action->generic_action.state_set);
195
196
197   XBT_OUT;
198
199   return (surf_action_t) action;
200 }
201
202 /* returns an array of link_Constant_t */
203 static xbt_dynar_t get_route(void *src, void *dst)
204 {
205   xbt_die("Calling this function does not make any sense");
206 }
207
208 static double get_link_bandwidth(const void *link)
209 {
210   DIE_IMPOSSIBLE;
211 }
212
213 static double get_link_latency(const void *link)
214 {
215   DIE_IMPOSSIBLE;
216 }
217
218 static int link_shared(const void *link)
219 {
220   DIE_IMPOSSIBLE;
221 }
222
223 static xbt_dict_t get_properties(void *link)
224 {
225   DIE_IMPOSSIBLE;
226 }
227
228 static void action_suspend(surf_action_t action)
229 {
230   ((surf_action_network_Constant_t) action)->suspended = 1;
231 }
232
233 static void action_resume(surf_action_t action)
234 {
235   if (((surf_action_network_Constant_t) action)->suspended)
236     ((surf_action_network_Constant_t) action)->suspended = 0;
237 }
238
239 static int action_is_suspended(surf_action_t action)
240 {
241   return ((surf_action_network_Constant_t) action)->suspended;
242 }
243
244 static void action_set_max_duration(surf_action_t action, double duration)
245 {
246   action->max_duration = duration;
247 }
248
249 static void finalize(void)
250 {
251   surf_model_exit(surf_network_model);
252   surf_network_model = NULL;
253
254   card_number = 0;
255 }
256
257 static void surf_network_model_init_internal(void)
258 {
259   surf_network_model = surf_model_init();
260
261   surf_network_model->name = "network constant";
262   surf_network_model->action_unref = action_unref;
263   surf_network_model->action_cancel = action_cancel;
264   surf_network_model->action_recycle = action_recycle;
265
266   surf_network_model->model_private->resource_used = resource_used;
267   surf_network_model->model_private->share_resources = share_resources;
268   surf_network_model->model_private->update_actions_state =
269     update_actions_state;
270   surf_network_model->model_private->update_resource_state =
271     update_resource_state;
272   surf_network_model->model_private->finalize = finalize;
273
274   surf_network_model->suspend = action_suspend;
275   surf_network_model->resume = action_resume;
276   surf_network_model->is_suspended = action_is_suspended;
277   surf_cpu_model->set_max_duration = action_set_max_duration;
278
279   surf_network_model->extension.network.communicate = communicate;
280   surf_network_model->extension.network.get_link_bandwidth =
281     get_link_bandwidth;
282   surf_network_model->extension.network.get_link_latency = get_link_latency;
283   surf_network_model->extension.network.link_shared = link_shared;
284
285   surf_network_model->get_properties = get_properties;
286
287   if (!random_latency)
288     random_latency = random_new(RAND, 100, 0.0, 1.0, .125, .034);
289 }
290
291 void surf_network_model_init_Constant(const char *filename)
292 {
293
294   if (surf_network_model)
295     return;
296   surf_network_model_init_internal();
297   define_callbacks(filename);
298   xbt_dynar_push(model_list, &surf_network_model);
299
300   update_model_description(surf_network_model_description,
301                            "Constant", surf_network_model);
302 }