Logo AND Algorithmique Numérique Distribuée

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