Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / s4u / s4u_Io.cpp
1 /* Copyright (c) 2018-2023. 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/ActivitySet.hpp>
7 #include <simgrid/s4u/Disk.hpp>
8 #include <simgrid/s4u/Io.hpp>
9 #include <xbt/log.h>
10
11 #include "src/kernel/activity/IoImpl.hpp"
12 #include "src/kernel/actor/ActorImpl.hpp"
13 #include "src/kernel/actor/SimcallObserver.hpp"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_io, s4u_activity, "S4U asynchronous I/Os");
16
17 namespace simgrid::s4u {
18
19 Io::Io(kernel::activity::IoImplPtr pimpl)
20 {
21   pimpl_ = pimpl;
22 }
23
24 IoPtr Io::init()
25 {
26   auto pimpl = kernel::activity::IoImplPtr(new kernel::activity::IoImpl());
27   return IoPtr(static_cast<Io*>(pimpl->get_iface()));
28 }
29
30 IoPtr Io::streamto_init(Host* from, const Disk* from_disk, Host* to, const Disk* to_disk)
31 {
32   auto res = Io::init()->set_source(from, from_disk)->set_destination(to, to_disk);
33   res->set_state(State::STARTING);
34   return res;
35 }
36
37 IoPtr Io::streamto_async(Host* from, const Disk* from_disk, Host* to, const Disk* to_disk,
38                          uint64_t simulated_size_in_bytes)
39 {
40   return Io::init()->set_size(simulated_size_in_bytes)->set_source(from, from_disk)->set_destination(to, to_disk);
41 }
42
43 void Io::streamto(Host* from, const Disk* from_disk, Host* to, const Disk* to_disk, uint64_t simulated_size_in_bytes)
44 {
45   streamto_async(from, from_disk, to, to_disk, simulated_size_in_bytes)->wait();
46 }
47
48 IoPtr Io::set_source(Host* from, const Disk* from_disk)
49 {
50   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
51              "Cannot change the source of an IO stream once it's started (state: %s)", to_c_str(state_));
52   kernel::actor::simcall_object_access(pimpl_.get(), [this, from, from_disk] {
53     boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_host(from);
54     if (from_disk != nullptr)
55       boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_disk(from_disk->get_impl());
56   });
57   // Setting 'source' may allow to start the activity, let's try
58   if (state_ == State::STARTING && remains_ <= 0)
59     XBT_DEBUG("This IO has a size of 0 byte. It cannot start yet");
60   else
61     start();
62
63   return this;
64 }
65
66 IoPtr Io::set_destination(Host* to, const Disk* to_disk)
67 {
68   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
69              "Cannot change the source of an IO stream once it's started (state: %s)", to_c_str(state_));
70   kernel::actor::simcall_object_access(pimpl_.get(), [this, to, to_disk] {
71     boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_dst_host(to);
72     if (to_disk != nullptr)
73       boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_dst_disk(to_disk->get_impl());
74   });
75   // Setting 'destination' may allow to start the activity, let's try
76   if (state_ == State::STARTING && remains_ <= 0)
77     XBT_DEBUG("This IO has a size of 0 byte. It cannot start yet");
78   else
79     start();
80
81   return this;
82 }
83
84 Io* Io::do_start()
85 {
86   kernel::actor::simcall_answered(
87       [this] { (*boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)).set_name(get_name()).start(); });
88
89   if (suspended_)
90     pimpl_->suspend();
91
92   state_ = State::STARTED;
93   fire_on_start();
94   fire_on_this_start();
95   return this;
96 }
97
98 ssize_t Io::deprecated_wait_any_for(const std::vector<IoPtr>& ios, double timeout) // XBT_ATTRIB_DEPRECATED_v339
99 {
100   ActivitySet set;
101   for (const auto& io : ios)
102     set.push(io);
103
104   auto* ret = set.wait_any_for(timeout).get();
105   for (size_t i = 0; i < ios.size(); i++)
106     if (ios[i].get() == ret)
107       return i;
108
109   return -1;
110 }
111
112 IoPtr Io::set_disk(const_sg_disk_t disk)
113 {
114   xbt_assert(state_ == State::INITED || state_ == State::STARTING, "Cannot set disk once the Io is started");
115
116   kernel::actor::simcall_answered(
117       [this, disk] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_disk(disk->get_impl()); });
118
119   // Setting the disk may allow to start the activity, let's try
120   if (state_ == State::STARTING)
121     start();
122
123  return this;
124 }
125
126 IoPtr Io::set_priority(double priority)
127 {
128   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
129              "Cannot change the priority of an io after its start");
130   kernel::actor::simcall_object_access(pimpl_.get(), [this, priority] {
131     boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_sharing_penalty(1. / priority);
132   });
133   return this;
134 }
135
136 IoPtr Io::set_size(sg_size_t size)
137 {
138   xbt_assert(state_ == State::INITED || state_ == State::STARTING, "Cannot set size once the Io is started");
139   kernel::actor::simcall_object_access(
140       pimpl_.get(), [this, size] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_size(size); });
141   set_remaining(size);
142   return this;
143 }
144
145 IoPtr Io::set_op_type(OpType type)
146 {
147   xbt_assert(state_ == State::INITED || state_ == State::STARTING, "Cannot set size once the Io is started");
148   kernel::actor::simcall_object_access(
149       pimpl_.get(), [this, type] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_type(type); });
150   return this;
151 }
152
153 IoPtr Io::update_priority(double priority)
154 {
155   kernel::actor::simcall_answered([this, priority] {
156     boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->update_sharing_penalty(1. / priority);
157   });
158   return this;
159 }
160
161 /** @brief Returns the amount of flops that remain to be done */
162 double Io::get_remaining() const
163 {
164   return kernel::actor::simcall_object_access(
165       pimpl_.get(), [this]() { return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_remaining(); });
166 }
167
168 sg_size_t Io::get_performed_ioops() const
169 {
170   return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_performed_ioops();
171 }
172
173 bool Io::is_assigned() const
174 {
175   if (boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_host() == nullptr) {
176     return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_disk() != nullptr;
177   } else {
178     return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_dst_host() != nullptr;
179   }
180 }
181
182 } // namespace simgrid::s4u