Logo AND Algorithmique Numérique Distribuée

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