Skip to content

Commit e927135

Browse files
committed
Add test if FSDAX is mapped with the MAP_SYNC flag
Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 2505265 commit e927135

File tree

6 files changed

+151
-43
lines changed

6 files changed

+151
-43
lines changed

.github/workflows/dax.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ permissions:
2222

2323
env:
2424
UMF_TESTS_DEVDAX_NAMESPACE : "0.0"
25+
UMF_TESTS_FSDAX_NAMESPACE : "1.0"
26+
UMF_TESTS_FSDAX_PATH: "/mnt/pmem1/file"
2527
BUILD_DIR : "${{github.workspace}}/build"
2628
INSTL_DIR : "${{github.workspace}}/../install-dir"
2729

@@ -39,11 +41,21 @@ jobs:
3941
steps:
4042
- name: Check if the devdax exists, print out UMF_TESTS_DEVDAX_PATH and UMF_TESTS_DEVDAX_SIZE
4143
run: |
42-
ndctl list -N --device-dax
44+
echo UMF_TESTS_DEVDAX_NAMESPACE="${UMF_TESTS_DEVDAX_NAMESPACE}"
45+
ndctl list --namespace=namespace${UMF_TESTS_DEVDAX_NAMESPACE} --device-dax
4346
ls -al /dev/dax${UMF_TESTS_DEVDAX_NAMESPACE}
4447
echo UMF_TESTS_DEVDAX_PATH="/dev/dax${UMF_TESTS_DEVDAX_NAMESPACE}"
4548
echo UMF_TESTS_DEVDAX_SIZE="$(ndctl list --namespace=namespace${UMF_TESTS_DEVDAX_NAMESPACE} | grep size | cut -d':' -f2 | cut -d',' -f1)"
4649
50+
- name: Check if the FSDAX exists
51+
run: |
52+
echo UMF_TESTS_FSDAX_NAMESPACE="${UMF_TESTS_FSDAX_NAMESPACE}"
53+
echo UMF_TESTS_FSDAX_PATH="${UMF_TESTS_FSDAX_PATH}"
54+
ndctl list --namespace=namespace${UMF_TESTS_FSDAX_NAMESPACE}
55+
ls -al /dev/$(echo $UMF_TESTS_FSDAX_PATH | cut -d'/' -f3)
56+
ls -al /mnt/$(echo $UMF_TESTS_FSDAX_PATH | cut -d'/' -f3)
57+
mount | grep -e "/dev/$(echo $UMF_TESTS_FSDAX_PATH | cut -d'/' -f3)"
58+
4759
- name: Checkout
4860
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
4961
with:
@@ -78,3 +90,7 @@ jobs:
7890
UMF_TESTS_DEVDAX_PATH="/dev/dax${UMF_TESTS_DEVDAX_NAMESPACE}"
7991
UMF_TESTS_DEVDAX_SIZE="$(ndctl list --namespace=namespace${UMF_TESTS_DEVDAX_NAMESPACE} | grep size | cut -d':' -f2 | cut -d',' -f1)"
8092
ctest -C ${{matrix.build_type}} -R devdax -V
93+
94+
- name: Run only tests of the file memory provider with FSDAX
95+
working-directory: ${{env.BUILD_DIR}}
96+
run: ctest -C ${{matrix.build_type}} -R umf-provider_file_memory -V

test/common/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ set(COMMON_SOURCES
99
provider_null.c
1010
provider_trace.c)
1111

