Logo AND Algorithmique Numérique Distribuée

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