Skip to content

Commit 64de550

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

File tree

6 files changed

+153
-46
lines changed

6 files changed

+153
-46
lines changed

.github/workflows/dax.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ permissions:
2323

2424
env:
2525
DEVDAX_NAMESPACE : "0.0"
26+
FSDAX_NAMESPACE : "1.0"
27+
FSDAX_PMEM: "pmem1"
28+
UMF_TESTS_FSDAX_PATH: "/mnt/pmem1/file"
2629
BUILD_DIR : "${{github.workspace}}/build"
2730
INSTL_DIR : "${{github.workspace}}/../install-dir"
2831

@@ -46,6 +49,16 @@ jobs:
4649
echo UMF_TESTS_DEVDAX_PATH="/dev/dax${{env.DEVDAX_NAMESPACE}}"
4750
echo UMF_TESTS_DEVDAX_SIZE="$(ndctl list --namespace=namespace${{env.DEVDAX_NAMESPACE}} | grep size | cut -d':' -f2 | cut -d',' -f1)"
4851
52+
- name: Check configuration of the FSDAX
53+
run: |
54+
echo FSDAX_NAMESPACE="${{env.FSDAX_NAMESPACE}}"
55+
echo UMF_TESTS_FSDAX_PATH="${{env.UMF_TESTS_FSDAX_PATH}}"
56+
ndctl list --namespace=namespace${{env.FSDAX_NAMESPACE}}
57+
ls -al /dev/${{env.FSDAX_PMEM}} /mnt/${{env.FSDAX_PMEM}}
58+
mount | grep -e "/dev/${{env.FSDAX_PMEM}}"
59+
touch ${{env.UMF_TESTS_FSDAX_PATH}}
60+
rm -f ${{env.UMF_TESTS_FSDAX_PATH}}
61+
4962
- name: Checkout
5063
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
5164
with:
@@ -80,3 +93,9 @@ jobs:
8093
UMF_TESTS_DEVDAX_PATH="/dev/dax${{env.DEVDAX_NAMESPACE}}"
8194
UMF_TESTS_DEVDAX_SIZE="$(ndctl list --namespace=namespace${{env.DEVDAX_NAMESPACE}} | grep size | cut -d':' -f2 | cut -d',' -f1)"
8295
ctest -C ${{matrix.build_type}} -R devdax -V
96+
97+
- name: Run the FSDAX tests
98+
working-directory: ${{env.BUILD_DIR}}
99+
run: >
100+
UMF_TESTS_FSDAX_PATH=${{env.UMF_TESTS_FSDAX_PATH}}
101+
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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
// Check if the file given by the 'path' argument was mapped with the MAP_SYNC flag:
16+
// 1) Open and read the /proc/self/smaps file.
17+
// 2) Look for the section of the 'path' file.
18+
// 3) Check if the VmFlags of the 'path' file contains the "sf" flag
19+
// marking that the file was mapped with the MAP_SYNC flag.
20+
bool is_mapped_with_MAP_SYNC(char *path, char *buf, size_t size_buf) {
21+
memset(buf, 0, size_buf);
22+
23+
int fd = open("/proc/self/smaps", O_RDONLY);
24+
if (fd == -1) {
25+
return false;
26+
}
27+
28+
// number of bytes read from the file
29+
ssize_t nbytes = 1;
30+
// string starting from the path of the smaps
31+
char *smaps = NULL;
32+
33+
// Read the "/proc/self/smaps" file
34+
// until the path of the smaps is found
35+
// or EOF is reached.
36+
while (nbytes > 0 && smaps == NULL) {
37+
memset(buf, 0, nbytes); // erase previous data
38+
nbytes = read(fd, buf, size_buf);
39+
// look for the path of the smaps
40+
smaps = strstr(buf, path);
41+
}
42+
43+
// String starting from the "sf" flag
44+
// marking that memory was mapped with the MAP_SYNC flag.
45+
char *sf_flag = NULL;
46+
47+
if (smaps) {
48+
// look for the "VmFlags:" string
49+
char *VmFlags = strstr(smaps, "VmFlags:");
50+
if (VmFlags) {
51+
// look for the EOL
52+
char *eol = strstr(VmFlags, "\n");
53+
if (eol) {
54+
// End the VmFlags string at EOL.
55+
*eol = 0;
56+
// Now the VmFlags string contains only one line with all VmFlags.
57+
58+
// Look for the "sf" flag in VmFlags
59+
// marking that memory was mapped
60+
// with the MAP_SYNC flag.
61+
sf_flag = strstr(VmFlags, "sf");
62+
}
63+
}
64+
}
65+
66+
return (sf_flag != NULL);
67+
}

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 & 46 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>
@@ -137,10 +139,6 @@ static void test_alloc_failure(umf_memory_provider_handle_t provider,
137139
// TESTS
138140

139141
// Test checking if devdax was mapped with the MAP_SYNC flag:
140-
// 1) Open and read the /proc/self/smaps file.
141-
// 2) Look for the section of the devdax (the /dev/daxX.Y path).
142-
// 3) Check if the VmFlags of the /dev/daxX.Y contains the "sf" flag
143-
// marking that the devdax was mapped with the MAP_SYNC flag.
144142
TEST_F(test, test_if_mapped_with_MAP_SYNC) {
145143
umf_memory_provider_handle_t hProvider = nullptr;
146144
umf_result_t umf_result;
@@ -167,56 +165,16 @@ TEST_F(test, test_if_mapped_with_MAP_SYNC) {
167165
umf_result = umfMemoryProviderAlloc(hProvider, size, 0, (void **)&buf);
168166
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
169167
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-
}
189168

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-
}
169+
bool flag_found = is_mapped_with_MAP_SYNC(path, buf, size);
212170

213171
umf_result = umfMemoryProviderFree(hProvider, buf, size);
214172
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_NOT_SUPPORTED);
215173

