Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Don't define (nor use) any MSG symbol when MSG is not built in.
[simgrid.git] / include / simgrid / plugins / file_system.h
1 /* Copyright (c) 2017-2020. 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 #ifndef SIMGRID_PLUGINS_FILE_SYSTEM_H_
7 #define SIMGRID_PLUGINS_FILE_SYSTEM_H_
8
9 #include <simgrid/forward.h>
10 #include <xbt/base.h>
11 #include <xbt/dict.h>
12
13 #ifdef __cplusplus
14 #include <xbt/Extendable.hpp>
15
16 #include <map>
17 #include <memory>
18 #include <string>
19 #endif /* C++ */
20
21 // C interface
22 ////////////////
23
24 SG_BEGIN_DECL
25 XBT_PUBLIC void sg_storage_file_system_init();
26 XBT_PUBLIC sg_file_t sg_file_open(const char* fullpath, void* data);
27 XBT_PUBLIC sg_size_t sg_file_read(sg_file_t fd, sg_size_t size);
28 XBT_PUBLIC sg_size_t sg_file_write(sg_file_t fd, sg_size_t size);
29 XBT_PUBLIC void sg_file_close(const_sg_file_t fd);
30
31 XBT_PUBLIC const char* sg_file_get_name(const_sg_file_t fd);
32 XBT_PUBLIC sg_size_t sg_file_get_size(const_sg_file_t fd);
33 XBT_PUBLIC void sg_file_dump(const_sg_file_t fd);
34 XBT_PUBLIC void* sg_file_get_data(const_sg_file_t fd);
35 XBT_PUBLIC void sg_file_set_data(sg_file_t fd, void* data);
36 XBT_PUBLIC void sg_file_seek(sg_file_t fd, sg_offset_t offset, int origin);
37 XBT_PUBLIC sg_size_t sg_file_tell(const_sg_file_t fd);
38 XBT_PUBLIC void sg_file_move(const_sg_file_t fd, const char* fullpath);
39 XBT_PUBLIC void sg_file_unlink(sg_file_t fd);
40 XBT_PUBLIC int sg_file_rcopy(sg_file_t file, sg_host_t host, const char* fullpath);
41 XBT_PUBLIC int sg_file_rmove(sg_file_t file, sg_host_t host, const char* fullpath);
42
43 XBT_PUBLIC sg_size_t sg_disk_get_size_free(const_sg_disk_t d);
44 XBT_PUBLIC sg_size_t sg_disk_get_size_used(const_sg_disk_t d);
45 XBT_PUBLIC sg_size_t sg_disk_get_size(const_sg_disk_t d);
46 XBT_PUBLIC const char* sg_disk_get_mount_point(const_sg_disk_t d);
47
48 XBT_PUBLIC sg_size_t sg_storage_get_size_free(const_sg_storage_t st);
49 XBT_PUBLIC sg_size_t sg_storage_get_size_used(const_sg_storage_t st);
50 XBT_PUBLIC sg_size_t sg_storage_get_size(const_sg_storage_t st);
51 XBT_PUBLIC xbt_dict_t sg_storage_get_content(const_sg_storage_t storage);
52
53 XBT_PUBLIC xbt_dict_t sg_host_get_storage_content(sg_host_t host);
54
55 #if SIMGRID_HAVE_MSG
56
57 typedef sg_file_t msg_file_t; // MSG backwards compatibility
58
59 #define MSG_file_open(fullpath, data) sg_file_open((fullpath), (data))
60 #define MSG_file_read(fd, size) sg_file_read((fd), (size))
61 #define MSG_file_write(fd, size) sg_file_write((fd), (size))
62 #define MSG_file_close(fd) sg_file_close(fd)
63 #define MSG_file_get_name(fd) sg_file_get_name(fd)
64 #define MSG_file_get_size(fd) sg_file_get_size(fd)
65 #define MSG_file_dump(fd) sg_file_dump(fd)
66 #define MSG_file_get_data(fd) sg_file_get_data(fd)
67 #define MSG_file_set_data(fd, data) sg_file_set_data((fd), (data))
68 #define MSG_file_seek(fd, offset, origin) sg_file_seek((fd), (offset), (origin))
69 #define MSG_file_tell(fd) sg_file_tell(fd)
70 #define MSG_file_move(fd, fullpath) sg_file_get_size((fd), (fullpath))
71 #define MSG_file_unlink(fd) sg_file_unlink(fd)
72 #define MSG_file_rcopy(file, host, fullpath) sg_file_rcopy((file), (host), (fullpath))
73 #define MSG_file_rmove(file, host, fullpath) sg_file_rmove((file), (host), (fullpath))
74
75 #define MSG_storage_file_system_init() sg_storage_file_system_init()
76 #define MSG_storage_get_free_size(st) sg_storage_get_size_free(st)
77 #define MSG_storage_get_used_size(st) sg_storage_get_size_used(st)
78 #define MSG_storage_get_size(st) sg_storage_get_size(st)
79 #define MSG_storage_get_content(st) sg_storage_get_content(st)
80
81 #define MSG_host_get_storage_content(st) sg_host_get_storage_content(st)
82
83 #endif // SIMGRID_HAVE_MSG
84
85 SG_END_DECL
86
87 // C++ interface
88 //////////////////
89
90 #ifdef __cplusplus
91
92 namespace simgrid {
93
94 extern template class XBT_PUBLIC xbt::Extendable<s4u::File>;
95
96 namespace s4u {
97
98 /** @brief A simulated file
99  *  @addtogroup Plugin_filesystem
100  *
101  * Used to simulate the time it takes to access to a file, but does not really store any information.
102  *
103  * They are located on @ref simgrid::s4u::Storage that are accessed from a given @ref simgrid::s4u::Host through
104  * mountpoints.
105  * For now, you cannot change the mountpoints programmatically, and must declare them from your platform file.
106  */
107 class XBT_PUBLIC File : public xbt::Extendable<File> {
108   sg_size_t size_ = 0;
109   std::string path_;
110   std::string fullpath_;
111   sg_size_t current_position_ = SEEK_SET;
112   int desc_id                 = 0;
113   Disk* local_disk_           = nullptr;
114   Storage* local_storage_     = nullptr;
115   std::string mount_point_;
116
117   Storage* find_local_storage_on(Host* host);
118   Disk* find_local_disk_on(const Host* host);
119   sg_size_t write_on_storage(sg_size_t size, bool write_inside);
120   sg_size_t write_on_disk(sg_size_t size, bool write_inside);
121
122 public:
123   File(const std::string& fullpath, void* userdata);
124   File(const std::string& fullpath, sg_host_t host, void* userdata);
125   File(const File&) = delete;
126   File& operator=(const File&) = delete;
127   ~File();
128
129   /** Retrieves the path to the file */
130   const char* get_path() const { return fullpath_.c_str(); }
131
132   /** Simulates a local read action. Returns the size of data actually read */
133   sg_size_t read(sg_size_t size);
134
135   /** Simulates a write action. Returns the size of data actually written. */
136   sg_size_t write(sg_size_t size, bool write_inside = false);
137
138   /** Allows to store user data on that host */
139   XBT_ATTRIB_DEPRECATED_v329("Please use set_data()") void set_userdata(void* data) { set_data(data); }
140   /** Retrieves the previously stored data */
141   XBT_ATTRIB_DEPRECATED_v329("Please use get_data()") void* get_userdata() { return get_data(); }
142
143   sg_size_t size() const;
144   void seek(sg_offset_t pos);             /** Sets the file head to the given position. */
145   void seek(sg_offset_t pos, int origin); /** Sets the file head to the given position from a given origin. */
146   sg_size_t tell() const;                 /** Retrieves the current file position */
147
148   /** Rename a file. WARNING: It is forbidden to move the file to another mount point */
149   void move(const std::string& fullpath) const;
150   int remote_copy(sg_host_t host, const std::string& fullpath);
151   int remote_move(sg_host_t host, const std::string& fullpath);
152
153   int unlink() const; /** Remove a file from the contents of a disk */
154   void dump() const;
155 };
156
157 class XBT_PUBLIC FileSystemDiskExt {
158   std::unique_ptr<std::map<std::string, sg_size_t>> content_;
159   std::map<Host*, std::string> remote_mount_points_;
160   std::string mount_point_;
161   sg_size_t used_size_ = 0;
162   sg_size_t size_      = static_cast<sg_size_t>(500 * 1024) * 1024 * 1024;
163
164 public:
165   static simgrid::xbt::Extension<Disk, FileSystemDiskExt> EXTENSION_ID;
166   explicit FileSystemDiskExt(const Disk* ptr);
167   FileSystemDiskExt(const FileSystemDiskExt&) = delete;
168   FileSystemDiskExt& operator=(const FileSystemDiskExt&) = delete;
169   std::map<std::string, sg_size_t>* parse_content(const std::string& filename);
170   std::map<std::string, sg_size_t>* get_content() const { return content_.get(); }
171   const char* get_mount_point() const { return mount_point_.c_str(); }
172   const char* get_mount_point(s4u::Host* remote_host) { return remote_mount_points_[remote_host].c_str(); }
173   void add_remote_mount(Host* host, const std::string& mount_point)
174   {
175     remote_mount_points_.insert({host, mount_point});
176   }
177   sg_size_t get_size() const { return size_; }
178   sg_size_t get_used_size() const { return used_size_; }
179   void decr_used_size(sg_size_t size);
180   void incr_used_size(sg_size_t size);
181 };
182
183 class XBT_PUBLIC FileSystemStorageExt {
184   std::unique_ptr<std::map<std::string, sg_size_t>> content_;
185   sg_size_t used_size_ = 0;
186   sg_size_t size_      = 0;
187
188 public:
189   static simgrid::xbt::Extension<Storage, FileSystemStorageExt> EXTENSION_ID;
190   explicit FileSystemStorageExt(const Storage* ptr);
191   FileSystemStorageExt(const FileSystemStorageExt&) = delete;
192   FileSystemStorageExt& operator=(const FileSystemStorageExt&) = delete;
193   std::map<std::string, sg_size_t>* parse_content(const std::string& filename);
194   std::map<std::string, sg_size_t>* get_content() { return content_.get(); }
195   sg_size_t get_size() const { return size_; }
196   sg_size_t get_used_size() const { return used_size_; }
197   void decr_used_size(sg_size_t size);
198   void incr_used_size(sg_size_t size);
199 };
200
201 class XBT_PUBLIC FileDescriptorHostExt {
202 public:
203   static simgrid::xbt::Extension<Host, FileDescriptorHostExt> EXTENSION_ID;
204   FileDescriptorHostExt() = default;
205   FileDescriptorHostExt(const FileDescriptorHostExt&) = delete;
206   FileDescriptorHostExt& operator=(const FileDescriptorHostExt&) = delete;
207   std::unique_ptr<std::vector<int>> file_descriptor_table        = nullptr; // Created lazily on need
208 };
209 } // namespace s4u
210 } // namespace simgrid
211 #endif
212 #endif