12+
if(LINUX)
13+
set(COMMON_SOURCES ${COMMON_SOURCES} test_helpers_linux.c)
14+
endif(LINUX)
15+
1216
add_umf_library(
1317
NAME umf_test_common
1418
TYPE STATIC

test/common/test_helpers_linux.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (C) 2024 Intel Corporation
2+
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
// This file contains tests for UMF pool API
5+
6+
#include <fcntl.h>
7+
#include <stdbool.h>
8+
#include <string.h>
9+
#include <sys/stat.h>
10+
#include <sys/types.h>
11+
#include <unistd.h>
12+
13+
#include "test_helpers_linux.h"
14+
15+
bool is_mapped_with_MAP_SYNC(char *path, char *buf, size_t size_buf) {
16+
memset(buf, 0, size_buf);
17+
18+
int fd = open("/proc/self/smaps", O_RDONLY);
19+
if (fd == -1) {
20+
return false;
21+
}
22+
23+
// number of bytes read from the file
24+
ssize_t nbytes = 1;
25+
// string starting from the path of the smaps
26+
char *smaps = NULL;
27+
28+
// Read the "/proc/self/smaps" file
29+
// until the path of the smaps is found
30+
// or EOF is reached.
31+
while (nbytes > 0 && smaps == NULL) {
32+
memset(buf, 0, nbytes); // erase previous data
33+
nbytes = read(fd, buf, size_buf);
34+
// look for the path of the smaps
35+
smaps = strstr(buf, path);
36+
}
37+
38+
// String starting from the "sf" flag
39+
// marking that memory was mapped with the MAP_SYNC flag.
40+
char *sf_flag = NULL;
41+
42+
if (smaps) {
43+
// look for the "VmFlags:" string
44+
char *VmFlags = strstr(smaps, "VmFlags:");
45+
if (VmFlags) {
46+
// look for the EOL
47+
char *eol = strstr(VmFlags, "\n");
48+
if (eol) {
49+
// End the VmFlags string at EOL.
50+
*eol = 0;
51+
// Now the VmFlags string contains only one line with all VmFlags.
52+
53+
// Look for the "sf" flag in VmFlags
54+
// marking that memory was mapped
55+
// with the MAP_SYNC flag.
56+
sf_flag = strstr(VmFlags, "sf");
57+
}
58+
}
59+
}
60+
61+
// fail if the "sf" flag was not found
62+
return (sf_flag != NULL);
63+
}

test/common/test_helpers_linux.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (C) 2024 Intel Corporation
2+
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
// This file contains helpers for tests for UMF pool API
5+
6+
#ifndef UMF_TEST_HELPERS_LINUX_H
7+
#define UMF_TEST_HELPERS_LINUX_H 1
8+
9+
#include <stddef.h>
10+
11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
14+
15+
bool is_mapped_with_MAP_SYNC(char *path, char *buf, size_t size_buf);
16+
17+
#ifdef __cplusplus
18+
}
19+
#endif
20+
21+
#endif /* UMF_TEST_HELPERS_LINUX_H */

test/provider_devdax_memory.cpp

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

55
#ifndef _WIN32
6+
#include "test_helpers_linux.h"
67
#include <fcntl.h>
78
#include <sys/stat.h>
89
#include <sys/types.h>
@@ -11,6 +12,7 @@
1112
#include "base.hpp"
1213

1314
#include "cpp_helpers.hpp"
15+
#include "test_helpers.h"
1416

