Logo AND Algorithmique Numérique Distribuée

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