Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
75f0634cea2b86e860093ad23ed9314855957cca
[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   if (!xbt_swag_size(surf_network_model->states.running_action_set)) {
56     return -1.0;
57   }
58   return sg_latency_factor;
59 }
60
61 static void netcste_update_actions_state(double now, double delta)
62 {
63   surf_action_network_Constant_t action = NULL;
64   surf_action_network_Constant_t next_action = NULL;
65   xbt_swag_t running_actions =
66       surf_network_model->states.running_action_set;
67
68   xbt_swag_foreach_safe(action, next_action, running_actions) {
69     if (action->latency > 0) {
70       if (action->latency > delta) {
71         double_update(&(action->latency), delta);
72       } else {
73         action->latency = 0.0;
74       }
75     }
76     double_update(&(action->generic_action.remains),
77                   action->generic_action.cost * delta / action->lat_init);
78     if (action->generic_action.max_duration != NO_MAX_DURATION)
79       double_update(&(action->generic_action.max_duration), delta);
80
81     if (action->generic_action.remains <= 0) {
82       action->generic_action.finish = surf_get_clock();
83       surf_network_model->action_state_set((surf_action_t) action,
84                                            SURF_ACTION_DONE);
85     } else if ((action->generic_action.max_duration != NO_MAX_DURATION)
86                && (action->generic_action.max_duration <= 0)) {
87       action->generic_action.finish = surf_get_clock();
88       surf_network_model->action_state_set((surf_action_t) action,
89                                            SURF_ACTION_DONE);
90     }
91   }
92 }
93
94 static void netcste_update_resource_state(void *id,
95                                           tmgr_trace_event_t event_type,
96                                           double value, double time)
97 {
98   DIE_IMPOSSIBLE;
99 }
100
101 static surf_action_t netcste_communicate(const char *src_name,
102                                          const char *dst_name, double size,
103                                          double rate)
104 {
105   surf_action_network_Constant_t action = NULL;
106
107   XBT_IN("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
108
109   action =
110       surf_action_new(sizeof(s_surf_action_network_Constant_t), size,
111                       surf_network_model, 0);
112
113   action->suspended = 0;
114
115   action->latency = sg_latency_factor;          //random_generate(random_latency);
116   action->lat_init = action->latency;
117
118   if (action->latency <= 0.0) {
119     action->generic_action.state_set =
120         surf_network_model->states.done_action_set;
121     xbt_swag_insert(action, action->generic_action.state_set);
122   }
123
124   XBT_OUT();
125
126   return (surf_action_t) action;
127 }
128
129 #ifdef HAVE_TRACING
130 static void netcste_action_set_category(surf_action_t action, const char *category)
131 {
132   //ignore completely the categories in constant model, they are not traced
133 }
134 #endif
135
136 static double netcste_get_link_bandwidth(const void *link)
137 {
138   DIE_IMPOSSIBLE;
139 }
140
141 static double netcste_get_link_latency(const void *link)
142 {
143   DIE_IMPOSSIBLE;
144 }
145
146 static int link_shared(const void *link)
147 {
148   DIE_IMPOSSIBLE;
149 }
150
151 static void netcste_action_suspend(surf_action_t action)
152 {
153   ((surf_action_network_Constant_t) action)->suspended = 1;
154 }
155
156 static void netcste_action_resume(surf_action_t action)
157 {
158   if (((surf_action_network_Constant_t) action)->suspended)
159     ((surf_action_network_Constant_t) action)->suspended = 0;
160 }
161
162 static int netcste_action_is_suspended(surf_action_t action)
163 {
164   return ((surf_action_network_Constant_t) action)->suspended;
165 }
166
167 static void netcste_finalize(void)
168 {
169   surf_model_exit(surf_network_model);
170   surf_network_model = NULL;
171 }
172
173
174
175 void surf_network_model_init_Constant()
176 {
177   xbt_assert(surf_network_model == NULL);
178   if (surf_network_model)
179     return;
180   surf_network_model = surf_model_init();
181
182   surf_network_model->name = "constant time network";
183   surf_network_model->action_unref = netcste_action_unref;
184   surf_network_model->action_cancel = netcste_action_cancel;
185   surf_network_model->action_recycle = net_action_recycle;
186   surf_network_model->get_remains = net_action_get_remains;
187 #ifdef HAVE_LATENCY_BOUND_TRACKING
188   surf_network_model->get_latency_limited = net_get_link_latency_limited;
189 #endif
190
191   surf_network_model->model_private->resource_used = netcste_resource_used;
192   surf_network_model->model_private->share_resources =
193       netcste_share_resources;
194   surf_network_model->model_private->update_actions_state =
195       netcste_update_actions_state;
196   surf_network_model->model_private->update_resource_state =
197       netcste_update_resource_state;
198   surf_network_model->model_private->finalize = netcste_finalize;
199
200   surf_network_model->suspend = netcste_action_suspend;
201   surf_network_model->resume = netcste_action_resume;
202   surf_network_model->is_suspended = netcste_action_is_suspended;
203   surf_cpu_model->set_max_duration = net_action_set_max_duration;
204
205   surf_network_model->extension.network.communicate = netcste_communicate;
206   surf_network_model->extension.network.get_link_bandwidth =
207       netcste_get_link_bandwidth;
208   surf_network_model->extension.network.get_link_latency =
209       netcste_get_link_latency;
210   surf_network_model->extension.network.link_shared = link_shared;
211 #ifdef HAVE_TRACING
212   surf_network_model->set_category = netcste_action_set_category;
213 #endif
214
215   if (!random_latency)
216     random_latency = random_new(RAND, 100, 0.0, 1.0, .125, .034);
217   netcste_define_callbacks();
218   xbt_dynar_push(model_list, &surf_network_model);
219
220   xbt_cfg_set_string(_surf_cfg_set, "routing", "none");
221   routing_model_create(sizeof(double), NULL);
222 }