1517
#include <umf/memory_provider.h>
1618
#include <umf/providers/provider_devdax_memory.h>
@@ -167,56 +169,16 @@ TEST_F(test, test_if_mapped_with_MAP_SYNC) {
167169
umf_result = umfMemoryProviderAlloc(hProvider, size, 0, (void **)&buf);
168170
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
169171
ASSERT_NE(buf, nullptr);
170-
memset(buf, 0, size);
171-
172-
int fd = open("/proc/self/smaps", O_RDONLY);
173-
ASSERT_NE(fd, -1);
174-
175-
// number of bytes read from the file
176-
ssize_t nbytes = 1;
177-
// string starting from the path of the devdax
178-
char *devdax = nullptr;
179-
180-
// Read the "/proc/self/smaps" file
181-
// until the path of the devdax is found
182-
// or EOF is reached.
183-
while (nbytes > 0 && devdax == nullptr) {
184-
memset(buf, 0, nbytes); // erase previous data
185-
nbytes = read(fd, buf, size);
186-
// look for the path of the devdax
187-
devdax = strstr(buf, path);
188-
}
189172

190-
// String starting from the "sf" flag
191-
// marking that memory was mapped with the MAP_SYNC flag.
192-
char *sf_flag = nullptr;
193-
194-
if (devdax) {
195-
// look for the "VmFlags:" string
196-
char *VmFlags = strstr(devdax, "VmFlags:");
197-
if (VmFlags) {
198-
// look for the EOL
199-
char *eol = strstr(VmFlags, "\n");
200-
if (eol) {
201-
// End the VmFlags string at EOL.
202-
*eol = 0;
203-
// Now the VmFlags string contains only one line with all VmFlags.
204-
205-
// Look for the "sf" flag in VmFlags
206-
// marking that memory was mapped
207-
// with the MAP_SYNC flag.
208-
sf_flag = strstr(VmFlags, "sf");
209-
}
210-
}
211-
}
173+
bool flag_found = is_mapped_with_MAP_SYNC(path, buf, size);
212174

213175
umf_result = umfMemoryProviderFree(hProvider, buf, size);
214176
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_NOT_SUPPORTED);
215177

216178
umfMemoryProviderDestroy(hProvider);
217179

218180
// fail test if the "sf" flag was not found
219-
ASSERT_NE(sf_flag, nullptr);
181+
ASSERT_EQ(flag_found, true);
220182
}
221183

222184
// positive tests using test_alloc_free_success

test/provider_file_memory.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
#include "cpp_helpers.hpp"
88
#include "test_helpers.h"
9+
#ifndef _WIN32
10+
#include "test_helpers_linux.h"
11+
#endif
912

1013
#include <umf/memory_provider.h>
1114
#include <umf/providers/provider_file_memory.h>
@@ -121,6 +124,45 @@ static void test_alloc_failure(umf_memory_provider_handle_t provider,
121124

122125
// TESTS
123126

127+
// Test checking if FSDAX was mapped with the MAP_SYNC flag:
128+
// 1) Open and read the /proc/self/smaps file.
129+
// 2) Look for the section of the FSDAX file (for example /mnt/pmem1/file path).
130+
// 3) Check if the VmFlags of the /mnt/pmem1/file contains the "sf" flag
131+
// marking that the FSDAX file was mapped with the MAP_SYNC flag.
132+
TEST_F(test, test_if_mapped_with_MAP_SYNC) {
133+
umf_memory_provider_handle_t hProvider = nullptr;
134+
umf_result_t umf_result;
135+
136+
char *path = getenv("UMF_TESTS_FSDAX_PATH");
137+
if (path == nullptr || path[0] == 0) {
138+
GTEST_SKIP() << "Test skipped, UMF_TESTS_FSDAX_PATH is not set";
139+
}
140+
141+
auto params = umfFileMemoryProviderParamsDefault(path);
142+
params.visibility = UMF_MEM_MAP_SYNC;
143+
144+
umf_result = umfMemoryProviderCreate(umfFileMemoryProviderOps(), &params,
145+
&hProvider);
146+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
147+
ASSERT_NE(hProvider, nullptr);
148+
149+
char *buf;
150+
size_t size = 2 * 1024 * 1024; // 2MB
151+
umf_result = umfMemoryProviderAlloc(hProvider, size, 0, (void **)&buf);
152+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
153+
ASSERT_NE(buf, nullptr);
154+
155+
bool flag_found = is_mapped_with_MAP_SYNC(path, buf, size);
156+
157+
umf_result = umfMemoryProviderFree(hProvider, buf, size);
158+
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_NOT_SUPPORTED);
159+
160+
umfMemoryProviderDestroy(hProvider);
161+
162+
// fail test if the "sf" flag was not found
163+
ASSERT_EQ(flag_found, true);
164+
}
165+
124166
// positive tests using test_alloc_free_success
125167

126168
umf_file_memory_provider_params_t get_file_params_shared(char *path) {

0 commit comments

Comments
 (0)