Thrill  0.1
linuxaio_request.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * foxxll/io/linuxaio_request.cpp
3  *
4  * Part of FOXXLL. See http://foxxll.org
5  *
6  * Copyright (C) 2011 Johannes Singler <[email protected]>
7  * Copyright (C) 2014 Timo Bingmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
15 
16 #if FOXXLL_HAVE_LINUXAIO_FILE
17 
18 #include <sys/syscall.h>
19 #include <unistd.h>
20 
23 
24 namespace foxxll {
25 
26 void linuxaio_request::completed(bool posted, bool canceled)
27 {
28  TLX_LOG << "linuxaio_request[" << this << "] completed(" <<
29  posted << "," << canceled << ")";
30 
31  auto* stats = file_->get_file_stats();
32  const double duration = timestamp() - time_posted_;
33 
34  if (!canceled)
35  {
36  if (op_ == READ) {
37  stats->read_op_finished(bytes_, duration);
38  }
39  else {
40  stats->write_op_finished(bytes_, duration);
41  }
42  }
43  else if (posted)
44  {
45  if (op_ == READ)
46  stats->read_canceled(bytes_);
47  else
48  stats->write_canceled(bytes_);
49  }
50 
52 }
53 
54 iocb* linuxaio_request::fill_control_block()
55 {
56  linuxaio_file* af = dynamic_cast<linuxaio_file*>(file_);
57 
58  // increment, I/O system retains a virtual counting_ptr reference
59  ReferenceCounter::inc_reference();
60 
61  memset(&cb_, 0, sizeof(cb_));
62  cb_.aio_data = reinterpret_cast<__u64>(this);
63  cb_.aio_fildes = af->file_des_;
64  cb_.aio_lio_opcode = (op_ == READ) ? IOCB_CMD_PREAD : IOCB_CMD_PWRITE;
65  cb_.aio_reqprio = 0;
66  cb_.aio_buf = static_cast<__u64>(reinterpret_cast<unsigned long>(buffer_));
67  cb_.aio_nbytes = bytes_;
68  cb_.aio_offset = offset_;
69 
70  // io_submit might considerable time, so we have to remember the current
71  // time before the call.
72  time_posted_ = timestamp();
73 
74  return &cb_;
75 }
76 
77 //! Cancel the request
78 //!
79 //! Routine is called by user, as part of the request interface.
80 bool linuxaio_request::cancel()
81 {
82  TLX_LOG << "linuxaio_request[" << this << "] cancel()";
83 
84  if (!file_) return false;
85 
86  request_ptr req(this);
87  linuxaio_queue* queue = dynamic_cast<linuxaio_queue*>(
88  disk_queues::get_instance()->get_queue(file_->get_queue_id()));
89  return queue->cancel_request(req);
90 }
91 
92 //! Cancel already posted request
93 bool linuxaio_request::cancel_aio(linuxaio_queue* queue)
94 {
95  TLX_LOG << "linuxaio_request[" << this << "] cancel_aio()";
96 
97  if (!file_) return false;
98 
99  io_event event;
100  long result = syscall(SYS_io_cancel, queue->get_io_context(), &cb_, &event);
101  if (result == 0) {
102  // successfully canceled
103  queue->handle_events(&event, 1, true);
104  }
105  return result == 0;
106 }
107 
108 } // namespace foxxll
109 
110 #endif // #if FOXXLL_HAVE_LINUXAIO_FILE
111 
112 /**************************************************************************/
tlx::counting_ptr< request > request_ptr
A reference counting pointer for request.
Definition: request.hpp:43
static double timestamp()
Returns number of seconds since the epoch, high resolution.
Definition: timer.hpp:36
void completed(bool canceled) override
FOXXLL library namespace
static instance_pointer get_instance()
return instance or create base instance if empty
Definition: singleton.hpp:41
request_queue * get_queue(disk_id_type disk)
#define TLX_LOG
Default logging method: output if the local debug variable is true.
Definition: core.hpp:141