Add a fibonacci test to cause context switches

The test creates tasks that pause until the children task have finished.
The value of the perf_event_paranoid file is checked to determine if we
can run the test.
This commit is contained in:
Rodrigo Arias 2024-03-21 16:58:08 +01:00
parent d1e4f46128
commit 37a567299e
3 changed files with 92 additions and 0 deletions

View File

@ -0,0 +1,20 @@
# Copyright (c) 2024 Barcelona Supercomputing Center (BSC)
# SPDX-License-Identifier: GPL-3.0-or-later
if(EXISTS "/proc/sys/kernel/perf_event_paranoid")
file(READ "/proc/sys/kernel/perf_event_paranoid" paranoid_raw)
string(REPLACE "\n" "" paranoid_value "${paranoid_raw}")
message(STATUS "Value of /proc/sys/kernel/perf_event_paranoid is ${paranoid_value}")
if(paranoid_value LESS_EQUAL 1)
message(STATUS "Value of perf_event_paranoid suitable for Kernel tests")
set(PERF_PARANOID_KERNEL ON)
else()
message(STATUS "Value of perf_event_paranoid NOT suitable for Kernel tests")
set(PERF_PARANOID_KERNEL OFF)
endif()
else()
message(STATUS "Missing /proc/sys/kernel/perf_event_paranoid")
set(PERF_PARANOID_KERNEL OFF)
endif()

View File

@ -56,3 +56,14 @@ nodes_rt_test(../nanos6/if0.c NAME if0 SORT)
nodes_rt_test(../nanos6/sched-add.c NAME sched-add SORT) nodes_rt_test(../nanos6/sched-add.c NAME sched-add SORT)
nodes_rt_test(../nanos6/taskloop.c NAME taskloop SORT) nodes_rt_test(../nanos6/taskloop.c NAME taskloop SORT)
nodes_rt_test(taskiter.c SORT) nodes_rt_test(taskiter.c SORT)
include(CheckPerfParanoid)
if (PERF_PARANOID_KERNEL)
message(STATUS "Enabling perf paranoid tests for NODES")
nodes_rt_test(fibonacci.c SORT LEVEL 3)
elseif(ENABLE_ALL_TESTS)
message(FATAL_ERROR "Cannot enable perf paranoid tests for NODES")
else()
message(STATUS "Disabling perf paranoid tests for NODES")
endif()

61
test/rt/nodes/fibonacci.c Normal file
View File

@ -0,0 +1,61 @@
/* Copyright (c) 2024 Barcelona Supercomputing Center (BSC)
* SPDX-License-Identifier: GPL-3.0-or-later */
#include "common.h"
#include "compat.h"
#include <stdio.h>
#include <stdlib.h>
static long
fib(long index)
{
long a, b;
if (index <= 1)
return index;
#pragma oss task shared(a) label("fibonacci")
a = fib(index-1);
#pragma oss task shared(b) label("fibonacci")
b = fib(index-2);
#pragma oss taskwait
return a + b;
}
int
main(void)
{
FILE *f = fopen("/proc/sys/kernel/perf_event_paranoid", "r");
if (f == NULL)
die("cannot open /proc/sys/kernel/perf_event_paranoid:");
char buf[16] = {0};
if (fread(buf, 1, 16, f) <= 0)
die("cannot read /proc/sys/kernel/perf_event_paranoid:");
fclose(f);
/* We need the value 1 or less:
* Access to performance monitoring and observability operations is limited.
* Consider adjusting /proc/sys/kernel/perf_event_paranoid setting to open
* access to performance monitoring and observability operations for processes
* without CAP_PERFMON, CAP_SYS_PTRACE or CAP_SYS_ADMIN Linux capability.
* More information can be found at 'Perf events and tool security' document:
* https://www.kernel.org/doc/html/latest/admin-guide/perf-security.html
* perf_event_paranoid setting is 3:
* -1: Allow use of (almost) all events by all users
* Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK
* >= 0: Disallow raw and ftrace function tracepoint access
* >= 1: Disallow CPU event access
* >= 2: Disallow kernel profiling
* To make the adjusted perf_event_paranoid setting permanent preserve it
* in /etc/sysctl.conf (e.g. kernel.perf_event_paranoid = <setting>)
*/
int level = atoi(buf);
if (level > 1)
die("requires perf_event_paranoid = 1 or less");
for (int i = 0; i < 30; i++)
fib(5);
#pragma oss taskwait
return 0;
}