Skip to content

Commit df7cd65

Browse files
committed
Convert mmapPerf to google-benchmark
Bug: 24272167 Change-Id: I831c9d26495e77e041a9461ddbafc70d7d11f9b7
1 parent d0f1a4d commit df7cd65

File tree

2 files changed

+57
-86
lines changed

2 files changed

+57
-86
lines changed

mmap-perf/Android.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ LOCAL_CFLAGS += -g -Wall -Werror -std=c++11 -Wno-missing-field-initializers -Wno
2828
LOCAL_FORCE_STATIC_EXECUTABLE := true
2929
LOCAL_CXX_STL := libc++_static
3030
LOCAL_STATIC_LIBRARIES := libc
31-
include $(BUILD_EXECUTABLE)
31+
include $(BUILD_NATIVE_BENCHMARK)

mmap-perf/mmapPerf.cpp

+56-85
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "benchmark/benchmark_api.h"
12
#include <string>
23
#include <cstring>
34
#include <cstdlib>
@@ -12,7 +13,9 @@
1213
#include <sys/mman.h>
1314

1415
using namespace std;
15-
static const size_t pageSize = 4096;
16+
static const size_t pageSize = PAGE_SIZE;
17+
static size_t fsize = 1024 * (1ull << 20);
18+
static size_t pagesTotal = fsize / pageSize;
1619

