Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cosmetics: adjust method names in debug messages.
[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, smx_activity_t synchro)
17 {
18   XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro.get(), (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     boost::static_pointer_cast<simgrid::kernel::activity::IoImpl>(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     boost::static_pointer_cast<simgrid::kernel::activity::IoImpl>(synchro)->finish();
34 }
35
36 namespace simgrid {
37 namespace kernel {
38 namespace activity {
39
40 IoImpl::IoImpl(std::string name, surf::StorageImpl* storage) : ActivityImpl(std::move(name)), storage_(storage)
41 {
42   this->state_ = SIMIX_RUNNING;
43
44   XBT_DEBUG("Create io impl %p", this);
45 }
46
47 IoImpl::~IoImpl()
48 {
49   if (surf_action_ != nullptr)
50     surf_action_->unref();
51   XBT_DEBUG("Destroy io %p", this);
52 }
53
54 IoImpl* IoImpl::start(sg_size_t size, simgrid::s4u::Io::OpType type)
55 {
56   surf_action_ = storage_->io_start(size, type);
57   surf_action_->set_data(this);
58
59   XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
60   simgrid::kernel::activity::IoImpl::on_start(this);
61
62   return this;
63 }
64
65 void IoImpl::cancel()
66 {
67   XBT_VERB("This exec %p is canceled", this);
68   if (surf_action_ != nullptr)
69     surf_action_->cancel();
70   state_ = SIMIX_CANCELED;
71 }
72
73 double IoImpl::get_remaining()
74 {
75   return surf_action_ ? surf_action_->get_remains() : 0;
76 }
77
78 void IoImpl::post()
79 {
80   performed_ioops_ = surf_action_->get_cost();
81   switch (surf_action_->get_state()) {
82     case simgrid::kernel::resource::Action::State::FAILED:
83       state_ = SIMIX_FAILED;
84       break;
85     case simgrid::kernel::resource::Action::State::FINISHED:
86       state_ = SIMIX_DONE;
87       break;
88     default:
89       THROW_IMPOSSIBLE;
90       break;
91   }
92   on_completion(this);
93
94   finish();
95 }
96
97 void IoImpl::finish()
98 {
99   for (smx_simcall_t const& simcall : simcalls_) {
100     switch (state_) {
101       case SIMIX_DONE:
102         /* do nothing, synchro done */
103         break;
104       case SIMIX_FAILED:
105         simcall->issuer->exception_ =
106             std::make_exception_ptr(simgrid::StorageFailureException(XBT_THROW_POINT, "Storage failed"));
107         break;
108       case SIMIX_CANCELED:
109         simcall->issuer->exception_ =
110             std::make_exception_ptr(simgrid::CancelException(XBT_THROW_POINT, "I/O Canceled"));
111         break;
112       default:
113         xbt_die("Internal error in IoImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
114     }
115
116     simcall->issuer->waiting_synchro = nullptr;
117     if (simcall->issuer->get_host()->is_on())
118       SIMIX_simcall_answer(simcall);
119     else
120       simcall->issuer->context_->iwannadie = true;
121   }
122 }
123
124 /*************
125  * Callbacks *
126  *************/
127 xbt::signal<void(IoImplPtr)> IoImpl::on_start;
128 xbt::signal<void(IoImplPtr)> IoImpl::on_completion;
129
130 } // namespace activity
131 } // namespace kernel
132 } // namespace simgrid