Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
adapt perl tesh to handle timeout from command line, and defaut to 50 for windows...
[simgrid.git] / src / surf / workstation.c
1 /* Copyright (c) 2004-2013. 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 xbt_dict_t ws_get_storage_list(void *workstation)
369 {
370   s_mount_t mnt;
371   unsigned int i;
372   xbt_dict_t storage_list = xbt_dict_new();
373   char *storage_name = NULL;
374
375   workstation_CLM03_t ws = (workstation_CLM03_t) surf_workstation_resource_priv(workstation);
376   xbt_dynar_t storages = ws->storage;
377
378   xbt_dynar_foreach(storages,i,mnt)
379   {
380         storage_name = ((storage_t)mnt.storage)->generic_resource.name;
381     xbt_dict_set(storage_list,mnt.name,storage_name,NULL);
382   }
383   return storage_list;
384 }
385
386 static surf_action_t ws_action_open(void *workstation, const char* mount,
387                                     const char* path)
388 {
389   storage_t st = find_storage_on_mount_list(workstation, mount);
390   XBT_DEBUG("OPEN on disk '%s'",st->generic_resource.name);
391   surf_model_t model = st->generic_resource.model;
392   return model->extension.storage.open(st, mount, path);
393 }
394
395 static surf_action_t ws_action_close(void *workstation, surf_file_t fd)
396 {
397   storage_t st = find_storage_on_mount_list(workstation, fd->mount);
398   XBT_DEBUG("CLOSE on disk '%s'",st->generic_resource.name);
399   surf_model_t model = st->generic_resource.model;
400   return model->extension.storage.close(st, fd);
401 }
402
403 static surf_action_t ws_action_read(void *workstation, sg_storage_size_t size,
404                                     surf_file_t fd)
405 {
406   storage_t st = find_storage_on_mount_list(workstation, fd->mount);
407   XBT_DEBUG("READ on disk '%s'",st->generic_resource.name);
408   surf_model_t model = st->generic_resource.model;
409   return model->extension.storage.read(st, size, fd);
410 }
411
412 static surf_action_t ws_action_write(void *workstation, sg_storage_size_t size,
413                                      surf_file_t fd)
414 {
415   storage_t st = find_storage_on_mount_list(workstation, fd->mount);
416   XBT_DEBUG("WRITE on disk '%s'",st->generic_resource.name);
417   surf_model_t model = st->generic_resource.model;
418   return model->extension.storage.write(st, size, fd);
419 }
420
421 static int ws_file_unlink(void *workstation, surf_file_t fd)
422 {
423   if (!fd){
424     XBT_WARN("No such file descriptor. Impossible to unlink");
425     return 0;
426   } else {
427 //    XBT_INFO("%s %zu", fd->storage, fd->size);
428     storage_t st = find_storage_on_mount_list(workstation, fd->mount);
429     xbt_dict_t content_dict = (st)->content;
430     /* Check if the file is on this storage */
431     if (!xbt_dict_get_or_null(content_dict, fd->name)){
432       XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
433           st->generic_resource.name);
434       return 0;
435     } else {
436       XBT_DEBUG("UNLINK on disk '%s'",st->generic_resource.name);
437       st->used_size -= fd->size;
438
439       // Remove the file from storage
440       xbt_dict_remove(content_dict,fd->name);
441
442       free(fd->name);
443       free(fd->mount);
444       xbt_free(fd);
445       return 1;
446     }
447   }
448 }
449
450 static surf_action_t ws_action_ls(void *workstation, const char* mount,
451                                   const char *path)
452 {
453   XBT_DEBUG("LS on mount '%s' and file '%s'",mount, path);
454   storage_t st = find_storage_on_mount_list(workstation, mount);
455   surf_model_t model = st->generic_resource.model;
456   return model->extension.storage.ls(st, path);
457 }
458
459 static sg_storage_size_t ws_file_get_size(void *workstation, surf_file_t fd)
460 {
461   return fd->size;
462 }
463
464 static xbt_dynar_t ws_file_get_info(void *workstation, surf_file_t fd)
465 {
466   storage_t st = find_storage_on_mount_list(workstation, fd->mount);
467   xbt_dynar_t info = xbt_dynar_new(sizeof(void*), NULL);
468   xbt_dynar_push_as(info, void *, (void*)fd->size);
469   xbt_dynar_push_as(info, void *, fd->mount);
470   xbt_dynar_push_as(info, void *, st->generic_resource.name);
471   xbt_dynar_push_as(info, void *, st->type_id);
472   xbt_dynar_push_as(info, void *, st->content_type);
473
474   return info;
475 }
476
477 static sg_storage_size_t ws_storage_get_free_size(void *workstation,const char* name)
478 {
479   storage_t st = find_storage_on_mount_list(workstation, name);
480   return st->size - st->used_size;
481 }
482
483 static sg_storage_size_t ws_storage_get_used_size(void *workstation,const char* name)
484 {
485   storage_t st = find_storage_on_mount_list(workstation, name);
486   return st->used_size;
487 }
488
489 static void surf_workstation_model_init_internal(void)
490 {
491   surf_workstation_model = surf_model_init();
492
493   surf_workstation_model->name = "Workstation";
494   surf_workstation_model->action_unref = ws_action_unref;
495   surf_workstation_model->action_cancel = ws_action_cancel;
496   surf_workstation_model->action_state_set = ws_action_state_set;
497
498   surf_workstation_model->model_private->resource_used = ws_resource_used;
499   surf_workstation_model->model_private->share_resources =
500       ws_share_resources;
501   surf_workstation_model->model_private->update_actions_state =
502       ws_update_actions_state;
503   surf_workstation_model->model_private->update_resource_state =
504       ws_update_resource_state;
505   surf_workstation_model->model_private->finalize = ws_finalize;
506
507   surf_workstation_model->suspend = ws_action_suspend;
508   surf_workstation_model->resume = ws_action_resume;
509   surf_workstation_model->is_suspended = ws_action_is_suspended;
510   surf_workstation_model->set_max_duration = ws_action_set_max_duration;
511   surf_workstation_model->set_priority = ws_action_set_priority;
512 #ifdef HAVE_TRACING
513   surf_workstation_model->set_category = ws_action_set_category;
514 #endif
515   surf_workstation_model->get_remains = ws_action_get_remains;
516 #ifdef HAVE_LATENCY_BOUND_TRACKING
517   surf_workstation_model->get_latency_limited = ws_get_latency_limited;
518 #endif
519
520   surf_workstation_model->extension.workstation.execute = ws_execute;
521   surf_workstation_model->extension.workstation.sleep = ws_action_sleep;
522   surf_workstation_model->extension.workstation.get_state = ws_get_state;
523   surf_workstation_model->extension.workstation.get_core = ws_get_core;
524   surf_workstation_model->extension.workstation.get_speed = ws_get_speed;
525   surf_workstation_model->extension.workstation.get_available_speed =
526       ws_get_available_speed;
527   surf_workstation_model->extension.workstation.get_current_power_peak = ws_get_current_power_peak;
528   surf_workstation_model->extension.workstation.get_power_peak_at = ws_get_power_peak_at;
529   surf_workstation_model->extension.workstation.get_nb_pstates = ws_get_nb_pstates;
530   surf_workstation_model->extension.workstation.set_power_peak_at = ws_set_power_peak_at;
531   surf_workstation_model->extension.workstation.get_consumed_energy = ws_get_consumed_energy;
532
533   surf_workstation_model->extension.workstation.communicate =
534       ws_communicate;
535   surf_workstation_model->extension.workstation.get_route = ws_get_route;
536   surf_workstation_model->extension.workstation.execute_parallel_task =
537       ws_execute_parallel_task;
538   surf_workstation_model->extension.workstation.get_link_bandwidth =
539       ws_get_link_bandwidth;
540   surf_workstation_model->extension.workstation.get_link_latency =
541       ws_get_link_latency;
542   surf_workstation_model->extension.workstation.link_shared =
543       ws_link_shared;
544   surf_workstation_model->extension.workstation.get_properties =
545       ws_get_properties;
546
547   surf_workstation_model->extension.workstation.open = ws_action_open;
548   surf_workstation_model->extension.workstation.close = ws_action_close;
549   surf_workstation_model->extension.workstation.read = ws_action_read;
550   surf_workstation_model->extension.workstation.write = ws_action_write;
551   surf_workstation_model->extension.workstation.unlink = ws_file_unlink;
552   surf_workstation_model->extension.workstation.ls = ws_action_ls;
553   surf_workstation_model->extension.workstation.get_size = ws_file_get_size;
554   surf_workstation_model->extension.workstation.get_info = ws_file_get_info;
555   surf_workstation_model->extension.workstation.get_free_size = ws_storage_get_free_size;
556   surf_workstation_model->extension.workstation.get_used_size = ws_storage_get_used_size;
557   surf_workstation_model->extension.workstation.get_storage_list = ws_get_storage_list;
558 }
559
560 void surf_workstation_model_init_current_default(void)
561 {
562   surf_workstation_model_init_internal();
563   xbt_cfg_setdefault_boolean(_sg_cfg_set, "network/crosstraffic", "yes");
564   surf_cpu_model_init_Cas01();
565   surf_network_model_init_LegrandVelho();
566
567   xbt_dynar_push(model_list, &surf_workstation_model);
568   sg_platf_host_add_cb(workstation_new);
569 }
570
571 void surf_workstation_model_init_compound()
572 {
573
574   xbt_assert(surf_cpu_model, "No CPU model defined yet!");
575   xbt_assert(surf_network_model, "No network model defined yet!");
576   surf_workstation_model_init_internal();
577   xbt_dynar_push(model_list, &surf_workstation_model);
578   sg_platf_host_add_cb(workstation_new);
579 }