Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove the use of surf_cpu_model->func
[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 "storage_private.h"
11 #include "surf/surf_resource.h"
12 #include "simgrid/sg_config.h"
13 #include "workstation_private.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_workstation, surf,
16                                 "Logging specific to the SURF workstation module");
17
18 surf_model_t surf_workstation_model = NULL;
19
20
21 void __init_ws(workstation_CLM03_t ws,  const char *id){
22   ws->generic_resource.model = surf_workstation_model;
23   ws->generic_resource.name = xbt_strdup(id);
24   ws->storage = xbt_lib_get_or_null(storage_lib,id,ROUTING_STORAGE_HOST_LEVEL);
25   ws->net_elm = xbt_lib_get_or_null(host_lib,id,ROUTING_HOST_LEVEL);
26   XBT_DEBUG("Create ws %s with %ld mounted disks",id,xbt_dynar_length(ws->storage));
27   xbt_lib_set(host_lib, id, SURF_WKS_LEVEL, ws);
28   ws->generic_resource.model.extension.cpu=cpu_model_cas01(0);
29 }
30 static void workstation_new(sg_platf_host_cbarg_t host)
31 {
32   workstation_CLM03_t workstation = xbt_new0(s_workstation_CLM03_t, 1);
33   __init_ws(workstation, host->id, level);
34 }
35
36 static int ws_resource_used(void *resource_id)
37 {
38   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
39   return -1;
40 }
41
42 static void ws_parallel_action_cancel(surf_action_t action)
43 {
44   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
45 }
46
47 static int ws_parallel_action_free(surf_action_t action)
48 {
49   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
50   return -1;
51 }
52
53 static int ws_action_unref(surf_action_t action)
54 {
55   if (action->model_type == surf_network_model)
56     return surf_network_model->action_unref(action);
57   else if (action->model_type == surf_cpu_model)
58     return action->model_type->action_unref(action);
59       // previously was: Adrien/Arnaud 6 feb
60           // surf_cpu_model->action_unref(action);
61   else if (action->model_type == surf_workstation_model)
62     return ws_parallel_action_free(action);
63   else
64     DIE_IMPOSSIBLE;
65   return 0;
66 }
67
68 static void ws_action_cancel(surf_action_t action)
69 {
70   if (action->model_type == surf_network_model)
71     surf_network_model->action_cancel(action);
72   else if (action->model_type == surf_cpu_model)
73     action->model_type->action_cancel(action);
74   else if (action->model_type == surf_workstation_model)
75     ws_parallel_action_cancel(action);
76   else
77     DIE_IMPOSSIBLE;
78   return;
79 }
80
81 static void ws_action_state_set(surf_action_t action,
82                                 e_surf_action_state_t state)
83 {
84   if (action->model_type == surf_network_model)
85     surf_network_model->action_state_set(action, state);
86   else if (action->model_type == surf_cpu_model)
87     action->model_type->action_state_set(action, state);
88   else if (action->model_type == surf_workstation_model)
89     surf_action_state_set(action, state);
90   else
91     DIE_IMPOSSIBLE;
92   return;
93 }
94
95 static double ws_share_resources(double now)
96 {
97 // invoke share_resources on CPU and network (layer 0)
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   surf_resource_t cpu = ((surf_resource_t) surf_cpu_resource_priv(workstation));
116   return cpu->model->extension.cpu.execute(workstation, size);
117 }
118
119 static surf_action_t ws_action_sleep(void *workstation, double duration)
120 {
121   surf_resource_t cpu = ((surf_resource_t) surf_cpu_resource_priv(workstation));
122   return cpu->model->extension.cpu.sleep(workstation, 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     action->model_type->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     action->model_type->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 action->model_type->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     action->model_type->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     action->model_type->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     action->model_type->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 action->model_type->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 = surf_workstation_resource_priv(workstation_src);
213   workstation_CLM03_t dst = surf_workstation_resource_priv(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   surf_resource_t cpu = ((surf_resource_t) surf_cpu_resource_priv(workstation));
222   return cpu->model->extension.cpu.get_state(workstation);
223 }
224
225 static double ws_get_speed(void *workstation, double load)
226 {
227   surf_resource_t cpu = ((surf_resource_t) surf_cpu_resource_priv(workstation));
228   return cpu->model->extension.cpu.get_speed(workstation, load);
229 }
230
231 static double ws_get_available_speed(void *workstation)
232 {
233   surf_resource_t cpu = ((surf_resource_t) surf_cpu_resource_priv(workstation));
234   return cpu->model->extension.cpu.get_available_speed(workstation);
235 }
236
237 static surf_action_t ws_execute_parallel_task(int workstation_nb,
238                                               void **workstation_list,
239                                               double *computation_amount,
240                                               double *communication_amount,
241                                               double rate)
242 {
243 #define cost_or_zero(array,pos) ((array)?(array)[pos]:0.0)
244   if ((workstation_nb == 1)
245       && (cost_or_zero(communication_amount, 0) == 0.0))
246     return ws_execute(workstation_list[0], computation_amount[0]);
247   else if ((workstation_nb == 1)
248            && (cost_or_zero(computation_amount, 0) == 0.0))
249     return ws_communicate(workstation_list[0], workstation_list[0],communication_amount[0], rate);
250   else if ((workstation_nb == 2)
251              && (cost_or_zero(computation_amount, 0) == 0.0)
252              && (cost_or_zero(computation_amount, 1) == 0.0)) {
253     int i,nb = 0;
254     double value = 0.0;
255
256     for (i = 0; i < workstation_nb * workstation_nb; i++) {
257       if (cost_or_zero(communication_amount, i) > 0.0) {
258         nb++;
259         value = cost_or_zero(communication_amount, i);
260       }
261     }
262     if (nb == 1)
263       return ws_communicate(workstation_list[0], workstation_list[1],value, rate);
264   }
265 #undef cost_or_zero
266
267   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
268   return NULL;
269 }
270
271
272 /* returns an array of network_link_CM02_t */
273 static xbt_dynar_t ws_get_route(void *workstation_src, void *workstation_dst)
274 {
275   XBT_DEBUG("ws_get_route");
276   workstation_CLM03_t src = surf_workstation_resource_priv(workstation_src);
277   workstation_CLM03_t dst = surf_workstation_resource_priv(workstation_dst);
278   return surf_network_model->extension.
279       network.get_route(src->net_elm,
280                   dst->net_elm);
281 }
282
283 static double ws_get_link_bandwidth(const void *link)
284 {
285   return surf_network_model->extension.network.get_link_bandwidth(link);
286 }
287
288 static double ws_get_link_latency(const void *link)
289 {
290   return surf_network_model->extension.network.get_link_latency(link);
291 }
292
293 static int ws_link_shared(const void *link)
294 {
295   return surf_network_model->extension.network.link_shared(link);
296 }
297
298 static void ws_finalize(void)
299 {
300   surf_model_exit(surf_workstation_model);
301   surf_workstation_model = NULL;
302 }
303
304 static xbt_dict_t ws_get_properties(const void *ws)
305 {
306   return surf_resource_properties(surf_cpu_resource_priv(ws));
307 }
308
309 static storage_t find_storage_on_mount_list(void *workstation,const char* storage)
310 {
311   storage_t st = NULL;
312   s_mount_t mnt;
313   unsigned int cursor;
314   workstation_CLM03_t ws = (workstation_CLM03_t) surf_workstation_resource_priv(workstation);
315   xbt_dynar_t storage_list = ws->storage;
316
317   XBT_DEBUG("Search for storage name '%s' on '%s'",storage,ws->generic_resource.name);
318   xbt_dynar_foreach(storage_list,cursor,mnt)
319   {
320     XBT_DEBUG("See '%s'",mnt.name);
321     if(!strcmp(storage,mnt.name)){
322       st = mnt.id;
323       break;
324     }
325   }
326   if(!st) xbt_die("Can't find mount '%s' for '%s'",storage,ws->generic_resource.name);
327   return st;
328 }
329
330 static surf_action_t ws_action_open(void *workstation, const char* mount, const char* path, const char* mode)
331 {
332   storage_t st = find_storage_on_mount_list(workstation, mount);
333   XBT_DEBUG("OPEN on disk '%s'",st->generic_resource.name);
334   surf_model_t model = st->generic_resource.model;
335   return model->extension.storage.open(st, mount, path, mode);
336 }
337
338 static surf_action_t ws_action_close(void *workstation, surf_file_t fp)
339 {
340   storage_t st = find_storage_on_mount_list(workstation, fp->storage);
341   XBT_DEBUG("CLOSE on disk '%s'",st->generic_resource.name);
342   surf_model_t model = st->generic_resource.model;
343   return model->extension.storage.close(st, fp);
344 }
345
346 static surf_action_t ws_action_read(void *workstation, void* ptr, size_t size, size_t nmemb, surf_file_t stream)
347 {
348   storage_t st = find_storage_on_mount_list(workstation, stream->storage);
349   XBT_DEBUG("READ on disk '%s'",st->generic_resource.name);
350   surf_model_t model = st->generic_resource.model;
351   return model->extension.storage.read(st, ptr, (double)size, nmemb, stream);
352 }
353
354 static surf_action_t ws_action_write(void *workstation, const void* ptr, size_t size, size_t nmemb, surf_file_t stream)
355 {
356   storage_t st = find_storage_on_mount_list(workstation, stream->storage);
357   XBT_DEBUG("WRITE on disk '%s'",st->generic_resource.name);
358   surf_model_t model = st->generic_resource.model;
359   return model->extension.storage.write(st,  ptr, size, nmemb, stream);
360 }
361
362 static surf_action_t ws_action_stat(void *workstation, surf_file_t stream)
363 {
364   storage_t st = find_storage_on_mount_list(workstation, stream->storage);
365   XBT_DEBUG("STAT on disk '%s'",st->generic_resource.name);
366   surf_model_t model = st->generic_resource.model;
367   return model->extension.storage.stat(st,  stream);
368 }
369
370 static surf_action_t ws_action_unlink(void *workstation, surf_file_t stream)
371 {
372   storage_t st = find_storage_on_mount_list(workstation, stream->storage);
373   XBT_DEBUG("UNLINK on disk '%s'",st->generic_resource.name);
374   surf_model_t model = st->generic_resource.model;
375   return model->extension.storage.unlink(st,  stream);
376 }
377
378 static surf_action_t ws_action_ls(void *workstation, const char* mount, const char *path)
379 {
380   XBT_DEBUG("LS on mount '%s' and file '%s'",mount, path);
381   storage_t st = find_storage_on_mount_list(workstation, mount);
382   surf_model_t model = st->generic_resource.model;
383   return model->extension.storage.ls(st, path);
384 }
385
386 static void surf_workstation_model_init_internal(void)
387 {
388   surf_workstation_model = surf_model_init();
389
390   // TODO  surf_workstation_model->extension.cpu=cpu_model_cas01(0);
391   surf_workstation_model->name = "Workstation";
392   surf_workstation_model->action_unref = ws_action_unref;
393   surf_workstation_model->action_cancel = ws_action_cancel;
394   surf_workstation_model->action_state_set = ws_action_state_set;
395
396   surf_workstation_model->model_private->resource_used = ws_resource_used;
397   surf_workstation_model->model_private->share_resources =
398       ws_share_resources;
399   surf_workstation_model->model_private->update_actions_state =
400       ws_update_actions_state;
401   surf_workstation_model->model_private->update_resource_state =
402       ws_update_resource_state;
403   surf_workstation_model->model_private->finalize = ws_finalize;
404
405   surf_workstation_model->suspend = ws_action_suspend;
406   surf_workstation_model->resume = ws_action_resume;
407   surf_workstation_model->is_suspended = ws_action_is_suspended;
408   surf_workstation_model->set_max_duration = ws_action_set_max_duration;
409   surf_workstation_model->set_priority = ws_action_set_priority;
410 #ifdef HAVE_TRACING
411   surf_workstation_model->set_category = ws_action_set_category;
412 #endif
413   surf_workstation_model->get_remains = ws_action_get_remains;
414 #ifdef HAVE_LATENCY_BOUND_TRACKING
415   surf_workstation_model->get_latency_limited = ws_get_latency_limited;
416 #endif
417
418   surf_workstation_model->extension.workstation.execute = ws_execute;
419   surf_workstation_model->extension.workstation.sleep = ws_action_sleep;
420   surf_workstation_model->extension.workstation.get_state = ws_get_state;
421   surf_workstation_model->extension.workstation.get_speed = ws_get_speed;
422   surf_workstation_model->extension.workstation.get_available_speed =
423       ws_get_available_speed;
424
425   surf_workstation_model->extension.workstation.communicate =
426       ws_communicate;
427   surf_workstation_model->extension.workstation.get_route = ws_get_route;
428   surf_workstation_model->extension.workstation.execute_parallel_task =
429       ws_execute_parallel_task;
430   surf_workstation_model->extension.workstation.get_link_bandwidth =
431       ws_get_link_bandwidth;
432   surf_workstation_model->extension.workstation.get_link_latency =
433       ws_get_link_latency;
434   surf_workstation_model->extension.workstation.link_shared =
435       ws_link_shared;
436   surf_workstation_model->extension.workstation.get_properties =
437       ws_get_properties;
438
439   surf_workstation_model->extension.workstation.open = ws_action_open;
440   surf_workstation_model->extension.workstation.close = ws_action_close;
441   surf_workstation_model->extension.workstation.read = ws_action_read;
442   surf_workstation_model->extension.workstation.write = ws_action_write;
443   surf_workstation_model->extension.workstation.stat = ws_action_stat;
444   surf_workstation_model->extension.workstation.unlink = ws_action_unlink;
445   surf_workstation_model->extension.workstation.ls = ws_action_ls;
446 }
447
448 void surf_workstation_model_init_current_default(void)
449 {
450
451   surf_workstation_model_init_internal();
452
453   xbt_cfg_setdefault_int(_sg_cfg_set, "network/crosstraffic", 1);
454   surf_network_model_init_LegrandVelho();
455
456   xbt_dynar_push(model_list, &surf_workstation_model);
457   sg_platf_host_add_cb(workstation_new);
458 //  sg_platf_postparse_add_cb(create_workstations);
459 }
460
461 void surf_workstation_model_init_compound()
462 {
463
464   xbt_assert(surf_cpu_model, "No CPU model defined yet!");
465   xbt_assert(surf_network_model, "No network model defined yet!");
466   surf_workstation_model_init_internal();
467   xbt_dynar_push(model_list, &surf_workstation_model);
468   sg_platf_host_add_cb(workstation_new);
469 //  sg_platf_postparse_add_cb(create_workstations);
470 }