This is a simple benchmark for determining the cost of disabling IB speculation. It forks a child process and does a simple ping-pong using pipes between the parent and child. The child process can have IB speculation disabled by running with 'd' as the first argument.
The test increases the number of iterations until the iterations take at least 1 second, to minimise noise.
This file is NOT intended for inclusion in the kernel source. It is presented here as a patch for reference and for others to replicate results.
The binary should be run with 'taskset' and pinned to a single core, since the goal is to benchmark process switching on a single core.
Signed-off-by: Anand K Mistry amistry@google.com ---
(no changes since v1)
.../testing/selftests/ib_spec/ib_spec_bench.c | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 tools/testing/selftests/ib_spec/ib_spec_bench.c
diff --git a/tools/testing/selftests/ib_spec/ib_spec_bench.c b/tools/testing/selftests/ib_spec/ib_spec_bench.c new file mode 100644 index 000000000000..e8eab910a9d0 --- /dev/null +++ b/tools/testing/selftests/ib_spec/ib_spec_bench.c @@ -0,0 +1,109 @@ +#include <stdio.h> +#include <time.h> +#include <stdint.h> +#include <assert.h> +#include <unistd.h> +#include <sys/prctl.h> + +#define PR_SPEC_IBPB_MODE 2 +#define PR_SPEC_IBPB_MODE_DEFAULT 0 +#define PR_SPEC_IBPB_MODE_SANDBOX 1 +#define PR_SPEC_IBPB_MODE_PROTECT 2 + +int64_t get_time_us() { + struct timespec ts = {0}; + assert(clock_gettime(CLOCK_MONOTONIC, &ts) == 0); + return (ts.tv_sec * 1000000) + (ts.tv_nsec/1000); +} + +void pong(int read_fd, int write_fd) { + int ret; + char buf; + + while (1) { + ret = read(read_fd, &buf, 1); + if (ret == 0) + return; + assert(ret == 1); + + assert(write(write_fd, &buf, 1) == 1); + } +} + +void ping_once(int write_fd, int read_fd) { + char buf = 42; + assert(write(write_fd, &buf, 1) == 1); + assert(read(read_fd, &buf, 1) == 1); +} + +int64_t ping_multi(int iters, int write_fd, int read_fd) { + int64_t start_time = get_time_us(); + for (int i = 0; i < iters; i++) + ping_once(write_fd, read_fd); + return get_time_us() - start_time; +} + +void run_test(int write_fd, int read_fd) { + int64_t iters = 1; + int64_t t; + for (int i = 0; i < 60; i++) { + t = ping_multi(iters, write_fd, read_fd); + printf("iters: %d, t: %dus, iter/sec: %d, us/iter: %d\n", + iters, t, (iters * 1000000LL) / t, t/iters); + + if (t > 1000000) + break; + iters <<= 1; + } +} + +int main(int argc, char* argv[]) { + int fds_ping[2], fds_pong[2]; + assert(pipe(fds_ping) == 0); + assert(pipe(fds_pong) == 0); + + int disable_ib = 0; + int spec_ibpb_mode = 0; + + if (argc > 1) { + int done = 0; + for (int i = 0; !done; i++) { + switch (argv[1][i]) { + case 0: + done = 1; + break; + case 'd': + disable_ib = 1; + break; + case 's': + spec_ibpb_mode = PR_SPEC_IBPB_MODE_SANDBOX; + break; + case 'p': + spec_ibpb_mode = PR_SPEC_IBPB_MODE_PROTECT; + break; + } + } + } + + pid_t pid = fork(); + assert(pid >= 0); + if (!pid) { + if (prctl(PR_SET_SPECULATION_CTRL, + PR_SPEC_IBPB_MODE, spec_ibpb_mode, 0, 0)) { + perror("Unable to set IBPB mode"); + } + + if (disable_ib) + assert(prctl(PR_SET_SPECULATION_CTRL, + PR_SPEC_INDIRECT_BRANCH, + PR_SPEC_DISABLE, 0, 0) == 0); + + close(fds_ping[1]); + pong(fds_ping[0], fds_pong[1]); + } else { + run_test(fds_ping[1], fds_pong[0]); + close(fds_ping[1]); + } + + return 0; +}