Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2b0b2edf62eb9cda949b2ce80a2a7bbf2fe2b1e7
[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
112 static void update_resource_state(void *id,
113                                   tmgr_trace_event_t event_type,
114                                   double value, double time)
115 {
116   DIE_IMPOSSIBLE;
117 }
118
119 static surf_action_t communicate(const char *src_name,const char *dst_name,int src, int dst, double size,
120     double rate)
121 {
122   surf_action_network_Constant_t action = NULL;
123
124   XBT_IN4("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
125
126   action = surf_action_new(sizeof(s_surf_action_network_Constant_t),size,surf_network_model,0);
127
128   action->suspended = 0;
129
130   action->latency = random_generate(random_latency);
131   action->lat_init = action->latency;
132
133   if (action->latency <= 0.0) {
134     action->generic_action.state_set =
135       surf_network_model->states.done_action_set;
136     xbt_swag_insert(action, action->generic_action.state_set);
137   }
138
139   XBT_OUT;
140
141   return (surf_action_t) action;
142 }
143
144 /* returns an array of link_Constant_t */
145 static xbt_dynar_t get_route(void *src, void *dst)
146 {
147   xbt_die("Calling this function does not make any sense");
148 }
149
150 static double get_link_bandwidth(const void *link)
151 {
152   DIE_IMPOSSIBLE;
153 }
154
155 static double get_link_latency(const void *link)
156 {
157   DIE_IMPOSSIBLE;
158 }
159
160 static int link_shared(const void *link)
161 {
162   DIE_IMPOSSIBLE;
163 }
164
165 static void action_suspend(surf_action_t action)
166 {
167   ((surf_action_network_Constant_t) action)->suspended = 1;
168 }
169
170 static void action_resume(surf_action_t action)
171 {
172   if (((surf_action_network_Constant_t) action)->suspended)
173     ((surf_action_network_Constant_t) action)->suspended = 0;
174 }
175
176 static int action_is_suspended(surf_action_t action)
177 {
178   return ((surf_action_network_Constant_t) action)->suspended;
179 }
180
181 static void action_set_max_duration(surf_action_t action, double duration)
182 {
183   action->max_duration = duration;
184 }
185
186 static void finalize(void)
187 {
188   surf_model_exit(surf_network_model);
189   surf_network_model = NULL;
190
191   card_number = 0;
192 }
193
194 static void surf_network_model_init_internal(void)
195 {
196   surf_network_model = surf_model_init();
197
198   surf_network_model->name = "network constant";
199   surf_network_model->action_unref = action_unref;
200   surf_network_model->action_cancel = action_cancel;
201   surf_network_model->action_recycle = action_recycle;
202
203   surf_network_model->model_private->resource_used = resource_used;
204   surf_network_model->model_private->share_resources = share_resources;
205   surf_network_model->model_private->update_actions_state =
206     update_actions_state;
207   surf_network_model->model_private->update_resource_state =
208     update_resource_state;
209   surf_network_model->model_private->finalize = finalize;
210
211   surf_network_model->suspend = action_suspend;
212   surf_network_model->resume = action_resume;
213   surf_network_model->is_suspended = action_is_suspended;
214   surf_cpu_model->set_max_duration = action_set_max_duration;
215
216   surf_network_model->extension.network.communicate = communicate;
217   surf_network_model->extension.network.get_link_bandwidth =
218     get_link_bandwidth;
219   surf_network_model->extension.network.get_link_latency = get_link_latency;
220   surf_network_model->extension.network.link_shared = link_shared;
221
222   if (!random_latency)
223     random_latency = random_new(RAND, 100, 0.0, 1.0, .125, .034);
224 }
225
226 void surf_network_model_init_Constant(const char *filename)
227 {
228
229   if (surf_network_model)
230     return;
231   surf_network_model_init_internal();
232   define_callbacks(filename);
233   xbt_dynar_push(model_list, &surf_network_model);
234
235   update_model_description(surf_network_model_description,
236                            "Constant", surf_network_model);
237 }