Logo AND Algorithmique Numérique Distribuée

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