Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change the way IO activities are initiated
[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/Actor.hpp"
7 #include "simgrid/s4u/Disk.hpp"
8 #include "simgrid/s4u/Io.hpp"
9 #include "simgrid/s4u/Storage.hpp"
10 #include "src/kernel/activity/IoImpl.hpp"
11 #include "xbt/log.h"
12
13 namespace simgrid {
14 namespace s4u {
15 xbt::signal<void(Io const&)> Io::on_start;
16 xbt::signal<void(Io const&)> Io::on_completion;
17
18 Io::Io()
19 {
20   pimpl_ = kernel::activity::IoImplPtr(new kernel::activity::IoImpl());
21 }
22
23 IoPtr Io::init()
24 {
25  return IoPtr(new Io());
26 }
27
28 Io* Io::start()
29 {
30   kernel::actor::simcall([this] {
31     if (storage_) {
32       (*boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_))
33           .set_name(get_name())
34           .set_storage(storage_->get_impl())
35           .set_size(size_)
36           .set_type(type_)
37           .start();
38     } else {
39       (*boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_))
40           .set_name(get_name())
41           .set_disk(disk_->get_impl())
42           .set_size(size_)
43           .set_type(type_)
44           .start();
45     }
46   });
47
48   if (suspended_)
49     pimpl_->suspend();
50
51   state_ = State::STARTED;
52   on_start(*this);
53   return this;
54 }
55
56 Io* Io::cancel()
57 {
58   simgrid::kernel::actor::simcall([this] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->cancel(); });
59   state_ = State::CANCELED;
60   return this;
61 }
62
63 Io* Io::wait()
64 {
65   return this->wait_for(-1);
66 }
67
68 Io* Io::wait_for(double timeout)
69 {
70   if (state_ == State::INITED)
71     vetoable_start();
72
73   kernel::actor::ActorImpl* issuer = Actor::self()->get_impl();
74   kernel::actor::simcall_blocking<void>([this, issuer, timeout] { this->get_impl()->wait_for(issuer, timeout); });
75   state_ = State::FINISHED;
76   this->release_dependencies();
77
78   on_completion(*this);
79   return this;
80 }
81
82 IoPtr Io::set_disk(sg_disk_t disk)
83 {
84   disk_ = disk;
85   return this;
86 }
87
88 IoPtr Io::set_storage(sg_storage_t storage)
89 {
90   storage_ = storage;
91   return this;
92 }
93
94 IoPtr Io::set_size(sg_size_t size)
95 {
96   size_ = size;
97   Activity::set_remaining(size_);
98   return this;
99 }
100
101 IoPtr Io::set_op_type(OpType type)
102 {
103   type_ = type;
104   return this;
105 }
106
107 /** @brief Returns the amount of flops that remain to be done */
108 double Io::get_remaining() const
109 {
110   return kernel::actor::simcall(
111       [this]() { return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_remaining(); });
112 }
113
114 sg_size_t Io::get_performed_ioops() const
115 {
116   return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_performed_ioops();
117 }
118
119 } // namespace s4u
120 } // namespace simgrid