Logo AND Algorithmique Numérique Distribuée

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