Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
introduce s4u::Io::streamto
[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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_io, s4u_activity, "S4U asynchronous I/Os");
15
16 namespace simgrid::s4u {
17 xbt::signal<void(Io const&)> Io::on_start;
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, Disk* from_disk, Host* to, 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, Disk* from_disk, Host* to, Disk* to_disk, uint64_t simulated_size_in_bytes)
38 {
39   return Io::init()->set_size(simulated_size_in_bytes)->set_source(from, from_disk)->set_destination(to, to_disk);
40 }
41
42 void Io::streamto(Host* from, Disk* from_disk, Host* to, Disk* to_disk, uint64_t simulated_size_in_bytes)
43 {
44   streamto_async(from, from_disk, to, to_disk, simulated_size_in_bytes)->wait();
45 }
46
47 IoPtr Io::set_source(Host* from, Disk* from_disk)
48 {
49   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
50              "Cannot change the source of an IO stream once it's started (state: %s)", to_c_str(state_));
51   kernel::actor::simcall_answered(
52       [this, from, from_disk] {
53         boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_host(from);
54         boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_disk(from_disk->get_impl());
55       });
56   // Setting 'source' may allow to start the activity, let's try
57   if (state_ == State::STARTING && remains_ <= 0)
58     XBT_DEBUG("This IO has a size of 0 byte. It cannot start yet");
59   else
60     vetoable_start();
61
62   return this;
63 }
64
65 IoPtr Io::set_destination(Host* to, Disk* to_disk)
66 {
67   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
68              "Cannot change the source of an IO stream once it's started (state: %s)", to_c_str(state_));
69   kernel::actor::simcall_answered(
70       [this, to, to_disk] {
71         boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_dst_host(to);
72         boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_dst_disk(to_disk->get_impl());
73       });
74   // Setting 'destination' may allow to start the activity, let's try
75   if (state_ == State::STARTING && remains_ <= 0)
76     XBT_DEBUG("This IO has a size of 0 byte. It cannot start yet");
77   else
78     vetoable_start();
79
80   return this;
81 }
82
83 Io* Io::start()
84 {
85   kernel::actor::simcall_answered(
86       [this] { (*boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)).set_name(get_name()).start(); });
87
88   if (suspended_)
89     pimpl_->suspend();
90
91   state_ = State::STARTED;
92   on_start(*this);
93   return this;
94 }
95
96 ssize_t Io::wait_any_for(const std::vector<IoPtr>& ios, double timeout)
97 {
98   std::vector<ActivityPtr> activities;
99   for (const auto& io : ios)
100     activities.push_back(boost::dynamic_pointer_cast<Activity>(io));
101   return Activity::wait_any_for(activities, timeout);
102 }
103
104 IoPtr Io::set_disk(const_sg_disk_t disk)
105 {
106   xbt_assert(state_ == State::INITED || state_ == State::STARTING, "Cannot set disk once the Io is started");
107
108   kernel::actor::simcall_answered(
109       [this, disk] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_disk(disk->get_impl()); });
110
111   // Setting the disk may allow to start the activity, let's try
112   if (state_ == State::STARTING)
113     vetoable_start();
114
115  return this;
116 }
117
118 IoPtr Io::set_priority(double priority)
119 {
120   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
121              "Cannot change the priority of an io after its start");
122   kernel::actor::simcall_answered([this, priority] {
123     boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_sharing_penalty(1. / priority);
124   });
125   return this;
126 }
127
128 IoPtr Io::set_size(sg_size_t size)
129 {
130   xbt_assert(state_ == State::INITED || state_ == State::STARTING, "Cannot set size once the Io is started");
131   kernel::actor::simcall_answered(
132       [this, size] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_size(size); });
133   set_remaining(size);
134   return this;
135 }
136
137 IoPtr Io::set_op_type(OpType type)
138 {
139   xbt_assert(state_ == State::INITED || state_ == State::STARTING, "Cannot set size once the Io is started");
140   kernel::actor::simcall_answered(
141       [this, type] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_type(type); });
142   return this;
143 }
144
145 IoPtr Io::update_priority(double priority)
146 {
147   kernel::actor::simcall_answered([this, priority] {
148     boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->update_sharing_penalty(1. / priority);
149   });
150   return this;
151 }
152
153 /** @brief Returns the amount of flops that remain to be done */
154 double Io::get_remaining() const
155 {
156   return kernel::actor::simcall_answered(
157       [this]() { return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_remaining(); });
158 }
159
160 sg_size_t Io::get_performed_ioops() const
161 {
162   return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_performed_ioops();
163 }
164
165 bool Io::is_assigned() const
166 {
167   if (boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_host() == nullptr) {
168     return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_disk() != nullptr;
169   } else {
170     return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_dst_host() != nullptr;
171   }
172 }
173
174 } // namespace simgrid::s4u