Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
closing a file is a noop. No need to deep down the stack
[simgrid.git] / src / surf / StorageImpl.hpp
1 /* Copyright (c) 2004-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <xbt/base.h>
8 #include <xbt/signal.hpp>
9
10 #include "simgrid/s4u/Storage.hpp"
11 #include "src/surf/PropertyHolder.hpp"
12 #include "surf_interface.hpp"
13 #include <map>
14
15 #ifndef STORAGE_INTERFACE_HPP_
16 #define STORAGE_INTERFACE_HPP_
17
18 namespace simgrid {
19 namespace surf {
20
21 /***********
22  * Classes *
23  ***********/
24
25 class StorageAction;
26
27 /*************
28  * Callbacks *
29  *************/
30
31 /** @ingroup SURF_callbacks
32  * @brief Callbacks handler which emit the callbacks after Storage creation *
33  * @details Callback functions have the following signature: `void(Storage*)`
34  */
35 XBT_PUBLIC_DATA(simgrid::xbt::signal<void(simgrid::surf::StorageImpl*)>) storageCreatedCallbacks;
36
37 /** @ingroup SURF_callbacks
38  * @brief Callbacks handler which emit the callbacks after Storage destruction *
39  * @details Callback functions have the following signature: `void(StoragePtr)`
40  */
41 XBT_PUBLIC_DATA(simgrid::xbt::signal<void(simgrid::surf::StorageImpl*)>) storageDestructedCallbacks;
42
43 /** @ingroup SURF_callbacks
44  * @brief Callbacks handler which emit the callbacks after Storage State changed *
45  * @details Callback functions have the following signature: `void(StorageAction *action, int previouslyOn, int
46  * currentlyOn)`
47  */
48 XBT_PUBLIC_DATA(simgrid::xbt::signal<void(simgrid::surf::StorageImpl*, int, int)>) storageStateChangedCallbacks;
49
50 /** @ingroup SURF_callbacks
51  * @brief Callbacks handler which emit the callbacks after StorageAction State changed *
52  * @details Callback functions have the following signature: `void(StorageAction *action, simgrid::surf::Action::State
53  * old, simgrid::surf::Action::State current)`
54  */
55 XBT_PUBLIC_DATA(simgrid::xbt::signal<void(simgrid::surf::StorageAction*, simgrid::surf::Action::State,
56                                           simgrid::surf::Action::State)>)
57 storageActionStateChangedCallbacks;
58
59 /*********
60  * Model *
61  *********/
62 /** @ingroup SURF_storage_interface
63  * @brief SURF storage model interface class
64  * @details A model is an object which handle the interactions between its Resources and its Actions
65  */
66 class StorageModel : public Model {
67 public:
68   StorageModel();
69   ~StorageModel();
70
71   virtual StorageImpl* createStorage(const char* id, const char* type_id, const char* content_name,
72                                      const char* attach) = 0;
73
74   std::vector<StorageImpl*> p_storageList;
75 };
76
77 /************
78  * Resource *
79  ************/
80 /** @ingroup SURF_storage_interface
81  * @brief SURF storage interface class
82  * @details A Storage represent a storage unit (e.g.: hard drive, usb key)
83  */
84 class StorageImpl : public simgrid::surf::Resource, public simgrid::surf::PropertyHolder {
85 public:
86   /** @brief Storage constructor */
87   StorageImpl(Model* model, const char* name, lmm_system_t maxminSystem, double bread, double bwrite,
88               const char* type_id, const char* content_name, sg_size_t size, const char* attach);
89
90   ~StorageImpl() override;
91
92 public:
93   /** @brief Public interface */
94   s4u::Storage piface_;
95   static StorageImpl* byName(const char* name);
96
97   /** @brief Check if the Storage is used (if an action currently uses its resources) */
98   bool isUsed() override;
99
100   void apply_event(tmgr_trace_event_t event, double value) override;
101
102   void turnOn() override;
103   void turnOff() override;
104
105   std::map<std::string, sg_size_t>* content_;
106   sg_size_t size_;
107   sg_size_t usedSize_;
108   std::string typeId_;
109   std::string attach_; // Name of the host to which this storage is attached.
110                        // Only used until the platform is fully parsed only.
111                        // Then the interface stores the Host directly.
112                        /**
113                         * @brief Open a file
114                         *
115                         * @param mount The mount point
116                         * @param path The path to the file
117                         *
118                         * @return The StorageAction corresponding to the opening
119                         */
120   virtual StorageAction* open(const char* mount, const char* path) = 0;
121
122   /**
123    * @brief Read a file
124    *
125    * @param fd The file descriptor to read
126    * @param size The size in bytes to read
127    * @return The StorageAction corresponding to the reading
128    */
129   virtual StorageAction* read(surf_file_t fd, sg_size_t size) = 0;
130
131   /**
132    * @brief Write a file
133    *
134    * @param fd The file descriptor to write
135    * @param size The size in bytes to write
136    * @return The StorageAction corresponding to the writing
137    */
138   virtual StorageAction* write(surf_file_t fd, sg_size_t size) = 0;
139
140   /**
141    * @brief Get the content of the current Storage
142    *
143    * @return A xbt_dict_t with path as keys and size in bytes as values
144    */
145   virtual std::map<std::string, sg_size_t>* getContent();
146
147   /**
148    * @brief Get the available size in bytes of the current Storage
149    *
150    * @return The available size in bytes of the current Storage
151    */
152   virtual sg_size_t getFreeSize();
153
154   /**
155    * @brief Get the used size in bytes of the current Storage
156    *
157    * @return The used size in bytes of the current Storage
158    */
159   virtual sg_size_t getUsedSize();
160
161   std::map<std::string, sg_size_t>* parseContent(const char* filename);
162   static std::unordered_map<std::string, StorageImpl*>* storages;
163   static std::unordered_map<std::string, StorageImpl*>* storagesMap() { return StorageImpl::storages; }
164
165   lmm_constraint_t constraintWrite_; /* Constraint for maximum write bandwidth*/
166   lmm_constraint_t constraintRead_;  /* Constraint for maximum write bandwidth*/
167 };
168
169 /**********
170  * Action *
171  **********/
172
173 /** @ingroup SURF_storage_interface
174  * @brief The possible type of action for the storage component
175  */
176 typedef enum {
177   READ = 0, /**< Read a file */
178   WRITE,    /**< Write in a file */
179   OPEN,     /**< Open a file */
180   CLOSE     /**< Close a file */
181 } e_surf_action_storage_type_t;
182
183 /** @ingroup SURF_storage_interface
184  * @brief SURF storage action interface class
185  */
186 class StorageAction : public Action {
187 public:
188   /**
189    * @brief StorageAction constructor
190    *
191    * @param model The StorageModel associated to this StorageAction
192    * @param cost The cost of this  NetworkAction in [TODO]
193    * @param failed [description]
194    * @param storage The Storage associated to this StorageAction
195    * @param type [description]
196    */
197   StorageAction(Model* model, double cost, bool failed, StorageImpl* storage, e_surf_action_storage_type_t type);
198
199   /**
200  * @brief StorageAction constructor
201  *
202  * @param model The StorageModel associated to this StorageAction
203  * @param cost The cost of this  StorageAction in [TODO]
204  * @param failed [description]
205  * @param var The lmm variable associated to this StorageAction if it is part of a LMM component
206  * @param storage The Storage associated to this StorageAction
207  * @param type [description]
208  */
209   StorageAction(Model* model, double cost, bool failed, lmm_variable_t var, StorageImpl* storage,
210                 e_surf_action_storage_type_t type);
211
212   void setState(simgrid::surf::Action::State state) override;
213
214   e_surf_action_storage_type_t type_;
215   StorageImpl* storage_;
216   FileImpl* file_;
217   double progress_;
218 };
219 }
220 }
221
222 typedef struct s_storage_type {
223   char* model;
224   char* content;
225   char* type_id;
226   xbt_dict_t properties;
227   std::map<std::string, std::string>* model_properties;
228   sg_size_t size;
229 } s_storage_type_t;
230 typedef s_storage_type_t* storage_type_t;
231
232 #endif /* STORAGE_INTERFACE_HPP_ */