Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cleaning the actor twice seems somewhat overplayed
[simgrid.git] / src / surf / StorageImpl.hpp
1 /* Copyright (c) 2004-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/kernel/resource/Action.hpp"
7 #include "simgrid/kernel/resource/Model.hpp"
8 #include "simgrid/kernel/resource/Resource.hpp"
9 #include "simgrid/s4u/Io.hpp"
10 #include "simgrid/s4u/Storage.hpp"
11 #include "src/kernel/resource/profile/trace_mgr.hpp"
12 #include "src/surf/PropertyHolder.hpp"
13 #include "surf_interface.hpp"
14
15 #include <map>
16
17 #ifndef STORAGE_INTERFACE_HPP_
18 #define STORAGE_INTERFACE_HPP_
19
20 /*********
21  * Model *
22  *********/
23
24 XBT_PUBLIC_DATA simgrid::kernel::resource::StorageModel* surf_storage_model;
25
26 namespace simgrid {
27 namespace kernel {
28 namespace resource {
29 /***********
30  * Classes *
31  ***********/
32
33 class StorageAction;
34 /** @ingroup SURF_storage_interface
35  * @brief The possible type of action for the storage component
36  */
37 /*************
38  * Callbacks *
39  *************/
40
41 /** @ingroup SURF_callbacks
42  * @brief Callbacks handler which emit the callbacks after StorageAction State changed *
43  * @details Callback functions have the following signature: `void(StorageAction *action,
44  * simgrid::kernel::resource::Action::State old, simgrid::kernel::resource::Action::State current)`
45  */
46 XBT_PUBLIC_DATA
47 simgrid::xbt::signal<void(StorageAction*, kernel::resource::Action::State, kernel::resource::Action::State)>
48     on_state_change;
49
50 /*********
51  * Model *
52  *********/
53 /** @ingroup SURF_storage_interface
54  * @brief SURF storage model interface class
55  * @details A model is an object which handle the interactions between its Resources and its Actions
56  */
57 class StorageModel : public kernel::resource::Model {
58 public:
59   StorageModel();
60   StorageModel(const StorageModel&) = delete;
61   StorageModel& operator=(const StorageModel&) = delete;
62   ~StorageModel();
63
64   virtual StorageImpl* createStorage(const std::string& id, const std::string& type_id, const std::string& content_name,
65                                      const std::string& attach) = 0;
66 };
67
68 /************
69  * Resource *
70  ************/
71 /** @ingroup SURF_storage_interface
72  * @brief SURF storage interface class
73  * @details A Storage represent a storage unit (e.g.: hard drive, usb key)
74  */
75 class StorageImpl : public Resource, public surf::PropertyHolder {
76 public:
77   /** @brief Storage constructor */
78   StorageImpl(Model* model, const std::string& name, kernel::lmm::System* maxmin_system, double bread, double bwrite,
79               const std::string& type_id, const std::string& content_name, sg_size_t size, const std::string& attach);
80   StorageImpl(const StorageImpl&) = delete;
81   StorageImpl& operator=(const StorageImpl&) = delete;
82
83   ~StorageImpl() override;
84
85   /** @brief Public interface */
86   s4u::Storage piface_;
87
88   /** @brief Check if the Storage is used (if an action currently uses its resources) */
89   bool is_used() override;
90
91   void apply_event(profile::Event* event, double value) override;
92
93   void turn_on() override;
94   void turn_off() override;
95
96   void destroy(); // Must be called instead of the destructor
97   virtual Action* io_start(sg_size_t size, s4u::Io::OpType type) = 0;
98   /**
99    * @brief Read a file
100    *
101    * @param size The size in bytes to read
102    * @return The StorageAction corresponding to the reading
103    */
104   virtual StorageAction* read(sg_size_t size) = 0;
105
106   /**
107    * @brief Write a file
108    *
109    * @param size The size in bytes to write
110    * @return The StorageAction corresponding to the writing
111    */
112   virtual StorageAction* write(sg_size_t size) = 0;
113   virtual std::string getHost() { return attach_; }
114
115   lmm::Constraint* constraintWrite_; /* Constraint for maximum write bandwidth*/
116   lmm::Constraint* constraintRead_;  /* Constraint for maximum write bandwidth*/
117
118   std::string typeId_;
119   std::string content_name; // Only used at parsing time then goes to the FileSystemExtension
120   sg_size_t size_;          // Only used at parsing time then goes to the FileSystemExtension
121
122 private:
123   bool currentlyDestroying_ = false;
124   // Name of the host to which this storage is attached. Only used at platform parsing time, then the interface stores
125   // the Host directly.
126   std::string attach_;
127 };
128
129 /**********
130  * Action *
131  **********/
132
133 /** @ingroup SURF_storage_interface
134  * @brief SURF storage action interface class
135  */
136 class StorageAction : public Action {
137 public:
138   static xbt::signal<void(StorageAction*, Action::State, Action::State)> on_state_change;
139
140   /**
141    * @brief StorageAction constructor
142    *
143    * @param model The StorageModel associated to this StorageAction
144    * @param cost The cost of this StorageAction in bytes
145    * @param failed [description]
146    * @param storage The Storage associated to this StorageAction
147    * @param type [description]
148    */
149   StorageAction(Model* model, double cost, bool failed, StorageImpl* storage, s4u::Io::OpType type)
150       : Action(model, cost, failed), type_(type), storage_(storage){};
151
152   /**
153  * @brief StorageAction constructor
154  *
155  * @param model The StorageModel associated to this StorageAction
156  * @param cost The cost of this  StorageAction in [TODO]
157  * @param failed [description]
158  * @param var The lmm variable associated to this StorageAction if it is part of a LMM component
159  * @param storage The Storage associated to this StorageAction
160  * @param type [description]
161  */
162   StorageAction(kernel::resource::Model* model, double cost, bool failed, kernel::lmm::Variable* var,
163                 StorageImpl* storage, s4u::Io::OpType type)
164       : Action(model, cost, failed, var), type_(type), storage_(storage){};
165
166   void set_state(simgrid::kernel::resource::Action::State state) override;
167
168   s4u::Io::OpType type_;
169   StorageImpl* storage_;
170 };
171
172 class StorageType {
173 public:
174   std::string id;
175   std::string model;
176   std::string content;
177   std::unordered_map<std::string, std::string>* properties;
178   std::unordered_map<std::string, std::string>* model_properties;
179   sg_size_t size;
180   StorageType(const std::string& id, const std::string& model, const std::string& content,
181               std::unordered_map<std::string, std::string>* properties,
182               std::unordered_map<std::string, std::string>* model_properties, sg_size_t size)
183       : id(id), model(model), content(content), properties(properties), model_properties(model_properties), size(size)
184   {
185   }
186 };
187
188 } // namespace resource
189 } // namespace kernel
190 } // namespace simgrid
191 #endif /* STORAGE_INTERFACE_HPP_ */