15 static const size_t pageSize = 4096;
20 int get() { return m_fd; }
21 void set(int fd) { m_fd = fd; }
23 Fd(int fd) : m_fd{fd} {}
32 void fillPageJunk(void *ptr)
34 uint64_t seed = (unsigned long long)rand() | ((unsigned long long)rand() << 32);
35 uint64_t *target = (uint64_t*)ptr;
36 for (int i = 0; i < pageSize / sizeof(uint64_t); i++) {
37 *target = seed ^ (uint64_t)(uintptr_t)target;
38 seed = (seed << 1) | ((seed >> 63) & 1);
46 void *m_ptr = nullptr;
54 FileMap(const string &name, size_t size, Hint hint = FILE_MAP_HINT_NONE) : m_name{name}, m_size{size} {
55 int fd = open(name.c_str(), O_CREAT | O_RDWR, S_IRWXU);
57 cerr << "open failed: " << fd << endl;
61 fallocate(m_fileFd.get(), 0, 0, size);
63 m_ptr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, m_fileFd.get(), 0);
64 if ((int)(uintptr_t)m_ptr == -1) {
65 cerr << "mmap failed: " << (int)(uintptr_t)m_ptr << endl;
70 case FILE_MAP_HINT_NONE: break;
71 case FILE_MAP_HINT_RAND:
72 madvise(m_ptr, m_size, MADV_RANDOM);
74 case FILE_MAP_HINT_LINEAR:
75 madvise(m_ptr, m_size, MADV_SEQUENTIAL);
78 for (int i = 0; i < m_size / pageSize; i++) {
79 uint8_t *targetPtr = (uint8_t*)m_ptr + 4096ull * i;
80 fillPageJunk(targetPtr);
83 void benchRandom(bool write) {
84 size_t pagesTotal = m_size / pageSize;
85 size_t pagesToHit = pagesTotal / 128;
88 chrono::time_point<chrono::high_resolution_clock> start, end;
89 start = chrono::high_resolution_clock::now();
90 for (int j = 0; j < pagesToHit; j++) {
91 int targetPage = rand() % pagesTotal;
92 uint8_t *targetPtr = (uint8_t*)m_ptr + 4096ull * targetPage;
100 end = chrono::high_resolution_clock::now();
101 nsTotal += chrono::duration_cast<chrono::nanoseconds>(end - start).count();
102 //cout << "random: " << nsTotal / 1000.0 / (pagesToHit) << "us/page" << endl;
103 cout << "random " << (write ? "write" : "read") << ": " << ((4096.0 * pagesToHit) / (1 << 20)) / (nsTotal / 1.0E9) << "MB/s" << endl;
105 void benchLinear(bool write) {
106 int pagesTotal = m_size / pageSize;
108 uint64_t nsTotal = 0;
110 chrono::time_point<chrono::high_resolution_clock> start, end;
111 start = chrono::high_resolution_clock::now();
112 for (int i = 0; i < iterations; i++) {
113 for (int j = 0; j < pagesTotal; j++) {
114 uint8_t *targetPtr = (uint8_t*)m_ptr + 4096ull * j;
123 end = chrono::high_resolution_clock::now();
124 nsTotal += chrono::duration_cast<chrono::nanoseconds>(end - start).count();
125 //cout << "linear: " << nsTotal / 1000.0 / (pagesTotal * iterations) << "us/page" << endl;
126 cout << "linear " << (write ? "write" : "read") << ": " << ((4096.0 * pagesTotal * iterations) / (1 << 20)) / (nsTotal / 1.0E9 ) << "MB/s" << endl;
129 int ret1 = msync(m_ptr, m_size, MS_SYNC | MS_INVALIDATE);
130 madvise(m_ptr, m_size, MADV_DONTNEED);
135 munmap(m_ptr, m_size);
140 int main(int argc, char *argv[])
147 FileMap file{"/data/local/tmp/mmap_test", 16000 * (1ull << 20)};
148 file.benchRandom(false);
151 FileMap file{"/data/local/tmp/mmap_test", 16000 * (1ull << 20)};
152 file.benchLinear(false);
155 FileMap file{"/data/local/tmp/mmap_test", 16000 * (1ull << 20)};
156 file.benchRandom(true);
159 FileMap file{"/data/local/tmp/mmap_test", 16000 * (1ull << 20)};
160 file.benchLinear(true);