-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbufferio_w.cpp
60 lines (50 loc) · 1.61 KB
/
bufferio_w.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <unistd.h>
#include <cstring>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <vector>
#include <thread>
#include <fcntl.h>
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#define WRITE_ONCE_BYTE_SIZE 4096
static char data[WRITE_ONCE_BYTE_SIZE] __attribute__((aligned(WRITE_ONCE_BYTE_SIZE))) = {'a'};
static const uint64_t kWriteCountPerThread = 1000 * 1000;
static const uint64_t kConcurrency = 64;
static const uint64_t kWriteBytesPerThread = sizeof(data) * kWriteCountPerThread;
static const uint64_t kTotalWriteBytes = kWriteBytesPerThread * kConcurrency;
uint64_t NowMicros() {
struct timeval tv;
gettimeofday(&tv, NULL);
return static_cast<uint64_t>(tv.tv_sec) * 1000000 + tv.tv_usec;
}
void writer(int index) {
std::string fname = "data" + std::to_string(index);
int fd = ::open(fname.c_str(), O_NOATIME | O_RDWR | O_CREAT, 0644);
int ret = posix_fallocate(fd, 0, kWriteBytesPerThread);
if (ret != 0) {
printf("fallocate err %d\n", ret);
}
lseek(fd, 0, SEEK_SET);
for (int32_t i = 0; i < kWriteCountPerThread; i++) {
::write(fd, data, WRITE_ONCE_BYTE_SIZE);
}
close(fd);
}
int main() {
uint64_t st, ed;
st = NowMicros();
std::vector<std::thread> threads;
for(int i = 0; i < kConcurrency; i++) {
std::thread worker(writer, i);
threads.push_back(std::move(worker));
}
for (int i = 0; i < kConcurrency; i++) {
threads[i].join();
}
ed = NowMicros();
printf("time elapsed microsecond(us) %lld, %lld MB/s\n", ed - st, kTotalWriteBytes / (ed - st));
return 0;
}