Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0fcfd2b59f0b601155b88eed40718cf9e1753f67
[simgrid.git] / src / surf / storage_interface.hpp
1 /* Copyright (c) 2004-2015. 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 "surf_interface.hpp"
11 #include "src/surf/PropertyHolder.hpp"
12
13 #ifndef STORAGE_INTERFACE_HPP_
14 #define STORAGE_INTERFACE_HPP_
15
16 namespace simgrid {
17 namespace surf {
18
19 /***********
20  * Classes *
21  ***********/
22
23 class StorageModel;
24 class Storage;
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::Storage*)>) 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::Storage*)>) 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 currentlyOn)`
46  */
47 XBT_PUBLIC_DATA(simgrid::xbt::signal<void(simgrid::surf::Storage*, 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 old, simgrid::surf::Action::State current)`
52  */
53 XBT_PUBLIC_DATA(simgrid::xbt::signal<void(simgrid::surf::StorageAction*, simgrid::surf::Action::State, simgrid::surf::Action::State)>) storageActionStateChangedCallbacks;
54
55 /*********
56  * Model *
57  *********/
58 /** @ingroup SURF_storage_interface
59  * @brief SURF storage model interface class
60  * @details A model is an object which handle the interactions between its Resources and its Actions
61  */
62 class StorageModel : public Model {
63 public:
64   StorageModel();
65   ~StorageModel();
66
67   /** @brief Create a Storage
68    *
69    * @param id [description]
70    * @param type_id [description]
71    * @param content_name [description]
72    * @param content_type [description]
73    * @param properties [description]
74    * @param attach [description]
75    * @return The created Storage
76    */
77   virtual Storage *createStorage(const char* id, const char* type_id,
78                                  const char* content_name, const char* content_type,
79                                  xbt_dict_t properties, const char *attach) = 0;
80
81   bool next_occuring_event_isIdempotent() {return true;}
82
83   std::vector<Storage*> p_storageList;
84 };
85
86 /************
87  * Resource *
88  ************/
89 /** @ingroup SURF_storage_interface
90  * @brief SURF storage interface class
91  * @details A Storage represent a storage unit (e.g.: hard drive, usb key)
92  */
93 class Storage : public simgrid::surf::Resource,
94         public simgrid::surf::PropertyHolder {
95 public:
96   /**
97    * @brief Storage constructor
98    *
99    * @param model StorageModel associated to this Storage
100    * @param name The name of the Storage
101    * @param props Dictionary of properties associated to this Storage
102    * @param type_id [description]
103    * @param content_name [description]
104    * @param content_type [description]
105    * @param size [description]
106    */
107   Storage(Model *model, const char *name, xbt_dict_t props,
108           const char* type_id, const char *content_name, const char *content_type,
109           sg_size_t size);
110
111   /**
112    * @brief Storage constructor
113    *
114    * @param model StorageModel associated to this Storage
115    * @param name The name of the Storage
116    * @param props Dictionary of properties associated to this Storage
117    */
118   Storage(Model *model, const char *name, xbt_dict_t props,
119           lmm_system_t maxminSystem, double bread, double bwrite,
120           double bconnection,
121           const char* type_id, const char *content_name, const char *content_type,
122           sg_size_t size, const char *attach);
123
124   ~Storage();
125
126   /** @brief Check if the Storage is used (if an action currently uses its resources) */
127   bool isUsed() override;
128
129   void apply_event(tmgr_trace_iterator_t event, double value) override;
130
131   void turnOn() override;
132   void turnOff() override;
133
134   xbt_dict_t content_;
135   char* contentType_;
136   sg_size_t size_;
137   sg_size_t usedSize_;
138   char * typeId_;
139   char* attach_; //FIXME: this is the name of the host. Use the host directly
140
141   /**
142    * @brief Open a file
143    *
144    * @param mount The mount point
145    * @param path The path to the file
146    *
147    * @return The StorageAction corresponding to the opening
148    */
149   virtual StorageAction *open(const char* mount, const char* path)=0;
150
151   /**
152    * @brief Close a file
153    *
154    * @param fd The file descriptor to close
155    * @return The StorageAction corresponding to the closing
156    */
157   virtual StorageAction *close(surf_file_t fd)=0;
158
159   /**
160    * @brief Read a file
161    *
162    * @param fd The file descriptor to read
163    * @param size The size in bytes to read
164    * @return The StorageAction corresponding to the reading
165    */
166   virtual StorageAction *read(surf_file_t fd, sg_size_t size)=0;
167
168   /**
169    * @brief Write a file
170    *
171    * @param fd The file descriptor to write
172    * @param size The size in bytes to write
173    * @return The StorageAction corresponding to the writing
174    */
175   virtual StorageAction *write(surf_file_t fd, sg_size_t size)=0;
176
177   /**
178    * @brief Get the content of the current Storage
179    *
180    * @return A xbt_dict_t with path as keys and size in bytes as values
181    */
182   virtual xbt_dict_t getContent();
183
184   /**
185    * @brief Get the size in bytes of the current Storage
186    *
187    * @return The size in bytes of the current Storage
188    */
189   virtual sg_size_t getSize();
190
191   /**
192    * @brief Get the available size in bytes of the current Storage
193    *
194    * @return The available size in bytes of the current Storage
195    */
196   virtual sg_size_t getFreeSize();
197
198   /**
199    * @brief Get the used size in bytes of the current Storage
200    *
201    * @return The used size in bytes of the current Storage
202    */
203   virtual sg_size_t getUsedSize();
204
205
206   xbt_dict_t parseContent(const char *filename);
207
208   std::vector<StorageAction*> writeActions_;
209
210   lmm_constraint_t constraintWrite_;    /* Constraint for maximum write bandwidth*/
211   lmm_constraint_t constraintRead_;     /* Constraint for maximum write bandwidth*/
212 };
213
214 /**********
215  * Action *
216  **********/
217
218 /** @ingroup SURF_storage_interface
219  * @brief The possible type of action for the storage component
220  */
221 typedef enum {
222   READ=0, /**< Read a file */
223   WRITE,  /**< Write in a file */
224   STAT,   /**< Stat a file */
225   OPEN,   /**< Open a file */
226   CLOSE  /**< Close a file */
227 } e_surf_action_storage_type_t;
228
229 /** @ingroup SURF_storage_interface
230  * @brief SURF storage action interface class
231  */
232 class StorageAction : public Action {
233 public:
234   /**
235    * @brief StorageAction constructor
236    *
237    * @param model The StorageModel associated to this StorageAction
238    * @param cost The cost of this  NetworkAction in [TODO]
239    * @param failed [description]
240    * @param storage The Storage associated to this StorageAction
241    * @param type [description]
242    */
243   StorageAction(Model *model, double cost, bool failed, Storage *storage,
244       e_surf_action_storage_type_t type);
245
246     /**
247    * @brief StorageAction constructor
248    *
249    * @param model The StorageModel associated to this StorageAction
250    * @param cost The cost of this  StorageAction in [TODO]
251    * @param failed [description]
252    * @param var The lmm variable associated to this StorageAction if it is part of a LMM component
253    * @param storage The Storage associated to this StorageAction
254    * @param type [description]
255    */
256   StorageAction(Model *model, double cost, bool failed, lmm_variable_t var,
257       Storage *storage, e_surf_action_storage_type_t type);
258
259   void setState(simgrid::surf::Action::State state) override;
260
261   e_surf_action_storage_type_t m_type;
262   Storage *p_storage;
263   surf_file_t p_file;
264   double progress;
265 };
266
267 }
268 }
269
270 typedef struct s_storage_type {
271   char *model;
272   char *content;
273   char *content_type;
274   char *type_id;
275   xbt_dict_t properties;
276   xbt_dict_t model_properties;
277   sg_size_t size;
278 } s_storage_type_t, *storage_type_t;
279
280 typedef struct s_mount {
281   void *storage;
282   char *name;
283 } s_mount_t, *mount_t;
284
285 typedef struct surf_file {
286   char *name;
287   char *mount;
288   sg_size_t size;
289   sg_size_t current_position;
290 } s_surf_file_t;
291
292 #endif /* STORAGE_INTERFACE_HPP_ */