Logo AND Algorithmique Numérique Distribuée

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