Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / kernel / resource / DiskImpl.cpp
index f2877f2..20c23ac 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2019. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2019-2023. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -8,46 +8,42 @@
 #include "simgrid/s4u/Engine.hpp"
 #include "src/kernel/EngineImpl.hpp"
 #include "src/kernel/lmm/maxmin.hpp"
+#include "src/kernel/resource/profile/Profile.hpp"
 
-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(disk_kernel, surf, "Logging specific to the disk kernel resource");
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(res_disk, ker_resource, "Disk resources, that fuel I/O activities");
+namespace simgrid::kernel::resource {
 
-simgrid::kernel::resource::DiskModel* surf_disk_model = nullptr;
+xbt::signal<void(DiskAction const&, Action::State, Action::State)> DiskAction::on_state_change;
 
-namespace simgrid {
-namespace kernel {
-namespace resource {
-
-/*********
- * Model *
- *********/
-
-DiskModel::DiskModel() : Model(Model::UpdateAlgo::FULL)
+/************
+ * Resource *
+ ************/
+DiskImpl::DiskImpl(const std::string& name, double read_bandwidth, double write_bandwidth)
+    : Resource_T(name), piface_(this)
 {
-  set_maxmin_system(new simgrid::kernel::lmm::System(true /* selective update */));
+  read_bw_.peak   = read_bandwidth;
+  read_bw_.scale  = 1.0;
+  write_bw_.peak  = write_bandwidth;
+  write_bw_.scale = 1.0;
 }
 
-DiskModel::~DiskModel()
+DiskImpl* DiskImpl::set_host(s4u::Host* host)
 {
-  surf_disk_model = nullptr;
+  xbt_assert(host, "Cannot set host, none given");
+  host_ = host;
+  return this;
 }
 
-/************
- * Resource *
- ************/
-
-DiskImpl::DiskImpl(kernel::resource::Model* model, const std::string& name, kernel::lmm::System* maxminSystem,
-                   double read_bw, double write_bw)
-    : Resource(model, name, maxminSystem->constraint_new(this, std::max(read_bw, write_bw))), piface_(name, this)
+DiskImpl* DiskImpl::set_read_constraint(lmm::Constraint* constraint_read)
 {
-  DiskImpl::turn_on();
-  XBT_DEBUG("Create resource with read_bw '%f' write_bw '%f'", read_bw, write_bw);
-  constraint_read_  = maxminSystem->constraint_new(this, read_bw);
-  constraint_write_ = maxminSystem->constraint_new(this, write_bw);
+  constraint_read_ = constraint_read;
+  return this;
 }
 
-DiskImpl::~DiskImpl()
+DiskImpl* DiskImpl::set_write_constraint(lmm::Constraint* constraint_write)
 {
-  xbt_assert(currently_destroying_, "Don't delete Disks directly. Call destroy() instead.");
+  constraint_write_ = constraint_write;
+  return this;
 }
 
 /** @brief Fire the required callbacks and destroy the object
@@ -56,50 +52,144 @@ DiskImpl::~DiskImpl()
  */
 void DiskImpl::destroy()
 {
-  if (not currently_destroying_) {
-    currently_destroying_ = true;
-    s4u::Disk::on_destruction(this->piface_);
-    delete this;
+  s4u::Disk::on_destruction(piface_);
+  piface_.on_this_destruction(piface_);
+  delete this;
+}
+
+void DiskImpl::turn_on()
+{
+  if (not is_on()) {
+    Resource::turn_on();
+    s4u::Disk::on_onoff(piface_);
+    piface_.on_this_onoff(piface_);
+  }
+}
+void DiskImpl::turn_off()
+{
+  if (is_on()) {
+    Resource::turn_off();
+    s4u::Disk::on_onoff(piface_);
+    piface_.on_this_onoff(piface_);
+    cancel_actions();
   }
 }
 
-bool DiskImpl::is_used()
+DiskImpl* DiskImpl::set_read_bandwidth_profile(profile::Profile* profile)
 {
-  THROW_UNIMPLEMENTED;
+  if (profile) {
+    xbt_assert(read_bw_.event == nullptr, "Cannot set a second read bandwidth profile to Disk %s", get_cname());
+    read_bw_.event = profile->schedule(&profile::future_evt_set, this);
+  }
+  return this;
 }
 
-void DiskImpl::apply_event(kernel::profile::Event* /*event*/, double /*value*/)
+DiskImpl* DiskImpl::set_write_bandwidth_profile(profile::Profile* profile)
 {
-  THROW_UNIMPLEMENTED;
+  if (profile) {
+    xbt_assert(write_bw_.event == nullptr, "Cannot set a second read bandwidth profile to Disk %s", get_cname());
+    write_bw_.event = profile->schedule(&profile::future_evt_set, this);
+  }
+  return this;
 }
 
-void DiskImpl::turn_on()
+void DiskImpl::seal()
 {
-  if (not is_on()) {
-    Resource::turn_on();
-    s4u::Disk::on_state_change(this->piface_);
+  if (is_sealed())
+    return;
+
+  xbt_assert(this->get_model(), "Cannot seal Disk (%s) without setting the model first", get_cname());
+  lmm::System* maxmin_system = get_model()->get_maxmin_system();
+  /* set readwrite constraint if not configured by user */
+  if (readwrite_bw_ == -1) {
+    readwrite_bw_ = std::max(read_bw_.peak, write_bw_.peak);
   }
+  this->set_read_constraint(maxmin_system->constraint_new(this, read_bw_.peak * read_bw_.scale))
+      ->set_write_constraint(maxmin_system->constraint_new(this, write_bw_.peak * write_bw_.scale))
+      ->set_constraint(maxmin_system->constraint_new(this, readwrite_bw_));
+  apply_sharing_policy_cfg();
+  XBT_DEBUG("Create resource with read_bw '%f' write_bw '%f'", read_bw_.peak, write_bw_.peak);
+  Resource::seal();
+  turn_on();
 }
-void DiskImpl::turn_off()
+
+constexpr kernel::lmm::Constraint::SharingPolicy to_maxmin_policy(s4u::Disk::SharingPolicy policy)
 {
-  if (is_on()) {
-    Resource::turn_off();
-    s4u::Disk::on_state_change(this->piface_);
+  kernel::lmm::Constraint::SharingPolicy lmm_policy = kernel::lmm::Constraint::SharingPolicy::SHARED;
+  if (policy == s4u::Disk::SharingPolicy::NONLINEAR)
+    lmm_policy = kernel::lmm::Constraint::SharingPolicy::NONLINEAR;
+  return lmm_policy;
+}
+
+void DiskImpl::set_read_bandwidth(double value)
+{
+  read_bw_.peak = value;
+  if (constraint_read_)
+    get_model()->get_maxmin_system()->update_constraint_bound(constraint_read_, read_bw_.peak * read_bw_.scale);
+}
+
+void DiskImpl::set_write_bandwidth(double value)
+{
+  write_bw_.peak = value;
+  if (constraint_write_) {
+    get_model()->get_maxmin_system()->update_constraint_bound(constraint_write_, write_bw_.peak* write_bw_.scale);
   }
 }
 
-xbt::signal<void(DiskAction const&, kernel::resource::Action::State, kernel::resource::Action::State)>
-    DiskAction::on_state_change;
+void DiskImpl::set_readwrite_bandwidth(double value)
+{
+   readwrite_bw_ = value;
+   if (get_constraint()) {
+    get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(), readwrite_bw_);
+  }
+}
+
+void DiskImpl::set_sharing_policy(s4u::Disk::Operation op, s4u::Disk::SharingPolicy policy,
+                                  const s4u::NonLinearResourceCb& cb)
+{
+  sharing_policy_[op]    = policy;
+  sharing_policy_cb_[op] = cb;
+  apply_sharing_policy_cfg();
+}
+
+s4u::Disk::SharingPolicy DiskImpl::get_sharing_policy(s4u::Disk::Operation op) const
+{
+  return sharing_policy_.at(op);
+}
+
+void DiskImpl::apply_sharing_policy_cfg()
+{
+  if (get_constraint())
+    get_constraint()->set_sharing_policy(to_maxmin_policy(sharing_policy_[s4u::Disk::Operation::READWRITE]),
+                                         sharing_policy_cb_[s4u::Disk::Operation::READWRITE]);
+  if (constraint_read_)
+    constraint_read_->set_sharing_policy(to_maxmin_policy(sharing_policy_[s4u::Disk::Operation::READ]),
+                                         sharing_policy_cb_[s4u::Disk::Operation::READ]);
+  if (constraint_write_)
+    constraint_write_->set_sharing_policy(to_maxmin_policy(sharing_policy_[s4u::Disk::Operation::WRITE]),
+                                          sharing_policy_cb_[s4u::Disk::Operation::WRITE]);
+}
+
+void DiskImpl::set_factor_cb(const std::function<s4u::Disk::IoFactorCb>& cb)
+{
+  xbt_assert(not is_sealed(), "Cannot set I/O factor callback in an already sealed disk(%s)", get_cname());
+  factor_cb_ = cb;
+}
 
 /**********
  * Action *
  **********/
-void DiskAction::set_state(Action::State state)
+void DiskAction::set_state(Action::State new_state)
+{
+  Action::State previous_state = get_state();
+  if (new_state != previous_state) { // Trigger only if the state changed
+    Action::set_state(new_state);
+    on_state_change(*this, previous_state, new_state);
+  }
+}
+
+void DiskAction::update_remains_lazy(double /*now*/)
 {
-  Action::State old = get_state();
-  Action::set_state(state);
-  on_state_change(*this, old, state);
+  THROW_IMPOSSIBLE;
 }
-} // namespace resource
-} // namespace kernel
-} // namespace simgrid
+} // namespace simgrid::kernel::resource