Logo AND Algorithmique Numérique Distribuée

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