Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / surf / network_constant.c
1 /* Copyright (c) 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "surf_private.h"
8 #include "surf/random_mgr.h"
9 #include "xbt/dict.h"
10 #include "xbt/str.h"
11 #include "xbt/log.h"
12
13 typedef struct surf_action_network_Constant {
14   s_surf_action_t generic_action;
15   double latency;
16   double lat_init;
17   int suspended;
18 } s_surf_action_network_Constant_t, *surf_action_network_Constant_t;
19
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
21 static random_data_t random_latency = NULL;
22 static int host_number_int = 0;
23
24 static void netcste_count_hosts(sg_platf_host_cbarg_t h) {
25   host_number_int++;
26 }
27
28 static void netcste_define_callbacks(void) {
29   sg_platf_host_add_cb(netcste_count_hosts);
30 }
31
32 static int netcste_resource_used(void *resource_id)
33 {
34   return 0;
35 }
36
37 static int netcste_action_unref(surf_action_t action)
38 {
39   action->refcount--;
40   if (!action->refcount) {
41     xbt_swag_remove(action, action->state_set);
42     surf_action_free(&action);
43     return 1;
44   }
45   return 0;
46 }
47
48 static void netcste_action_cancel(surf_action_t action)
49 {
50   return;
51 }
52
53 static double netcste_share_resources(double now)
54 {
55   surf_action_network_Constant_t action = NULL;
56   xbt_swag_t running_actions =
57       surf_network_model->states.running_action_set;
58   double min = -1.0;
59
60   xbt_swag_foreach(action, running_actions) {
61     if (action->latency > 0) {
62       if (min < 0)
63         min = action->latency;
64       else if (action->latency < min)
65         min = action->latency;
66     }
67   }
68
69   return min;
70 }
71
72 static void netcste_update_actions_state(double now, double delta)
73 {
74   surf_action_network_Constant_t action = NULL;
75   surf_action_network_Constant_t next_action = NULL;
76   xbt_swag_t running_actions =
77       surf_network_model->states.running_action_set;
78
79   xbt_swag_foreach_safe(action, next_action, running_actions) {
80     if (action->latency > 0) {
81       if (action->latency > delta) {
82         double_update(&(action->latency), delta);
83       } else {
84         action->latency = 0.0;
85       }
86     }
87     double_update(&(action->generic_action.remains),
88                   action->generic_action.cost * delta / action->lat_init);
89     if (action->generic_action.max_duration != NO_MAX_DURATION)
90       double_update(&(action->generic_action.max_duration), delta);
91
92     if (action->generic_action.remains <= 0) {
93       action->generic_action.finish = surf_get_clock();
94       surf_network_model->action_state_set((surf_action_t) action,
95                                            SURF_ACTION_DONE);
96     } else if ((action->generic_action.max_duration != NO_MAX_DURATION)
97                && (action->generic_action.max_duration <= 0)) {
98       action->generic_action.finish = surf_get_clock();
99       surf_network_model->action_state_set((surf_action_t) action,
100                                            SURF_ACTION_DONE);
101     }
102   }
103 }
104
105 static void netcste_update_resource_state(void *id,
106                                           tmgr_trace_event_t event_type,
107                                           double value, double time)
108 {
109   DIE_IMPOSSIBLE;
110 }
111
112 static surf_action_t netcste_communicate(sg_routing_edge_t src,
113                                          sg_routing_edge_t dst,
114                                          double size, double rate)
115 {
116   surf_action_network_Constant_t action = NULL;
117
118   char *src_name = src->name;
119   char *dst_name = dst->name;
120
121   XBT_IN("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
122
123   action =
124       surf_action_new(sizeof(s_surf_action_network_Constant_t), size,
125                       surf_network_model, 0);
126
127   action->suspended = 0;
128
129   action->latency = sg_latency_factor;          //random_generate(random_latency);
130   action->lat_init = action->latency;
131
132   if (action->latency <= 0.0) {
133     action->generic_action.state_set =
134         surf_network_model->states.done_action_set;
135     xbt_swag_insert(action, action->generic_action.state_set);
136   }
137
138   XBT_OUT();
139
140   return (surf_action_t) action;
141 }
142
143 #ifdef HAVE_TRACING
144 static void netcste_action_set_category(surf_action_t action, const char *category)
145 {
146   //ignore completely the categories in constant model, they are not traced
147 }
148 #endif
149
150 static double netcste_get_link_bandwidth(const void *link)
151 {
152   DIE_IMPOSSIBLE;
153   return -1.0; /* useless since DIE actually abort(), but eclipse prefer to have a useless and harmless return */
154 }
155
156 static double netcste_get_link_latency(const void *link)
157 {
158   DIE_IMPOSSIBLE;
159   return -1.0; /* useless since DIE actually abort(), but eclipse prefer to have a useless and harmless return */
160 }
161
162 static int link_shared(const void *link)
163 {
164   DIE_IMPOSSIBLE;
165   return -1; /* useless since DIE actually abort(), but eclipse prefer to have a useless and harmless return */
166 }
167
168 static void netcste_action_suspend(surf_action_t action)
169 {
170   ((surf_action_network_Constant_t) action)->suspended = 1;
171 }
172
173 static void netcste_action_resume(surf_action_t action)
174 {
175   if (((surf_action_network_Constant_t) action)->suspended)
176     ((surf_action_network_Constant_t) action)->suspended = 0;
177 }
178
179 static int netcste_action_is_suspended(surf_action_t action)
180 {
181   return ((surf_action_network_Constant_t) action)->suspended;
182 }
183
184 static void netcste_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()
193 {
194   xbt_assert(surf_network_model == NULL);
195   if (surf_network_model)
196     return;
197   surf_network_model = surf_model_init();
198
199   surf_network_model->name = "constant time network";
200   surf_network_model->action_unref = netcste_action_unref;
201   surf_network_model->action_cancel = netcste_action_cancel;
202   surf_network_model->action_recycle = net_action_recycle;
203   surf_network_model->get_remains = surf_action_get_remains;
204 #ifdef HAVE_LATENCY_BOUND_TRACKING
205   surf_network_model->get_latency_limited = net_get_link_latency_limited;
206 #endif
207
208   surf_network_model->model_private->resource_used = netcste_resource_used;
209   surf_network_model->model_private->share_resources =
210       netcste_share_resources;
211   surf_network_model->model_private->update_actions_state =
212       netcste_update_actions_state;
213   surf_network_model->model_private->update_resource_state =
214       netcste_update_resource_state;
215   surf_network_model->model_private->finalize = netcste_finalize;
216
217   surf_network_model->suspend = netcste_action_suspend;
218   surf_network_model->resume = netcste_action_resume;
219   surf_network_model->is_suspended = netcste_action_is_suspended;
220   surf_cpu_model->set_max_duration = surf_action_set_max_duration;
221
222   surf_network_model->extension.network.communicate = netcste_communicate;
223   surf_network_model->extension.network.get_link_bandwidth =
224       netcste_get_link_bandwidth;
225   surf_network_model->extension.network.get_link_latency =
226       netcste_get_link_latency;
227   surf_network_model->extension.network.link_shared = link_shared;
228 #ifdef HAVE_TRACING
229   surf_network_model->set_category = netcste_action_set_category;
230 #endif
231
232   if (!random_latency)
233     random_latency = random_new(RAND, 100, 0.0, 1.0, .125, .034);
234   netcste_define_callbacks();
235   xbt_dynar_push(model_list, &surf_network_model);
236
237   routing_model_create(NULL);
238 }