1720
class Fd {
1821
int m_fd = -1;
@@ -79,48 +82,21 @@ class FileMap {
7982
fillPageJunk(targetPtr);
8083
}
8184
}
82-
double benchRandom(bool write) {
83-
size_t pagesTotal = m_size / pageSize;
84-
size_t pagesToHit = pagesTotal / 128;
85-
uint64_t nsTotal = 0;
86-
87-
chrono::time_point<chrono::high_resolution_clock> start, end;
88-
start = chrono::high_resolution_clock::now();
89-
for (int j = 0; j < pagesToHit; j++) {
90-
int targetPage = rand() % pagesTotal;
91-
uint8_t *targetPtr = (uint8_t*)m_ptr + 4096ull * targetPage;
92-
if (write) {
93-
*targetPtr = dummy;
94-
}
95-
else {
96-
dummy += *targetPtr;
97-
}
98-
}
99-
end = chrono::high_resolution_clock::now();
100-
nsTotal += chrono::duration_cast<chrono::nanoseconds>(end - start).count();
101-
return ((4096.0 * pagesToHit) / (1 << 20)) / (nsTotal / 1.0E9);
85+
void benchRandomRead(unsigned int targetPage) {
86+
uint8_t *targetPtr = (uint8_t*)m_ptr + pageSize * targetPage;
87+
dummy += *targetPtr;
10288
}
103-
double benchLinear(bool write) {
104-
int pagesTotal = m_size / pageSize;
105-
int iterations = 4;
106-
uint64_t nsTotal = 0;
107-
108-
chrono::time_point<chrono::high_resolution_clock> start, end;
109-
start = chrono::high_resolution_clock::now();
110-
for (int i = 0; i < iterations; i++) {
111-
for (int j = 0; j < pagesTotal; j++) {
112-
uint8_t *targetPtr = (uint8_t*)m_ptr + 4096ull * j;
113-
if (write) {
114-
*targetPtr = dummy;
115-
}
116-
else {
117-
dummy += *targetPtr;
118-
}
119-
}
120-
}
121-
end = chrono::high_resolution_clock::now();
122-
nsTotal += chrono::duration_cast<chrono::nanoseconds>(end - start).count();
123-
return ((4096.0 * pagesTotal * iterations) / (1 << 20)) / (nsTotal / 1.0E9 );
89+
void benchRandomWrite(unsigned int targetPage) {
90+
uint8_t *targetPtr = (uint8_t*)m_ptr + pageSize * targetPage;
91+
*targetPtr = dummy;
92+
}
93+
void benchLinearRead(unsigned int j) {
94+
uint8_t *targetPtr = (uint8_t*)m_ptr + pageSize * j;
95+
dummy += *targetPtr;
96+
}
97+
void benchLinearWrite(unsigned int j) {
98+
uint8_t *targetPtr = (uint8_t*)m_ptr + pageSize * j;
99+
*targetPtr = dummy;
124100
}
125101
void dropCache() {
126102
int ret1 = msync(m_ptr, m_size, MS_SYNC | MS_INVALIDATE);
@@ -134,51 +110,46 @@ class FileMap {
134110

135111
};
136112

137-
int main(int argc, char *argv[])
138-
{
139-
double randomRead, randomWrite, linearRead, linearWrite;
140-
size_t fsize = 0;
141-
srand(0);
142-
143-
if (argc == 1)
144-
fsize = 1024 * (1ull << 20);
145-
else if (argc == 2) {
146-
long long sz = atoll(argv[1]);
147-
if (sz > 0 && (sz << 20) < SIZE_MAX)
148-
fsize = atoll(argv[1]) * (1ull << 20);
113+
static void benchRandomRead(benchmark::State& state) {
114+
FileMap file{"/data/local/tmp/mmap_test", fsize};
115+
while (state.KeepRunning()) {
116+
unsigned int targetPage = rand() % pagesTotal;
117+
file.benchRandomRead(targetPage);
149118
}
119+
state.SetBytesProcessed(state.iterations() * pageSize);
120+
}
121+
BENCHMARK(benchRandomRead);
150122

151-
if (fsize <= 0) {
152-
cout << "Error: invalid argument" << endl;
153-
cerr << "Usage: " << argv[0] << " [fsize_in_MB]" << endl;
154-
exit(1);
123+
static void benchRandomWrite(benchmark::State& state) {
124+
FileMap file{"/data/local/tmp/mmap_test", fsize};
125+
while (state.KeepRunning()) {
126+
unsigned int targetPage = rand() % pagesTotal;
127+
file.benchRandomWrite(targetPage);
155128
}
156-
cerr << "Using filesize=" << fsize << endl;
129+
state.SetBytesProcessed(state.iterations() * pageSize);
130+
}
131+
BENCHMARK(benchRandomWrite);
157132

158-
{
159-
cerr << "Running random_read..." << endl;
160-
FileMap file{"/data/local/tmp/mmap_test", fsize};
161-
randomRead = file.benchRandom(false);
162-
}
163-
{
164-
cerr << "Running linear_read..." << endl;
165-
FileMap file{"/data/local/tmp/mmap_test", fsize};
166-
linearRead = file.benchLinear(false);
167-
}
168-
{
169-
cerr << "Running random_write..." << endl;
170-
FileMap file{"/data/local/tmp/mmap_test", fsize};
171-
randomWrite = file.benchRandom(true);
172-
}
173-
{
174-
cerr << "Running linear_write..." << endl;
175-
FileMap file{"/data/local/tmp/mmap_test", fsize};
176-
linearWrite = file.benchLinear(true);
177-
}
178-
cout << "Success" << endl;
179-
cout << "random_read : " << randomRead << " : MB/s" << endl;
180-
cout << "linear_read : " << linearRead << " : MB/s" << endl;
181-
cout << "random_write : " << randomWrite << " : MB/s" << endl;
182-
cout << "linear_write : " << linearWrite << " : MB/s" << endl;
183-
return 0;
133+
static void benchLinearRead(benchmark::State& state) {
134+
FileMap file{"/data/local/tmp/mmap_test", fsize};
135+
unsigned int j = 0;
136+
while (state.KeepRunning()) {
137+
file.benchLinearRead(j);
138+
j = (j + 1) % pagesTotal;
139+
}
140+
state.SetBytesProcessed(state.iterations() * pageSize);
141+
}
142+
BENCHMARK(benchLinearRead);
143+
144+
static void benchLinearWrite(benchmark::State& state) {
145+
FileMap file{"/data/local/tmp/mmap_test", fsize};
146+
unsigned int j = 0;
147+
while (state.KeepRunning()) {
148+
file.benchLinearWrite(j);
149+
j = (j + 1) % pagesTotal;
150+
}
151+
state.SetBytesProcessed(state.iterations() * pageSize);
184152
}
153+
BENCHMARK(benchLinearWrite);
154+
155+
BENCHMARK_MAIN()

0 commit comments

Comments
 (0)