Logo AND Algorithmique Numérique Distribuée

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