Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into disk
[simgrid.git] / examples / s4u / io-file-system / s4u-io-file-system.cpp
1 /* Copyright (c) 2006-2019. 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 <string>
7 #include <vector>
8
9 #include "simgrid/plugins/file_system.h"
10 #include "simgrid/s4u.hpp"
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
13
14 class MyHost {
15 public:
16   void show_info(std::vector<simgrid::s4u::Disk*> const& disks)
17   {
18     XBT_INFO("Storage info on %s:", simgrid::s4u::Host::current()->get_cname());
19
20     for (auto const& d : disks) {
21       const char* mountpoint = d->get_property("mount");
22
23       // Retrieve disk's information
24       XBT_INFO("    %s (%s) Used: %llu; Free: %llu; Total: %llu.", d->get_cname(), mountpoint, sg_disk_get_size_used(d),
25                sg_disk_get_size_free(d), sg_disk_get_size(d));
26     }
27   }
28
29   void operator()()
30   {
31     std::vector<simgrid::s4u::Disk*> const& disks = simgrid::s4u::Host::current()->get_disks();
32
33     show_info(disks);
34
35     // Open an non-existing file to create it
36     std::string filename     = "/scratch/tmp/data.txt";
37     simgrid::s4u::File* file = new simgrid::s4u::File(filename, nullptr);
38
39     sg_size_t write = file->write(200000); // Write 200,000 bytes
40     XBT_INFO("Create a %llu bytes file named '%s' on /scratch", write, filename.c_str());
41
42     // check that sizes have changed
43     show_info(disks);
44
45     // Now retrieve the size of created file and read it completely
46     const sg_size_t file_size = file->size();
47     file->seek(0);
48     const sg_size_t read = file->read(file_size);
49     XBT_INFO("Read %llu bytes on %s", read, filename.c_str());
50
51     // Now write 100,000 bytes in tmp/data.txt
52     write = file->write(100000); // Write 100,000 bytes
53     XBT_INFO("Write %llu bytes on %s", write, filename.c_str());
54
55     // Now rename file from ./tmp/data.txt to ./tmp/simgrid.readme
56     std::string newpath = "/scratch/tmp/simgrid.readme";
57     XBT_INFO("Move '%s' to '%s'", file->get_path(), newpath.c_str());
58     file->move(newpath);
59
60     // Test attaching some user data to the file
61     file->set_userdata(new std::string("777"));
62     std::string* file_data = static_cast<std::string*>(file->get_userdata());
63     XBT_INFO("User data attached to the file: %s", file_data->c_str());
64     delete file_data;
65
66     // Close the file
67     delete file;
68
69     show_info(disks);
70
71     // Reopen the file and then unlink it
72     file = new simgrid::s4u::File("/scratch/tmp/simgrid.readme", nullptr);
73     XBT_INFO("Unlink file: '%s'", file->get_path());
74     file->unlink();
75     delete file; // Unlinking the file on "disk" does not free the object
76
77     show_info(disks);
78   }
79 };
80
81 int main(int argc, char** argv)
82 {
83   simgrid::s4u::Engine e(&argc, argv);
84   sg_storage_file_system_init();
85   e.load_platform(argv[1]);
86   simgrid::s4u::Actor::create("host", simgrid::s4u::Host::by_name("bob"), MyHost());
87   e.run();
88
89   return 0;
90 }