Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / examples / s4u / io / s4u-io.cpp
1 /* Copyright (c) 2006-2017. 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/s4u.hpp"
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
12
13 class MyHost {
14 public:
15   void show_info(std::unordered_map<std::string, simgrid::s4u::Storage*> const& mounts)
16   {
17     XBT_INFO("Storage info on %s:", simgrid::s4u::Host::current()->getCname());
18
19     for (auto const& kv : mounts) {
20       std::string mountpoint         = kv.first;
21       simgrid::s4u::Storage* storage = kv.second;
22
23       // Retrieve disk's information
24       XBT_INFO("    %s (%s) Used: %llu; Free: %llu; Total: %llu.", storage->getCname(), mountpoint.c_str(),
25                sg_storage_get_size_used(storage), sg_storage_get_size_free(storage), sg_storage_get_size(storage));
26     }
27   }
28
29   void operator()() {
30     std::unordered_map<std::string, simgrid::s4u::Storage*> const& mounts =
31         simgrid::s4u::Host::current()->getMountedStorages();
32
33     show_info(mounts);
34
35     // Open an non-existing file to create it
36     std::string filename     = "/home/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 /sd1", write, filename.c_str());
41
42     // check that sizes have changed
43     show_info(mounts);
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     simgrid::s4u::Storage* storage = simgrid::s4u::Storage::byName("Disk4");
56
57     // Now rename file from ./tmp/data.txt to ./tmp/simgrid.readme
58     std::string newpath = "/home/tmp/simgrid.readme";
59     XBT_INFO("Move '%s' to '%s'", file->getPath(), newpath.c_str());
60     file->move(newpath);
61
62     // Test attaching some user data to the file
63     file->setUserdata(new std::string("777"));
64     std::string* file_data = static_cast<std::string*>(file->getUserdata());
65     XBT_INFO("User data attached to the file: %s", file_data->c_str());
66     delete file_data;
67
68     // Close the file
69     delete file;
70
71     // Now attach some user data to disk1
72     XBT_INFO("Get/set data for storage element: %s", storage->getCname());
73     XBT_INFO("    Uninitialized storage data: '%s'", static_cast<char*>(storage->getUserdata()));
74
75     storage->setUserdata(new std::string("Some user data"));
76     std::string* storage_data = static_cast<std::string*>(storage->getUserdata());
77     XBT_INFO("    Set and get data: '%s'", storage_data->c_str());
78     delete storage_data;
79   }
80 };
81
82 int main(int argc, char **argv)
83 {
84   simgrid::s4u::Engine e(&argc, argv);
85   sg_storage_file_system_init();
86   e.loadPlatform("../../platforms/storage/storage.xml");
87   simgrid::s4u::Actor::createActor("host", simgrid::s4u::Host::by_name("denise"), MyHost());
88   e.run();
89
90   return 0;
91 }