Logo AND Algorithmique Numérique Distribuée

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