Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not expose Activity::set_remaining publicly.
[simgrid.git] / src / s4u / s4u_Io.cpp
1 /* Copyright (c) 2018-2022. 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/s4u/Disk.hpp>
7 #include <simgrid/s4u/Io.hpp>
8 #include <xbt/log.h>
9
10 #include "src/kernel/activity/IoImpl.hpp"
11 #include "src/kernel/actor/ActorImpl.hpp"
12 #include "src/kernel/actor/SimcallObserver.hpp"
13
14 namespace simgrid::s4u {
15 xbt::signal<void(Io const&)> Io::on_start;
16
17 Io::Io(kernel::activity::IoImplPtr pimpl)
18 {
19   pimpl_ = pimpl;
20 }
21
22 IoPtr Io::init()
23 {
24   auto pimpl = kernel::activity::IoImplPtr(new kernel::activity::IoImpl());
25   return IoPtr(static_cast<Io*>(pimpl->get_iface()));
26 }
27
28 Io* Io::start()
29 {
30   kernel::actor::simcall_answered(
31       [this] { (*boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)).set_name(get_name()).start(); });
32
33   if (suspended_)
34     pimpl_->suspend();
35
36   state_ = State::STARTED;
37   on_start(*this);
38   return this;
39 }
40
41 ssize_t Io::wait_any_for(const std::vector<IoPtr>& ios, double timeout)
42 {
43   std::vector<ActivityPtr> activities;
44   for (const auto& io : ios)
45     activities.push_back(boost::dynamic_pointer_cast<Activity>(io));
46   return Activity::wait_any_for(activities, timeout);
47 }
48
49 IoPtr Io::set_disk(const_sg_disk_t disk)
50 {
51   xbt_assert(state_ == State::INITED || state_ == State::STARTING, "Cannot set disk once the Io is started");
52
53   kernel::actor::simcall_answered(
54       [this, disk] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_disk(disk->get_impl()); });
55
56   // Setting the disk may allow to start the activity, let's try
57   if (state_ == State::STARTING)
58     vetoable_start();
59
60  return this;
61 }
62
63 IoPtr Io::set_priority(double priority)
64 {
65   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
66              "Cannot change the priority of an io after its start");
67   kernel::actor::simcall_answered([this, priority] {
68     boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_sharing_penalty(1. / priority);
69   });
70   return this;
71 }
72
73 IoPtr Io::set_size(sg_size_t size)
74 {
75   xbt_assert(state_ == State::INITED || state_ == State::STARTING, "Cannot set size once the Io is started");
76   kernel::actor::simcall_answered(
77       [this, size] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_size(size); });
78   set_remaining(size);
79   return this;
80 }
81
82 IoPtr Io::set_op_type(OpType type)
83 {
84   xbt_assert(state_ == State::INITED || state_ == State::STARTING, "Cannot set size once the Io is started");
85   kernel::actor::simcall_answered(
86       [this, type] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_type(type); });
87   return this;
88 }
89
90 IoPtr Io::update_priority(double priority)
91 {
92   kernel::actor::simcall_answered([this, priority] {
93     boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->update_sharing_penalty(1. / priority);
94   });
95   return this;
96 }
97
98 /** @brief Returns the amount of flops that remain to be done */
99 double Io::get_remaining() const
100 {
101   return kernel::actor::simcall_answered(
102       [this]() { return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_remaining(); });
103 }
104
105 sg_size_t Io::get_performed_ioops() const
106 {
107   return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_performed_ioops();
108 }
109
110 bool Io::is_assigned() const
111 {
112   return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_disk() != nullptr;
113 }
114
115 } // namespace simgrid::s4u