1#include <fcntl.h>
2#include <port/unistd.h>
3#include <stdlib.h>
4#include <time.h>
5#include <stdlib.h>
6#if !defined(__APPLE__) && !defined(__FreeBSD__)
7#include <malloc.h>
8#endif
9#include <poll.h>
10#include <iostream>
11#include <iomanip>
12#include <vector>
13#include <random>
14#include <pcg_random.hpp>
15#include <IO/ReadHelpers.h>
16#include <Poco/Exception.h>
17#include <Common/Exception.h>
18#include <Common/randomSeed.h>
19#include <Common/ThreadPool.h>
20#include <Common/Stopwatch.h>
21#include <port/clock.h>
22
23namespace DB
24{
25 namespace ErrorCodes
26 {
27 extern const int CANNOT_OPEN_FILE;
28 extern const int CANNOT_CLOSE_FILE;
29 extern const int CANNOT_READ_FROM_FILE_DESCRIPTOR;
30 extern const int CANNOT_WRITE_TO_FILE_DESCRIPTOR;
31 extern const int CANNOT_FSYNC;
32 extern const int SYSTEM_ERROR;
33 }
34}
35
36
37enum Mode
38{
39 MODE_READ,
40 MODE_WRITE,
41};
42
43
44int mainImpl(int argc, char ** argv)
45{
46 using namespace DB;
47
48 const char * file_name = 0;
49 Mode mode = MODE_READ;
50 UInt64 min_offset = 0;
51 UInt64 max_offset = 0;
52 UInt64 block_size = 0;
53 UInt64 descriptors = 0;
54 UInt64 count = 0;
55
56 if (argc != 8)
57 {
58 std::cerr << "Usage: " << argv[0] << " file_name r|w min_offset max_offset block_size descriptors count" << std::endl;
59 return 1;
60 }
61
62 file_name = argv[1];
63 min_offset = parse<UInt64>(argv[3]);
64 max_offset = parse<UInt64>(argv[4]);
65 block_size = parse<UInt64>(argv[5]);
66 descriptors = parse<UInt64>(argv[6]);
67 count = parse<UInt64>(argv[7]);
68
69 if (!strcmp(argv[2], "r"))
70 mode = MODE_READ;
71 else if (!strcmp(argv[2], "w"))
72 mode = MODE_WRITE;
73 else
74 throw Poco::Exception("Invalid mode");
75
76 std::vector<int> fds(descriptors);
77 for (size_t i = 0; i < descriptors; ++i)
78 {
79 fds[i] = open(file_name, O_SYNC | ((mode == MODE_READ) ? O_RDONLY : O_WRONLY));
80 if (-1 == fds[i])
81 throwFromErrno("Cannot open file", ErrorCodes::CANNOT_OPEN_FILE);
82 }
83
84 std::vector<char> buf(block_size);
85
86 pcg64 rng(randomSeed());
87
88 Stopwatch watch;
89
90 std::vector<pollfd> polls(descriptors);
91
92 for (size_t i = 0; i < descriptors; ++i)
93 {
94 polls[i].fd = fds[i];
95 polls[i].events = (mode == MODE_READ) ? POLLIN : POLLOUT;
96 polls[i].revents = 0;
97 }
98
99 size_t ops = 0;
100 while (ops < count)
101 {
102 if (poll(&polls[0], descriptors, -1) <= 0)
103 throwFromErrno("poll failed", ErrorCodes::SYSTEM_ERROR);
104 for (size_t i = 0; i < descriptors; ++i)
105 {
106 if (!polls[i].revents)
107 continue;
108
109 if (polls[i].revents != polls[i].events)
110 throw Poco::Exception("revents indicates error");
111 polls[i].revents = 0;
112 ++ops;
113
114 long rand_result1 = rng();
115 long rand_result2 = rng();
116 long rand_result3 = rng();
117
118 size_t rand_result = rand_result1 ^ (rand_result2 << 22) ^ (rand_result3 << 43);
119 size_t offset;
120 offset = min_offset + rand_result % ((max_offset - min_offset) / block_size) * block_size;
121
122 if (mode == MODE_READ)
123 {
124 if (static_cast<int>(block_size) != pread(fds[i], &buf[0], block_size, offset))
125 throwFromErrno("Cannot read", ErrorCodes::CANNOT_READ_FROM_FILE_DESCRIPTOR);
126 }
127 else
128 {
129 if (static_cast<int>(block_size) != pwrite(fds[i], &buf[0], block_size, offset))
130 throwFromErrno("Cannot write", ErrorCodes::CANNOT_WRITE_TO_FILE_DESCRIPTOR);
131 }
132 }
133 }
134
135 for (size_t i = 0; i < descriptors; ++i)
136 {
137 if (fsync(fds[i]))
138 throwFromErrno("Cannot fsync", ErrorCodes::CANNOT_FSYNC);
139 }
140
141 watch.stop();
142
143 for (size_t i = 0; i < descriptors; ++i)
144 {
145 if (0 != close(fds[i]))
146 throwFromErrno("Cannot close file", ErrorCodes::CANNOT_CLOSE_FILE);
147 }
148
149 std::cout << std::fixed << std::setprecision(2)
150 << "Done " << count << " ops" << " in " << watch.elapsedSeconds() << " sec."
151 << ", " << count / watch.elapsedSeconds() << " ops/sec."
152 << ", " << count * block_size / watch.elapsedSeconds() / 1000000 << " MB/sec."
153 << std::endl;
154
155 return 0;
156}
157
158
159int main(int argc, char ** argv)
160{
161 try
162 {
163 return mainImpl(argc, argv);
164 }
165 catch (const Poco::Exception & e)
166 {
167 std::cerr << e.what() << ", " << e.message() << std::endl;
168 return 1;
169 }
170}
171