Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
storage : Allow to write inside a file without reducing its final size (for parallel IO)
[simgrid.git] / include / simgrid / plugins / file_system.h
1 /* Copyright (c) 2017-2019. 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 // C interface
14 ////////////////
15 typedef sg_file_t msg_file_t; // MSG backwards compatibility
16
17 SG_BEGIN_DECL()
18 XBT_PUBLIC void sg_storage_file_system_init();
19 XBT_PUBLIC sg_file_t sg_file_open(const char* fullpath, void* data);
20 XBT_PUBLIC sg_size_t sg_file_read(sg_file_t fd, sg_size_t size);
21 XBT_PUBLIC sg_size_t sg_file_write(sg_file_t fd, sg_size_t size);
22 XBT_PUBLIC void sg_file_close(sg_file_t fd);
23
24 XBT_PUBLIC const char* sg_file_get_name(sg_file_t fd);
25 XBT_PUBLIC sg_size_t sg_file_get_size(sg_file_t fd);
26 XBT_PUBLIC void sg_file_dump(sg_file_t fd);
27 XBT_PUBLIC void* sg_file_get_data(sg_file_t fd);
28 XBT_PUBLIC void sg_file_set_data(sg_file_t fd, void* data);
29 XBT_PUBLIC void sg_file_seek(sg_file_t fd, sg_offset_t offset, int origin);
30 XBT_PUBLIC sg_size_t sg_file_tell(sg_file_t fd);
31 XBT_PUBLIC void sg_file_move(sg_file_t fd, const char* fullpath);
32 XBT_PUBLIC void sg_file_unlink(sg_file_t fd);
33 XBT_PUBLIC int sg_file_rcopy(sg_file_t file, sg_host_t host, const char* fullpath);
34 XBT_PUBLIC int sg_file_rmove(sg_file_t file, sg_host_t host, const char* fullpath);
35
36 XBT_PUBLIC sg_size_t sg_storage_get_size_free(sg_storage_t st);
37 XBT_PUBLIC sg_size_t sg_storage_get_size_used(sg_storage_t st);
38 XBT_PUBLIC sg_size_t sg_storage_get_size(sg_storage_t st);
39 XBT_PUBLIC xbt_dict_t sg_storage_get_content(sg_storage_t storage);
40
41 XBT_PUBLIC xbt_dict_t sg_host_get_storage_content(sg_host_t host);
42
43 #define MSG_file_open(fullpath, data) sg_file_open(fullpath, data)
44 #define MSG_file_read(fd, size) sg_file_read(fd, size)
45 #define MSG_file_write(fd, size) sg_file_write(fd, size)
46 #define MSG_file_close(fd) sg_file_close(fd)
47 #define MSG_file_get_name(fd) sg_file_get_name(fd)
48 #define MSG_file_get_size(fd) sg_file_get_size(fd)
49 #define MSG_file_dump(fd) sg_file_dump(fd)
50 #define MSG_file_get_data(fd) sg_file_get_data(fd)
51 #define MSG_file_set_data(fd, data) sg_file_set_data(fd, data)
52 #define MSG_file_seek(fd, offset, origin) sg_file_seek(fd, offset, origin)
53 #define MSG_file_tell(fd) sg_file_tell(fd)
54 #define MSG_file_move(fd, fullpath) sg_file_get_size(fd, fullpath)
55 #define MSG_file_unlink(fd) sg_file_unlink(fd)
56 #define MSG_file_rcopy(file, host, fullpath) sg_file_rcopy(file, host, fullpath)
57 #define MSG_file_rmove(file, host, fullpath) sg_file_rmove(file, host, fullpath)
58
59 #define MSG_storage_file_system_init() sg_storage_file_system_init()
60 #define MSG_storage_get_free_size(st) sg_storage_get_size_free(st)
61 #define MSG_storage_get_used_size(st) sg_storage_get_size_used(st)
62 #define MSG_storage_get_size(st) sg_storage_get_size(st)
63 #define MSG_storage_get_content(st) sg_storage_get_content(st)
64
65 #define MSG_host_get_storage_content(st) sg_host_get_storage_content(st)
66
67 SG_END_DECL()
68
69 // C++ interface
70 //////////////////
71
72 #ifdef __cplusplus
73 #include <xbt/Extendable.hpp>
74
75 #include <map>
76 #include <memory>
77 #include <string>
78
79 namespace simgrid {
80 namespace s4u {
81
82 /** @brief A simulated file
83  *
84  * Used to simulate the time it takes to access to a file, but does not really store any information.
85  *
86  * They are located on @ref simgrid::s4u::Storage that are accessed from a given @ref simgrid::s4u::Host through
87  * mountpoints.
88  * For now, you cannot change the mountpoints programatically, and must declare them from your platform file.
89  */
90 class XBT_PUBLIC File {
91 public:
92   File(const std::string& fullpath, void* userdata);
93   File(const std::string& fullpath, sg_host_t host, void* userdata);
94   File(const File&) = delete;
95   File& operator=(const File&) = delete;
96   ~File();
97
98   /** Retrieves the path to the file */
99   const char* get_path() { return fullpath_.c_str(); }
100
101   /** Simulates a local read action. Returns the size of data actually read */
102   sg_size_t read(sg_size_t size);
103
104   /** Simulates a write action. Returns the size of data actually written. */
105   sg_size_t write(sg_size_t size, int write_inside=0);
106
107   /** Allows to store user data on that host */
108   void set_userdata(void* data) { userdata_ = data; }
109   /** Retrieves the previously stored data */
110   void* get_userdata() { return userdata_; }
111
112   sg_size_t size();
113   void seek(sg_offset_t pos);             /** Sets the file head to the given position. */
114   void seek(sg_offset_t pos, int origin); /** Sets the file head to the given position from a given origin. */
115   sg_size_t tell();                       /** Retrieves the current file position */
116
117   /** Rename a file. WARNING: It is forbidden to move the file to another mount point */
118   void move(const std::string& fullpath);
119   int remote_copy(sg_host_t host, const char* fullpath);
120   int remote_move(sg_host_t host, const char* fullpath);
121
122   int unlink(); /** Remove a file from the contents of a disk */
123   void dump();
124
125   int desc_id = 0;
126   Storage* local_storage_;
127   std::string mount_point_;
128
129 private:
130   sg_size_t size_;
131   std::string path_;
132   std::string fullpath_;
133   sg_size_t current_position_ = SEEK_SET;
134   void* userdata_             = nullptr;
135 };
136
137 class XBT_PUBLIC FileSystemStorageExt {
138 public:
139   static simgrid::xbt::Extension<Storage, FileSystemStorageExt> EXTENSION_ID;
140   explicit FileSystemStorageExt(Storage* ptr);
141   FileSystemStorageExt(const FileSystemStorageExt&) = delete;
142   FileSystemStorageExt& operator=(const FileSystemStorageExt&) = delete;
143   std::map<std::string, sg_size_t>* parse_content(const std::string& filename);
144   std::map<std::string, sg_size_t>* get_content() { return content_.get(); }
145   sg_size_t get_size() { return size_; }
146   sg_size_t get_used_size() { return used_size_; }
147   void decr_used_size(sg_size_t size) { used_size_ -= size; }
148   void incr_used_size(sg_size_t size) { used_size_ += size; }
149
150 private:
151   std::unique_ptr<std::map<std::string, sg_size_t>> content_;
152   sg_size_t used_size_ = 0;
153   sg_size_t size_     = 0;
154 };
155
156 class XBT_PUBLIC FileDescriptorHostExt {
157 public:
158   static simgrid::xbt::Extension<Host, FileDescriptorHostExt> EXTENSION_ID;
159   FileDescriptorHostExt() = default;
160   FileDescriptorHostExt(const FileDescriptorHostExt&) = delete;
161   FileDescriptorHostExt& operator=(const FileDescriptorHostExt&) = delete;
162   std::unique_ptr<std::vector<int>> file_descriptor_table        = nullptr; // Created lazily on need
163 };
164 } // namespace s4u
165 } // namespace simgrid
166 #endif
167 #endif