2018-08-20 19:48:15 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
2020-02-17 14:27:09 +00:00
|
|
|
#include <pcg_random.hpp>
|
2012-09-28 10:47:21 +00:00
|
|
|
#include <Poco/Exception.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/Stopwatch.h>
|
2020-02-17 14:27:09 +00:00
|
|
|
#include <Common/ThreadPool.h>
|
|
|
|
#include <Common/randomSeed.h>
|
|
|
|
|
|
|
|
#include <iomanip>
|
|
|
|
#include <iostream>
|
|
|
|
#include <random>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#if defined (OS_LINUX)
|
|
|
|
# include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
2012-09-28 10:47:21 +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_READ_FROM_FILE_DESCRIPTOR;
|
|
|
|
extern const int CANNOT_WRITE_TO_FILE_DESCRIPTOR;
|
|
|
|
extern const int CANNOT_FSYNC;
|
|
|
|
extern const int SYSTEM_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2012-09-28 10:47:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
enum Mode
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
MODE_READ,
|
|
|
|
MODE_WRITE,
|
2012-09-28 10:47:21 +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
|
|
|
Mode mode = MODE_READ;
|
2018-08-20 19:48:15 +00:00
|
|
|
UInt64 min_offset = 0;
|
|
|
|
UInt64 max_offset = 0;
|
|
|
|
UInt64 block_size = 0;
|
|
|
|
UInt64 descriptors = 0;
|
|
|
|
UInt64 count = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (argc != 8)
|
|
|
|
{
|
|
|
|
std::cerr << "Usage: " << argv[0] << " file_name r|w min_offset max_offset block_size descriptors count" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_name = argv[1];
|
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]);
|
|
|
|
descriptors = parse<UInt64>(argv[6]);
|
|
|
|
count = parse<UInt64>(argv[7]);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (!strcmp(argv[2], "r"))
|
|
|
|
mode = MODE_READ;
|
|
|
|
else if (!strcmp(argv[2], "w"))
|
|
|
|
mode = MODE_WRITE;
|
|
|
|
else
|
|
|
|
throw Poco::Exception("Invalid mode");
|
|
|
|
|
|
|
|
std::vector<int> fds(descriptors);
|
|
|
|
for (size_t i = 0; i < descriptors; ++i)
|
|
|
|
{
|
|
|
|
fds[i] = open(file_name, O_SYNC | ((mode == MODE_READ) ? O_RDONLY : O_WRONLY));
|
|
|
|
if (-1 == fds[i])
|
2018-11-21 20:56:37 +00:00
|
|
|
throwFromErrno("Cannot open file", ErrorCodes::CANNOT_OPEN_FILE);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<char> buf(block_size);
|
|
|
|
|
2017-09-09 23:17:38 +00:00
|
|
|
pcg64 rng(randomSeed());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
Stopwatch watch;
|
|
|
|
|
|
|
|
std::vector<pollfd> polls(descriptors);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < descriptors; ++i)
|
|
|
|
{
|
|
|
|
polls[i].fd = fds[i];
|
|
|
|
polls[i].events = (mode == MODE_READ) ? POLLIN : POLLOUT;
|
|
|
|
polls[i].revents = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ops = 0;
|
|
|
|
while (ops < count)
|
|
|
|
{
|
|
|
|
if (poll(&polls[0], descriptors, -1) <= 0)
|
2018-11-21 20:56:37 +00:00
|
|
|
throwFromErrno("poll failed", ErrorCodes::SYSTEM_ERROR);
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < descriptors; ++i)
|
|
|
|
{
|
|
|
|
if (!polls[i].revents)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (polls[i].revents != polls[i].events)
|
|
|
|
throw Poco::Exception("revents indicates error");
|
|
|
|
polls[i].revents = 0;
|
|
|
|
++ops;
|
|
|
|
|
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;
|
|
|
|
offset = min_offset + rand_result % ((max_offset - min_offset) / block_size) * block_size;
|
|
|
|
|
|
|
|
if (mode == MODE_READ)
|
|
|
|
{
|
|
|
|
if (static_cast<int>(block_size) != pread(fds[i], &buf[0], block_size, offset))
|
2018-11-21 20:56:37 +00:00
|
|
|
throwFromErrno("Cannot read", ErrorCodes::CANNOT_READ_FROM_FILE_DESCRIPTOR);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (static_cast<int>(block_size) != pwrite(fds[i], &buf[0], block_size, offset))
|
2018-11-21 20:56:37 +00:00
|
|
|
throwFromErrno("Cannot write", ErrorCodes::CANNOT_WRITE_TO_FILE_DESCRIPTOR);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < descriptors; ++i)
|
|
|
|
{
|
|
|
|
if (fsync(fds[i]))
|
2018-11-21 20:56:37 +00:00
|
|
|
throwFromErrno("Cannot fsync", ErrorCodes::CANNOT_FSYNC);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
watch.stop();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < descriptors; ++i)
|
|
|
|
{
|
|
|
|
if (0 != close(fds[i]))
|
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 << " ops" << " in " << watch.elapsedSeconds() << " sec."
|
|
|
|
<< ", " << count / watch.elapsedSeconds() << " ops/sec."
|
|
|
|
<< ", " << count * block_size / watch.elapsedSeconds() / 1000000 << " MB/sec."
|
|
|
|
<< std::endl;
|
|
|
|
|
|
|
|
return 0;
|
2012-09-28 10:47:21 +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-09-28 10:47:21 +00:00
|
|
|
}
|