Logo AND Algorithmique Numérique Distribuée

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