1 /* Copyright (c) 2019-2021. The SimGrid Team. All rights reserved. */
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. */
6 #include "DiskImpl.hpp"
8 #include "simgrid/s4u/Engine.hpp"
9 #include "src/kernel/EngineImpl.hpp"
10 #include "src/kernel/lmm/maxmin.hpp"
11 #include "src/kernel/resource/profile/Profile.hpp"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(res_disk, ker_resource, "Disk resources, that fuel I/O activities");
19 xbt::signal<void(DiskAction const&, Action::State, Action::State)> DiskAction::on_state_change;
25 DiskModel::DiskModel(const std::string& name) : Model(name)
27 set_maxmin_system(new lmm::System(true /* selective update */));
33 DiskImpl::DiskImpl(const std::string& name, double read_bandwidth, double write_bandwidth)
34 : Resource_T(name), piface_(this)
36 read_bw_.peak = read_bandwidth;
38 write_bw_.peak = write_bandwidth;
39 write_bw_.scale = 1.0;
42 DiskImpl* DiskImpl::set_host(s4u::Host* host)
44 xbt_assert(host, "Cannot set host, none given");
49 DiskImpl* DiskImpl::set_read_constraint(lmm::Constraint* constraint_read)
51 constraint_read_ = constraint_read;
55 DiskImpl* DiskImpl::set_write_constraint(lmm::Constraint* constraint_write)
57 constraint_write_ = constraint_write;
61 /** @brief Fire the required callbacks and destroy the object
63 * Don't delete directly a Disk, call d->destroy() instead.
65 void DiskImpl::destroy()
67 s4u::Disk::on_destruction(piface_);
71 bool DiskImpl::is_used() const
73 return get_model()->get_maxmin_system()->constraint_used(get_constraint());
76 void DiskImpl::turn_on()
80 s4u::Disk::on_state_change(piface_);
83 void DiskImpl::turn_off()
87 s4u::Disk::on_state_change(piface_);
91 DiskImpl* DiskImpl::set_read_bandwidth_profile(profile::Profile* profile)
94 xbt_assert(read_bw_.event == nullptr, "Cannot set a second read bandwidth profile to Disk %s", get_cname());
95 read_bw_.event = profile->schedule(&profile::future_evt_set, this);
100 DiskImpl* DiskImpl::set_write_bandwidth_profile(profile::Profile* profile)
103 xbt_assert(write_bw_.event == nullptr, "Cannot set a second read bandwidth profile to Disk %s", get_cname());
104 write_bw_.event = profile->schedule(&profile::future_evt_set, this);
109 void DiskImpl::seal()
114 xbt_assert(this->get_model(), "Cannot seal Disk (%s) without setting the model first", get_cname());
115 lmm::System* maxmin_system = get_model()->get_maxmin_system();
116 this->set_read_constraint(maxmin_system->constraint_new(this, read_bw_.peak * read_bw_.scale))
117 ->set_write_constraint(maxmin_system->constraint_new(this, write_bw_.peak * write_bw_.scale))
118 ->set_constraint(maxmin_system->constraint_new(this, std::max(read_bw_.peak, write_bw_.peak)));
119 XBT_DEBUG("Create resource with read_bw '%f' write_bw '%f'", read_bw_.peak, write_bw_.peak);
127 void DiskAction::set_state(Action::State new_state)
129 Action::State previous_state = get_state();
130 if (new_state != previous_state) { // Trigger only if the state changed
131 Action::set_state(new_state);
132 on_state_change(*this, previous_state, new_state);
135 } // namespace resource
136 } // namespace kernel
137 } // namespace simgrid