Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2d21d2793c1d06b656aaeefe4462edd562e57d7e
[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
6 #include "src/kernel/activity/IoImpl.hpp"
7 #include "simgrid/kernel/resource/Action.hpp"
8 #include "src/simix/smx_io_private.hpp"
9 #include "src/surf/StorageImpl.hpp"
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_io);
12 namespace simgrid {
13 namespace kernel {
14 namespace activity {
15
16 IoImpl::IoImpl(std::string name, surf::StorageImpl* storage) : ActivityImpl(name), storage_(storage)
17 {
18   this->state_ = SIMIX_RUNNING;
19
20   XBT_DEBUG("Create io impl %p", this);
21 }
22
23 IoImpl::~IoImpl()
24 {
25   if (surf_action_ != nullptr)
26     surf_action_->unref();
27   XBT_DEBUG("Destroy io %p", this);
28 }
29
30 IoImpl* IoImpl::start(sg_size_t size, simgrid::s4u::Io::OpType type)
31 {
32   surf_action_ = storage_->io_start(size, type);
33   surf_action_->set_data(this);
34
35   XBT_DEBUG("Create IO synchro %p %s", this, name_.c_str());
36   simgrid::kernel::activity::IoImpl::on_start(this);
37
38   return this;
39 }
40
41 void IoImpl::cancel()
42 {
43   XBT_VERB("This exec %p is canceled", this);
44   if (surf_action_ != nullptr)
45     surf_action_->cancel();
46 }
47
48 double IoImpl::get_remaining()
49 {
50   return surf_action_ ? surf_action_->get_remains() : 0;
51 }
52
53 void IoImpl::post()
54 {
55   performed_ioops_ = surf_action_->get_cost();
56   switch (surf_action_->get_state()) {
57     case simgrid::kernel::resource::Action::State::FAILED:
58       state_ = SIMIX_FAILED;
59       break;
60     case simgrid::kernel::resource::Action::State::FINISHED:
61       state_ = SIMIX_DONE;
62       break;
63     default:
64       THROW_IMPOSSIBLE;
65       break;
66   }
67   on_completion(this);
68
69   SIMIX_io_finish(this);
70 }
71 /*************
72  * Callbacks *
73  *************/
74 xbt::signal<void(IoImplPtr)> IoImpl::on_start;
75 xbt::signal<void(IoImplPtr)> IoImpl::on_completion;
76
77 } // namespace activity
78 } // namespace kernel
79 } // namespace simgrid