2015-03-04 10:47:53 +00:00
|
|
|
#include <DB/IO/ReadBufferAIO.h>
|
|
|
|
#include <DB/Common/ProfileEvents.h>
|
|
|
|
#include <DB/Core/ErrorCodes.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
ReadBufferAIO::ReadBufferAIO(const std::string & filename_, size_t buffer_size_, int flags_, mode_t mode_,
|
|
|
|
char * existing_memory_)
|
|
|
|
: BufferWithOwnMemory(buffer_size_, existing_memory_, BLOCK_SIZE),
|
|
|
|
fill_buffer(BufferWithOwnMemory(buffer_size_, existing_memory_, BLOCK_SIZE)),
|
|
|
|
filename(filename_)
|
|
|
|
{
|
|
|
|
ProfileEvents::increment(ProfileEvents::FileOpen);
|
|
|
|
|
|
|
|
int open_flags = ((flags_ == -1) ? O_RDONLY : flags_);
|
|
|
|
open_flags |= O_DIRECT;
|
|
|
|
|
2015-03-05 08:18:16 +00:00
|
|
|
fd = ::open(filename_.c_str(), open_flags, mode_);
|
2015-03-04 10:47:53 +00:00
|
|
|
if (fd == -1)
|
|
|
|
throwFromErrno("Cannot open file " + filename_, errno == ENOENT ? ErrorCodes::FILE_DOESNT_EXIST : ErrorCodes::CANNOT_OPEN_FILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadBufferAIO::~ReadBufferAIO()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2015-03-05 08:18:16 +00:00
|
|
|
(void) waitForCompletion();
|
2015-03-04 10:47:53 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
|
2015-03-05 08:18:16 +00:00
|
|
|
::close(fd);
|
2015-03-04 10:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ReadBufferAIO::getFileName() const
|
|
|
|
{
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReadBufferAIO::nextImpl()
|
|
|
|
{
|
2015-03-05 08:18:16 +00:00
|
|
|
if (!waitForCompletion())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
swapBuffers();
|
2015-03-04 10:47:53 +00:00
|
|
|
|
|
|
|
// Create request.
|
2015-03-05 08:18:16 +00:00
|
|
|
::memset(&cb, 0, sizeof(cb));
|
2015-03-04 10:47:53 +00:00
|
|
|
|
|
|
|
cb.aio_lio_opcode = IOCB_CMD_PREAD;
|
|
|
|
cb.aio_fildes = fd;
|
2015-03-05 08:18:16 +00:00
|
|
|
cb.aio_buf = reinterpret_cast<UInt64>(fill_buffer.internalBuffer().begin());
|
|
|
|
cb.aio_nbytes = fill_buffer.internalBuffer().size();
|
2015-03-04 10:47:53 +00:00
|
|
|
cb.aio_offset = 0;
|
|
|
|
cb.aio_reqprio = 0;
|
|
|
|
|
|
|
|
// Submit request.
|
|
|
|
while (io_submit(aio_context.ctx, request_ptrs.size(), &request_ptrs[0]) < 0)
|
|
|
|
if (errno != EINTR)
|
|
|
|
throw Exception("Cannot submit request for asynchronous IO", ErrorCodes::AIO_SUBMIT_ERROR);
|
|
|
|
|
2015-03-05 08:18:16 +00:00
|
|
|
asm volatile("" ::: "memory");
|
2015-03-04 10:47:53 +00:00
|
|
|
is_pending_read = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-05 08:18:16 +00:00
|
|
|
bool ReadBufferAIO::waitForCompletion()
|
2015-03-04 10:47:53 +00:00
|
|
|
{
|
|
|
|
if (is_pending_read)
|
|
|
|
{
|
2015-03-05 08:18:16 +00:00
|
|
|
is_pending_read = false;
|
|
|
|
asm volatile("" ::: "memory");
|
|
|
|
|
|
|
|
::memset(&events[0], 0, sizeof(events[0]) * events.size());
|
2015-03-04 10:47:53 +00:00
|
|
|
|
|
|
|
while (io_getevents(aio_context.ctx, events.size(), events.size(), &events[0], nullptr) < 0)
|
|
|
|
if (errno != EINTR)
|
|
|
|
throw Exception("Failed to wait for asynchronous IO completion", ErrorCodes::AIO_COMPLETION_ERROR);
|
|
|
|
|
2015-03-05 08:18:16 +00:00
|
|
|
size_t bytes_read = events[0].res;
|
|
|
|
fill_buffer.buffer().resize(bytes_read);
|
|
|
|
|
|
|
|
return bytes_read == fill_buffer.internalBuffer().size();
|
2015-03-04 10:47:53 +00:00
|
|
|
}
|
2015-03-05 08:18:16 +00:00
|
|
|
else
|
|
|
|
return true;
|
2015-03-04 10:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReadBufferAIO::swapBuffers()
|
|
|
|
{
|
2015-03-05 08:18:16 +00:00
|
|
|
internalBuffer().swap(fill_buffer.internalBuffer());
|
|
|
|
buffer().swap(fill_buffer.buffer());
|
|
|
|
std::swap(position(), fill_buffer.position());
|
2015-03-04 10:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|