Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / examples / cpp / io-file-remote / s4u-io-file-remote.cpp
1 /* Copyright (c) 2014-2023. 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/plugins/file_system.h>
7 #include <simgrid/s4u.hpp>
8 #include <string>
9
10 #define INMEGA (1024 * 1024)
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(remote_io, "Messages specific for this io example");
13 namespace sg4 = simgrid::s4u;
14
15 static void host(std::vector<std::string> args)
16 {
17   sg4::File* file          = sg4::File::open(args[1], nullptr);
18   const char* filename     = file->get_path();
19   XBT_INFO("Opened file '%s'", filename);
20   file->dump();
21   XBT_INFO("Try to write %llu MiB to '%s'", file->size() / 1024, filename);
22   sg_size_t write = file->write(file->size() * 1024);
23   XBT_INFO("Have written %llu MiB to '%s'.", write / (1024 * 1024), filename);
24
25   if (args.size() > 4) {
26     if (std::stoi(args[4]) != 0) {
27       XBT_INFO("Move '%s' (of size %llu) from '%s' to '%s'", filename, file->size(), sg4::Host::current()->get_cname(),
28                args[2].c_str());
29       file->remote_move(sg4::Host::by_name(args[2]), args[3]);
30     } else {
31       XBT_INFO("Copy '%s' (of size %llu) from '%s' to '%s'", filename, file->size(), sg4::Host::current()->get_cname(),
32                args[2].c_str());
33       file->remote_copy(sg4::Host::by_name(args[2]), args[3]);
34     }
35   }
36   file->close();
37 }
38
39 int main(int argc, char** argv)
40 {
41   sg4::Engine e(&argc, argv);
42   sg_storage_file_system_init();
43   e.load_platform(argv[1]);
44   e.register_function("host", host);
45   e.load_deployment(argv[2]);
46   std::vector<sg4::Host*> all_hosts = e.get_all_hosts();
47
48   for (auto const& h : all_hosts) {
49     for (auto const& d : h->get_disks())
50       XBT_INFO("Init: %s: %llu/%llu MiB used/free on '%s@%s'", h->get_cname(), sg_disk_get_size_used(d) / INMEGA,
51                sg_disk_get_size_free(d) / INMEGA, d->get_cname(), d->get_host()->get_cname());
52   }
53
54   e.run();
55
56   for (auto const& h : all_hosts) {
57     for (auto const& d : h->get_disks())
58       XBT_INFO("End: %llu/%llu MiB used/free on '%s@%s'", sg_disk_get_size_used(d) / INMEGA,
59                sg_disk_get_size_free(d) / INMEGA, d->get_cname(), h->get_cname());
60   }
61
62   XBT_INFO("Simulation time %g", sg4::Engine::get_clock());
63   return 0;
64 }