Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix NS3 builds
[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(std::string id, std::string type_id, std::string content_name,
72                                      std::string 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, std::string name, lmm_system_t maxminSystem, double bread, double bwrite,
88               std::string type_id, std::string content_name, sg_size_t size, std::string attach);
89
90   ~StorageImpl() override;
91
92   /** @brief Public interface */
93   s4u::Storage piface_;
94   static StorageImpl* byName(const char* name);
95
96   /** @brief Check if the Storage is used (if an action currently uses its resources) */
97   bool isUsed() override;
98
99   void apply_event(tmgr_trace_event_t event, double value) override;
100
101   void turnOn() override;
102   void turnOff() override;
103
104   /**
105    * @brief Read a file
106    *
107    * @param size The size in bytes to read
108    * @return The StorageAction corresponding to the reading
109    */
110   virtual StorageAction* read(sg_size_t size) = 0;
111
112   /**
113    * @brief Write a file
114    *
115    * @param size The size in bytes to write
116    * @return The StorageAction corresponding to the writing
117    */
118   virtual StorageAction* write(sg_size_t size) = 0;
119
120   /**
121    * @brief Get the content of the current Storage
122    *
123    * @return A xbt_dict_t with path as keys and size in bytes as values
124    */
125   virtual std::map<std::string, sg_size_t>* getContent();
126
127   /**
128    * @brief Get the available size in bytes of the current Storage
129    *
130    * @return The available size in bytes of the current Storage
131    */
132   virtual sg_size_t getFreeSize();
133
134   /**
135    * @brief Get the used size in bytes of the current Storage
136    *
137    * @return The used size in bytes of the current Storage
138    */
139   virtual sg_size_t getUsedSize();
140   virtual sg_size_t getSize() { return size_; }
141   virtual std::string getHost() { return attach_; }
142
143   std::map<std::string, sg_size_t>* parseContent(std::string filename);
144   static std::unordered_map<std::string, StorageImpl*>* storagesMap() { return StorageImpl::storages; }
145
146   lmm_constraint_t constraintWrite_; /* Constraint for maximum write bandwidth*/
147   lmm_constraint_t constraintRead_;  /* Constraint for maximum write bandwidth*/
148
149   std::string typeId_;
150   sg_size_t usedSize_ = 0;
151
152 private:
153   sg_size_t size_;
154   static std::unordered_map<std::string, StorageImpl*>* storages;
155   std::map<std::string, sg_size_t>* content_;
156   // Name of the host to which this storage is attached. Only used at platform parsing time, then the interface stores
157   // the Host directly.
158   std::string attach_;
159 };
160
161 /**********
162  * Action *
163  **********/
164
165 /** @ingroup SURF_storage_interface
166  * @brief The possible type of action for the storage component
167  */
168 typedef enum {
169   READ = 0, /**< Read a file */
170   WRITE     /**< Write in a file */
171 } e_surf_action_storage_type_t;
172
173 /** @ingroup SURF_storage_interface
174  * @brief SURF storage action interface class
175  */
176 class StorageAction : public Action {
177 public:
178   /**
179    * @brief StorageAction constructor
180    *
181    * @param model The StorageModel associated to this StorageAction
182    * @param cost The cost of this  NetworkAction in [TODO]
183    * @param failed [description]
184    * @param storage The Storage associated to this StorageAction
185    * @param type [description]
186    */
187   StorageAction(Model* model, double cost, bool failed, StorageImpl* storage, e_surf_action_storage_type_t type)
188       : Action(model, cost, failed), type_(type), storage_(storage){};
189
190   /**
191  * @brief StorageAction constructor
192  *
193  * @param model The StorageModel associated to this StorageAction
194  * @param cost The cost of this  StorageAction in [TODO]
195  * @param failed [description]
196  * @param var The lmm variable associated to this StorageAction if it is part of a LMM component
197  * @param storage The Storage associated to this StorageAction
198  * @param type [description]
199  */
200   StorageAction(Model* model, double cost, bool failed, lmm_variable_t var, StorageImpl* storage,
201                 e_surf_action_storage_type_t type)
202       : Action(model, cost, failed, var), type_(type), storage_(storage){};
203
204   void setState(simgrid::surf::Action::State state) override;
205
206   e_surf_action_storage_type_t type_;
207   StorageImpl* storage_;
208   FileImpl* file_ = nullptr;
209 };
210 }
211 }
212
213 typedef struct s_storage_type {
214   char* model;
215   char* content;
216   char* type_id;
217   xbt_dict_t properties;
218   std::map<std::string, std::string>* model_properties;
219   sg_size_t size;
220 } s_storage_type_t;
221 typedef s_storage_type_t* storage_type_t;
222
223 #endif /* STORAGE_INTERFACE_HPP_ */