Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Take simcalls {execution,io}_wait toward modernity.
[simgrid.git] / src / kernel / activity / IoImpl.cpp
1 /* Copyright (c) 2007-2020. 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/StorageImpl.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::set_timeout(double timeout)
23 {
24   s4u::Host* host   = get_disk() ? get_disk()->get_host() : s4u::Host::by_name(get_storage()->get_host());
25   timeout_detector_ = host->pimpl_cpu->sleep(timeout);
26   timeout_detector_->set_activity(this);
27   return *this;
28 }
29
30 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
31 {
32   type_ = type;
33   return *this;
34 }
35
36 IoImpl& IoImpl::set_size(sg_size_t size)
37 {
38   size_ = size;
39   return *this;
40 }
41
42 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
43 {
44   disk_ = disk;
45   return *this;
46 }
47
48 IoImpl& IoImpl::set_storage(resource::StorageImpl* storage)
49 {
50   storage_ = storage;
51   return *this;
52 }
53
54 IoImpl* IoImpl::start()
55 {
56   state_ = State::RUNNING;
57   if (storage_)
58     surf_action_ = storage_->io_start(size_, type_);
59   else
60     surf_action_ = disk_->io_start(size_, type_);
61   surf_action_->set_activity(this);
62
63   XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
64   IoImpl::on_start(*this);
65
66   return this;
67 }
68
69 void IoImpl::post()
70 {
71   performed_ioops_ = surf_action_->get_cost();
72   if (surf_action_->get_state() == resource::Action::State::FAILED) {
73     if ((storage_ && not storage_->is_on()) || (disk_ && not disk_->is_on()))
74       state_ = State::FAILED;
75     else
76       state_ = State::CANCELED;
77   } else if (surf_action_->get_state() == resource::Action::State::FINISHED) {
78     state_ = State::DONE;
79   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
80     state_ = State::TIMEOUT;
81   }
82
83   if (timeout_detector_) {
84     timeout_detector_->unref();
85     timeout_detector_ = nullptr;
86   }
87
88   on_completion(*this);
89
90   /* Answer all simcalls associated with the synchro */
91   finish();
92 }
93
94 void IoImpl::finish()
95 {
96   while (not simcalls_.empty()) {
97     const s_smx_simcall* simcall = simcalls_.front();
98     simcalls_.pop_front();
99     switch (state_) {
100       case State::DONE:
101         /* do nothing, synchro done */
102         break;
103       case State::FAILED:
104         simcall->issuer_->context_->set_wannadie();
105         simcall->issuer_->exception_ =
106             std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
107         break;
108       case State::CANCELED:
109         simcall->issuer_->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
110         break;
111       case State::TIMEOUT:
112         XBT_DEBUG("IoImpl::finish(): execution timeouted");
113         simcall->issuer_->exception_ = std::make_exception_ptr(simgrid::TimeoutException(XBT_THROW_POINT, "Timeouted"));
114         break;
115       default:
116         xbt_die("Internal error in IoImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
117     }
118
119     simcall->issuer_->waiting_synchro = nullptr;
120     simcall->issuer_->simcall_answer();
121   }
122 }
123
124 /*************
125  * Callbacks *
126  *************/
127 xbt::signal<void(IoImpl const&)> IoImpl::on_start;
128 xbt::signal<void(IoImpl const&)> IoImpl::on_completion;
129
130 } // namespace activity
131 } // namespace kernel
132 } // namespace simgrid