2012-09-28 10:47:21 +00:00
|
|
|
#include <fcntl.h>
|
2018-03-23 16:05:14 +00:00
|
|
|
#include <port/unistd.h>
|
2012-09-28 10:47:21 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdlib.h>
|
2016-12-24 01:03:10 +00:00
|
|
|
#if !defined(__APPLE__) && !defined(__FreeBSD__)
|
2012-09-28 10:47:21 +00:00
|
|
|
#include <malloc.h>
|
2016-10-26 22:27:38 +00:00
|
|
|
#endif
|
2012-09-28 10:47:21 +00:00
|
|
|
#include <poll.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <vector>
|
2016-10-26 22:27:38 +00:00
|
|
|
#include <random>
|
2017-09-09 23:17:38 +00:00
|
|
|
#include <pcg_random.hpp>
|
2018-09-05 09:47:57 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
2012-09-28 10:47:21 +00:00
|
|
|
#include <Poco/Exception.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
2017-09-09 23:17:38 +00:00
|
|
|
#include <Common/randomSeed.h>
|
2017-06-23 20:22:35 +00:00
|
|
|
#include <common/ThreadPool.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Stopwatch.h>
|
2018-06-19 18:09:09 +00:00
|
|
|
#include <port/clock.h>
|
2012-09-28 10:47:21 +00:00
|
|
|
|
|
|
|
using DB::throwFromErrno;
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * file_name = 0;
|
|
|
|
Mode mode = MODE_READ;
|
2018-09-05 09:47:57 +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-09-05 09:47:57 +00:00
|
|
|
min_offset = DB::parse<UInt64>(argv[3]);
|
|
|
|
max_offset = DB::parse<UInt64>(argv[4]);
|
|
|
|
block_size = DB::parse<UInt64>(argv[5]);
|
|
|
|
descriptors = DB::parse<UInt64>(argv[6]);
|
|
|
|
count = DB::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])
|
|
|
|
throwFromErrno("Cannot open file");
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
throwFromErrno("poll failed");
|
|
|
|
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;
|
|
|
|
|
|
|
|
long rand_result1 = rng();
|
|
|
|
long rand_result2 = rng();
|
|
|
|
long rand_result3 = rng();
|
|
|
|
|
|
|
|
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))
|
|
|
|
throwFromErrno("Cannot read");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (static_cast<int>(block_size) != pwrite(fds[i], &buf[0], block_size, offset))
|
|
|
|
throwFromErrno("Cannot write");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < descriptors; ++i)
|
|
|
|
{
|
|
|
|
if (fsync(fds[i]))
|
|
|
|
throwFromErrno("Cannot fsync");
|
|
|
|
}
|
|
|
|
|
|
|
|
watch.stop();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < descriptors; ++i)
|
|
|
|
{
|
|
|
|
if (0 != close(fds[i]))
|
|
|
|
throwFromErrno("Cannot close file");
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|