Logo AND Algorithmique Numérique Distribuée

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