Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
tracing vivaldi x,y,h for each host
[simgrid.git] / src / surf / network_vivaldi.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_Vivaldi {
15   s_surf_action_t generic_action;
16   double latency;
17   double lat_init;
18   int suspended;
19 } s_surf_action_network_Vivaldi_t, *surf_action_network_Vivaldi_t;
20
21 typedef struct s_netviva_coords {
22   double x,y,h;
23 } s_netviva_coords_t, *netviva_coords_t;
24 xbt_dict_t coords; /* Host name -> coordinates */
25
26 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
27 static random_data_t random_latency = NULL;
28 static int host_number_int = 0;
29
30 static void netviva_count_hosts(void)
31 {
32   host_number_int++;
33 }
34
35 static void netviva_define_callbacks(const char *file)
36 {
37   surfxml_add_callback(STag_surfxml_host_cb_list, &netviva_count_hosts);
38 }
39
40 static int netviva_resource_used(void *resource_id) {
41   /* nothing to do here */
42   return 0;
43 }
44
45 static int netviva_action_unref(surf_action_t action)
46 {
47   action->refcount--;
48   if (!action->refcount) {
49     xbt_swag_remove(action, action->state_set);
50     free(action);
51     return 1;
52   }
53   return 0;
54 }
55
56 static void netviva_action_cancel(surf_action_t action)
57 {
58   return;
59 }
60
61 static void netviva_action_recycle(surf_action_t action)
62 {
63   return;
64 }
65
66 static double netviva_action_get_remains(surf_action_t action)
67 {
68   return action->remains;
69 }
70
71 /* look for the next event to finish */
72 static double netviva_share_resources(double now)
73 {
74   surf_action_network_Vivaldi_t action = NULL;
75   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
76   double min = -1.0;
77
78   xbt_swag_foreach(action, running_actions) {
79     if (action->latency > 0) {
80       if (min < 0)
81         min = action->latency;
82       else if (action->latency < min)
83         min = action->latency;
84     }
85   }
86
87   return min;
88 }
89
90 static void netviva_update_actions_state(double now, double delta) {
91   /* Advance the actions by delta seconds */
92   surf_action_network_Vivaldi_t action = NULL;
93   surf_action_network_Vivaldi_t next_action = NULL;
94   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
95
96   xbt_swag_foreach_safe(action, next_action, running_actions) {
97     if (action->latency > 0) {
98       if (action->latency > delta) {
99         double_update(&(action->latency), delta);
100       } else {
101         action->latency = 0.0;
102       }
103     }
104     double_update(&(action->generic_action.remains),
105                   action->generic_action.cost * delta / action->lat_init);
106     if (action->generic_action.max_duration != NO_MAX_DURATION)
107       double_update(&(action->generic_action.max_duration), delta);
108
109     if (action->generic_action.remains <= 0) { /* This action is done, inform SURF about it */
110       action->generic_action.finish = surf_get_clock();
111       surf_network_model->action_state_set((surf_action_t) action,
112                                            SURF_ACTION_DONE);
113     } else if ((action->generic_action.max_duration != NO_MAX_DURATION)
114                && (action->generic_action.max_duration <= 0)) {
115       action->generic_action.finish = surf_get_clock();
116       surf_network_model->action_state_set((surf_action_t) action,
117                                            SURF_ACTION_DONE);
118     }
119   }
120 }
121
122 static void netviva_update_resource_state(void *id,
123                                   tmgr_trace_event_t event_type,
124                                   double value, double time)
125 { /* Called as soon as there is a trace modification */
126   DIE_IMPOSSIBLE;
127 }
128
129 static surf_action_t netviva_communicate(const char *src_name, const char *dst_name,
130                                  int src, int dst, double size, double rate)
131 {
132   netviva_coords_t c1,c2;
133   c1 = xbt_dict_get(coords,src_name);
134   c2 = xbt_dict_get(coords,dst_name);
135
136   surf_action_network_Vivaldi_t action = NULL;
137
138   action =
139     surf_action_new(sizeof(s_surf_action_network_Vivaldi_t), size,
140                     surf_network_model, 0);
141
142   action->suspended = 0;
143
144   action->latency = sqrt((c1->x-c2->x)*(c1->x-c2->x) + (c1->y-c2->y)*(c1->y-c2->y)) + fabs(c1->h)+ fabs(c2->h) ;          //random_generate(random_latency);
145   action->lat_init = action->latency;
146
147   if (action->latency <= 0.0) {
148     action->generic_action.state_set =
149       surf_network_model->states.done_action_set;
150     xbt_swag_insert(action, action->generic_action.state_set);
151   }
152
153   return (surf_action_t) action;
154 }
155
156 /* returns an array of link_Vivaldi_t */
157 static xbt_dynar_t netviva_get_route(void *src, void *dst)
158 {
159   xbt_die("Calling this function does not make any sense");
160 }
161
162 static double netviva_get_link_bandwidth(const void *link)
163 {
164   DIE_IMPOSSIBLE;
165 }
166
167 static double netviva_get_link_latency(const void *link)
168 {
169   DIE_IMPOSSIBLE;
170 }
171
172 static int link_shared(const void *link)
173 {
174   DIE_IMPOSSIBLE;
175 }
176
177 static void netviva_action_suspend(surf_action_t action)
178 {
179   ((surf_action_network_Vivaldi_t) action)->suspended = 1;
180 }
181
182 static void netviva_action_resume(surf_action_t action)
183 {
184   if (((surf_action_network_Vivaldi_t) action)->suspended)
185     ((surf_action_network_Vivaldi_t) action)->suspended = 0;
186 }
187
188 static int netviva_action_is_suspended(surf_action_t action)
189 {
190   return ((surf_action_network_Vivaldi_t) action)->suspended;
191 }
192
193 static void netviva_action_set_max_duration(surf_action_t action, double duration)
194 {
195   action->max_duration = duration;
196 }
197
198 static void netviva_finalize(void)
199 {
200   surf_model_exit(surf_network_model);
201   surf_network_model = NULL;
202 }
203
204
205 static void net_define_callbacks(const char *file)
206 {
207   /* Figuring out the network links */
208 }
209
210 static void netviva_parse_host(void) {
211   netviva_coords_t coord = xbt_new(s_netviva_coords_t,1);
212
213   xbt_dynar_t ctn =xbt_str_split_str(A_surfxml_host_vivaldi," ");
214
215   coord->x = atof(xbt_dynar_get_as(ctn, 0, char*));
216   coord->y = atof(xbt_dynar_get_as(ctn, 1, char*));
217   coord->h = atof(xbt_dynar_get_as(ctn, 2, char*));
218
219 #ifdef HAVE_TRACING
220   TRACE_surf_vivaldi_parse_host (A_surfxml_host_id, coord->x, coord->y, coord->h);
221 #endif
222
223   xbt_dynar_free(&ctn);
224   xbt_dict_set(coords, A_surfxml_host_id,coord,NULL);
225 }
226
227 void surf_network_model_init_Vivaldi(const char *filename)
228 {
229   xbt_assert(surf_network_model == NULL);
230   if (surf_network_model)
231     return;
232   surf_network_model = surf_model_init();
233   coords = xbt_dict_new();
234
235   INFO0("Blih");
236   surf_network_model->name = "Vivaldi time network";
237   surf_network_model->action_unref = netviva_action_unref;
238   surf_network_model->action_cancel = netviva_action_cancel;
239   surf_network_model->action_recycle = netviva_action_recycle;
240   surf_network_model->get_remains = netviva_action_get_remains;
241
242   surf_network_model->model_private->resource_used = netviva_resource_used;
243   surf_network_model->model_private->share_resources = netviva_share_resources;
244   surf_network_model->model_private->update_actions_state =
245     netviva_update_actions_state;
246   surf_network_model->model_private->update_resource_state =
247     netviva_update_resource_state;
248   surf_network_model->model_private->finalize = netviva_finalize;
249
250   surf_network_model->suspend = netviva_action_suspend;
251   surf_network_model->resume = netviva_action_resume;
252   surf_network_model->is_suspended = netviva_action_is_suspended;
253   surf_cpu_model->set_max_duration = netviva_action_set_max_duration;
254
255   surf_network_model->extension.network.communicate = netviva_communicate;
256   surf_network_model->extension.network.get_link_bandwidth =
257     netviva_get_link_bandwidth;
258   surf_network_model->extension.network.get_link_latency = netviva_get_link_latency;
259   surf_network_model->extension.network.link_shared = link_shared;
260
261   if (!random_latency)
262     random_latency = random_new(RAND, 100, 0.0, 1.0, .125, .034);
263   netviva_define_callbacks(filename);
264   xbt_dynar_push(model_list, &surf_network_model);
265
266   /* Callbacks settings */
267   surfxml_add_callback(STag_surfxml_host_cb_list, &netviva_parse_host);
268
269   update_model_description(surf_network_model_description,
270                            "Vivaldi", surf_network_model);
271
272 #ifdef HAVE_TRACING
273   __TRACE_host_variable(0,"vivaldi_x",0,"declare");
274   __TRACE_host_variable(0,"vivaldi_y",0,"declare");
275   __TRACE_host_variable(0,"vivaldi_h",0,"declare");
276 #endif
277
278   xbt_cfg_set_string(_surf_cfg_set, "routing", "none");
279   routing_model_create(sizeof(double), NULL);
280 }
281