Logo AND Algorithmique Numérique Distribuée

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