Logo AND Algorithmique Numérique Distribuée

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