Logo AND Algorithmique Numérique Distribuée

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