216174
umfMemoryProviderDestroy(hProvider);
217175

218176
// fail test if the "sf" flag was not found
219-
ASSERT_NE(sf_flag, nullptr);
177+
ASSERT_EQ(flag_found, true);
220178
}
221179

222180
// positive tests using test_alloc_free_success

test/provider_file_memory.cpp

Lines changed: 38 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,41 @@ 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+
TEST_F(test, test_if_mapped_with_MAP_SYNC) {
129+
umf_memory_provider_handle_t hProvider = nullptr;
130+
umf_result_t umf_result;
131+
132+
char *path = getenv("UMF_TESTS_FSDAX_PATH");
133+
if (path == nullptr || path[0] == 0) {
134+
GTEST_SKIP() << "Test skipped, UMF_TESTS_FSDAX_PATH is not set";
135+
}
136+
137+
auto params = umfFileMemoryProviderParamsDefault(path);
138+
params.visibility = UMF_MEM_MAP_SYNC;
139+
140+
umf_result = umfMemoryProviderCreate(umfFileMemoryProviderOps(), &params,
141+
&hProvider);
142+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
143+
ASSERT_NE(hProvider, nullptr);
144+
145+
char *buf;
146+
size_t size = 2 * 1024 * 1024; // 2MB
147+
umf_result = umfMemoryProviderAlloc(hProvider, size, 0, (void **)&buf);
148+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
149+
ASSERT_NE(buf, nullptr);
150+
151+
bool flag_found = is_mapped_with_MAP_SYNC(path, buf, size);
152+
153+
umf_result = umfMemoryProviderFree(hProvider, buf, size);
154+
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_NOT_SUPPORTED);
155+
156+
umfMemoryProviderDestroy(hProvider);
157+
158+
// fail test if the "sf" flag was not found
159+
ASSERT_EQ(flag_found, true);
160+
}
161+
124162
// positive tests using test_alloc_free_success
125163

126164
umf_file_memory_provider_params_t get_file_params_shared(char *path) {

0 commit comments

Comments
 (0)