Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
more fluent version
[simgrid.git] / src / kernel / resource / DiskImpl.hpp
1 /* Copyright (c) 2019-2021. 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/Disk.hpp"
10 #include "simgrid/s4u/Io.hpp"
11 #include "src/surf/surf_interface.hpp"
12 #include <xbt/PropertyHolder.hpp>
13
14 #include <map>
15
16 #ifndef DISK_INTERFACE_HPP_
17 #define DISK_INTERFACE_HPP_
18
19 /*********
20  * Model *
21  *********/
22
23 XBT_PUBLIC_DATA simgrid::kernel::resource::DiskModel* surf_disk_model;
24
25 namespace simgrid {
26 namespace kernel {
27 namespace resource {
28 /***********
29  * Classes *
30  ***********/
31
32 class DiskAction;
33
34 /*********
35  * Model *
36  *********/
37 class DiskModel : public Model {
38 public:
39   DiskModel();
40   DiskModel(const DiskModel&) = delete;
41   DiskModel& operator=(const DiskModel&) = delete;
42   ~DiskModel() override;
43
44   virtual DiskImpl* create_disk(const std::string& name, double read_bandwidth, double write_bandwidth) = 0;
45 };
46
47 /************
48  * Resource *
49  ************/
50 class DiskImpl : public Resource, public xbt::PropertyHolder {
51   s4u::Host* host_           = nullptr;
52   s4u::Disk piface_;
53   double read_bw_ = -1.0;
54   double write_bw_ = 1.0;
55   lmm::Constraint* constraint_write_ = nullptr; /* Constraint for maximum write bandwidth*/
56   lmm::Constraint* constraint_read_ = nullptr;  /* Constraint for maximum read bandwidth*/
57
58 protected:
59   ~DiskImpl() override = default; // Disallow direct deletion. Call destroy() instead.
60
61 public:
62   DiskImpl(const std::string& name, double read_bandwidth, double write_bandwidth)
63     : Resource(name),
64       piface_(name, this),
65       read_bw_(read_bandwidth),
66       write_bw_(write_bandwidth){}
67   DiskImpl(const DiskImpl&) = delete;
68   DiskImpl& operator=(const DiskImpl&) = delete;
69
70   /** @brief Public interface */
71   const s4u::Disk* get_iface() const { return &piface_; }
72   s4u::Disk* get_iface() { return &piface_; }
73   DiskImpl* set_host(s4u::Host* host);
74   s4u::Host* get_host() const { return host_; }
75
76   DiskImpl* set_read_bandwidth(double read_bw);
77   double get_read_bandwidth() const { return read_bw_; }
78
79   DiskImpl* set_write_bandwidth(double write_bw);
80   double get_write_bandwidth() const { return write_bw_; }
81
82   DiskImpl* set_read_constraint(lmm::Constraint* constraint_read);
83   lmm::Constraint* get_read_constraint() const { return constraint_read_; }
84
85   DiskImpl* set_write_constraint(lmm::Constraint* constraint_write);
86   lmm::Constraint* get_write_constraint() const { return constraint_write_; }
87
88   /** @brief Check if the Disk is used (if an action currently uses its resources) */
89   bool is_used() const override;
90   void apply_event(profile::Event* event, double value) override;
91   void turn_on() override;
92   void turn_off() override;
93
94   void seal();
95   void destroy(); // Must be called instead of the destructor
96   virtual DiskAction* io_start(sg_size_t size, s4u::Io::OpType type) = 0;
97   virtual DiskAction* read(sg_size_t size)                           = 0;
98   virtual DiskAction* write(sg_size_t size)                          = 0;
99 };
100
101 /**********
102  * Action *
103  **********/
104
105 class DiskAction : public Action {
106 public:
107   static xbt::signal<void(DiskAction const&, Action::State, Action::State)> on_state_change;
108
109   DiskAction(Model* model, double cost, bool failed, DiskImpl* disk, s4u::Io::OpType type)
110       : Action(model, cost, failed), type_(type), disk_(disk){};
111
112   /**
113    * @brief diskAction constructor
114    *
115    * @param model The DiskModel associated to this DiskAction
116    * @param cost The cost of this DiskAction in bytes
117    * @param failed [description]
118    * @param var The lmm variable associated to this DiskAction if it is part of a LMM component
119    * @param disk The Disk associated to this DiskAction
120    * @param type [description]
121    */
122   DiskAction(kernel::resource::Model* model, double cost, bool failed, kernel::lmm::Variable* var, DiskImpl* disk,
123              s4u::Io::OpType type)
124       : Action(model, cost, failed, var), type_(type), disk_(disk){};
125
126   void set_state(simgrid::kernel::resource::Action::State state) override;
127
128   s4u::Io::OpType type_;
129   DiskImpl* disk_;
130 };
131
132 } // namespace resource
133 } // namespace kernel
134 } // namespace simgrid
135 #endif /* DISK_INTERFACE_HPP_ */