Logo AND Algorithmique Numérique Distribuée

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