Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
also change the namespace of kernel/resource/{Action,Model}
[simgrid.git] / src / surf / StorageImpl.hpp
1 /* Copyright (c) 2004-2018. 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/kernel/resource/Action.hpp"
12 #include "src/kernel/resource/Model.hpp"
13 #include "src/kernel/resource/Resource.hpp"
14 #include "src/surf/PropertyHolder.hpp"
15 #include "surf_interface.hpp"
16 #include <map>
17
18 #ifndef STORAGE_INTERFACE_HPP_
19 #define STORAGE_INTERFACE_HPP_
20
21 namespace simgrid {
22 namespace surf {
23
24 /***********
25  * Classes *
26  ***********/
27
28 class StorageAction;
29
30 /*************
31  * Callbacks *
32  *************/
33
34 /** @ingroup SURF_callbacks
35  * @brief Callbacks handler which emit the callbacks after Storage creation *
36  * @details Callback functions have the following signature: `void(Storage*)`
37  */
38 XBT_PUBLIC_DATA(simgrid::xbt::signal<void(StorageImpl*)>) storageCreatedCallbacks;
39
40 /** @ingroup SURF_callbacks
41  * @brief Callbacks handler which emit the callbacks after Storage destruction *
42  * @details Callback functions have the following signature: `void(StoragePtr)`
43  */
44 XBT_PUBLIC_DATA(simgrid::xbt::signal<void(StorageImpl*)>) storageDestructedCallbacks;
45
46 /** @ingroup SURF_callbacks
47  * @brief Callbacks handler which emit the callbacks after Storage State changed *
48  * @details Callback functions have the following signature: `void(StorageAction *action, int previouslyOn, int
49  * currentlyOn)`
50  */
51 XBT_PUBLIC_DATA(simgrid::xbt::signal<void(StorageImpl*, int, int)>) storageStateChangedCallbacks;
52
53 /** @ingroup SURF_callbacks
54  * @brief Callbacks handler which emit the callbacks after StorageAction State changed *
55  * @details Callback functions have the following signature: `void(StorageAction *action,
56  * simgrid::kernel::resource::Action::State old, simgrid::kernel::resource::Action::State current)`
57  */
58 XBT_PUBLIC_DATA(
59     simgrid::xbt::signal<void(StorageAction*, kernel::resource::Action::State, kernel::resource::Action::State)>)
60 storageActionStateChangedCallbacks;
61
62 /*********
63  * Model *
64  *********/
65 /** @ingroup SURF_storage_interface
66  * @brief SURF storage model interface class
67  * @details A model is an object which handle the interactions between its Resources and its Actions
68  */
69 class StorageModel : public kernel::resource::Model {
70 public:
71   StorageModel();
72   ~StorageModel();
73
74   virtual StorageImpl* createStorage(std::string id, std::string type_id, std::string content_name,
75                                      std::string attach) = 0;
76
77   std::vector<StorageImpl*> p_storageList;
78 };
79
80 /************
81  * Resource *
82  ************/
83 /** @ingroup SURF_storage_interface
84  * @brief SURF storage interface class
85  * @details A Storage represent a storage unit (e.g.: hard drive, usb key)
86  */
87 class StorageImpl : public kernel::resource::Resource, public PropertyHolder {
88 public:
89   /** @brief Storage constructor */
90   StorageImpl(kernel::resource::Model* model, std::string name, lmm_system_t maxminSystem, double bread, double bwrite,
91               std::string type_id, std::string content_name, sg_size_t size, std::string attach);
92
93   ~StorageImpl() override;
94
95   /** @brief Public interface */
96   s4u::Storage piface_;
97   static StorageImpl* byName(std::string name);
98
99   /** @brief Check if the Storage is used (if an action currently uses its resources) */
100   bool isUsed() override;
101
102   void apply_event(tmgr_trace_event_t event, double value) override;
103
104   void turnOn() override;
105   void turnOff() override;
106
107   /**
108    * @brief Read a file
109    *
110    * @param size The size in bytes to read
111    * @return The StorageAction corresponding to the reading
112    */
113   virtual StorageAction* read(sg_size_t size) = 0;
114
115   /**
116    * @brief Write a file
117    *
118    * @param size The size in bytes to write
119    * @return The StorageAction corresponding to the writing
120    */
121   virtual StorageAction* write(sg_size_t size) = 0;
122   virtual std::string getHost() { return attach_; }
123
124   static std::unordered_map<std::string, StorageImpl*>* storagesMap() { return StorageImpl::storages; }
125
126   kernel::lmm::Constraint* constraintWrite_; /* Constraint for maximum write bandwidth*/
127   kernel::lmm::Constraint* constraintRead_;  /* Constraint for maximum write bandwidth*/
128
129   std::string typeId_;
130   std::string content_name; // Only used at parsing time then goes to the FileSystemExtension
131   sg_size_t size_;          // Only used at parsing time then goes to the FileSystemExtension
132
133 private:
134   static std::unordered_map<std::string, StorageImpl*>* storages;
135   // Name of the host to which this storage is attached. Only used at platform parsing time, then the interface stores
136   // the Host directly.
137   std::string attach_;
138 };
139
140 /**********
141  * Action *
142  **********/
143
144 /** @ingroup SURF_storage_interface
145  * @brief The possible type of action for the storage component
146  */
147 enum e_surf_action_storage_type_t {
148   READ = 0, /**< Read a file */
149   WRITE     /**< Write in a file */
150 };
151
152 /** @ingroup SURF_storage_interface
153  * @brief SURF storage action interface class
154  */
155 class StorageAction : public kernel::resource::Action {
156 public:
157   /**
158    * @brief StorageAction constructor
159    *
160    * @param model The StorageModel associated to this StorageAction
161    * @param cost The cost of this  NetworkAction in [TODO]
162    * @param failed [description]
163    * @param storage The Storage associated to this StorageAction
164    * @param type [description]
165    */
166   StorageAction(kernel::resource::Model* model, double cost, bool failed, StorageImpl* storage,
167                 e_surf_action_storage_type_t type)
168       : Action(model, cost, failed), type_(type), storage_(storage){};
169
170   /**
171  * @brief StorageAction constructor
172  *
173  * @param model The StorageModel associated to this StorageAction
174  * @param cost The cost of this  StorageAction in [TODO]
175  * @param failed [description]
176  * @param var The lmm variable associated to this StorageAction if it is part of a LMM component
177  * @param storage The Storage associated to this StorageAction
178  * @param type [description]
179  */
180   StorageAction(kernel::resource::Model* model, double cost, bool failed, kernel::lmm::Variable* var,
181                 StorageImpl* storage, e_surf_action_storage_type_t type)
182       : Action(model, cost, failed, var), type_(type), storage_(storage){};
183
184   void setState(simgrid::kernel::resource::Action::State state) override;
185
186   e_surf_action_storage_type_t type_;
187   StorageImpl* storage_;
188 };
189
190 class StorageType {
191 public:
192   std::string id;
193   std::string model;
194   std::string content;
195   std::map<std::string, std::string>* properties;
196   std::map<std::string, std::string>* model_properties;
197   sg_size_t size;
198   StorageType(std::string id, std::string model, std::string content, std::map<std::string, std::string>* properties,
199               std::map<std::string, std::string>* model_properties, sg_size_t size)
200       : id(id), model(model), content(content), properties(properties), model_properties(model_properties), size(size)
201   {
202   }
203 };
204 }
205 }
206
207 #endif /* STORAGE_INTERFACE_HPP_ */