Logo AND Algorithmique Numérique Distribuée

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