Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move the actor lifecycle markers from Context to ActorImpl
[simgrid.git] / src / kernel / activity / IoImpl.cpp
1 /* Copyright (c) 2007-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/Exception.hpp>
7 #include <simgrid/kernel/routing/NetPoint.hpp>
8 #include <simgrid/s4u/Engine.hpp>
9 #include <simgrid/s4u/Host.hpp>
10
11 #include "mc/mc.h"
12 #include "src/kernel/activity/IoImpl.hpp"
13 #include "src/kernel/actor/ActorImpl.hpp"
14 #include "src/kernel/actor/SimcallObserver.hpp"
15 #include "src/kernel/resource/CpuImpl.hpp"
16 #include "src/kernel/resource/DiskImpl.hpp"
17 #include "src/mc/mc_replay.hpp"
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_io, kernel, "Kernel io-related synchronization");
20
21 namespace simgrid {
22 namespace kernel {
23 namespace activity {
24
25 IoImpl::IoImpl()
26 {
27   piface_ = new s4u::Io(this);
28   actor::ActorImpl* self = actor::ActorImpl::self();
29   if (self) {
30     set_actor(self);
31     self->activities_.emplace_back(this);
32   }
33 }
34
35 IoImpl& IoImpl::set_sharing_penalty(double sharing_penalty)
36 {
37   sharing_penalty_ = sharing_penalty;
38   return *this;
39 }
40
41 IoImpl& IoImpl::update_sharing_penalty(double sharing_penalty)
42 {
43   sharing_penalty_ = sharing_penalty;
44   surf_action_->set_sharing_penalty(sharing_penalty);
45   return *this;
46 }
47
48 IoImpl& IoImpl::set_timeout(double timeout)
49 {
50   const s4u::Host* host = get_disk()->get_host();
51   timeout_detector_     = host->get_cpu()->sleep(timeout);
52   timeout_detector_->set_activity(this);
53   return *this;
54 }
55
56 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
57 {
58   type_ = type;
59   return *this;
60 }
61
62 IoImpl& IoImpl::set_size(sg_size_t size)
63 {
64   size_ = size;
65   return *this;
66 }
67
68 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
69 {
70   disk_ = disk;
71   return *this;
72 }
73
74 IoImpl* IoImpl::start()
75 {
76   set_state(State::RUNNING);
77   surf_action_ =
78       disk_->get_host()->get_netpoint()->get_englobing_zone()->get_disk_model()->io_start(disk_, size_, type_);
79   surf_action_->set_sharing_penalty(sharing_penalty_);
80   surf_action_->set_activity(this);
81   set_start_time(surf_action_->get_start_time());
82
83   XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
84
85   return this;
86 }
87
88 void IoImpl::post()
89 {
90   performed_ioops_ = surf_action_->get_cost();
91   if (surf_action_->get_state() == resource::Action::State::FAILED) {
92     if (disk_ && not disk_->is_on())
93       set_state(State::FAILED);
94     else
95       set_state(State::CANCELED);
96   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
97     if (surf_action_->get_remains() > 0.0) {
98       surf_action_->set_state(resource::Action::State::FAILED);
99       set_state(State::TIMEOUT);
100     } else {
101       set_state(State::DONE);
102     }
103   } else {
104     set_state(State::DONE);
105   }
106
107   clean_action();
108   if (timeout_detector_) {
109     timeout_detector_->unref();
110     timeout_detector_ = nullptr;
111   }
112
113   /* Answer all simcalls associated with the synchro */
114   finish();
115 }
116 void IoImpl::set_exception(actor::ActorImpl* issuer)
117 {
118   switch (get_state()) {
119     case State::FAILED:
120       issuer->set_wannadie();
121       static_cast<s4u::Io*>(get_iface())->complete(s4u::Activity::State::FAILED);
122       issuer->exception_ = std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
123       break;
124     case State::CANCELED:
125       issuer->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
126       break;
127     case State::TIMEOUT:
128       issuer->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
129       break;
130     default:
131       xbt_assert(get_state() == State::DONE, "Internal error in IoImpl::finish(): unexpected synchro state %s",
132                  get_state_str());
133   }
134 }
135
136 void IoImpl::finish()
137 {
138   XBT_DEBUG("IoImpl::finish() in state %s", get_state_str());
139   while (not simcalls_.empty()) {
140     actor::Simcall* simcall = simcalls_.front();
141     simcalls_.pop_front();
142
143     /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
144      * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
145      * simcall */
146
147     if (simcall->call_ == actor::Simcall::Type::NONE) // FIXME: maybe a better way to handle this case
148       continue;                                       // if process handling comm is killed
149
150     handle_activity_waitany(simcall);
151
152     set_exception(simcall->issuer_);
153
154     simcall->issuer_->waiting_synchro_ = nullptr;
155     simcall->issuer_->simcall_answer();
156   }
157 }
158
159 } // namespace activity
160 } // namespace kernel
161 } // namespace simgrid