Skip to content

Commit 79c8d0a

Browse files
committed
add freebsd specific memory usage tests
1 parent bfc585c commit 79c8d0a

4 files changed

Lines changed: 107 additions & 18 deletions

File tree

src/common/memusage.c

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@
3333
#include <stdint.h>
3434

3535
#if defined(OS_FREEBSD)
36-
#include <fcntl.h>
37-
#include <kvm.h>
3836
#include <limits.h>
39-
#include <paths.h>
4037
#include <sys/param.h>
4138
#include <sys/types.h>
4239
#include <sys/sysctl.h>
@@ -262,33 +259,39 @@ static int freebsd_sys_memusage(struct sys_memusage *mu)
262259

263260
static int freebsd_proc_memusage(struct proc_memusage *mu)
264261
{
265-
int count;
266-
kvm_t *kd;
267262
pid_t mypid;
268-
char errbuf[LINE_MAX];
269-
struct kinfo_proc *procinfo;
263+
struct kinfo_proc procinfo;
264+
size_t size;
265+
size_t page_size;
266+
int mib[4];
270267

271268
mypid = getpid();
272-
kd = kvm_openfiles(NULL, _PATH_DEVNULL, NULL, O_RDONLY, errbuf);
273-
274-
if (!kd)
269+
size = sizeof(procinfo);
270+
memset(&procinfo, 0, sizeof(procinfo));
271+
mib[0] = CTL_KERN;
272+
mib[1] = KERN_PROC;
273+
mib[2] = KERN_PROC_PID;
274+
mib[3] = mypid;
275+
276+
if (sysctl(mib, 4, &procinfo, &size, NULL, 0) < 0)
275277
return -1;
276278

277-
procinfo = kvm_getprocs(kd, KERN_PROC_PID, mypid, &count);
278-
279-
if (!procinfo)
279+
if (size == 0) {
280+
errno = ESRCH;
280281
return -1;
282+
}
281283

282-
mu->mu_rss = procinfo->ki_rssize;
283-
mu->mu_text = procinfo->ki_tsize;
284-
mu->mu_data = procinfo->ki_dsize;
285-
mu->mu_stack = procinfo->ki_ssize;
284+
page_size = (size_t)getpagesize();
285+
mu->mu_rss = BYTES_TO_KIB((uint64_t)procinfo.ki_rssize * page_size);
286+
mu->mu_text = BYTES_TO_KIB((uint64_t)procinfo.ki_tsize * page_size);
287+
mu->mu_data = BYTES_TO_KIB((uint64_t)procinfo.ki_dsize * page_size);
288+
mu->mu_stack = BYTES_TO_KIB((uint64_t)procinfo.ki_ssize * page_size);
286289

287290
/* ki_swrss is the resident set size before last swap, this
288291
* is the closest approximation to Linux's "VmHWM" which is the
289292
* peak resident set size of the process.
290293
*/
291-
mu->mu_hwm = procinfo->ki_swrss;
294+
mu->mu_hwm = BYTES_TO_KIB((uint64_t)procinfo.ki_swrss * page_size);
292295

293296
/* Not exposed on FreeBSD */
294297
mu->mu_lib = 0;

tests/API/probes/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,14 @@ target_include_directories(test_memusage_platform PUBLIC
5858
"${CMAKE_SOURCE_DIR}/src/common"
5959
)
6060
add_oscap_test("test_memusage_platform.sh" LABELS api probes freebsd macos linux)
61+
62+
add_oscap_test_executable(test_memusage_freebsd_stress
63+
"test_memusage_freebsd_stress.c"
64+
"${CMAKE_SOURCE_DIR}/src/common/memusage.c"
65+
"${CMAKE_SOURCE_DIR}/src/common/bfind.c"
66+
)
67+
target_include_directories(test_memusage_freebsd_stress PUBLIC
68+
"${CMAKE_SOURCE_DIR}/src/common"
69+
)
70+
target_link_libraries(test_memusage_freebsd_stress Threads::Threads)
71+
add_oscap_test("test_memusage_freebsd_stress.sh" LABELS api probes freebsd)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// SPDX-License-Identifier: LGPL-2.1-or-later
2+
3+
#ifdef HAVE_CONFIG_H
4+
#include <config.h>
5+
#endif
6+
7+
#include <errno.h>
8+
#include <pthread.h>
9+
#include <stdio.h>
10+
11+
#include "memusage.h"
12+
13+
#define STRESS_THREADS 4
14+
#define STRESS_ITERATIONS 2000
15+
16+
struct thread_result {
17+
int err;
18+
};
19+
20+
static void *worker(void *arg)
21+
{
22+
struct thread_result *result = arg;
23+
int i;
24+
25+
for (i = 0; i < STRESS_ITERATIONS; ++i) {
26+
struct proc_memusage proc_mu = {0};
27+
28+
if (oscap_proc_memusage(&proc_mu) != 0) {
29+
result->err = errno ? errno : -1;
30+
return NULL;
31+
}
32+
}
33+
34+
result->err = 0;
35+
return NULL;
36+
}
37+
38+
int main(void)
39+
{
40+
#if defined(OS_FREEBSD)
41+
pthread_t threads[STRESS_THREADS];
42+
struct thread_result results[STRESS_THREADS];
43+
int i;
44+
45+
for (i = 0; i < STRESS_THREADS; ++i) {
46+
results[i].err = 0;
47+
if (pthread_create(&threads[i], NULL, worker, &results[i]) != 0) {
48+
perror("pthread_create");
49+
return 1;
50+
}
51+
}
52+
53+
for (i = 0; i < STRESS_THREADS; ++i) {
54+
if (pthread_join(threads[i], NULL) != 0) {
55+
perror("pthread_join");
56+
return 1;
57+
}
58+
if (results[i].err != 0) {
59+
fprintf(stderr, "oscap_proc_memusage failed in worker %d with errno=%d\n", i, results[i].err);
60+
return 1;
61+
}
62+
}
63+
#endif
64+
65+
return 0;
66+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
. $builddir/tests/test_common.sh
4+
5+
if [ "$(uname)" != "FreeBSD" ] ; then
6+
exit 255
7+
fi
8+
9+
./test_memusage_freebsd_stress

0 commit comments

Comments
 (0)