Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do the same for IoImpl as for ExecImpl when timeout on completion
[simgrid.git] / src / kernel / activity / IoImpl.cpp
1 /* Copyright (c) 2007-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 #include "src/kernel/activity/IoImpl.hpp"
6 #include "mc/mc.h"
7 #include "simgrid/Exception.hpp"
8 #include "simgrid/kernel/resource/Action.hpp"
9 #include "simgrid/s4u/Host.hpp"
10 #include "src/kernel/resource/DiskImpl.hpp"
11 #include "src/mc/mc_replay.hpp"
12 #include "src/simix/smx_private.hpp"
13 #include "src/surf/cpu_interface.hpp"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
16
17 namespace simgrid {
18 namespace kernel {
19 namespace activity {
20
21 IoImpl& IoImpl::set_timeout(double timeout)
22 {
23   const s4u::Host* host = get_disk()->get_host();
24   timeout_detector_ = host->pimpl_cpu->sleep(timeout);
25   timeout_detector_->set_activity(this);
26   return *this;
27 }
28
29 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
30 {
31   type_ = type;
32   return *this;
33 }
34
35 IoImpl& IoImpl::set_size(sg_size_t size)
36 {
37   size_ = size;
38   return *this;
39 }
40
41 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
42 {
43   disk_ = disk;
44   return *this;
45 }
46
47 IoImpl* IoImpl::start()
48 {
49   state_ = State::RUNNING;
50   surf_action_ = disk_->io_start(size_, type_);
51   surf_action_->set_activity(this);
52
53   XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
54
55   return this;
56 }
57
58 void IoImpl::post()
59 {
60   performed_ioops_ = surf_action_->get_cost();
61   if (surf_action_->get_state() == resource::Action::State::FAILED) {
62     if (disk_ && not disk_->is_on())
63       state_ = State::FAILED;
64     else
65       state_ = State::CANCELED;
66   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
67     if (surf_action_->get_remains() > 0.0) {
68       surf_action_->set_state(resource::Action::State::FAILED);
69       state_ = State::TIMEOUT;
70     } else {
71       state_ = State::DONE;
72     }
73   } else {
74     state_ = State::DONE;
75   }
76
77   clean_action();
78   if (timeout_detector_) {
79     timeout_detector_->unref();
80     timeout_detector_ = nullptr;
81   }
82
83   /* Answer all simcalls associated with the synchro */
84   finish();
85 }
86
87 void IoImpl::finish()
88 {
89   while (not simcalls_.empty()) {
90     const s_smx_simcall* simcall = simcalls_.front();
91     simcalls_.pop_front();
92     switch (state_) {
93       case State::DONE:
94         /* do nothing, synchro done */
95         break;
96       case State::FAILED:
97         simcall->issuer_->context_->set_wannadie();
98         simcall->issuer_->exception_ =
99             std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
100         break;
101       case State::CANCELED:
102         simcall->issuer_->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
103         break;
104       case State::TIMEOUT:
105         XBT_DEBUG("IoImpl::finish(): execution timeouted");
106         simcall->issuer_->exception_ = std::make_exception_ptr(simgrid::TimeoutException(XBT_THROW_POINT, "Timeouted"));
107         break;
108       default:
109         xbt_die("Internal error in IoImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
110     }
111
112     simcall->issuer_->waiting_synchro_ = nullptr;
113     simcall->issuer_->simcall_answer();
114   }
115 }
116
117 } // namespace activity
118 } // namespace kernel
119 } // namespace simgrid