Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix another race in log initializations.
[simgrid.git] / src / surf / workstation.c
1 /* Copyright (c) 2004, 2005, 2006, 2007, 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 "xbt/ex.h"
8 #include "xbt/dict.h"
9 #include "portable.h"
10 #include "surf_private.h"
11 #include "surf/surf_resource.h"
12
13
14
15 typedef struct workstation_CLM03 {
16   s_surf_resource_t generic_resource;   /* Must remain first to add this to a trace */
17   void *cpu;
18 } s_workstation_CLM03_t, *workstation_CLM03_t;
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_workstation, surf,
21                                 "Logging specific to the SURF workstation module");
22
23 surf_model_t surf_workstation_model = NULL;
24
25 static workstation_CLM03_t workstation_new(const char *name, void *cpu)
26 {
27   workstation_CLM03_t workstation = xbt_new0(s_workstation_CLM03_t, 1);
28
29   workstation->generic_resource.model = surf_workstation_model;
30   workstation->generic_resource.name = xbt_strdup(name);
31   workstation->cpu = cpu;
32
33   xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, workstation);
34
35   return workstation;
36 }
37
38 void create_workstations(void)
39 {
40   xbt_lib_cursor_t cursor = NULL;
41   char *name = NULL;
42   void **cpu = NULL;
43
44   xbt_lib_foreach(host_lib, cursor, name, cpu) {
45           if(cpu[SURF_CPU_LEVEL])
46                   workstation_new(name, cpu[SURF_CPU_LEVEL]);
47   }
48 }
49
50 static int ws_resource_used(void *resource_id)
51 {
52   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
53   return -1;
54 }
55
56 static void ws_parallel_action_cancel(surf_action_t action)
57 {
58   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
59 }
60
61 static int ws_parallel_action_free(surf_action_t action)
62 {
63   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
64   return -1;
65 }
66
67 static int ws_action_unref(surf_action_t action)
68 {
69   if (action->model_type == surf_network_model)
70     return surf_network_model->action_unref(action);
71   else if (action->model_type == surf_cpu_model)
72     return surf_cpu_model->action_unref(action);
73   else if (action->model_type == surf_workstation_model)
74     return ws_parallel_action_free(action);
75   else
76     DIE_IMPOSSIBLE;
77   return 0;
78 }
79
80 static void ws_action_cancel(surf_action_t action)
81 {
82   if (action->model_type == surf_network_model)
83     surf_network_model->action_cancel(action);
84   else if (action->model_type == surf_cpu_model)
85     surf_cpu_model->action_cancel(action);
86   else if (action->model_type == surf_workstation_model)
87     ws_parallel_action_cancel(action);
88   else
89     DIE_IMPOSSIBLE;
90   return;
91 }
92
93 static void ws_action_state_set(surf_action_t action,
94                                 e_surf_action_state_t state)
95 {
96   if (action->model_type == surf_network_model)
97     surf_network_model->action_state_set(action, state);
98   else if (action->model_type == surf_cpu_model)
99     surf_cpu_model->action_state_set(action, state);
100   else if (action->model_type == surf_workstation_model)
101     surf_action_state_set(action, state);
102   else
103     DIE_IMPOSSIBLE;
104   return;
105 }
106
107 static double ws_share_resources(double now)
108 {
109   return -1.0;
110 }
111
112 static void ws_update_actions_state(double now, double delta)
113 {
114   return;
115 }
116
117 static void ws_update_resource_state(void *id,
118                                      tmgr_trace_event_t event_type,
119                                      double value, double date)
120 {
121   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
122 }
123
124 static surf_action_t ws_execute(void *workstation, double size)
125 {
126   return surf_cpu_model->extension.cpu.
127       execute(((workstation_CLM03_t) workstation)->cpu, size);
128 }
129
130 static surf_action_t ws_action_sleep(void *workstation, double duration)
131 {
132   return surf_cpu_model->extension.cpu.
133       sleep(((workstation_CLM03_t) workstation)->cpu, duration);
134 }
135
136 static void ws_action_suspend(surf_action_t action)
137 {
138   if (action->model_type == surf_network_model)
139     surf_network_model->suspend(action);
140   else if (action->model_type == surf_cpu_model)
141     surf_cpu_model->suspend(action);
142   else
143     DIE_IMPOSSIBLE;
144 }
145
146 static void ws_action_resume(surf_action_t action)
147 {
148   if (action->model_type == surf_network_model)
149     surf_network_model->resume(action);
150   else if (action->model_type == surf_cpu_model)
151     surf_cpu_model->resume(action);
152   else
153     DIE_IMPOSSIBLE;
154 }
155
156 static int ws_action_is_suspended(surf_action_t action)
157 {
158   if (action->model_type == surf_network_model)
159     return surf_network_model->is_suspended(action);
160   if (action->model_type == surf_cpu_model)
161     return surf_cpu_model->is_suspended(action);
162   DIE_IMPOSSIBLE;
163   return -1;
164 }
165
166 static void ws_action_set_max_duration(surf_action_t action,
167                                        double duration)
168 {
169   if (action->model_type == surf_network_model)
170     surf_network_model->set_max_duration(action, duration);
171   else if (action->model_type == surf_cpu_model)
172     surf_cpu_model->set_max_duration(action, duration);
173   else
174     DIE_IMPOSSIBLE;
175 }
176
177 static void ws_action_set_priority(surf_action_t action, double priority)
178 {
179   if (action->model_type == surf_network_model)
180     surf_network_model->set_priority(action, priority);
181   else if (action->model_type == surf_cpu_model)
182     surf_cpu_model->set_priority(action, priority);
183   else
184     DIE_IMPOSSIBLE;
185 }
186
187 #ifdef HAVE_TRACING
188 static void ws_action_set_category(surf_action_t action, const char *category)
189 {
190   if (action->model_type == surf_network_model)
191     surf_network_model->set_category(action, category);
192   else if (action->model_type == surf_cpu_model)
193     surf_cpu_model->set_category(action, category);
194   else
195     DIE_IMPOSSIBLE;
196 }
197 #endif
198
199 #ifdef HAVE_LATENCY_BOUND_TRACKING
200 static int ws_get_latency_limited(surf_action_t action)
201 {
202   if (action->model_type == surf_network_model)
203     return surf_network_model->get_latency_limited(action);
204   else
205     return 0;
206 }
207 #endif
208
209 static double ws_action_get_remains(surf_action_t action)
210 {
211   if (action->model_type == surf_network_model)
212     return surf_network_model->get_remains(action);
213   if (action->model_type == surf_cpu_model)
214     return surf_cpu_model->get_remains(action);
215   DIE_IMPOSSIBLE;
216   return -1.0;
217 }
218
219 static surf_action_t ws_communicate(void *workstation_src,
220                                     void *workstation_dst, double size,
221                                     double rate)
222 {
223   workstation_CLM03_t src = (workstation_CLM03_t) workstation_src;
224   workstation_CLM03_t dst = (workstation_CLM03_t) workstation_dst;
225   return surf_network_model->extension.network.
226       communicate(surf_resource_name(src->cpu),
227                   surf_resource_name(dst->cpu), size, rate);
228 }
229
230 static e_surf_resource_state_t ws_get_state(void *workstation)
231 {
232   return surf_cpu_model->extension.cpu.
233       get_state(((workstation_CLM03_t) workstation)->cpu);
234 }
235
236 static double ws_get_speed(void *workstation, double load)
237 {
238   return surf_cpu_model->extension.cpu.
239       get_speed(((workstation_CLM03_t) workstation)->cpu, load);
240 }
241
242 static double ws_get_available_speed(void *workstation)
243 {
244   return surf_cpu_model->extension.cpu.
245       get_available_speed(((workstation_CLM03_t)
246                            workstation)->cpu);
247 }
248
249 static surf_action_t ws_execute_parallel_task(int workstation_nb,
250                                               void **workstation_list,
251                                               double *computation_amount,
252                                               double *communication_amount,
253                                               double amount, double rate)
254 {
255   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
256   return NULL;
257 }
258
259
260 /* returns an array of network_link_CM02_t */
261 static xbt_dynar_t ws_get_route(void *src, void *dst)
262 {
263   return surf_network_model->extension.
264       network.get_route(surf_resource_name(src), surf_resource_name(dst));
265 }
266
267 static double ws_get_link_bandwidth(const void *link)
268 {
269   return surf_network_model->extension.network.get_link_bandwidth(link);
270 }
271
272 static double ws_get_link_latency(const void *link)
273 {
274   return surf_network_model->extension.network.get_link_latency(link);
275 }
276
277 static int ws_link_shared(const void *link)
278 {
279   return surf_network_model->extension.network.get_link_latency(link);
280 }
281
282 static void ws_finalize(void)
283 {
284   surf_model_exit(surf_workstation_model);
285   surf_workstation_model = NULL;
286 }
287
288 static xbt_dict_t ws_get_properties(const void *ws)
289 {
290   return surf_resource_properties(((workstation_CLM03_t) ws)->cpu);
291 }
292
293 static void surf_workstation_model_init_internal(void)
294 {
295   surf_workstation_model = surf_model_init();
296
297   surf_workstation_model->name = "Workstation";
298   surf_workstation_model->action_unref = ws_action_unref;
299   surf_workstation_model->action_cancel = ws_action_cancel;
300   surf_workstation_model->action_state_set = ws_action_state_set;
301
302   surf_workstation_model->model_private->resource_used = ws_resource_used;
303   surf_workstation_model->model_private->share_resources =
304       ws_share_resources;
305   surf_workstation_model->model_private->update_actions_state =
306       ws_update_actions_state;
307   surf_workstation_model->model_private->update_resource_state =
308       ws_update_resource_state;
309   surf_workstation_model->model_private->finalize = ws_finalize;
310
311   surf_workstation_model->suspend = ws_action_suspend;
312   surf_workstation_model->resume = ws_action_resume;
313   surf_workstation_model->is_suspended = ws_action_is_suspended;
314   surf_workstation_model->set_max_duration = ws_action_set_max_duration;
315   surf_workstation_model->set_priority = ws_action_set_priority;
316 #ifdef HAVE_TRACING
317   surf_workstation_model->set_category = ws_action_set_category;
318 #endif
319   surf_workstation_model->get_remains = ws_action_get_remains;
320 #ifdef HAVE_LATENCY_BOUND_TRACKING
321   surf_workstation_model->get_latency_limited = ws_get_latency_limited;
322 #endif
323
324   surf_workstation_model->extension.workstation.execute = ws_execute;
325   surf_workstation_model->extension.workstation.sleep = ws_action_sleep;
326   surf_workstation_model->extension.workstation.get_state = ws_get_state;
327   surf_workstation_model->extension.workstation.get_speed = ws_get_speed;
328   surf_workstation_model->extension.workstation.get_available_speed =
329       ws_get_available_speed;
330
331   surf_workstation_model->extension.workstation.communicate =
332       ws_communicate;
333   surf_workstation_model->extension.workstation.get_route = ws_get_route;
334   surf_workstation_model->extension.workstation.execute_parallel_task =
335       ws_execute_parallel_task;
336   surf_workstation_model->extension.workstation.get_link_bandwidth =
337       ws_get_link_bandwidth;
338   surf_workstation_model->extension.workstation.get_link_latency =
339       ws_get_link_latency;
340   surf_workstation_model->extension.workstation.link_shared =
341       ws_link_shared;
342   surf_workstation_model->extension.workstation.get_properties =
343       ws_get_properties;
344
345 }
346
347 void surf_workstation_model_init_current_default(void)
348 {
349   surf_workstation_model_init_internal();
350   xbt_cfg_setdefault_int(_surf_cfg_set, "network/crosstraffic", 1);
351   surf_cpu_model_init_Cas01();
352   surf_network_model_init_LegrandVelho();
353
354   xbt_dynar_push(model_list, &surf_workstation_model);
355   sg_platf_postparse_add_cb(create_workstations);
356 }
357
358 void surf_workstation_model_init_compound()
359 {
360
361   xbt_assert(surf_cpu_model, "No CPU model defined yet!");
362   xbt_assert(surf_network_model, "No network model defined yet!");
363   surf_workstation_model_init_internal();
364   xbt_dynar_push(model_list, &surf_workstation_model);
365   sg_platf_postparse_add_cb(create_workstations);
366 }