Skip to content

Commit 3732466

Browse files
committed
Memory Mapped IO Test
1 parent d6e85c7 commit 3732466

5 files changed

+136
-25
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
build/
22
.vscode/
3+
*.rpt
4+
*.o
5+
*.s

CMakeLists.txt

+22-14
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
11
cmake_minimum_required(VERSION 3.10)
22

3-
project(PerfTest)
3+
project(PerfTest LANGUAGES CXX C)
44

5-
set(LOCAL "/usr/local/include")
6-
set(CMAKE_CXX_STANDARD 11)
7-
add_definitions("-msse4 -g")
85
set(SOURCE ${PROJECT_SOURCE_DIR}/src)
96
#set(EXECUTABLE_OUTPUT_PATH bin)
107
include_directories(${LOCAL})
8+
set(CMAKE_VERBOSE_MAKEFILE ON)
9+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -pthread -std=c++17 -fopenmp")
1110

1211
add_executable(RowColumnAccess ${SOURCE}/array_access_test.cpp)
13-
target_link_libraries(RowColumnAccess PRIVATE "${LOCAL}/../lib/libbenchmark.a")
1412
add_executable(CPUPinning ${SOURCE}/cpu_pinning.cpp)
15-
add_executable(FileReadPerf ${SOURCE}/memory_mapped_io.cpp)
1613
add_executable(GoogleBench ${SOURCE}/google_benchmark.cpp)
17-
target_link_libraries(GoogleBench PRIVATE "${LOCAL}/../lib/libbenchmark.a")
1814
add_executable(CachePerf ${SOURCE}/cache_perf.cpp)
19-
target_link_libraries(CachePerf PRIVATE "${LOCAL}/../lib/libbenchmark.a")
2015
add_executable(AtomicPerf ${SOURCE}/atomic_perf.cpp)
21-
target_link_libraries(AtomicPerf PRIVATE "${LOCAL}/../lib/libbenchmark.a")
2216
add_executable(VectorizedTest ${SOURCE}/vectorized_test.cpp)
23-
target_link_libraries(VectorizedTest PRIVATE "${LOCAL}/../lib/libbenchmark.a")
2417
add_executable(MiscPerf ${SOURCE}/misc_perf.cpp)
25-
target_link_libraries(MiscPerf PRIVATE "${LOCAL}/../lib/libbenchmark.a")
2618
add_executable(LockPerf ${SOURCE}/lock_benchmark.cpp)
27-
target_link_libraries(LockPerf PRIVATE "${LOCAL}/../lib/libbenchmark.a")
28-
19+
add_executable(FixTest ${SOURCE}/bench_fixture.cpp)
2920
add_executable(RuntimePolymorphism ${SOURCE}/virtual_functions.cpp)
30-
target_link_libraries(RuntimePolymorphism PRIVATE "${LOCAL}/../lib/libbenchmark.a")
21+
add_executable(CacheThrash ${SOURCE}/cache_thrashing.cpp)
22+
add_executable(MemoryMappedFile ${SOURCE}/memory_mapped_io.cpp)
23+
add_executable(VecMemCpy ${SOURCE}/vecmemcpy.cpp)
24+
add_executable(VecStdMemCpy ${SOURCE}/vecstdmemcpy.cpp)
25+
26+
target_link_libraries(RowColumnAccess benchmark)
27+
target_link_libraries(GoogleBench benchmark)
28+
target_link_libraries(CachePerf benchmark)
29+
target_link_libraries(AtomicPerf benchmark)
30+
target_link_libraries(VectorizedTest benchmark)
31+
target_link_libraries(MiscPerf benchmark)
32+
target_link_libraries(LockPerf benchmark)
33+
target_link_libraries(FixTest benchmark)
34+
target_link_libraries(RuntimePolymorphism benchmark)
35+
target_link_libraries(CacheThrash benchmark)
36+
target_link_libraries(MemoryMappedFile benchmark)
37+
target_link_libraries(VecMemCpy benchmark)
38+
target_link_libraries(VecStdMemCpy benchmark)

src/array_access_test.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <iostream>
22
#include <chrono>
33
#include <benchmark/benchmark.h>
4+
#include <vector>
45

56
using namespace std;
67

