Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7ed8284b37ba6a36b0152fbf1a121fa461c423d4
[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 surf_action_network_Constant {
16   s_surf_action_t generic_action;
17   double latency;
18   double lat_init;
19   int suspended;
20 } s_surf_action_network_Constant_t, *surf_action_network_Constant_t;
21
22 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
23 static random_data_t random_latency = NULL;
24 static int card_number = 0;
25 static int host_number = 0;
26
27 static void count_hosts(void)
28 {
29   host_number++;
30 }
31
32 static void define_callbacks(const char *file)
33 {
34   /* Figuring out the network links */
35   surfxml_add_callback(STag_surfxml_host_cb_list, &count_hosts);
36 }
37
38 static int resource_used(void *resource_id)
39 {
40   return 0;
41 }
42
43 static int action_unref(surf_action_t action)
44 {
45   action->refcount--;
46   if (!action->refcount) {
47     xbt_swag_remove(action, action->state_set);
48     free(action);
49     return 1;
50   }
51   return 0;
52 }
53
54 static void action_cancel(surf_action_t action)
55 {
56   return;
57 }
58
59 static void action_recycle(surf_action_t action)
60 {
61   return;
62 }
63
64 static double share_resources(double now)
65 {
66   surf_action_network_Constant_t action = NULL;
67   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
68   double min = -1.0;
69
70   xbt_swag_foreach(action, running_actions) {
71     if (action->latency > 0) {
72       if (min < 0)
73         min = action->latency;
74       else if (action->latency < min)
75         min = action->latency;
76     }
77   }
78
79   return min;
80 }
81
82 static void update_actions_state(double now, double delta)
83 {
84   surf_action_network_Constant_t action = NULL;
85   surf_action_network_Constant_t next_action = NULL;
86   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
87
88   xbt_swag_foreach_safe(action, next_action, running_actions) {
89     if (action->latency > 0) {
90       if (action->latency > delta) {
91         double_update(&(action->latency), delta);
92       } else {
93         action->latency = 0.0;
94       }
95     }
96     double_update(&(action->generic_action.remains),
97                   action->generic_action.cost * delta / action->lat_init);
98     if (action->generic_action.max_duration != NO_MAX_DURATION)
99       double_update(&(action->generic_action.max_duration), delta);
100
101     if (action->generic_action.remains <= 0) {
102       action->generic_action.finish = surf_get_clock();
103       surf_network_model->action_state_set((surf_action_t) action, SURF_ACTION_DONE);
104     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
105                (action->generic_action.max_duration <= 0)) {
106       action->generic_action.finish = surf_get_clock();
107       surf_network_model->action_state_set((surf_action_t) action, SURF_ACTION_DONE);
108     }
109   }
110
111   return;
112 }
113
114 static void update_resource_state(void *id,
115                                   tmgr_trace_event_t event_type,
116                                   double value, double time)
117 {
118   DIE_IMPOSSIBLE;
119 }
120
121 static surf_action_t communicate(const char *src_name,const char *dst_name,int src, int dst, double size,
122     double rate)
123 {
124   surf_action_network_Constant_t action = NULL;
125
126   XBT_IN4("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
127
128   action = xbt_new0(s_surf_action_network_Constant_t, 1);
129
130   action->generic_action.refcount = 1;
131   action->generic_action.cost = size;
132   action->generic_action.remains = size;
133   action->generic_action.max_duration = NO_MAX_DURATION;
134   action->generic_action.start = surf_get_clock();
135   action->generic_action.finish = -1.0;
136   action->generic_action.model_type = surf_network_model;
137   action->suspended = 0;
138
139   action->latency = random_generate(random_latency);
140   action->lat_init = action->latency;
141
142   if (action->latency <= 0.0)
143     action->generic_action.state_set =
144       surf_network_model->states.done_action_set;
145   else
146     action->generic_action.state_set =
147       surf_network_model->states.running_action_set;
148
149   xbt_swag_insert(action, action->generic_action.state_set);
150
151
152   XBT_OUT;
153
154   return (surf_action_t) action;
155 }
156
157 /* returns an array of link_Constant_t */
158 static xbt_dynar_t get_route(void *src, void *dst)
159 {
160   xbt_die("Calling this function does not make any sense");
161 }
162
163 static double get_link_bandwidth(const void *link)
164 {
165   DIE_IMPOSSIBLE;
166 }
167
168 static double get_link_latency(const void *link)
169 {
170   DIE_IMPOSSIBLE;
171 }
172
173 static int link_shared(const void *link)
174 {
175   DIE_IMPOSSIBLE;
176 }
177
178 static void action_suspend(surf_action_t action)
179 {
180   ((surf_action_network_Constant_t) action)->suspended = 1;
181 }
182
183 static void action_resume(surf_action_t action)
184 {
185   if (((surf_action_network_Constant_t) action)->suspended)
186     ((surf_action_network_Constant_t) action)->suspended = 0;
187 }
188
189 static int action_is_suspended(surf_action_t action)
190 {
191   return ((surf_action_network_Constant_t) action)->suspended;
192 }
193
194 static void action_set_max_duration(surf_action_t action, double duration)
195 {
196   action->max_duration = duration;
197 }
198
199 static void finalize(void)
200 {
201   surf_model_exit(surf_network_model);
202   surf_network_model = NULL;
203
204   card_number = 0;
205 }
206
207 static void surf_network_model_init_internal(void)
208 {
209   surf_network_model = surf_model_init();
210
211   surf_network_model->name = "network constant";
212   surf_network_model->action_unref = action_unref;
213   surf_network_model->action_cancel = action_cancel;
214   surf_network_model->action_recycle = action_recycle;
215
216   surf_network_model->model_private->resource_used = resource_used;
217   surf_network_model->model_private->share_resources = share_resources;
218   surf_network_model->model_private->update_actions_state =
219     update_actions_state;
220   surf_network_model->model_private->update_resource_state =
221     update_resource_state;
222   surf_network_model->model_private->finalize = finalize;
223
224   surf_network_model->suspend = action_suspend;
225   surf_network_model->resume = action_resume;
226   surf_network_model->is_suspended = action_is_suspended;
227   surf_cpu_model->set_max_duration = action_set_max_duration;
228
229   surf_network_model->extension.network.communicate = communicate;
230   surf_network_model->extension.network.get_link_bandwidth =
231     get_link_bandwidth;
232   surf_network_model->extension.network.get_link_latency = get_link_latency;
233   surf_network_model->extension.network.link_shared = link_shared;
234
235   if (!random_latency)
236     random_latency = random_new(RAND, 100, 0.0, 1.0, .125, .034);
237 }
238
239 void surf_network_model_init_Constant(const char *filename)
240 {
241
242   if (surf_network_model)
243     return;
244   surf_network_model_init_internal();
245   define_callbacks(filename);
246   xbt_dynar_push(model_list, &surf_network_model);
247
248   update_model_description(surf_network_model_description,
249                            "Constant", surf_network_model);
250 }