Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
783616f9b81d8ee018c2957e8868d2e6f4b754b5
[simgrid.git] / src / kernel / activity / IoImpl.cpp
1 /* Copyright (c) 2007-2019. 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
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
16
17 void simcall_HANDLER_io_wait(smx_simcall_t simcall, simgrid::kernel::activity::IoImpl* synchro)
18 {
19   XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro, (int)synchro->state_);
20
21   /* Associate this simcall to the synchro */
22   synchro->register_simcall(simcall);
23
24   if (MC_is_active() || MC_record_replay_is_active())
25     synchro->state_ = simgrid::kernel::activity::State::DONE;
26
27   /* If the synchro is already finished then perform the error handling */
28   if (synchro->state_ != simgrid::kernel::activity::State::RUNNING)
29     synchro->finish();
30 }
31
32 namespace simgrid {
33 namespace kernel {
34 namespace activity {
35
36 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
37 {
38   type_ = type;
39   return *this;
40 }
41
42 IoImpl& IoImpl::set_size(sg_size_t size)
43 {
44   size_ = size;
45   return *this;
46 }
47
48 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
49 {
50   disk_ = disk;
51   return *this;
52 }
53
54 IoImpl& IoImpl::set_storage(resource::StorageImpl* storage)
55 {
56   storage_ = storage;
57   return *this;
58 }
59
60 IoImpl* IoImpl::start()
61 {
62   state_ = State::RUNNING;
63   if (storage_)
64     surf_action_ = storage_->io_start(size_, type_);
65   else
66     surf_action_ = disk_->io_start(size_, type_);
67   surf_action_->set_activity(this);
68
69   XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
70   IoImpl::on_start(*this);
71
72   return this;
73 }
74
75 void IoImpl::post()
76 {
77   performed_ioops_ = surf_action_->get_cost();
78   if (surf_action_->get_state() == resource::Action::State::FAILED) {
79     if ((storage_ && not storage_->is_on()) || (disk_ && not disk_->is_on()))
80       state_ = State::FAILED;
81     else
82       state_ = State::CANCELED;
83   } else if (surf_action_->get_state() == resource::Action::State::FINISHED) {
84     state_ = State::DONE;
85   }
86   on_completion(*this);
87
88   /* Answer all simcalls associated with the synchro */
89   finish();
90 }
91
92 void IoImpl::finish()
93 {
94   while (not simcalls_.empty()) {
95     smx_simcall_t simcall = simcalls_.front();
96     simcalls_.pop_front();
97     switch (state_) {
98       case State::DONE:
99         /* do nothing, synchro done */
100         break;
101       case State::FAILED:
102         simcall->issuer_->context_->iwannadie = true;
103         simcall->issuer_->exception_ =
104             std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
105         break;
106       case State::CANCELED:
107         simcall->issuer_->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
108         break;
109       default:
110         xbt_die("Internal error in IoImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
111     }
112
113     simcall->issuer_->waiting_synchro = nullptr;
114     simcall->issuer_->simcall_answer();
115   }
116 }
117
118 /*************
119  * Callbacks *
120  *************/
121 xbt::signal<void(IoImpl const&)> IoImpl::on_start;
122 xbt::signal<void(IoImpl const&)> IoImpl::on_completion;
123
124 } // namespace activity
125 } // namespace kernel
126 } // namespace simgrid