Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix comments.
[simgrid.git] / src / surf / StorageImpl.hpp
1 /* Copyright (c) 2004-2020. 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 "surf_interface.hpp"
12 #include "xbt/PropertyHolder.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() override;
50
51   virtual StorageImpl* createStorage(std::string& filename, int lineno, const std::string& id,
52                                      const std::string& type_id, const std::string& content_name,
53                                      const std::string& attach) = 0;
54 };
55
56 /************
57  * Resource *
58  ************/
59 /** @ingroup SURF_storage_interface
60  * @brief SURF storage interface class
61  * @details A Storage represent a storage unit (e.g.: hard drive, usb key)
62  */
63 class StorageImpl : public Resource, public xbt::PropertyHolder {
64   s4u::Storage piface_;
65   lmm::Constraint* constraint_read_;  /* Constraint for maximum read bandwidth*/
66   lmm::Constraint* constraint_write_; /* Constraint for maximum write bandwidth*/
67
68   std::string typeId_;
69   bool currently_destroying_ = false;
70   // Name of the host to which this storage is attached. Only used at platform parsing time, then the interface stores
71   // the Host directly.
72   std::string attach_;
73
74 public:
75   const std::string content_name_; // Only used at parsing time then goes to the FileSystemExtension
76   const sg_size_t size_;           // Only used at parsing time then goes to the FileSystemExtension
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   const s4u::Storage* get_iface() const { return &piface_; }
86   s4u::Storage* get_iface() { return &piface_; }
87   const char* get_type() const { return typeId_.c_str(); }
88   lmm::Constraint* get_read_constraint() const { return constraint_read_; }
89   lmm::Constraint* get_write_constraint() const { return constraint_write_; }
90   /** @brief Check if the Storage is used (if an action currently uses its resources) */
91   bool is_used() const override;
92
93   void apply_event(profile::Event* event, double value) override;
94
95   void turn_on() override;
96   void turn_off() override;
97
98   void destroy(); // Must be called instead of the destructor
99   virtual StorageAction* io_start(sg_size_t size, s4u::Io::OpType type) = 0;
100   /**
101    * @brief Read a file
102    *
103    * @param size The size in bytes to read
104    * @return The StorageAction corresponding to the reading
105    */
106   virtual StorageAction* read(sg_size_t size) = 0;
107
108   /**
109    * @brief Write a file
110    *
111    * @param size The size in bytes to write
112    * @return The StorageAction corresponding to the writing
113    */
114   virtual StorageAction* write(sg_size_t size) = 0;
115   const std::string& get_host() const { return attach_; }
116 };
117
118 /**********
119  * Action *
120  **********/
121
122 /** @ingroup SURF_storage_interface
123  * @brief SURF storage action interface class
124  */
125 class StorageAction : public Action {
126 public:
127   /**
128    * @brief Callbacks handler which emit the callbacks after StorageAction State changed *
129    * @details Callback functions have the following signature: `void(StorageAction& action,
130    * simgrid::kernel::resource::Action::State old, simgrid::kernel::resource::Action::State current)`
131    */
132   static xbt::signal<void(StorageAction const&, Action::State, Action::State)> on_state_change;
133
134   /**
135    * @brief StorageAction constructor
136    *
137    * @param model The StorageModel associated to this StorageAction
138    * @param cost The cost of this StorageAction in bytes
139    * @param failed [description]
140    * @param storage The Storage associated to this StorageAction
141    * @param type [description]
142    */
143   StorageAction(Model* model, double cost, bool failed, StorageImpl* storage, s4u::Io::OpType type)
144       : Action(model, cost, failed), type_(type), storage_(storage){};
145
146   /**
147  * @brief StorageAction constructor
148  *
149  * @param model The StorageModel associated to this StorageAction
150  * @param cost The cost of this  StorageAction in [TODO]
151  * @param failed [description]
152  * @param var The lmm variable associated to this StorageAction if it is part of a LMM component
153  * @param storage The Storage associated to this StorageAction
154  * @param type [description]
155  */
156   StorageAction(kernel::resource::Model* model, double cost, bool failed, kernel::lmm::Variable* var,
157                 StorageImpl* storage, s4u::Io::OpType type)
158       : Action(model, cost, failed, var), type_(type), storage_(storage){};
159
160   void set_state(simgrid::kernel::resource::Action::State state) override;
161
162   s4u::Io::OpType type_;
163   StorageImpl* storage_;
164 };
165
166 class StorageType {
167 public:
168   std::string id;
169   std::string model;
170   std::string content;
171   std::unordered_map<std::string, std::string>* properties;
172   std::unordered_map<std::string, std::string>* model_properties;
173   sg_size_t size;
174   StorageType(const std::string& id, const std::string& model, const std::string& content,
175               std::unordered_map<std::string, std::string>* properties,
176               std::unordered_map<std::string, std::string>* model_properties, sg_size_t size)
177       : id(id), model(model), content(content), properties(properties), model_properties(model_properties), size(size)
178   {
179   }
180 };
181
182 } // namespace resource
183 } // namespace kernel
184 } // namespace simgrid
185 #endif /* STORAGE_INTERFACE_HPP_ */