Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
start snake_casing s4u::Engine
[simgrid.git] / examples / s4u / io-file-remote / s4u-io-file-remote.cpp
1 /* Copyright (c) 2014-2018. 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
14 static int host(int argc, char* argv[])
15 {
16   simgrid::s4u::File file(argv[1], nullptr);
17   const char* filename = file.getPath();
18   XBT_INFO("Opened file '%s'", filename);
19   file.dump();
20
21   XBT_INFO("Try to read %llu from '%s'", file.size(), filename);
22   sg_size_t read = file.read(file.size());
23   XBT_INFO("Have read %llu from '%s'. Offset is now at: %llu", read, filename, file.tell());
24   XBT_INFO("Seek back to the beginning of the stream...");
25   file.seek(0, SEEK_SET);
26   XBT_INFO("Offset is now at: %llu", file.tell());
27
28   if (argc > 5) {
29     simgrid::s4u::File remoteFile(argv[2], nullptr);
30     filename = remoteFile.getPath();
31     XBT_INFO("Opened file '%s'", filename);
32     XBT_INFO("Try to write %llu MiB to '%s'", remoteFile.size() / 1024, filename);
33     sg_size_t write = remoteFile.write(remoteFile.size() * 1024);
34     XBT_INFO("Have written %llu bytes to '%s'.", write, filename);
35
36     if (std::stoi(argv[5]) != 0) {
37       XBT_INFO("Move '%s' (of size %llu) from '%s' to '%s'", filename, remoteFile.size(),
38                simgrid::s4u::Host::current()->get_cname(), argv[3]);
39       remoteFile.remoteMove(simgrid::s4u::Host::by_name(argv[3]), argv[4]);
40     } else {
41       XBT_INFO("Copy '%s' (of size %llu) from '%s' to '%s'", filename, remoteFile.size(),
42                simgrid::s4u::Host::current()->get_cname(), argv[3]);
43       remoteFile.remoteCopy(simgrid::s4u::Host::by_name(argv[3]), argv[4]);
44     }
45   }
46
47   return 0;
48 }
49
50 int main(int argc, char** argv)
51 {
52   simgrid::s4u::Engine e(&argc, argv);
53   sg_storage_file_system_init();
54   e.load_platform(argv[1]);
55   e.register_function("host", host);
56   e.load_deployment(argv[2]);
57   std::vector<simgrid::s4u::Storage*> allStorages = e.getAllStorages();
58
59   for (auto const& s : allStorages) {
60     XBT_INFO("Init: %llu/%llu MiB used/free on '%s'", sg_storage_get_size_used(s) / INMEGA,
61              sg_storage_get_size_free(s) / INMEGA, s->get_cname());
62   }
63
64   e.run();
65
66   for (auto const& s : allStorages) {
67     XBT_INFO("End: %llu/%llu MiB used/free on '%s'", sg_storage_get_size_used(s) / INMEGA,
68              sg_storage_get_size_free(s) / INMEGA, s->get_cname());
69   }
70
71   XBT_INFO("Simulation time %g", simgrid::s4u::Engine::getClock());
72   return 0;
73 }