daily pastebin goal
27%
SHARE
TWEET

Untitled

a guest Feb 21st, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <unistd.h>
  3. #include <cstring>
  4. #include <sys/mman.h>
  5. #include <vector>
  6. #include <thread>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9.  
  10. #define likely(x)      __builtin_expect(!!(x), 1)
  11. #define unlikely(x)    __builtin_expect(!!(x), 0)
  12.  
  13. static char data[4096] __attribute__((aligned(4096))) = {'a'};
  14. static int32_t map_size = 4096 * 4;
  15.  
  16. void MapRegion(int fd, uint64_t file_offset, char** base) {
  17.   void* ptr = mmap(nullptr, map_size, PROT_READ | PROT_WRITE,
  18.            MAP_SHARED,
  19.            fd,
  20.            file_offset);
  21.   if (unlikely(ptr == MAP_FAILED)) {
  22.     *base = nullptr;
  23.     return;
  24.   }
  25.   *base = reinterpret_cast<char*>(ptr);
  26. }
  27.  
  28. void UnMapRegion(char* base) {
  29.   munmap(base, map_size);
  30. }
  31.  
  32. void writer(int index) {
  33.   std::string fname = "data" + std::to_string(index);
  34.   std::string batch = "batch" + std::to_string(index);
  35.  
  36.   char* base = nullptr;
  37.   char* cursor = nullptr;
  38.   uint64_t mmap_offset = 0, file_offset = 0;
  39.  
  40.   int data_fd = ::open(fname.c_str(), O_RDWR | O_CREAT | O_DIRECT, 0645);
  41.   int batch_fd = ::open(batch.c_str(), O_RDWR | O_CREAT | O_DIRECT, 0645);
  42.   posix_fallocate(data_fd, 0, (4096UL * 1000000));
  43.   posix_fallocate(batch_fd, 0, map_size);
  44.  
  45.   MapRegion(batch_fd, 0, &base);
  46.   if (unlikely(base == nullptr)) {
  47.     return;
  48.   }
  49.   cursor = base;
  50.   file_offset += map_size;
  51.  
  52.   for (int32_t i = 0; i < 1000000; i++) {
  53.     if (unlikely(mmap_offset >= map_size)) {
  54.       pwrite64(data_fd, base, map_size, file_offset);
  55.       cursor = base;
  56.  
  57.       file_offset += map_size;
  58.       mmap_offset = 0;
  59.     }
  60.     memcpy(cursor, data, 4096);
  61.     cursor += 4096;
  62.     mmap_offset += 4096;
  63.   }
  64.   UnMapRegion(base);
  65.  
  66.   close(data_fd);
  67.   close(batch_fd);
  68. }
  69.  
  70. int main() {
  71.   std::vector<std::thread> threads;
  72.   for(int i = 0; i < 64; i++) {
  73.     std::thread worker(writer, i);
  74.     threads.push_back(std::move(worker));
  75.   }
  76.   for (int i = 0; i < 64; i++) {
  77.     threads[i].join();
  78.   }
  79.  
  80.   return 0;
  81. }
  82.  
  83. // g++ -std=c++11 -O2 -pthread mmap_batch.cc -o benchmark
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top