1
+ #include " benchmark/benchmark_api.h"
1
2
#include < string>
2
3
#include < cstring>
3
4
#include < cstdlib>
12
13
#include < sys/mman.h>
13
14
14
15
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;
16
19
17
20
class Fd {
18
21
int m_fd = -1 ;
@@ -79,48 +82,21 @@ class FileMap {
79
82
fillPageJunk (targetPtr);
80
83
}
81
84
}
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;
102
88
}
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;
124
100
}
125
101
void dropCache () {
126
102
int ret1 = msync (m_ptr, m_size, MS_SYNC | MS_INVALIDATE);
@@ -134,51 +110,46 @@ class FileMap {
134
110
135
111
};
136
112
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);
149
118
}
119
+ state.SetBytesProcessed (state.iterations () * pageSize);
120
+ }
121
+ BENCHMARK (benchRandomRead);
150
122
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);
155
128
}
156
- cerr << " Using filesize=" << fsize << endl;
129
+ state.SetBytesProcessed (state.iterations () * pageSize);
130
+ }
131
+ BENCHMARK (benchRandomWrite);
157
132
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);
184
152
}
153
+ BENCHMARK (benchLinearWrite);
154
+
155
+ BENCHMARK_MAIN ()
0 commit comments