Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
filesystem, correctly update file size when seeking past it
[simgrid.git] / include / simgrid / plugins / file_system.h
1 /* Copyright (c) 2017-2022. 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/config.h>
10 #include <simgrid/forward.h>
11 #include <xbt/base.h>
12 #include <xbt/dict.h>
13
14 #ifdef __cplusplus
15 #include <xbt/Extendable.hpp>
16
17 #include <map>
18 #include <memory>
19 #include <string>
20 #endif /* C++ */
21
22 // C interface
23 ////////////////
24
25 SG_BEGIN_DECL
26 XBT_PUBLIC void sg_storage_file_system_init();
27 XBT_PUBLIC sg_file_t sg_file_open(const char* fullpath, void* data);
28 XBT_PUBLIC sg_size_t sg_file_read(sg_file_t fd, sg_size_t size);
29 XBT_PUBLIC sg_size_t sg_file_write(sg_file_t fd, sg_size_t size);
30 XBT_PUBLIC void sg_file_close(sg_file_t fd);
31
32 XBT_PUBLIC const char* sg_file_get_name(const_sg_file_t fd);
33 XBT_PUBLIC sg_size_t sg_file_get_size(const_sg_file_t fd);
34 XBT_PUBLIC void sg_file_dump(const_sg_file_t fd);
35 XBT_PUBLIC void* sg_file_get_data(const_sg_file_t fd);
36 XBT_PUBLIC void sg_file_set_data(sg_file_t fd, void* data);
37 XBT_PUBLIC void sg_file_seek(sg_file_t fd, sg_offset_t offset, int origin);
38 XBT_PUBLIC sg_size_t sg_file_tell(const_sg_file_t fd);
39 XBT_PUBLIC void sg_file_move(const_sg_file_t fd, const char* fullpath);
40 XBT_PUBLIC void sg_file_unlink(sg_file_t fd);
41 XBT_PUBLIC int sg_file_rcopy(sg_file_t file, sg_host_t host, const char* fullpath);
42 XBT_PUBLIC int sg_file_rmove(sg_file_t file, sg_host_t host, const char* fullpath);
43
44 XBT_PUBLIC sg_size_t sg_disk_get_size_free(const_sg_disk_t d);
45 XBT_PUBLIC sg_size_t sg_disk_get_size_used(const_sg_disk_t d);
46 XBT_PUBLIC sg_size_t sg_disk_get_size(const_sg_disk_t d);
47 XBT_PUBLIC const char* sg_disk_get_mount_point(const_sg_disk_t d);
48
49 #if SIMGRID_HAVE_MSG
50
51 typedef sg_file_t msg_file_t; // MSG backwards compatibility
52
53 #define MSG_file_open(fullpath, data) sg_file_open((fullpath), (data))
54 #define MSG_file_read(fd, size) sg_file_read((fd), (size))
55 #define MSG_file_write(fd, size) sg_file_write((fd), (size))
56 #define MSG_file_close(fd) sg_file_close(fd)
57 #define MSG_file_get_name(fd) sg_file_get_name(fd)
58 #define MSG_file_get_size(fd) sg_file_get_size(fd)
59 #define MSG_file_dump(fd) sg_file_dump(fd)
60 #define MSG_file_get_data(fd) sg_file_get_data(fd)
61 #define MSG_file_set_data(fd, data) sg_file_set_data((fd), (data))
62 #define MSG_file_seek(fd, offset, origin) sg_file_seek((fd), (offset), (origin))
63 #define MSG_file_tell(fd) sg_file_tell(fd)
64 #define MSG_file_move(fd, fullpath) sg_file_get_size((fd), (fullpath))
65 #define MSG_file_unlink(fd) sg_file_unlink(fd)
66 #define MSG_file_rcopy(file, host, fullpath) sg_file_rcopy((file), (host), (fullpath))
67 #define MSG_file_rmove(file, host, fullpath) sg_file_rmove((file), (host), (fullpath))
68
69 #define MSG_storage_file_system_init() sg_storage_file_system_init()
70
71 #endif // SIMGRID_HAVE_MSG
72
73 SG_END_DECL
74
75 // C++ interface
76 //////////////////
77
78 #ifdef __cplusplus
79
80 namespace simgrid {
81
82 extern template class XBT_PUBLIC xbt::Extendable<s4u::File>;
83
84 namespace s4u {
85
86 /** @brief A simulated file
87  *  @ingroup plugin_filesystem
88  *
89  * Used to simulate the time it takes to access to a file, but does not really store any information.
90  *
91  * They are located on @ref simgrid::s4u::Disk that are accessed from a given @ref simgrid::s4u::Host
92  */
93 class XBT_PUBLIC File : public xbt::Extendable<File> {
94   sg_size_t size_ = 0;
95   std::string path_;
96   std::string fullpath_;
97   sg_size_t current_position_ = SEEK_SET;
98   int desc_id                 = 0;
99   const Disk* local_disk_     = nullptr;
100   std::string mount_point_;
101
102   const Disk* find_local_disk_on(const Host* host);
103
104 protected:
105   File(const std::string& fullpath, void* userdata);
106   File(const std::string& fullpath, const_sg_host_t host, void* userdata);
107   File(const File&) = delete;
108   File& operator=(const File&) = delete;
109   ~File();
110
111 public:
112   static File* open(const std::string& fullpath, void* userdata);
113   static File* open(const std::string& fullpath, const_sg_host_t host, void* userdata);
114   void close();
115
116   /** Retrieves the path to the file */
117   const char* get_path() const { return fullpath_.c_str(); }
118
119   /** Simulates a local read action. Returns the size of data actually read */
120   sg_size_t read(sg_size_t size);
121
122   /** Simulates a write action. Returns the size of data actually written. */
123   sg_size_t write(sg_size_t size, bool write_inside = false);
124
125   sg_size_t size() const;
126   void seek(sg_offset_t pos);             /** Sets the file head to the given position. */
127   void seek(sg_offset_t pos, int origin); /** Sets the file head to the given position from a given origin. */
128   sg_size_t tell() const;                 /** Retrieves the current file position */
129   void update_position(sg_offset_t);      /** set new position in file, grow it if necessary, and increased usage */
130
131   /** Rename a file. WARNING: It is forbidden to move the file to another mount point */
132   void move(const std::string& fullpath) const;
133   int remote_copy(sg_host_t host, const std::string& fullpath);
134   int remote_move(sg_host_t host, const std::string& fullpath);
135
136   int unlink() const; /** Remove a file from the contents of a disk */
137   void dump() const;
138 };
139
140 class XBT_PUBLIC FileSystemDiskExt {
141   std::unique_ptr<std::map<std::string, sg_size_t, std::less<>>> content_;
142   std::map<Host*, std::string> remote_mount_points_;
143   std::string mount_point_;
144   sg_size_t used_size_ = 0;
145   sg_size_t size_      = static_cast<sg_size_t>(500 * 1024) * 1024 * 1024;
146
147 public:
148   static simgrid::xbt::Extension<Disk, FileSystemDiskExt> EXTENSION_ID;
149   explicit FileSystemDiskExt(const Disk* ptr);
150   FileSystemDiskExt(const FileSystemDiskExt&) = delete;
151   FileSystemDiskExt& operator=(const FileSystemDiskExt&) = delete;
152   std::map<std::string, sg_size_t, std::less<>>* parse_content(const std::string& filename);
153   std::map<std::string, sg_size_t, std::less<>>* get_content() const { return content_.get(); }
154   const char* get_mount_point() const { return mount_point_.c_str(); }
155   const char* get_mount_point(s4u::Host* remote_host) { return remote_mount_points_[remote_host].c_str(); }
156   void add_remote_mount(Host* host, const std::string& mount_point);
157   sg_size_t get_size() const { return size_; }
158   sg_size_t get_used_size() const { return used_size_; }
159   void decr_used_size(sg_size_t size);
160   void incr_used_size(sg_size_t size);
161 };
162
163 class XBT_PUBLIC FileDescriptorHostExt {
164 public:
165   static simgrid::xbt::Extension<Host, FileDescriptorHostExt> EXTENSION_ID;
166   FileDescriptorHostExt() = default;
167   FileDescriptorHostExt(const FileDescriptorHostExt&) = delete;
168   FileDescriptorHostExt& operator=(const FileDescriptorHostExt&) = delete;
169   std::unique_ptr<std::vector<int>> file_descriptor_table        = nullptr; // Created lazily on need
170 };
171 } // namespace s4u
172 } // namespace simgrid
173 #endif
174 #endif