Logo AND Algorithmique Numérique Distribuée

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