Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3b3b9df0907c1b28ebda2716d6b83e39201872d6
[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,
330                                     const char* path)
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);
336 }
337
338 static surf_action_t ws_action_close(void *workstation, surf_file_t fd)
339 {
340   storage_t st = find_storage_on_mount_list(workstation, fd->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, fd);
344 }
345
346 static surf_action_t ws_action_read(void *workstation, void* ptr, size_t size,
347                                     surf_file_t fd)
348 {
349   storage_t st = find_storage_on_mount_list(workstation, fd->storage);
350   XBT_DEBUG("READ on disk '%s'",st->generic_resource.name);
351   surf_model_t model = st->generic_resource.model;
352   return model->extension.storage.read(st, ptr, size, fd);
353 }
354
355 static surf_action_t ws_action_write(void *workstation, const void* ptr,
356                                      size_t size, surf_file_t fd)
357 {
358   storage_t st = find_storage_on_mount_list(workstation, fd->storage);
359   XBT_DEBUG("WRITE on disk '%s'",st->generic_resource.name);
360   surf_model_t model = st->generic_resource.model;
361   return model->extension.storage.write(st,  ptr, size, fd);
362 }
363
364 static int ws_file_unlink(void *workstation, surf_file_t fd)
365 {
366   if (!fd){
367     XBT_WARN("No such file descriptor. Impossible to unlink");
368     return 0;
369   } else {
370 //    XBT_INFO("%s %zu", fd->storage, fd->size);
371     storage_t st = find_storage_on_mount_list(workstation, fd->storage);
372     xbt_dict_t content_dict = (st)->content;
373     /* Check if the file is on this storage */
374     if (!xbt_dict_get_or_null(content_dict, fd->name)){
375       XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
376           st->generic_resource.name);
377       return 0;
378     } else {
379       XBT_DEBUG("UNLINK on disk '%s'",st->generic_resource.name);
380       st->used_size -= fd->size;
381
382       // Remove the file from storage
383       xbt_dict_remove(content_dict,fd->name);
384
385       free(fd->name);
386       free(fd->storage);
387       xbt_free(fd);
388       return 1;
389     }
390   }
391 }
392
393 static surf_action_t ws_action_ls(void *workstation, const char* mount,
394                                   const char *path)
395 {
396   XBT_DEBUG("LS on mount '%s' and file '%s'",mount, path);
397   storage_t st = find_storage_on_mount_list(workstation, mount);
398   surf_model_t model = st->generic_resource.model;
399   return model->extension.storage.ls(st, path);
400 }
401
402 static size_t ws_file_get_size(void *workstation, surf_file_t fd)
403 {
404   return fd->size;
405 }
406
407 static void surf_workstation_model_init_internal(void)
408 {
409   surf_workstation_model = surf_model_init();
410
411   surf_workstation_model->name = "Workstation";
412   surf_workstation_model->action_unref = ws_action_unref;
413   surf_workstation_model->action_cancel = ws_action_cancel;
414   surf_workstation_model->action_state_set = ws_action_state_set;
415
416   surf_workstation_model->model_private->resource_used = ws_resource_used;
417   surf_workstation_model->model_private->share_resources =
418       ws_share_resources;
419   surf_workstation_model->model_private->update_actions_state =
420       ws_update_actions_state;
421   surf_workstation_model->model_private->update_resource_state =
422       ws_update_resource_state;
423   surf_workstation_model->model_private->finalize = ws_finalize;
424
425   surf_workstation_model->suspend = ws_action_suspend;
426   surf_workstation_model->resume = ws_action_resume;
427   surf_workstation_model->is_suspended = ws_action_is_suspended;
428   surf_workstation_model->set_max_duration = ws_action_set_max_duration;
429   surf_workstation_model->set_priority = ws_action_set_priority;
430 #ifdef HAVE_TRACING
431   surf_workstation_model->set_category = ws_action_set_category;
432 #endif
433   surf_workstation_model->get_remains = ws_action_get_remains;
434 #ifdef HAVE_LATENCY_BOUND_TRACKING
435   surf_workstation_model->get_latency_limited = ws_get_latency_limited;
436 #endif
437
438   surf_workstation_model->extension.workstation.execute = ws_execute;
439   surf_workstation_model->extension.workstation.sleep = ws_action_sleep;
440   surf_workstation_model->extension.workstation.get_state = ws_get_state;
441   surf_workstation_model->extension.workstation.get_speed = ws_get_speed;
442   surf_workstation_model->extension.workstation.get_available_speed =
443       ws_get_available_speed;
444
445   surf_workstation_model->extension.workstation.communicate =
446       ws_communicate;
447   surf_workstation_model->extension.workstation.get_route = ws_get_route;
448   surf_workstation_model->extension.workstation.execute_parallel_task =
449       ws_execute_parallel_task;
450   surf_workstation_model->extension.workstation.get_link_bandwidth =
451       ws_get_link_bandwidth;
452   surf_workstation_model->extension.workstation.get_link_latency =
453       ws_get_link_latency;
454   surf_workstation_model->extension.workstation.link_shared =
455       ws_link_shared;
456   surf_workstation_model->extension.workstation.get_properties =
457       ws_get_properties;
458
459   surf_workstation_model->extension.workstation.open = ws_action_open;
460   surf_workstation_model->extension.workstation.close = ws_action_close;
461   surf_workstation_model->extension.workstation.read = ws_action_read;
462   surf_workstation_model->extension.workstation.write = ws_action_write;
463   surf_workstation_model->extension.workstation.unlink = ws_file_unlink;
464   surf_workstation_model->extension.workstation.ls = ws_action_ls;
465   surf_workstation_model->extension.workstation.get_size = ws_file_get_size;
466 }
467
468 void surf_workstation_model_init_current_default(void)
469 {
470   surf_workstation_model_init_internal();
471   xbt_cfg_setdefault_boolean(_sg_cfg_set, "network/crosstraffic", xbt_strdup("yes"));
472   surf_cpu_model_init_Cas01();
473   surf_network_model_init_LegrandVelho();
474
475   xbt_dynar_push(model_list, &surf_workstation_model);
476   sg_platf_host_add_cb(workstation_new);
477 }
478
479 void surf_workstation_model_init_compound()
480 {
481
482   xbt_assert(surf_cpu_model, "No CPU model defined yet!");
483   xbt_assert(surf_network_model, "No network model defined yet!");
484   surf_workstation_model_init_internal();
485   xbt_dynar_push(model_list, &surf_workstation_model);
486   sg_platf_host_add_cb(workstation_new);
487 }