Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
tend to the google coding standards in all S4U API
[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 <unordered_map>
7
8 #include "simgrid/s4u.hpp"
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
11
12 class MyHost {
13 public:
14   void show_info(std::unordered_map<std::string, simgrid::s4u::Storage*> const& mounts)
15   {
16     XBT_INFO("Storage info on %s:", simgrid::s4u::Host::current()->getCname());
17
18     for (const auto&kv : mounts) {
19       const char* mountpoint = kv.first.c_str();
20       simgrid::s4u::Storage* storage = kv.second;
21
22       // Retrieve disk's information
23       sg_size_t free_size = storage->getSizeFree();
24       sg_size_t used_size = storage->getSizeUsed();
25       sg_size_t size      = storage->getSize();
26
27       XBT_INFO("    %s (%s) Used: %llu; Free: %llu; Total: %llu.", storage->getName(), mountpoint, used_size, free_size,
28                size);
29     }
30   }
31
32   void operator()() {
33     std::unordered_map<std::string, simgrid::s4u::Storage*> const& mounts =
34         simgrid::s4u::Host::current()->getMountedStorages();
35
36     show_info(mounts);
37
38     // Open an non-existing file to create it
39     const char* filename = "/home/tmp/data.txt";
40     simgrid::s4u::File* file = new simgrid::s4u::File(filename, nullptr);
41
42     sg_size_t write = file->write(200000);  // Write 200,000 bytes
43     XBT_INFO("Create a %llu bytes file named '%s' on /sd1", write, filename);
44
45     // check that sizes have changed
46     show_info(mounts);
47
48     // Now retrieve the size of created file and read it completely
49     const sg_size_t file_size = file->size();
50     file->seek(0);
51     const sg_size_t read = file->read(file_size);
52     XBT_INFO("Read %llu bytes on %s", read, filename);
53
54     // Now write 100,000 bytes in tmp/data.txt
55     write = file->write(100000);  // Write 100,000 bytes
56     XBT_INFO("Write %llu bytes on %s", write, filename);
57
58     simgrid::s4u::Storage* storage = simgrid::s4u::Storage::byName("Disk4");
59
60     // Now rename file from ./tmp/data.txt to ./tmp/simgrid.readme
61     const char *newpath = "/home/tmp/simgrid.readme";
62     XBT_INFO("Move '%s' to '%s'", file->getPath(), newpath);
63     file->move(newpath);
64
65     // Test attaching some user data to the file
66     file->setUserdata(xbt_strdup("777"));
67     XBT_INFO("User data attached to the file: %s", (char*)file->getUserdata());
68     xbt_free(file->getUserdata());
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->getName());
75     XBT_INFO("    Uninitialized storage data: '%s'", (char*)storage->getUserdata());
76
77     storage->setUserdata(xbt_strdup("Some user data"));
78     XBT_INFO("    Set and get data: '%s'", (char*)storage->getUserdata());
79
80     xbt_free(storage->getUserdata());
81   }
82 };
83
84 int main(int argc, char **argv)
85 {
86   simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
87   e->loadPlatform("../../platforms/storage/storage.xml");
88   simgrid::s4u::Actor::createActor("host", simgrid::s4u::Host::by_name("denise"), MyHost());
89   e->run();
90   delete e;
91   return 0;
92 }