Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
handle modifications of the DTD in surf
[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 int ws_get_core(void *workstation)
231 {
232   return surf_cpu_model->extension.cpu.
233       get_core(workstation);
234 }
235
236
237
238 static double ws_get_available_speed(void *workstation)
239 {
240   return surf_cpu_model->extension.cpu.
241       get_available_speed(workstation);
242 }
243
244 static double ws_get_current_power_peak(void *workstation)
245 {
246   return surf_cpu_model->extension.cpu.
247       get_current_power_peak(workstation);
248 }
249
250 static double ws_get_power_peak_at(void *workstation, int pstate_index)
251 {
252   return surf_cpu_model->extension.cpu.
253       get_power_peak_at(workstation, pstate_index);
254 }
255
256 static int ws_get_nb_pstates(void *workstation)
257 {
258   return surf_cpu_model->extension.cpu.
259       get_nb_pstates(workstation);
260 }
261
262 static void ws_set_power_peak_at(void *workstation, int pstate_index)
263 {
264   surf_cpu_model->extension.cpu.
265       set_power_peak_at(workstation, pstate_index);
266 }
267
268 static double ws_get_consumed_energy(void *workstation)
269 {
270   return surf_cpu_model->extension.cpu.
271       get_consumed_energy(workstation);
272 }
273
274
275 static surf_action_t ws_execute_parallel_task(int workstation_nb,
276                                               void **workstation_list,
277                                               double *computation_amount,
278                                               double *communication_amount,
279                                               double rate)
280 {
281 #define cost_or_zero(array,pos) ((array)?(array)[pos]:0.0)
282   if ((workstation_nb == 1)
283       && (cost_or_zero(communication_amount, 0) == 0.0))
284     return ws_execute(workstation_list[0], computation_amount[0]);
285   else if ((workstation_nb == 1)
286            && (cost_or_zero(computation_amount, 0) == 0.0))
287     return ws_communicate(workstation_list[0], workstation_list[0],communication_amount[0], rate);
288   else if ((workstation_nb == 2)
289              && (cost_or_zero(computation_amount, 0) == 0.0)
290              && (cost_or_zero(computation_amount, 1) == 0.0)) {
291     int i,nb = 0;
292     double value = 0.0;
293
294     for (i = 0; i < workstation_nb * workstation_nb; i++) {
295       if (cost_or_zero(communication_amount, i) > 0.0) {
296         nb++;
297         value = cost_or_zero(communication_amount, i);
298       }
299     }
300     if (nb == 1)
301       return ws_communicate(workstation_list[0], workstation_list[1],value, rate);
302   }
303 #undef cost_or_zero
304
305   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
306   return NULL;
307 }
308
309
310 /* returns an array of network_link_CM02_t */
311 static xbt_dynar_t ws_get_route(void *workstation_src, void *workstation_dst)
312 {
313   XBT_DEBUG("ws_get_route");
314   workstation_CLM03_t src = surf_workstation_resource_priv(workstation_src);
315   workstation_CLM03_t dst = surf_workstation_resource_priv(workstation_dst);
316   return surf_network_model->extension.
317       network.get_route(src->net_elm,
318                   dst->net_elm);
319 }
320
321 static double ws_get_link_bandwidth(const void *link)
322 {
323   return surf_network_model->extension.network.get_link_bandwidth(link);
324 }
325
326 static double ws_get_link_latency(const void *link)
327 {
328   return surf_network_model->extension.network.get_link_latency(link);
329 }
330
331 static int ws_link_shared(const void *link)
332 {
333   return surf_network_model->extension.network.link_shared(link);
334 }
335
336 static void ws_finalize(void)
337 {
338   surf_model_exit(surf_workstation_model);
339   surf_workstation_model = NULL;
340 }
341
342 static xbt_dict_t ws_get_properties(const void *ws)
343 {
344   return surf_resource_properties(surf_cpu_resource_priv(ws));
345 }
346
347 static storage_t find_storage_on_mount_list(void *workstation,const char* mount)
348 {
349   storage_t st = NULL;
350   s_mount_t mnt;
351   unsigned int cursor;
352   workstation_CLM03_t ws = (workstation_CLM03_t) surf_workstation_resource_priv(workstation);
353   xbt_dynar_t storage_list = ws->storage;
354
355   XBT_DEBUG("Search for storage name '%s' on '%s'",mount,ws->generic_resource.name);
356   xbt_dynar_foreach(storage_list,cursor,mnt)
357   {
358     XBT_DEBUG("See '%s'",mnt.name);
359     if(!strcmp(mount,mnt.name)){
360       st = mnt.storage;
361       break;
362     }
363   }
364   if(!st) xbt_die("Can't find mount '%s' for '%s'",mount,ws->generic_resource.name);
365   return st;
366 }
367
368 static surf_action_t ws_action_open(void *workstation, const char* mount,
369                                     const char* path)
370 {
371   storage_t st = find_storage_on_mount_list(workstation, mount);
372   XBT_DEBUG("OPEN on disk '%s'",st->generic_resource.name);
373   surf_model_t model = st->generic_resource.model;
374   return model->extension.storage.open(st, mount, path);
375 }
376
377 static surf_action_t ws_action_close(void *workstation, surf_file_t fd)
378 {
379   storage_t st = find_storage_on_mount_list(workstation, fd->mount);
380   XBT_DEBUG("CLOSE on disk '%s'",st->generic_resource.name);
381   surf_model_t model = st->generic_resource.model;
382   return model->extension.storage.close(st, fd);
383 }
384
385 static surf_action_t ws_action_read(void *workstation, size_t size,
386                                     surf_file_t fd)
387 {
388   storage_t st = find_storage_on_mount_list(workstation, fd->mount);
389   XBT_DEBUG("READ on disk '%s'",st->generic_resource.name);
390   surf_model_t model = st->generic_resource.model;
391   return model->extension.storage.read(st, size, fd);
392 }
393
394 static surf_action_t ws_action_write(void *workstation, size_t size, 
395                                      surf_file_t fd)
396 {
397   storage_t st = find_storage_on_mount_list(workstation, fd->mount);
398   XBT_DEBUG("WRITE on disk '%s'",st->generic_resource.name);
399   surf_model_t model = st->generic_resource.model;
400   return model->extension.storage.write(st, size, fd);
401 }
402
403 static int ws_file_unlink(void *workstation, surf_file_t fd)
404 {
405   if (!fd){
406     XBT_WARN("No such file descriptor. Impossible to unlink");
407     return 0;
408   } else {
409 //    XBT_INFO("%s %zu", fd->storage, fd->size);
410     storage_t st = find_storage_on_mount_list(workstation, fd->mount);
411     xbt_dict_t content_dict = (st)->content;
412     /* Check if the file is on this storage */
413     if (!xbt_dict_get_or_null(content_dict, fd->name)){
414       XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
415           st->generic_resource.name);
416       return 0;
417     } else {
418       XBT_DEBUG("UNLINK on disk '%s'",st->generic_resource.name);
419       st->used_size -= fd->size;
420
421       // Remove the file from storage
422       xbt_dict_remove(content_dict,fd->name);
423
424       free(fd->name);
425       free(fd->mount);
426       xbt_free(fd);
427       return 1;
428     }
429   }
430 }
431
432 static surf_action_t ws_action_ls(void *workstation, const char* mount,
433                                   const char *path)
434 {
435   XBT_DEBUG("LS on mount '%s' and file '%s'",mount, path);
436   storage_t st = find_storage_on_mount_list(workstation, mount);
437   surf_model_t model = st->generic_resource.model;
438   return model->extension.storage.ls(st, path);
439 }
440
441 static size_t ws_file_get_size(void *workstation, surf_file_t fd)
442 {
443   return fd->size;
444 }
445
446 static void surf_workstation_model_init_internal(void)
447 {
448   surf_workstation_model = surf_model_init();
449
450   surf_workstation_model->name = "Workstation";
451   surf_workstation_model->action_unref = ws_action_unref;
452   surf_workstation_model->action_cancel = ws_action_cancel;
453   surf_workstation_model->action_state_set = ws_action_state_set;
454
455   surf_workstation_model->model_private->resource_used = ws_resource_used;
456   surf_workstation_model->model_private->share_resources =
457       ws_share_resources;
458   surf_workstation_model->model_private->update_actions_state =
459       ws_update_actions_state;
460   surf_workstation_model->model_private->update_resource_state =
461       ws_update_resource_state;
462   surf_workstation_model->model_private->finalize = ws_finalize;
463
464   surf_workstation_model->suspend = ws_action_suspend;
465   surf_workstation_model->resume = ws_action_resume;
466   surf_workstation_model->is_suspended = ws_action_is_suspended;
467   surf_workstation_model->set_max_duration = ws_action_set_max_duration;
468   surf_workstation_model->set_priority = ws_action_set_priority;
469 #ifdef HAVE_TRACING
470   surf_workstation_model->set_category = ws_action_set_category;
471 #endif
472   surf_workstation_model->get_remains = ws_action_get_remains;
473 #ifdef HAVE_LATENCY_BOUND_TRACKING
474   surf_workstation_model->get_latency_limited = ws_get_latency_limited;
475 #endif
476
477   surf_workstation_model->extension.workstation.execute = ws_execute;
478   surf_workstation_model->extension.workstation.sleep = ws_action_sleep;
479   surf_workstation_model->extension.workstation.get_state = ws_get_state;
480   surf_workstation_model->extension.workstation.get_core = ws_get_core;
481   surf_workstation_model->extension.workstation.get_speed = ws_get_speed;
482   surf_workstation_model->extension.workstation.get_available_speed =
483       ws_get_available_speed;
484   surf_workstation_model->extension.workstation.get_current_power_peak = ws_get_current_power_peak;
485   surf_workstation_model->extension.workstation.get_power_peak_at = ws_get_power_peak_at;
486   surf_workstation_model->extension.workstation.get_nb_pstates = ws_get_nb_pstates;
487   surf_workstation_model->extension.workstation.set_power_peak_at = ws_set_power_peak_at;
488   surf_workstation_model->extension.workstation.get_consumed_energy = ws_get_consumed_energy;
489
490   surf_workstation_model->extension.workstation.communicate =
491       ws_communicate;
492   surf_workstation_model->extension.workstation.get_route = ws_get_route;
493   surf_workstation_model->extension.workstation.execute_parallel_task =
494       ws_execute_parallel_task;
495   surf_workstation_model->extension.workstation.get_link_bandwidth =
496       ws_get_link_bandwidth;
497   surf_workstation_model->extension.workstation.get_link_latency =
498       ws_get_link_latency;
499   surf_workstation_model->extension.workstation.link_shared =
500       ws_link_shared;
501   surf_workstation_model->extension.workstation.get_properties =
502       ws_get_properties;
503
504   surf_workstation_model->extension.workstation.open = ws_action_open;
505   surf_workstation_model->extension.workstation.close = ws_action_close;
506   surf_workstation_model->extension.workstation.read = ws_action_read;
507   surf_workstation_model->extension.workstation.write = ws_action_write;
508   surf_workstation_model->extension.workstation.unlink = ws_file_unlink;
509   surf_workstation_model->extension.workstation.ls = ws_action_ls;
510   surf_workstation_model->extension.workstation.get_size = ws_file_get_size;
511 }
512
513 void surf_workstation_model_init_current_default(void)
514 {
515   surf_workstation_model_init_internal();
516   xbt_cfg_setdefault_boolean(_sg_cfg_set, "network/crosstraffic", xbt_strdup("yes"));
517   surf_cpu_model_init_Cas01();
518   surf_network_model_init_LegrandVelho();
519
520   xbt_dynar_push(model_list, &surf_workstation_model);
521   sg_platf_host_add_cb(workstation_new);
522 }
523
524 void surf_workstation_model_init_compound()
525 {
526
527   xbt_assert(surf_cpu_model, "No CPU model defined yet!");
528   xbt_assert(surf_network_model, "No network model defined yet!");
529   surf_workstation_model_init_internal();
530   xbt_dynar_push(model_list, &surf_workstation_model);
531   sg_platf_host_add_cb(workstation_new);
532 }