Logo AND Algorithmique Numérique Distribuée

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