aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@oath.com>2022-02-04 12:24:41 +0000
committerHåvard Pettersen <havardpe@oath.com>2022-02-04 12:30:29 +0000
commitc0d48f230485e5003f720a6fbfcddae5f5672195 (patch)
treeaa67ac8fb01ff3e864e2292856221ae223b59573
parente04dae38c31f2012e3bcbd3c1cba5ab74a0f9dd0 (diff)
test per-thread nice values
-rw-r--r--vespalib/CMakeLists.txt1
-rw-r--r--vespalib/src/tests/nice/CMakeLists.txt8
-rw-r--r--vespalib/src/tests/nice/nice_test.cpp44
3 files changed, 53 insertions, 0 deletions
diff --git a/vespalib/CMakeLists.txt b/vespalib/CMakeLists.txt
index 5e9426ba6ee..09cf354d027 100644
--- a/vespalib/CMakeLists.txt
+++ b/vespalib/CMakeLists.txt
@@ -85,6 +85,7 @@ vespa_define_module(
src/tests/net/tls/policy_checking_certificate_verifier
src/tests/net/tls/protocol_snooping
src/tests/net/tls/transport_options
+ src/tests/nice
src/tests/objects/nbostream
src/tests/optimized
src/tests/overload
diff --git a/vespalib/src/tests/nice/CMakeLists.txt b/vespalib/src/tests/nice/CMakeLists.txt
new file mode 100644
index 00000000000..e813637002f
--- /dev/null
+++ b/vespalib/src/tests/nice/CMakeLists.txt
@@ -0,0 +1,8 @@
+# Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+vespa_add_executable(vespalib_nice_test_app TEST
+ SOURCES
+ nice_test.cpp
+ DEPENDS
+ vespalib
+)
+vespa_add_test(NAME vespalib_nice_test_app COMMAND vespalib_nice_test_app)
diff --git a/vespalib/src/tests/nice/nice_test.cpp b/vespalib/src/tests/nice/nice_test.cpp
new file mode 100644
index 00000000000..15234380375
--- /dev/null
+++ b/vespalib/src/tests/nice/nice_test.cpp
@@ -0,0 +1,44 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include <vespa/vespalib/testkit/test_kit.h>
+
+#include <unistd.h>
+
+// Reducing the nice value or setting it directly with setpriority
+// requires extra permissions/capabilities. This means that we can use
+// nice values to prioritize between threads, but not at task-level
+// within a thread.
+
+void set_nice_value(int value, int thread) {
+ errno = 0;
+ int old_nice = nice(0);
+ ASSERT_EQUAL(errno, 0);
+ ASSERT_GREATER_EQUAL(value, old_nice);
+ int new_nice = nice(value - old_nice);
+ ASSERT_EQUAL(errno, 0);
+ ASSERT_EQUAL(new_nice, value);
+ fprintf(stderr, "nice value for thread %d changed: %d->%d\n", thread, old_nice, new_nice);
+}
+
+int get_nice_value(int thread) {
+ errno = 0;
+ int old_nice = nice(0);
+ ASSERT_EQUAL(errno, 0);
+ fprintf(stderr, "nice value for thread %d is: %d\n", thread, old_nice);
+ return old_nice;
+}
+
+int init_nice = get_nice_value(-1);
+
+// Linux has a separate nice value per thread. Note that this is not
+// portable and conflicts with the POSIX API. Also note that we can
+// only test once since nice values cannot be reduced again and test
+// threads are re-used.
+
+TEST_MT("require that nice value is tracked per thread", 5) {
+ set_nice_value(init_nice + thread_id, thread_id);
+ TEST_BARRIER();
+ EXPECT_EQUAL(get_nice_value(thread_id), int(init_nice + thread_id));
+}
+
+TEST_MAIN() { TEST_RUN_ALL(); }