Logo AND Algorithmique Numérique Distribuée

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