2012-01-16 19:14:36 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2012-01-16 19:24:10 +00:00
|
|
|
#include <time.h>
|
2012-01-16 19:14:36 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <vector>
|
2016-10-26 22:27:38 +00:00
|
|
|
#include <random>
|
2012-01-16 19:14:36 +00:00
|
|
|
|
|
|
|
#include <Poco/NumberParser.h>
|
|
|
|
#include <Poco/NumberFormatter.h>
|
|
|
|
#include <Poco/Exception.h>
|
|
|
|
|
2015-10-05 01:35:28 +00:00
|
|
|
#include <DB/Common/Exception.h>
|
2012-05-14 20:37:10 +00:00
|
|
|
|
2016-08-02 01:46:05 +00:00
|
|
|
#include <DB/Common/ThreadPool.h>
|
2015-10-05 00:44:40 +00:00
|
|
|
#include <DB/Common/Stopwatch.h>
|
2012-01-16 19:14:36 +00:00
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
#include <cstdlib>
|
2012-09-28 10:42:41 +00:00
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <common/apple_rt.h>
|
|
|
|
#endif
|
2012-01-16 19:14:36 +00:00
|
|
|
|
2012-05-14 20:37:10 +00:00
|
|
|
using DB::throwFromErrno;
|
|
|
|
|
|
|
|
|
2012-01-16 19:14:36 +00:00
|
|
|
enum Mode
|
|
|
|
{
|
2012-09-28 10:42:41 +00:00
|
|
|
MODE_NONE = 0,
|
|
|
|
MODE_READ = 1,
|
|
|
|
MODE_WRITE = 2,
|
|
|
|
MODE_ALIGNED = 4,
|
|
|
|
MODE_DIRECT = 8,
|
|
|
|
MODE_SYNC = 16,
|
2012-01-16 19:14:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-09-28 10:42:41 +00:00
|
|
|
struct AlignedBuffer
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
char * data;
|
2014-10-16 01:21:03 +00:00
|
|
|
|
2012-09-28 10:42:41 +00:00
|
|
|
AlignedBuffer(int size_)
|
|
|
|
{
|
2012-10-03 10:11:12 +00:00
|
|
|
size_t page = sysconf(_SC_PAGESIZE);
|
2012-09-28 10:42:41 +00:00
|
|
|
size = size_;
|
2016-11-02 09:29:39 +00:00
|
|
|
int rc = posix_memalign(reinterpret_cast<void **>(&data), page, (size + page - 1) / page * page);
|
|
|
|
if (data == nullptr || rc != 0)
|
2012-09-28 10:42:41 +00:00
|
|
|
throwFromErrno("memalign failed");
|
|
|
|
}
|
2014-10-16 01:21:03 +00:00
|
|
|
|
2012-09-28 10:42:41 +00:00
|
|
|
~AlignedBuffer()
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 count)
|
2012-01-16 19:14:36 +00:00
|
|
|
{
|
2016-08-02 01:46:05 +00:00
|
|
|
AlignedBuffer direct_buf(block_size);
|
|
|
|
std::vector<char> simple_buf(block_size);
|
2014-10-16 01:21:03 +00:00
|
|
|
|
2016-08-02 01:46:05 +00:00
|
|
|
char * buf;
|
|
|
|
if ((mode & MODE_DIRECT))
|
|
|
|
buf = direct_buf.data;
|
|
|
|
else
|
|
|
|
buf = &simple_buf[0];
|
2014-10-16 01:21:03 +00:00
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
std::mt19937 rng;
|
2012-01-16 19:14:36 +00:00
|
|
|
|
2016-08-02 01:46:05 +00:00
|
|
|
timespec times;
|
|
|
|
clock_gettime(CLOCK_THREAD_CPUTIME_ID, ×);
|
2016-10-26 22:27:38 +00:00
|
|
|
rng.seed(times.tv_nsec);
|
2012-01-16 19:24:10 +00:00
|
|
|
|
2016-08-02 01:46:05 +00:00
|
|
|
for (size_t i = 0; i < count; ++i)
|
|
|
|
{
|
2016-10-26 22:27:38 +00:00
|
|
|
long rand_result1 = rng();
|
|
|
|
long rand_result2 = rng();
|
|
|
|
long rand_result3 = rng();
|
2016-08-02 01:46:05 +00:00
|
|
|
|
|
|
|
size_t rand_result = rand_result1 ^ (rand_result2 << 22) ^ (rand_result3 << 43);
|
|
|
|
size_t offset;
|
|
|
|
if ((mode & MODE_DIRECT) || (mode & MODE_ALIGNED))
|
|
|
|
offset = min_offset + rand_result % ((max_offset - min_offset) / block_size) * block_size;
|
|
|
|
else
|
|
|
|
offset = min_offset + rand_result % (max_offset - min_offset - block_size + 1);
|
|
|
|
|
|
|
|
if (mode & MODE_READ)
|
2012-01-16 19:14:36 +00:00
|
|
|
{
|
2016-08-02 01:46:05 +00:00
|
|
|
if (static_cast<int>(block_size) != pread(fd, buf, block_size, offset))
|
|
|
|
throwFromErrno("Cannot read");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (static_cast<int>(block_size) != pwrite(fd, buf, block_size, offset))
|
|
|
|
throwFromErrno("Cannot write");
|
2012-01-16 19:14:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int mainImpl(int argc, char ** argv)
|
|
|
|
{
|
|
|
|
const char * file_name = 0;
|
2012-09-28 10:42:41 +00:00
|
|
|
int mode = MODE_NONE;
|
2012-01-16 19:14:36 +00:00
|
|
|
size_t min_offset = 0;
|
|
|
|
size_t max_offset = 0;
|
|
|
|
size_t block_size = 0;
|
|
|
|
size_t threads = 0;
|
|
|
|
size_t count = 0;
|
|
|
|
|
|
|
|
if (argc != 8)
|
|
|
|
{
|
2012-09-28 10:42:41 +00:00
|
|
|
std::cerr << "Usage: " << argv[0] << " file_name (r|w)[a][d][s] min_offset max_offset block_size threads count" << std::endl <<
|
|
|
|
"a - aligned, d - direct, s - sync" << std::endl;
|
2012-01-16 19:14:36 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_name = argv[1];
|
|
|
|
min_offset = Poco::NumberParser::parseUnsigned64(argv[3]);
|
|
|
|
max_offset = Poco::NumberParser::parseUnsigned64(argv[4]);
|
|
|
|
block_size = Poco::NumberParser::parseUnsigned64(argv[5]);
|
|
|
|
threads = Poco::NumberParser::parseUnsigned(argv[6]);
|
|
|
|
count = Poco::NumberParser::parseUnsigned(argv[7]);
|
2014-10-16 01:21:03 +00:00
|
|
|
|
2012-09-28 10:42:41 +00:00
|
|
|
for (int i = 0; argv[2][i]; ++i)
|
|
|
|
{
|
|
|
|
char c = argv[2][i];
|
|
|
|
switch(c)
|
|
|
|
{
|
|
|
|
case 'r':
|
|
|
|
mode |= MODE_READ;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
mode |= MODE_WRITE;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
mode |= MODE_ALIGNED;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
mode |= MODE_DIRECT;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
mode |= MODE_SYNC;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw Poco::Exception("Invalid mode");
|
|
|
|
}
|
|
|
|
}
|
2014-10-16 01:21:03 +00:00
|
|
|
|
2016-08-02 01:46:05 +00:00
|
|
|
ThreadPool pool(threads);
|
2012-01-16 19:14:36 +00:00
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
#ifndef __APPLE__
|
2012-09-28 10:42:41 +00:00
|
|
|
int fd = open(file_name, ((mode & MODE_READ) ? O_RDONLY : O_WRONLY) | ((mode & MODE_DIRECT) ? O_DIRECT : 0) | ((mode & MODE_SYNC) ? O_SYNC : 0));
|
2016-10-26 22:27:38 +00:00
|
|
|
#else
|
|
|
|
int fd = open(file_name, ((mode & MODE_READ) ? O_RDONLY : O_WRONLY) | ((mode & MODE_SYNC) ? O_SYNC : 0));
|
|
|
|
#endif
|
2012-01-16 19:14:36 +00:00
|
|
|
if (-1 == fd)
|
|
|
|
throwFromErrno("Cannot open file");
|
2016-10-26 22:27:38 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
if (mode & MODE_DIRECT)
|
|
|
|
if (fcntl(fd, F_NOCACHE, 1) == -1)
|
|
|
|
throwFromErrno("Cannot open file");
|
|
|
|
#endif
|
2012-01-16 19:14:36 +00:00
|
|
|
Stopwatch watch;
|
2014-10-16 01:21:03 +00:00
|
|
|
|
2012-01-16 19:14:36 +00:00
|
|
|
for (size_t i = 0; i < threads; ++i)
|
2016-08-02 01:46:05 +00:00
|
|
|
pool.schedule(std::bind(thread, fd, mode, min_offset, max_offset, block_size, count));
|
2012-01-16 19:14:36 +00:00
|
|
|
pool.wait();
|
2014-10-16 01:21:03 +00:00
|
|
|
|
2012-09-28 10:42:41 +00:00
|
|
|
fsync(fd);
|
2012-01-16 19:14:36 +00:00
|
|
|
|
|
|
|
watch.stop();
|
|
|
|
|
|
|
|
if (0 != close(fd))
|
|
|
|
throwFromErrno("Cannot close file");
|
|
|
|
|
|
|
|
std::cout << std::fixed << std::setprecision(2)
|
2012-09-28 10:42:41 +00:00
|
|
|
<< "Done " << count << " * " << threads << " ops";
|
|
|
|
if (mode & MODE_ALIGNED)
|
|
|
|
std::cout << " (aligned)";
|
|
|
|
if (mode & MODE_DIRECT)
|
|
|
|
std::cout << " (direct)";
|
|
|
|
if (mode & MODE_SYNC)
|
|
|
|
std::cout << " (sync)";
|
|
|
|
std::cout << " in " << watch.elapsedSeconds() << " sec."
|
2012-01-16 19:14:36 +00:00
|
|
|
<< ", " << count * threads / watch.elapsedSeconds() << " ops/sec."
|
|
|
|
<< ", " << count * threads * block_size / watch.elapsedSeconds() / 1000000 << " MB/sec."
|
|
|
|
<< std::endl;
|
2014-10-16 01:21:03 +00:00
|
|
|
|
2012-01-16 19:14:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char ** argv)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return mainImpl(argc, argv);
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception & e)
|
|
|
|
{
|
|
|
|
std::cerr << e.what() << ", " << e.message() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|