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-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 #include <vector>
8
9 #include "simgrid/s4u.h"
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
12
13 class MyHost {
14 public:
15
16   void show_info(boost::unordered_map <std::string, simgrid::s4u::Storage*> const&mounts) {
17     XBT_INFO("Storage info on %s:",
18       simgrid::s4u::Host::current()->name().c_str());
19
20     for (const auto&kv : mounts) {
21       const char* mountpoint = kv.first.c_str();
22       simgrid::s4u::Storage &storage = *kv.second;
23
24       // Retrieve disk's information
25       sg_size_t free_size = storage.sizeFree();
26       sg_size_t used_size = storage.sizeUsed();
27       sg_size_t size = storage.size();
28
29       XBT_INFO("    %s (%s) Used: %llu; Free: %llu; Total: %llu.",
30           storage.name(), mountpoint, used_size, free_size, size);
31     }
32   }
33
34   void operator()() {
35     boost::unordered_map <std::string, simgrid::s4u::Storage *> const& mounts =
36       simgrid::s4u::Host::current()->mountedStorages();
37
38     show_info(mounts);
39
40     // Open an non-existing file to create it
41     const char *filename = "/home/tmp/data.txt";
42     simgrid::s4u::File *file = new simgrid::s4u::File(filename, NULL);
43
44     sg_size_t write = file->write(200000);  // Write 200,000 bytes
45     XBT_INFO("Create a %llu bytes file named '%s' on /sd1", write, filename);
46
47     // check that sizes have changed
48     show_info(mounts);
49
50     // Now retrieve the size of created file and read it completely
51     const sg_size_t file_size = file->size();
52     file->seek(0);
53     const sg_size_t read = file->read(file_size);
54     XBT_INFO("Read %llu bytes on %s", read, filename);
55
56     // Now write 100,000 bytes in tmp/data.txt
57     write = file->write(100000);  // Write 100,000 bytes
58     XBT_INFO("Write %llu bytes on %s", write, filename);
59
60     simgrid::s4u::Storage &storage = simgrid::s4u::Storage::byName("Disk4");
61
62     // Now rename file from ./tmp/data.txt to ./tmp/simgrid.readme
63     const char *newpath = "/home/tmp/simgrid.readme";
64     XBT_INFO("Move '%s' to '%s'", file->path(), newpath);
65     file->move(newpath);
66
67     // Test attaching some user data to the file
68     file->setUserdata(xbt_strdup("777"));
69     XBT_INFO("User data attached to the file: %s", (char*)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       // Dump disks contents
83       XBT_INFO("*** Dump content of %s ***",Host::current()->name());
84       xbt_dict_t contents = NULL;
85       contents = MSG_host_get_storage_content(MSG_host_self()); // contents is a dict of dicts
86       xbt_dict_cursor_t curs, curs2 = NULL;
87       char* mountname;
88       xbt_dict_t content;
89       char* path;
90       sg_size_t *size;
91       xbt_dict_foreach(contents, curs, mountname, content){
92         XBT_INFO("Print the content of mount point: %s",mountname);
93         xbt_dict_foreach(content,curs2,path,size){
94            XBT_INFO("%s size: %llu bytes", path,*((sg_size_t*)size));
95         }
96       xbt_dict_free(&content);
97       }
98       xbt_dict_free(&contents);
99      */
100   }
101 };
102
103 int main(int argc, char **argv)
104 {
105   simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
106   e->loadPlatform("../../platforms/storage/storage.xml");
107   simgrid::s4u::Actor("host", simgrid::s4u::Host::by_name("denise"), MyHost());
108   e->run();
109   return 0;
110 }