Logo AND Algorithmique Numérique Distribuée

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