Logo AND Algorithmique Numérique Distribuée

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