[RFC PATCH v9 6/8] [squash] Test pidns translation cache
Ákos Uzonyi
uzonyi.akos at gmail.com
Sun Aug 16 22:18:49 UTC 2020
---
tests/.gitignore | 1 +
tests/Makefile.am | 2 ++
tests/pidns-cache.c | 62 ++++++++++++++++++++++++++++++++++++++++++
tests/pidns-cache.test | 16 +++++++++++
4 files changed, 81 insertions(+)
create mode 100644 tests/pidns-cache.c
create mode 100755 tests/pidns-cache.test
diff --git a/tests/.gitignore b/tests/.gitignore
index 4d1153fc..c824ca5e 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -525,6 +525,7 @@ pidfd_open-yy
pidfd_open--pidns-translation
pidfd_send_signal
pidfd_send_signal--pidns-translation
+pidns-cache
pipe
pipe2
pkey_alloc
diff --git a/tests/Makefile.am b/tests/Makefile.am
index f2c5da83..8aaa0cae 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -190,6 +190,7 @@ check_PROGRAMS = $(PURE_EXECUTABLES) \
perf_event_open_unabbrev \
pidfd_open--pidns-translation \
pidfd_send_signal--pidns-translation \
+ pidns-cache \
poll-P \
ppoll-P \
ppoll-v \
@@ -425,6 +426,7 @@ MISC_TESTS = \
opipe.test \
options-syntax.test \
pc.test \
+ pidns-cache.test \
printpath-umovestr-legacy.test \
printstrn-umoven-legacy.test \
qual_fault-syntax.test \
diff --git a/tests/pidns-cache.c b/tests/pidns-cache.c
new file mode 100644
index 00000000..5996abc1
--- /dev/null
+++ b/tests/pidns-cache.c
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2020 The strace developers.
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "tests.h"
+#include "scno.h"
+#include "pidns.h"
+
+#if defined __NR_getpid && (!defined __NR_getxpid || __NR_getxpid != __NR_getpid)
+
+# include <stdio.h>
+# include <unistd.h>
+# include <sys/time.h>
+
+# define SYSCALL_COUNT 50
+
+/**
+ * Max us per syscall that translation should take. 1 ms is plenty enough time
+ * for the cache, and very short for translating without the cache.
+ */
+# define MAX_TRANSLATION_US_PER_SYSCALL 1000
+
+static long
+execute_syscalls(void)
+{
+ /* Load our PID in the cache */
+ syscall(__NR_getpid);
+
+ struct timeval stop, start;
+ gettimeofday(&start, NULL);
+
+ for (int i = 0; i < SYSCALL_COUNT; i++)
+ syscall(__NR_getpid);
+
+ gettimeofday(&stop, NULL);
+
+ return (stop.tv_usec - start.tv_usec) +
+ (stop.tv_sec - start.tv_sec) * 1000000;
+}
+
+int
+main(void)
+{
+ long max_us = execute_syscalls() +
+ SYSCALL_COUNT * MAX_TRANSLATION_US_PER_SYSCALL;
+
+ pidns_test_init();
+
+ if (execute_syscalls() > max_us)
+ error_msg_and_fail("pidns translation took too long");
+
+ return 0;
+}
+
+#else
+
+SKIP_MAIN_UNDEFINED("__NR_getpid")
+
+#endif
diff --git a/tests/pidns-cache.test b/tests/pidns-cache.test
new file mode 100755
index 00000000..fa98ed8c
--- /dev/null
+++ b/tests/pidns-cache.test
@@ -0,0 +1,16 @@
+#!/bin/sh
+#
+# Test pidns translation cache.
+#
+# Copyright (c) 2020 The strace developers.
+# All rights reserved.
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+. "${srcdir=.}/init.sh"
+
+require_min_kernel_version_or_skip 4.9
+check_prog timeout
+
+run_prog > /dev/null
+run_strace --pidns-translation -f -e trace=getpid $args
--
2.28.0
More information about the Strace-devel
mailing list