Logo AND Algorithmique Numérique Distribuée

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