Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5fac5af006b6e14ce49940803de1d52f0ea5bbe9
[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(std::move(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, get_cname());
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   state_ = SIMIX_CANCELED;
47 }
48
49 double IoImpl::get_remaining()
50 {
51   return surf_action_ ? surf_action_->get_remains() : 0;
52 }
53
54 void IoImpl::post()
55 {
56   performed_ioops_ = surf_action_->get_cost();
57   switch (surf_action_->get_state()) {
58     case simgrid::kernel::resource::Action::State::FAILED:
59       state_ = SIMIX_FAILED;
60       break;
61     case simgrid::kernel::resource::Action::State::FINISHED:
62       state_ = SIMIX_DONE;
63       break;
64     default:
65       THROW_IMPOSSIBLE;
66       break;
67   }
68   on_completion(this);
69
70   SIMIX_io_finish(this);
71 }
72 /*************
73  * Callbacks *
74  *************/
75 xbt::signal<void(IoImplPtr)> IoImpl::on_start;
76 xbt::signal<void(IoImplPtr)> IoImpl::on_completion;
77
78 } // namespace activity
79 } // namespace kernel
80 } // namespace simgrid