Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'stable'
[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 {
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<ActivityPtr> activities;
45   for (const auto& io : ios)
46     activities.push_back(boost::dynamic_pointer_cast<Activity>(io));
47   return Activity::wait_any_for(activities, timeout);
48 }
49
50 IoPtr Io::set_disk(const_sg_disk_t disk)
51 {
52   xbt_assert(state_ == State::INITED || state_ == State::STARTING, "Cannot set disk once the Io is started");
53
54   kernel::actor::simcall(
55       [this, disk] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_disk(disk->get_impl()); });
56
57   // Setting the disk may allow to start the activity, let's try
58   if (state_ == State::STARTING)
59     vetoable_start();
60
61  return this;
62 }
63
64 IoPtr Io::set_priority(double priority)
65 {
66   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
67              "Cannot change the priority of an io after its start");
68   kernel::actor::simcall([this, priority] {
69     boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_sharing_penalty(1. / priority);
70   });
71   return this;
72 }
73
74 IoPtr Io::set_size(sg_size_t size)
75 {
76   xbt_assert(state_ == State::INITED || state_ == State::STARTING, "Cannot set size once the Io is started");
77   kernel::actor::simcall(
78       [this, size] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_size(size); });
79   Activity::set_remaining(size);
80   return this;
81 }
82
83 IoPtr Io::set_op_type(OpType type)
84 {
85   xbt_assert(state_ == State::INITED || state_ == State::STARTING, "Cannot set size once the Io is started");
86   kernel::actor::simcall(
87       [this, type] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_type(type); });
88   return this;
89 }
90
91 IoPtr Io::update_priority(double priority)
92 {
93   kernel::actor::simcall([this, priority] {
94     boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->update_sharing_penalty(1. / priority);
95   });
96   return this;
97 }
98
99 /** @brief Returns the amount of flops that remain to be done */
100 double Io::get_remaining() const
101 {
102   return kernel::actor::simcall(
103       [this]() { return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_remaining(); });
104 }
105
106 sg_size_t Io::get_performed_ioops() const
107 {
108   return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_performed_ioops();
109 }
110
111 bool Io::is_assigned() const
112 {
113   return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_disk() != nullptr;
114 }
115
116 } // namespace s4u
117 } // namespace simgrid