Skip to content

Instantly share code, notes, and snippets.

@Leviathan1995
Last active February 19, 2019 14:01
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Leviathan1995/048f89fb679ef1ecc13978029b20643c to your computer and use it in GitHub Desktop.
#include <iostream>
#include <unistd.h>
#include <cstring>
#include <sys/mman.h>
#include <vector>
#include <thread>
#include <sys/stat.h>
#include <fcntl.h>
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
static char data[4096] __attribute__((aligned(4096))) = {'a'};
void writer(int index) {
std::string fname = "data" + std::to_string(index);
int data_fd = ::open(fname.c_str(), O_RDWR | O_CREAT | O_APPEND | O_DIRECT, 0645);
for (int32_t i = 0; i < 1000000; i++) {
::write(data_fd, data, 4096);
}
close(data_fd);
}
int main() {
std::vector<std::thread> threads;
for(int i = 0; i < 64; i++) {
std::thread worker(writer, i);
threads.push_back(std::move(worker));
}
for (int i = 0; i < 64; i++) {
threads[i].join();
}
return 0;
}
// g++ -std=c++11 -O2 -pthread direct_io.cc -o benchmark
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment