Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bbff29d12c1b84a5d71afea0b7024c511969db3c
[simgrid.git] / examples / c / io-file-remote / io-file-remote.c
1 /* Copyright (c) 2014-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/actor.h"
7 #include "simgrid/disk.h"
8 #include "simgrid/engine.h"
9 #include "simgrid/host.h"
10 #include <simgrid/plugins/file_system.h>
11
12 #include "xbt/asserts.h"
13 #include "xbt/log.h"
14
15 #define INMEGA (1024 * 1024)
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(remote_io, "Messages specific for this io example");
18
19 static void host(int argc, char* argv[])
20 {
21   sg_file_t file       = sg_file_open(argv[1], NULL);
22   const char* filename = sg_file_get_name(file);
23   XBT_INFO("Opened file '%s'", filename);
24   sg_file_dump(file);
25
26   XBT_INFO("Try to write %llu MiB to '%s'", sg_file_get_size(file) / 1024, filename);
27   sg_size_t write = sg_file_write(file, sg_file_get_size(file) * 1024);
28   XBT_INFO("Have written %llu MiB to '%s'.", write / (1024 * 1024), filename);
29
30   if (argc > 4) {
31     if (atoi(argv[4]) != 0) {
32       XBT_INFO("Move '%s' (of size %llu) from '%s' to '%s'", filename, sg_file_get_size(file), sg_host_self_get_name(),
33                argv[2]);
34       sg_file_rmove(file, sg_host_by_name(argv[2]), argv[3]);
35     } else {
36       XBT_INFO("Copy '%s' (of size %llu) from '%s' to '%s'", filename, sg_file_get_size(file), sg_host_self_get_name(),
37                argv[2]);
38       sg_file_rcopy(file, sg_host_by_name(argv[2]), argv[3]);
39     }
40   }
41   sg_file_close(file);
42 }
43
44 int main(int argc, char** argv)
45 {
46   simgrid_init(&argc, argv);
47   sg_storage_file_system_init();
48   simgrid_load_platform(argv[1]);
49
50   simgrid_register_function("host", host);
51   simgrid_load_deployment(argv[2]);
52
53   size_t host_count = sg_host_count();
54   sg_host_t* hosts  = sg_host_list();
55
56   for (long i = 0; i < host_count; i++) {
57     unsigned int disk_count;
58     sg_disk_t* disks;
59     sg_host_get_disks(hosts[i], &disk_count, &disks);
60     for (unsigned int j = 0; j < disk_count; j++)
61       XBT_INFO("Init: %s: %llu/%llu MiB used/free on '%s@%s'", sg_host_get_name(hosts[i]),
62                sg_disk_get_size_used(disks[j]) / INMEGA, sg_disk_get_size_free(disks[j]) / INMEGA,
63                sg_disk_get_name(disks[j]), sg_host_get_name(sg_disk_get_host(disks[j])));
64     free(disks);
65   }
66
67   simgrid_run();
68
69   for (long i = 0; i < host_count; i++) {
70     unsigned int disk_count;
71     sg_disk_t* disks;
72     sg_host_get_disks(hosts[i], &disk_count, &disks);
73     for (unsigned int j = 0; j < disk_count; j++)
74       XBT_INFO("End: %llu/%llu MiB used/free on '%s@%s'", sg_disk_get_size_used(disks[j]) / INMEGA,
75                sg_disk_get_size_free(disks[j]) / INMEGA, sg_disk_get_name(disks[j]), sg_host_get_name(hosts[i]));
76     free(disks);
77   }
78
79   free(hosts);
80
81   XBT_INFO("Simulation time %g", simgrid_get_clock());
82   return 0;
83 }