Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
convert getCname to get_cname
[simgrid.git] / examples / s4u / io-file-system / s4u-io-file-system.cpp
1 /* Copyright (c) 2006-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 <string>
7 #include <unordered_map>
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::unordered_map<std::string, simgrid::s4u::Storage*> const& mounts)
17   {
18     XBT_INFO("Storage info on %s:", simgrid::s4u::Host::current()->get_cname());
19
20     for (auto const& kv : mounts) {
21       std::string mountpoint         = kv.first;
22       simgrid::s4u::Storage* storage = kv.second;
23
24       // Retrieve disk's information
25       XBT_INFO("    %s (%s) Used: %llu; Free: %llu; Total: %llu.", storage->get_cname(), mountpoint.c_str(),
26                sg_storage_get_size_used(storage), sg_storage_get_size_free(storage), sg_storage_get_size(storage));
27     }
28   }
29
30   void operator()()
31   {
32     std::unordered_map<std::string, simgrid::s4u::Storage*> const& mounts =
33         simgrid::s4u::Host::current()->getMountedStorages();
34
35     show_info(mounts);
36
37     // Open an non-existing file to create it
38     std::string filename     = "/home/tmp/data.txt";
39     simgrid::s4u::File* file = new simgrid::s4u::File(filename, nullptr);
40
41     sg_size_t write = file->write(200000); // Write 200,000 bytes
42     XBT_INFO("Create a %llu bytes file named '%s' on /sd1", write, filename.c_str());
43
44     // check that sizes have changed
45     show_info(mounts);
46
47     // Now retrieve the size of created file and read it completely
48     const sg_size_t file_size = file->size();
49     file->seek(0);
50     const sg_size_t read = file->read(file_size);
51     XBT_INFO("Read %llu bytes on %s", read, filename.c_str());
52
53     // Now write 100,000 bytes in tmp/data.txt
54     write = file->write(100000); // Write 100,000 bytes
55     XBT_INFO("Write %llu bytes on %s", write, filename.c_str());
56
57     simgrid::s4u::Storage* storage = simgrid::s4u::Storage::byName("Disk4");
58
59     // Now rename file from ./tmp/data.txt to ./tmp/simgrid.readme
60     std::string newpath = "/home/tmp/simgrid.readme";
61     XBT_INFO("Move '%s' to '%s'", file->getPath(), newpath.c_str());
62     file->move(newpath);
63
64     // Test attaching some user data to the file
65     file->setUserdata(new std::string("777"));
66     std::string* file_data = static_cast<std::string*>(file->getUserdata());
67     XBT_INFO("User data attached to the file: %s", file_data->c_str());
68     delete file_data;
69
70     // Close the file
71     delete file;
72
73     // Now attach some user data to disk1
74     XBT_INFO("Get/set data for storage element: %s", storage->get_cname());
75     XBT_INFO("    Uninitialized storage data: '%s'", static_cast<char*>(storage->getUserdata()));
76
77     storage->setUserdata(new std::string("Some user data"));
78     std::string* storage_data = static_cast<std::string*>(storage->getUserdata());
79     XBT_INFO("    Set and get data: '%s'", storage_data->c_str());
80
81     delete storage_data;
82
83     // Reopen the file and then unlink it
84     file = new simgrid::s4u::File("/home/tmp/simgrid.readme", nullptr);
85     XBT_INFO("Unlink file: '%s'", file->getPath());
86     file->unlink();
87     delete file; // Unlinking the file on "disk" does not free the object
88
89     show_info(mounts);
90   }
91 };
92
93 int main(int argc, char** argv)
94 {
95   simgrid::s4u::Engine e(&argc, argv);
96   sg_storage_file_system_init();
97   e.loadPlatform(argv[1]);
98   simgrid::s4u::Actor::createActor("host", simgrid::s4u::Host::by_name("denise"), MyHost());
99   e.run();
100
101   return 0;
102 }