2020-05-11 00:44:09 +00:00
|
|
|
#if !defined(OS_LINUX)
|
2019-01-13 18:51:57 +00:00
|
|
|
int main(int, char **) { return 0; }
|
2018-06-19 18:09:09 +00:00
|
|
|
#else
|
|
|
|
|
2012-10-04 08:09:07 +00:00
|
|
|
#include <fcntl.h>
|
2020-02-17 14:27:09 +00:00
|
|
|
#include <unistd.h>
|
2012-10-04 08:09:07 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <vector>
|
|
|
|
#include <Poco/Exception.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
2019-01-13 18:51:57 +00:00
|
|
|
#include <Common/ThreadPool.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Stopwatch.h>
|
2020-05-18 01:19:50 +00:00
|
|
|
#include <Common/randomSeed.h>
|
2020-12-16 21:23:41 +00:00
|
|
|
#include <common/getPageSize.h>
|
2020-05-18 01:19:50 +00:00
|
|
|
#include <pcg_random.hpp>
|
2017-09-07 21:04:48 +00:00
|
|
|
#include <IO/BufferWithOwnMemory.h>
|
2018-08-20 19:48:15 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
2012-10-04 08:09:07 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2018-06-18 02:19:42 +00:00
|
|
|
#include <IO/AIO.h>
|
2019-01-13 18:51:57 +00:00
|
|
|
#include <malloc.h>
|
2012-10-04 08:09:07 +00:00
|
|
|
#include <sys/syscall.h>
|
|
|
|
|
2018-03-23 16:05:14 +00:00
|
|
|
|
2018-11-21 20:56:37 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_OPEN_FILE;
|
|
|
|
extern const int CANNOT_CLOSE_FILE;
|
|
|
|
extern const int CANNOT_IO_SUBMIT;
|
|
|
|
extern const int CANNOT_IO_GETEVENTS;
|
|
|
|
}
|
|
|
|
}
|
2012-10-04 08:09:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
enum Mode
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
MODE_READ = 1,
|
|
|
|
MODE_WRITE = 2,
|
2012-10-04 08:09:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-08-02 01:46:05 +00:00
|
|
|
void thread(int fd, int mode, size_t min_offset, size_t max_offset, size_t block_size, size_t buffers_count, size_t count)
|
2012-10-04 08:09:07 +00:00
|
|
|
{
|
2018-11-21 20:56:37 +00:00
|
|
|
using namespace DB;
|
|
|
|
|
2018-06-18 02:19:42 +00:00
|
|
|
AIOContext ctx;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-04-06 15:27:39 +00:00
|
|
|
std::vector<Memory<>> buffers(buffers_count);
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < buffers_count; ++i)
|
2020-10-29 19:52:12 +00:00
|
|
|
buffers[i] = Memory<>(block_size, ::getPageSize());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-05-18 01:19:50 +00:00
|
|
|
pcg64_fast rng(randomSeed());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
size_t in_progress = 0;
|
|
|
|
size_t blocks_sent = 0;
|
|
|
|
std::vector<bool> buffer_used(buffers_count, false);
|
|
|
|
std::vector<iocb> iocbs(buffers_count);
|
|
|
|
std::vector<iocb*> query_cbs;
|
|
|
|
std::vector<io_event> events(buffers_count);
|
|
|
|
|
|
|
|
while (blocks_sent < count || in_progress > 0)
|
|
|
|
{
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Prepare queries.
|
2017-04-01 07:20:54 +00:00
|
|
|
query_cbs.clear();
|
|
|
|
for (size_t i = 0; i < buffers_count; ++i)
|
|
|
|
{
|
|
|
|
if (blocks_sent >= count || in_progress >= buffers_count)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (buffer_used[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
buffer_used[i] = true;
|
|
|
|
++blocks_sent;
|
|
|
|
++in_progress;
|
|
|
|
|
2017-09-07 21:19:43 +00:00
|
|
|
char * buf = buffers[i].data();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-05-18 01:19:50 +00:00
|
|
|
uint64_t rand_result1 = rng();
|
|
|
|
uint64_t rand_result2 = rng();
|
|
|
|
uint64_t rand_result3 = rng();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
size_t rand_result = rand_result1 ^ (rand_result2 << 22) ^ (rand_result3 << 43);
|
|
|
|
size_t offset = min_offset + rand_result % ((max_offset - min_offset) / block_size) * block_size;
|
|
|
|
|
|
|
|
iocb & cb = iocbs[i];
|
|
|
|
memset(&cb, 0, sizeof(cb));
|
|
|
|
cb.aio_buf = reinterpret_cast<UInt64>(buf);
|
|
|
|
cb.aio_fildes = fd;
|
|
|
|
cb.aio_nbytes = block_size;
|
|
|
|
cb.aio_offset = offset;
|
|
|
|
cb.aio_data = static_cast<UInt64>(i);
|
|
|
|
|
|
|
|
if (mode == MODE_READ)
|
|
|
|
{
|
|
|
|
cb.aio_lio_opcode = IOCB_CMD_PREAD;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cb.aio_lio_opcode = IOCB_CMD_PWRITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
query_cbs.push_back(&cb);
|
|
|
|
}
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Send queries.
|
2017-04-01 07:20:54 +00:00
|
|
|
if (io_submit(ctx.ctx, query_cbs.size(), &query_cbs[0]) < 0)
|
2018-11-21 20:56:37 +00:00
|
|
|
throwFromErrno("io_submit failed", ErrorCodes::CANNOT_IO_SUBMIT);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Receive answers. If we have something else to send, then receive at least one answer (after that send them), otherwise wait all answers.
|
2017-04-01 07:20:54 +00:00
|
|
|
memset(&events[0], 0, buffers_count * sizeof(events[0]));
|
|
|
|
int evs = io_getevents(ctx.ctx, (blocks_sent < count ? 1 : in_progress), buffers_count, &events[0], nullptr);
|
|
|
|
if (evs < 0)
|
2018-11-21 20:56:37 +00:00
|
|
|
throwFromErrno("io_getevents failed", ErrorCodes::CANNOT_IO_GETEVENTS);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < evs; ++i)
|
|
|
|
{
|
|
|
|
int b = static_cast<int>(events[i].data);
|
|
|
|
if (events[i].res != static_cast<int>(block_size))
|
|
|
|
throw Poco::Exception("read/write error");
|
|
|
|
--in_progress;
|
|
|
|
buffer_used[b] = false;
|
|
|
|
}
|
|
|
|
}
|
2012-10-04 08:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int mainImpl(int argc, char ** argv)
|
|
|
|
{
|
2018-11-21 20:56:37 +00:00
|
|
|
using namespace DB;
|
|
|
|
|
2020-05-09 22:59:34 +00:00
|
|
|
const char * file_name = nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
int mode = MODE_READ;
|
2018-08-20 19:48:15 +00:00
|
|
|
UInt64 min_offset = 0;
|
|
|
|
UInt64 max_offset = 0;
|
|
|
|
UInt64 block_size = 0;
|
|
|
|
UInt64 buffers_count = 0;
|
|
|
|
UInt64 threads_count = 0;
|
|
|
|
UInt64 count = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (argc != 9)
|
|
|
|
{
|
|
|
|
std::cerr << "Usage: " << argv[0] << " file_name r|w min_offset max_offset block_size threads buffers count" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_name = argv[1];
|
|
|
|
if (argv[2][0] == 'w')
|
|
|
|
mode = MODE_WRITE;
|
2018-11-21 20:56:37 +00:00
|
|
|
min_offset = parse<UInt64>(argv[3]);
|
|
|
|
max_offset = parse<UInt64>(argv[4]);
|
|
|
|
block_size = parse<UInt64>(argv[5]);
|
|
|
|
threads_count = parse<UInt64>(argv[6]);
|
|
|
|
buffers_count = parse<UInt64>(argv[7]);
|
|
|
|
count = parse<UInt64>(argv[8]);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
int fd = open(file_name, ((mode == MODE_READ) ? O_RDONLY : O_WRONLY) | O_DIRECT);
|
|
|
|
if (-1 == fd)
|
2018-11-21 20:56:37 +00:00
|
|
|
throwFromErrno("Cannot open file", ErrorCodes::CANNOT_OPEN_FILE);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
ThreadPool pool(threads_count);
|
|
|
|
|
|
|
|
Stopwatch watch;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < threads_count; ++i)
|
2020-05-18 01:19:50 +00:00
|
|
|
pool.scheduleOrThrowOnError([=]{ thread(fd, mode, min_offset, max_offset, block_size, buffers_count, count); });
|
2017-04-01 07:20:54 +00:00
|
|
|
pool.wait();
|
|
|
|
|
|
|
|
watch.stop();
|
|
|
|
|
|
|
|
if (0 != close(fd))
|
2018-11-21 20:56:37 +00:00
|
|
|
throwFromErrno("Cannot close file", ErrorCodes::CANNOT_CLOSE_FILE);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
std::cout << std::fixed << std::setprecision(2)
|
|
|
|
<< "Done " << count << " * " << threads_count << " ops";
|
|
|
|
std::cout << " in " << watch.elapsedSeconds() << " sec."
|
|
|
|
<< ", " << count * threads_count / watch.elapsedSeconds() << " ops/sec."
|
|
|
|
<< ", " << count * threads_count * block_size / watch.elapsedSeconds() / 1000000 << " MB/sec."
|
|
|
|
<< std::endl;
|
|
|
|
|
|
|
|
return 0;
|
2012-10-04 08:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char ** argv)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return mainImpl(argc, argv);
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception & e)
|
|
|
|
{
|
|
|
|
std::cerr << e.what() << ", " << e.message() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2012-10-04 08:09:07 +00:00
|
|
|
}
|
2018-06-19 18:09:09 +00:00
|
|
|
#endif
|