Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add MSG_file_rename function
[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 #include <inttypes.h>
15
16 typedef struct workstation_CLM03 {
17   s_surf_resource_t generic_resource;   /* Must remain first to add this to a trace */
18   void *net_elm;
19   xbt_dynar_t storage;
20 } s_workstation_CLM03_t, *workstation_CLM03_t;
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_workstation, surf,
23                                 "Logging specific to the SURF workstation module");
24
25 surf_model_t surf_workstation_model = NULL;
26
27 static void workstation_new(sg_platf_host_cbarg_t host)
28 {
29   workstation_CLM03_t workstation = xbt_new0(s_workstation_CLM03_t, 1);
30
31   workstation->generic_resource.model = surf_workstation_model;
32   workstation->generic_resource.name = xbt_strdup(host->id);
33   workstation->storage = xbt_lib_get_or_null(storage_lib,host->id,ROUTING_STORAGE_HOST_LEVEL);
34   workstation->net_elm = xbt_lib_get_or_null(host_lib,host->id,ROUTING_HOST_LEVEL);
35   XBT_DEBUG("Create workstation %s with %ld mounted disks",host->id,xbt_dynar_length(workstation->storage));
36   xbt_lib_set(host_lib, host->id, SURF_WKS_LEVEL, workstation);
37 }
38
39 static int ws_resource_used(void *resource_id)
40 {
41   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
42   return -1;
43 }
44
45 static void ws_parallel_action_cancel(surf_action_t action)
46 {
47   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
48 }
49
50 static int ws_parallel_action_free(surf_action_t action)
51 {
52   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
53   return -1;
54 }
55
56 static int ws_action_unref(surf_action_t action)
57 {
58   if (action->model_type == surf_network_model)
59     return surf_network_model->action_unref(action);
60   else if (action->model_type == surf_cpu_model)
61     return surf_cpu_model->action_unref(action);
62   else if (action->model_type == surf_workstation_model)
63     return ws_parallel_action_free(action);
64   else
65     DIE_IMPOSSIBLE;
66   return 0;
67 }
68
69 static void ws_action_cancel(surf_action_t action)
70 {
71   if (action->model_type == surf_network_model)
72     surf_network_model->action_cancel(action);
73   else if (action->model_type == surf_cpu_model)
74     surf_cpu_model->action_cancel(action);
75   else if (action->model_type == surf_workstation_model)
76     ws_parallel_action_cancel(action);
77   else
78     DIE_IMPOSSIBLE;
79   return;
80 }
81
82 static void ws_action_state_set(surf_action_t action,
83                                 e_surf_action_state_t state)
84 {
85   if (action->model_type == surf_network_model)
86     surf_network_model->action_state_set(action, state);
87   else if (action->model_type == surf_cpu_model)
88     surf_cpu_model->action_state_set(action, state);
89   else if (action->model_type == surf_workstation_model)
90     surf_action_state_set(action, state);
91   else
92     DIE_IMPOSSIBLE;
93   return;
94 }
95
96 static double ws_share_resources(double now)
97 {
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   return surf_cpu_model->extension.cpu.
122       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     surf_cpu_model->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     surf_cpu_model->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 surf_cpu_model->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     surf_cpu_model->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     surf_cpu_model->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     surf_cpu_model->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 surf_cpu_model->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   return surf_cpu_model->extension.cpu.
222       get_state(workstation);
223 }
224
225 static double ws_get_speed(void *workstation, double load)
226 {
227   return surf_cpu_model->extension.cpu.
228       get_speed(workstation, load);
229 }
230
231 static int ws_get_core(void *workstation)
232 {
233   return surf_cpu_model->extension.cpu.
234       get_core(workstation);
235 }
236
237
238
239 static double ws_get_available_speed(void *workstation)
240 {
241   return surf_cpu_model->extension.cpu.
242       get_available_speed(workstation);
243 }
244
245 static double ws_get_current_power_peak(void *workstation)
246 {
247   return surf_cpu_model->extension.cpu.
248       get_current_power_peak(workstation);
249 }
250
251 static double ws_get_power_peak_at(void *workstation, int pstate_index)
252 {
253   return surf_cpu_model->extension.cpu.
254       get_power_peak_at(workstation, pstate_index);
255 }
256
257 static int ws_get_nb_pstates(void *workstation)
258 {
259   return surf_cpu_model->extension.cpu.
260       get_nb_pstates(workstation);
261 }
262
263 static void ws_set_power_peak_at(void *workstation, int pstate_index)
264 {
265   surf_cpu_model->extension.cpu.
266       set_power_peak_at(workstation, pstate_index);
267 }
268
269 static double ws_get_consumed_energy(void *workstation)
270 {
271   return surf_cpu_model->extension.cpu.
272       get_consumed_energy(workstation);
273 }
274
275
276 static surf_action_t ws_execute_parallel_task(int workstation_nb,
277                                               void **workstation_list,
278                                               double *computation_amount,
279                                               double *communication_amount,
280                                               double rate)
281 {
282 #define cost_or_zero(array,pos) ((array)?(array)[pos]:0.0)
283   if ((workstation_nb == 1)
284       && (cost_or_zero(communication_amount, 0) == 0.0))
285     return ws_execute(workstation_list[0], computation_amount[0]);
286   else if ((workstation_nb == 1)
287            && (cost_or_zero(computation_amount, 0) == 0.0))
288     return ws_communicate(workstation_list[0], workstation_list[0],communication_amount[0], rate);
289   else if ((workstation_nb == 2)
290              && (cost_or_zero(computation_amount, 0) == 0.0)
291              && (cost_or_zero(computation_amount, 1) == 0.0)) {
292     int i,nb = 0;
293     double value = 0.0;
294
295     for (i = 0; i < workstation_nb * workstation_nb; i++) {
296       if (cost_or_zero(communication_amount, i) > 0.0) {
297         nb++;
298         value = cost_or_zero(communication_amount, i);
299       }
300     }
301     if (nb == 1)
302       return ws_communicate(workstation_list[0], workstation_list[1],value, rate);
303   }
304 #undef cost_or_zero
305
306   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
307   return NULL;
308 }
309
310
311 /* returns an array of network_link_CM02_t */
312 static xbt_dynar_t ws_get_route(void *workstation_src, void *workstation_dst)
313 {
314   XBT_DEBUG("ws_get_route");
315   workstation_CLM03_t src = surf_workstation_resource_priv(workstation_src);
316   workstation_CLM03_t dst = surf_workstation_resource_priv(workstation_dst);
317   return surf_network_model->extension.
318       network.get_route(src->net_elm,
319                   dst->net_elm);
320 }
321
322 static double ws_get_link_bandwidth(const void *link)
323 {
324   return surf_network_model->extension.network.get_link_bandwidth(link);
325 }
326
327 static double ws_get_link_latency(const void *link)
328 {
329   return surf_network_model->extension.network.get_link_latency(link);
330 }
331
332 static int ws_link_shared(const void *link)
333 {
334   return surf_network_model->extension.network.link_shared(link);
335 }
336
337 static void ws_finalize(void)
338 {
339   surf_model_exit(surf_workstation_model);
340   surf_workstation_model = NULL;
341 }
342
343 static xbt_dict_t ws_get_properties(const void *ws)
344 {
345   return surf_resource_properties(surf_cpu_resource_priv(ws));
346 }
347
348 static storage_t find_storage_on_mount_list(void *workstation,const char* mount)
349 {
350   storage_t st = NULL;
351   s_mount_t mnt;
352   unsigned int cursor;
353   workstation_CLM03_t ws = (workstation_CLM03_t) surf_workstation_resource_priv(workstation);
354   xbt_dynar_t storage_list = ws->storage;
355
356   XBT_DEBUG("Search for storage name '%s' on '%s'",mount,ws->generic_resource.name);
357   xbt_dynar_foreach(storage_list,cursor,mnt)
358   {
359     XBT_DEBUG("See '%s'",mnt.name);
360     if(!strcmp(mount,mnt.name)){
361       st = mnt.storage;
362       break;
363     }
364   }
365   if(!st) xbt_die("Can't find mount '%s' for '%s'",mount,ws->generic_resource.name);
366   return st;
367 }
368
369 static xbt_dict_t ws_get_storage_list(void *workstation)
370 {
371   s_mount_t mnt;
372   unsigned int i;
373   xbt_dict_t storage_list = xbt_dict_new_homogeneous(NULL);
374   char *storage_name = NULL;
375
376   workstation_CLM03_t ws = (workstation_CLM03_t) surf_workstation_resource_priv(workstation);
377   xbt_dynar_t storages = ws->storage;
378
379   xbt_dynar_foreach(storages,i,mnt){
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, surf_file_t fd, sg_storage_size_t size)
404 {
405   storage_t st = find_storage_on_mount_list(workstation, fd->mount);
406   XBT_DEBUG("READ on disk '%s'",st->generic_resource.name);
407   surf_model_t model = st->generic_resource.model;
408   return model->extension.storage.read(st, fd, size);
409 }
410
411 static surf_action_t ws_action_write(void *workstation, surf_file_t fd, sg_storage_size_t size)
412 {
413   storage_t st = find_storage_on_mount_list(workstation, fd->mount);
414   XBT_DEBUG("WRITE on disk '%s'",st->generic_resource.name);
415   surf_model_t model = st->generic_resource.model;
416   return model->extension.storage.write(st, fd, size);
417 }
418
419 static int ws_file_unlink(void *workstation, surf_file_t fd)
420 {
421   if (!fd){
422     XBT_WARN("No such file descriptor. Impossible to unlink");
423     return 0;
424   } else {
425 //    XBT_INFO("%s %zu", fd->storage, fd->size);
426     storage_t st = find_storage_on_mount_list(workstation, fd->mount);
427     xbt_dict_t content_dict = (st)->content;
428     /* Check if the file is on this storage */
429     if (!xbt_dict_get_or_null(content_dict, fd->name)){
430       XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
431           st->generic_resource.name);
432       return 0;
433     } else {
434       XBT_DEBUG("UNLINK on disk '%s'",st->generic_resource.name);
435       st->used_size -= fd->size;
436
437       // Remove the file from storage
438       xbt_dict_remove(content_dict,fd->name);
439
440       free(fd->name);
441       free(fd->mount);
442       xbt_free(fd);
443       return 1;
444     }
445   }
446 }
447
448 static surf_action_t ws_action_ls(void *workstation, const char* mount,
449                                   const char *path)
450 {
451   XBT_DEBUG("LS on mount '%s' and file '%s'",mount, path);
452   storage_t st = find_storage_on_mount_list(workstation, mount);
453   surf_model_t model = st->generic_resource.model;
454   return model->extension.storage.ls(st, path);
455 }
456
457 static sg_storage_size_t ws_file_get_size(void *workstation, surf_file_t fd)
458 {
459   return fd->size;
460 }
461
462 static xbt_dynar_t ws_file_get_info(void *workstation, surf_file_t fd)
463 {
464   storage_t st = find_storage_on_mount_list(workstation, fd->mount);
465   sg_storage_size_t *psize = xbt_new(sg_storage_size_t, 1);
466   *psize = fd->size;
467   xbt_dynar_t info = xbt_dynar_new(sizeof(void*), NULL);
468   xbt_dynar_push_as(info, sg_storage_size_t *, psize);
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 void ws_file_rename(void *workstation, surf_file_t fd, const char* new_name)
478 {
479   storage_t storage = find_storage_on_mount_list(workstation, fd->mount);
480
481   const char* old_full_name = fd->name;
482   xbt_dynar_t dyn = NULL;
483   const char* separator;
484   const char* ctype = storage->content_type;
485
486   // TODO: PV: use an enum and a switch case to manage content type properly
487   if(!strcmp(ctype, "txt_unix"))
488         separator = strdup("/");
489   else
490         separator = strdup("\\");
491
492   // Split file with separator and replace file name
493   dyn = xbt_str_split(old_full_name, separator);
494   xbt_dynar_pop_ptr(dyn);
495   xbt_dynar_push(dyn, &new_name);
496   char *new_full_name = xbt_str_join(dyn, separator);
497
498   sg_storage_size_t *psize;
499   psize = (sg_storage_size_t*) xbt_dict_get_or_null(storage->content,old_full_name);
500   xbt_dict_remove(storage->content, old_full_name);
501   xbt_dict_set(storage->content,new_full_name,psize,NULL);
502   XBT_DEBUG("Change file name from %s to %s, size '%" PRIu64 "'",fd->name, new_full_name, *psize);
503 }
504
505 static sg_storage_size_t ws_storage_get_free_size(void *workstation,const char* name)
506 {
507   storage_t st = find_storage_on_mount_list(workstation, name);
508   return st->size - st->used_size;
509 }
510
511 static sg_storage_size_t ws_storage_get_used_size(void *workstation,const char* name)
512 {
513   storage_t st = find_storage_on_mount_list(workstation, name);
514   return st->used_size;
515 }
516
517 static void surf_workstation_model_init_internal(void)
518 {
519   surf_workstation_model = surf_model_init();
520
521   surf_workstation_model->name = "Workstation";
522   surf_workstation_model->action_unref = ws_action_unref;
523   surf_workstation_model->action_cancel = ws_action_cancel;
524   surf_workstation_model->action_state_set = ws_action_state_set;
525
526   surf_workstation_model->model_private->resource_used = ws_resource_used;
527   surf_workstation_model->model_private->share_resources =
528       ws_share_resources;
529   surf_workstation_model->model_private->update_actions_state =
530       ws_update_actions_state;
531   surf_workstation_model->model_private->update_resource_state =
532       ws_update_resource_state;
533   surf_workstation_model->model_private->finalize = ws_finalize;
534
535   surf_workstation_model->suspend = ws_action_suspend;
536   surf_workstation_model->resume = ws_action_resume;
537   surf_workstation_model->is_suspended = ws_action_is_suspended;
538   surf_workstation_model->set_max_duration = ws_action_set_max_duration;
539   surf_workstation_model->set_priority = ws_action_set_priority;
540 #ifdef HAVE_TRACING
541   surf_workstation_model->set_category = ws_action_set_category;
542 #endif
543   surf_workstation_model->get_remains = ws_action_get_remains;
544 #ifdef HAVE_LATENCY_BOUND_TRACKING
545   surf_workstation_model->get_latency_limited = ws_get_latency_limited;
546 #endif
547
548   surf_workstation_model->extension.workstation.execute = ws_execute;
549   surf_workstation_model->extension.workstation.sleep = ws_action_sleep;
550   surf_workstation_model->extension.workstation.get_state = ws_get_state;
551   surf_workstation_model->extension.workstation.get_core = ws_get_core;
552   surf_workstation_model->extension.workstation.get_speed = ws_get_speed;
553   surf_workstation_model->extension.workstation.get_available_speed =
554       ws_get_available_speed;
555   surf_workstation_model->extension.workstation.get_current_power_peak = ws_get_current_power_peak;
556   surf_workstation_model->extension.workstation.get_power_peak_at = ws_get_power_peak_at;
557   surf_workstation_model->extension.workstation.get_nb_pstates = ws_get_nb_pstates;
558   surf_workstation_model->extension.workstation.set_power_peak_at = ws_set_power_peak_at;
559   surf_workstation_model->extension.workstation.get_consumed_energy = ws_get_consumed_energy;
560
561   surf_workstation_model->extension.workstation.communicate =
562       ws_communicate;
563   surf_workstation_model->extension.workstation.get_route = ws_get_route;
564   surf_workstation_model->extension.workstation.execute_parallel_task =
565       ws_execute_parallel_task;
566   surf_workstation_model->extension.workstation.get_link_bandwidth =
567       ws_get_link_bandwidth;
568   surf_workstation_model->extension.workstation.get_link_latency =
569       ws_get_link_latency;
570   surf_workstation_model->extension.workstation.link_shared =
571       ws_link_shared;
572   surf_workstation_model->extension.workstation.get_properties =
573       ws_get_properties;
574
575   surf_workstation_model->extension.workstation.open = ws_action_open;
576   surf_workstation_model->extension.workstation.close = ws_action_close;
577   surf_workstation_model->extension.workstation.read = ws_action_read;
578   surf_workstation_model->extension.workstation.write = ws_action_write;
579   surf_workstation_model->extension.workstation.unlink = ws_file_unlink;
580   surf_workstation_model->extension.workstation.ls = ws_action_ls;
581   surf_workstation_model->extension.workstation.get_size = ws_file_get_size;
582   surf_workstation_model->extension.workstation.get_info = ws_file_get_info;
583   surf_workstation_model->extension.workstation.rename = ws_file_rename;
584   surf_workstation_model->extension.workstation.get_free_size = ws_storage_get_free_size;
585   surf_workstation_model->extension.workstation.get_used_size = ws_storage_get_used_size;
586   surf_workstation_model->extension.workstation.get_storage_list = ws_get_storage_list;
587 }
588
589 void surf_workstation_model_init_current_default(void)
590 {
591   surf_workstation_model_init_internal();
592   xbt_cfg_setdefault_boolean(_sg_cfg_set, "network/crosstraffic", "yes");
593   surf_cpu_model_init_Cas01();
594   surf_network_model_init_LegrandVelho();
595
596   xbt_dynar_push(model_list, &surf_workstation_model);
597   sg_platf_host_add_cb(workstation_new);
598 }
599
600 void surf_workstation_model_init_compound()
601 {
602
603   xbt_assert(surf_cpu_model, "No CPU model defined yet!");
604   xbt_assert(surf_network_model, "No network model defined yet!");
605   surf_workstation_model_init_internal();
606   xbt_dynar_push(model_list, &surf_workstation_model);
607   sg_platf_host_add_cb(workstation_new);
608 }