Logo AND Algorithmique Numérique Distribuée

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