1 /* Copyright (c) 2017-2019. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_PLUGINS_FILE_SYSTEM_H_
7 #define SIMGRID_PLUGINS_FILE_SYSTEM_H_
9 #include <simgrid/forward.h>
14 #include <xbt/Extendable.hpp>
23 typedef sg_file_t msg_file_t; // MSG backwards compatibility
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);
32 XBT_PUBLIC const char* sg_file_get_name(sg_file_t fd);
33 XBT_PUBLIC sg_size_t sg_file_get_size(sg_file_t fd);
34 XBT_PUBLIC void sg_file_dump(sg_file_t fd);
35 XBT_PUBLIC void* sg_file_get_data(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(sg_file_t fd);
39 XBT_PUBLIC void sg_file_move(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);
44 XBT_PUBLIC sg_size_t sg_disk_get_size_free(sg_disk_t d);
45 XBT_PUBLIC sg_size_t sg_disk_get_size_used(sg_disk_t d);
46 XBT_PUBLIC sg_size_t sg_disk_get_size(sg_disk_t d);
48 XBT_PUBLIC sg_size_t sg_storage_get_size_free(sg_storage_t st);
49 XBT_PUBLIC sg_size_t sg_storage_get_size_used(sg_storage_t st);
50 XBT_PUBLIC sg_size_t sg_storage_get_size(sg_storage_t st);
51 XBT_PUBLIC xbt_dict_t sg_storage_get_content(sg_storage_t storage);
53 XBT_PUBLIC xbt_dict_t sg_host_get_storage_content(sg_host_t host);
55 #define MSG_file_open(fullpath, data) sg_file_open(fullpath, data)
56 #define MSG_file_read(fd, size) sg_file_read(fd, size)
57 #define MSG_file_write(fd, size) sg_file_write(fd, size)
58 #define MSG_file_close(fd) sg_file_close(fd)
59 #define MSG_file_get_name(fd) sg_file_get_name(fd)
60 #define MSG_file_get_size(fd) sg_file_get_size(fd)
61 #define MSG_file_dump(fd) sg_file_dump(fd)
62 #define MSG_file_get_data(fd) sg_file_get_data(fd)
63 #define MSG_file_set_data(fd, data) sg_file_set_data(fd, data)
64 #define MSG_file_seek(fd, offset, origin) sg_file_seek(fd, offset, origin)
65 #define MSG_file_tell(fd) sg_file_tell(fd)
66 #define MSG_file_move(fd, fullpath) sg_file_get_size(fd, fullpath)
67 #define MSG_file_unlink(fd) sg_file_unlink(fd)
68 #define MSG_file_rcopy(file, host, fullpath) sg_file_rcopy(file, host, fullpath)
69 #define MSG_file_rmove(file, host, fullpath) sg_file_rmove(file, host, fullpath)
71 #define MSG_storage_file_system_init() sg_storage_file_system_init()
72 #define MSG_storage_get_free_size(st) sg_storage_get_size_free(st)
73 #define MSG_storage_get_used_size(st) sg_storage_get_size_used(st)
74 #define MSG_storage_get_size(st) sg_storage_get_size(st)
75 #define MSG_storage_get_content(st) sg_storage_get_content(st)
77 #define MSG_host_get_storage_content(st) sg_host_get_storage_content(st)
89 /** @brief A simulated file
91 * Used to simulate the time it takes to access to a file, but does not really store any information.
93 * They are located on @ref simgrid::s4u::Storage that are accessed from a given @ref simgrid::s4u::Host through
95 * For now, you cannot change the mountpoints programatically, and must declare them from your platform file.
97 class XBT_PUBLIC File {
99 File(const std::string& fullpath, void* userdata);
100 File(const std::string& fullpath, sg_host_t host, void* userdata);
101 File(const File&) = delete;
102 File& operator=(const File&) = delete;
105 /** Retrieves the path to the file */
106 const char* get_path() { return fullpath_.c_str(); }
108 /** Simulates a local read action. Returns the size of data actually read */
109 sg_size_t read(sg_size_t size);
111 /** Simulates a write action. Returns the size of data actually written. */
112 sg_size_t write(sg_size_t size, int write_inside=0);
114 /** Allows to store user data on that host */
115 void set_userdata(void* data) { userdata_ = data; }
116 /** Retrieves the previously stored data */
117 void* get_userdata() { return userdata_; }
120 void seek(sg_offset_t pos); /** Sets the file head to the given position. */
121 void seek(sg_offset_t pos, int origin); /** Sets the file head to the given position from a given origin. */
122 sg_size_t tell(); /** Retrieves the current file position */
124 /** Rename a file. WARNING: It is forbidden to move the file to another mount point */
125 void move(const std::string& fullpath);
126 int remote_copy(sg_host_t host, const char* fullpath);
127 int remote_move(sg_host_t host, const char* fullpath);
129 int unlink(); /** Remove a file from the contents of a disk */
133 Disk* local_disk_ = nullptr;
134 Storage* local_storage_ = nullptr;
135 std::string mount_point_;
140 std::string fullpath_;
141 sg_size_t current_position_ = SEEK_SET;
142 void* userdata_ = nullptr;
145 class XBT_PUBLIC FileSystemDiskExt {
147 static simgrid::xbt::Extension<Disk, FileSystemDiskExt> EXTENSION_ID;
148 explicit FileSystemDiskExt(Disk* ptr);
149 FileSystemDiskExt(const FileSystemDiskExt&) = delete;
150 FileSystemDiskExt& operator=(const FileSystemDiskExt&) = delete;
151 std::map<std::string, sg_size_t>* parse_content(const std::string& filename);
152 std::map<std::string, sg_size_t>* get_content() { return content_.get(); }
153 sg_size_t get_size() { return size_; }
154 sg_size_t get_used_size() { return used_size_; }
155 void decr_used_size(sg_size_t size) { used_size_ -= size; }
156 void incr_used_size(sg_size_t size) { used_size_ += size; }
159 std::unique_ptr<std::map<std::string, sg_size_t>> content_;
160 sg_size_t used_size_ = 0;
164 class XBT_PUBLIC FileSystemStorageExt {
166 static simgrid::xbt::Extension<Storage, FileSystemStorageExt> EXTENSION_ID;
167 explicit FileSystemStorageExt(Storage* ptr);
168 FileSystemStorageExt(const FileSystemStorageExt&) = delete;
169 FileSystemStorageExt& operator=(const FileSystemStorageExt&) = delete;
170 std::map<std::string, sg_size_t>* parse_content(const std::string& filename);
171 std::map<std::string, sg_size_t>* get_content() { return content_.get(); }
172 sg_size_t get_size() { return size_; }
173 sg_size_t get_used_size() { return used_size_; }
174 void decr_used_size(sg_size_t size) { used_size_ -= size; }
175 void incr_used_size(sg_size_t size) { used_size_ += size; }
178 std::unique_ptr<std::map<std::string, sg_size_t>> content_;
179 sg_size_t used_size_ = 0;
183 class XBT_PUBLIC FileDescriptorHostExt {
185 static simgrid::xbt::Extension<Host, FileDescriptorHostExt> EXTENSION_ID;
186 FileDescriptorHostExt() = default;
187 FileDescriptorHostExt(const FileDescriptorHostExt&) = delete;
188 FileDescriptorHostExt& operator=(const FileDescriptorHostExt&) = delete;
189 std::unique_ptr<std::vector<int>> file_descriptor_table = nullptr; // Created lazily on need
192 } // namespace simgrid