@@ -15,6 +16,7 @@ int** allocateArray(uint64_t size) {
1516
//Cache hit
1617
void row_wise_access(int** array, int size) {
1718
int result = 0;
19+
// loop
1820
for(int i=0; i<size; i++)
1921
for(int j=0; j<size; j++) {
2022
result += array[i][j];
@@ -31,6 +33,7 @@ void deallocateArray(int** arr, int size) {
3133
//Cache miss
3234
void column_wise_access(int** array, int size) {
3335
int result = 0;
36+
// loop
3437
for(int i=0; i<size; i++)
3538
for(int j=0; j<size; j++) {
3639
result += array[j][i];
@@ -41,7 +44,8 @@ static void BM_row_wise_access(benchmark::State& state) {
4144
int size = state.range(0);
4245
int** arr = allocateArray(size);
4346
for (auto _ : state) {
44-
row_wise_access(arr, size);
47+
row_wise_access(arr, size);
48+
benchmark::ClobberMemory();
4549
}
4650
deallocateArray(arr, size);
4751
}

src/lock_benchmark.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <benchmark/benchmark.h>
22
#include <atomic>
33

4-
static uint64_t lock_underlying = 0;
4+
uint32_t lock_underlying = 0;
55

66
class SpinLock {
77
std::atomic_flag locked = ATOMIC_FLAG_INIT ;
@@ -16,27 +16,27 @@ class SpinLock {
1616

1717
static SpinLock spinlock;
1818

19-
void lock(uint64_t* underlying) {
19+
void lock(uint32_t* underlying) {
2020
do {} while(not __sync_bool_compare_and_swap(underlying, 0, 1));
2121
}
2222

23-
void unlock(uint64_t* underlying) {
23+
void unlock(uint32_t* underlying) {
2424
do {} while(not __sync_bool_compare_and_swap(underlying, 1, 0));
2525
}
2626

2727
void readModifyWrite(int* num) {
2828
lock(&lock_underlying);
29-
for (int i=0; i<1000; i++) {
29+
/*for (int i=0; i<1000; i++) {
3030
*num = *num + 1;
31-
}
31+
}*/
3232
unlock(&lock_underlying);
3333
}
3434

3535
void readModifyWriteGccLock(int *num) {
3636
spinlock.lock();
37-
for (int i=0; i<1000; i++) {
37+
/*for (int i=0; i<1000; i++) {
3838
*num = *num + 1;
39-
}
39+
}*/
4040
spinlock.unlock();
4141
}
4242

src/memory_mapped_io.cpp

+99-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,103 @@
11
#include <iostream>
2-
#include <fstream>
2+
#include <fcntl.h>
3+
#include <sys/mman.h>
4+
#include <unistd.h>
5+
#include <cstring>
6+
#include <benchmark/benchmark.h>
37

4-
int main() {
5-
return 0;
8+
static constexpr auto kMaxSize = 8192;
9+
char data[kMaxSize];
10+
11+
static void BM_MemMappedRead(benchmark::State& state) {
12+
int fd = open("../resources/data.bin", O_RDONLY | O_CLOEXEC);
13+
if (fd < 0) {
14+
std::cout << "Error in Opening file" << errno << "\n";
15+
}
16+
17+
char* map = (char*)mmap(NULL, kMaxSize, PROT_READ, MAP_SHARED, fd, 0);
18+
if (!map) {
19+
std::cout << "Error in mapping " << errno << std::endl;
20+
}
21+
22+
size_t size = state.range(0);
23+
for (auto _ : state) {
24+
std::memcpy(data, map, size);
25+
}
26+
27+
close(fd);
28+
munmap(map, kMaxSize);
29+
}
30+
31+
static void BM_MemMappedWrite(benchmark::State& state) {
32+
int fd = open("../resources/data_write_map.bin", O_RDWR | O_CLOEXEC);
33+
if (fd < 0) {
34+
std::cout << "Error in Opening file " << errno << "\n";
35+
}
36+
37+
char* map = (char*)mmap(NULL, kMaxSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
38+
if (!map) {
39+
std::cout << "Error in mapping " << errno << std::endl;
40+
}
41+
42+
size_t size = state.range(0);
43+
for (auto _ : state) {
44+
std::memcpy(map, data, size);
45+
}
46+
47+
close(fd);
48+
munmap(map, kMaxSize);
49+
}
50+
51+
52+
#define min_(x, y) x < y ? x : y
53+
54+
static void BM_ReadSyscall(benchmark::State& state) {
55+
int fd = open("../resources/data.bin", O_RDONLY | O_CLOEXEC);
56+
if (fd < 0) {
57+
std::cout << "Error in Opening file" << errno << "\n";
58+
}
59+
60+
size_t size = state.range(0);
61+
for (auto _ : state) {
62+
int total_read = 0;
63+
if (lseek(fd, 0, SEEK_SET) < 0) {
64+
std::cerr << "Error in Seeking " << errno << std::endl;
65+
return;
66+
}
67+
while(total_read < size) {
68+
size_t read_size = min_(size, size - total_read);
69+
total_read += read(fd, data + total_read, read_size);
70+
}
71+
}
72+
73+
close(fd);
674
}
775

76+
static void BM_WriteSyscall(benchmark::State& state) {
77+
int fd = open("../resources/data_write_sys.bin", O_RDONLY | O_CLOEXEC);
78+
if (fd < 0) {
79+
std::cout << "Error in Opening file" << errno << "\n";
80+
}
81+
82+
size_t size = state.range(0);
83+
for (auto _ : state) {
84+
int total_write = 0;
85+
if (lseek(fd, 0, SEEK_SET) < 0) {
86+
std::cerr << "Error in Seeking " << errno << std::endl;
87+
return;
88+
}
89+
while(total_write < size) {
90+
size_t write_size = min_(size, size - total_write);
91+
total_write += read(fd, data + total_write, write_size);
92+
}
93+
}
94+
95+
close(fd);
96+
}
97+
98+
BENCHMARK(BM_MemMappedRead)->Range(64, 8 << 10);
99+
BENCHMARK(BM_ReadSyscall)->Range(64, 8 << 10);
100+
BENCHMARK(BM_MemMappedWrite)->Range(64, 8 << 10);
101+
BENCHMARK(BM_WriteSyscall)->Range(64, 8 << 10);
102+
BENCHMARK_MAIN();
103+

0 commit comments

Comments
 (0)