Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics and typos
[simgrid.git] / src / kernel / resource / DiskImpl.cpp
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 "DiskImpl.hpp"
7
8 #include "simgrid/s4u/Engine.hpp"
9 #include "src/kernel/EngineImpl.hpp"
10 #include "src/kernel/lmm/maxmin.hpp"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(res_disk, ker_resource, "Disk resources, that fuel I/O activities");
13
14 namespace simgrid {
15 namespace kernel {
16 namespace resource {
17
18 xbt::signal<void(DiskAction const&, Action::State, Action::State)> DiskAction::on_state_change;
19
20 /*********
21  * Model *
22  *********/
23
24 DiskModel::DiskModel(const std::string& name) : Model(name)
25 {
26   set_maxmin_system(new lmm::System(true /* selective update */));
27 }
28
29 /************
30  * Resource *
31  ************/
32 DiskImpl* DiskImpl::set_host(s4u::Host* host)
33 {
34   xbt_assert(host, "Cannot set host, none given");
35   host_ = host;
36   return this;
37 }
38
39 DiskImpl* DiskImpl::set_read_bandwidth(double read_bw)
40 {
41   read_bw_ = read_bw;
42   return this;
43 }
44
45 DiskImpl* DiskImpl::set_write_bandwidth(double write_bw)
46 {
47   write_bw_ = write_bw;
48   return this;
49 }
50
51 DiskImpl* DiskImpl::set_read_constraint(lmm::Constraint* constraint_read)
52 {
53   constraint_read_ = constraint_read;
54   return this;
55 }
56
57 DiskImpl* DiskImpl::set_write_constraint(lmm::Constraint* constraint_write)
58 {
59   constraint_write_ = constraint_write;
60   return this;
61 }
62
63 /** @brief Fire the required callbacks and destroy the object
64  *
65  * Don't delete directly a Disk, call d->destroy() instead.
66  */
67 void DiskImpl::destroy()
68 {
69   s4u::Disk::on_destruction(piface_);
70   delete this;
71 }
72
73 bool DiskImpl::is_used() const
74 {
75   return get_model()->get_maxmin_system()->constraint_used(get_constraint());
76 }
77
78 void DiskImpl::apply_event(kernel::profile::Event* /*event*/, double /*value*/)
79 {
80   THROW_UNIMPLEMENTED;
81 }
82
83 void DiskImpl::turn_on()
84 {
85   if (not is_on()) {
86     Resource::turn_on();
87     s4u::Disk::on_state_change(piface_);
88   }
89 }
90 void DiskImpl::turn_off()
91 {
92   if (is_on()) {
93     Resource::turn_off();
94     s4u::Disk::on_state_change(piface_);
95   }
96 }
97
98 void DiskImpl::seal()
99 {
100   xbt_assert(this->get_model(), "Cannot seal Disk (%s) without setting the model first", get_cname());
101   lmm::System* maxmin_system = get_model()->get_maxmin_system();
102   this->set_read_constraint(maxmin_system->constraint_new(this, read_bw_))
103       ->set_write_constraint(maxmin_system->constraint_new(this, write_bw_))
104       ->set_constraint(maxmin_system->constraint_new(this, std::max(read_bw_, write_bw_)));
105   XBT_DEBUG("Create resource with read_bw '%f' write_bw '%f'", read_bw_, write_bw_);
106   Resource::seal();
107   turn_on();
108 }
109
110 /**********
111  * Action *
112  **********/
113 void DiskAction::set_state(Action::State new_state)
114 {
115   Action::State previous_state = get_state();
116   if (new_state != previous_state) { // Trigger only if the state changed
117     Action::set_state(new_state);
118     on_state_change(*this, previous_state, new_state);
119   }
120 }
121 } // namespace resource
122 } // namespace kernel
123 } // namespace simgrid