All of lore.kernel.org
 help / color / mirror / Atom feed
From: Anand K Mistry <amistry@google.com>
To: x86@kernel.org
Cc: joelaf@google.com, asteinhauser@google.com, bp@alien8.de,
	tglx@linutronix.de, Anand K Mistry <amistry@google.com>,
	Shuah Khan <shuah@kernel.org>,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org
Subject: [RFC PATCH v2 2/2] selftests: Benchmark for the cost of disabling IB speculation
Date: Thu, 29 Apr 2021 18:44:10 +1000	[thread overview]
Message-ID: <20210429184101.RFC.v2.2.I0d44ed3cc06ff2924c1b5e418e0599e1c1731c3c@changeid> (raw)
In-Reply-To: <20210429084410.783998-1-amistry@google.com>

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;
+}
-- 
2.31.1.498.g6c1eba8ee3d-goog


      parent reply	other threads:[~2021-04-29  8:45 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-29  8:44 [RFC PATCH v2 0/2] x86/speculation: Add finer control for when to issue IBPB Anand K Mistry
2021-04-29  8:44 ` [RFC PATCH v2 1/2] x86/speculation: Allow per-process control of " Anand K Mistry
2021-05-03  8:48   ` Thomas Gleixner
2021-05-11  8:39     ` Anand K. Mistry
2021-04-29  8:44 ` Anand K Mistry [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210429184101.RFC.v2.2.I0d44ed3cc06ff2924c1b5e418e0599e1c1731c3c@changeid \
    --to=amistry@google.com \
    --cc=asteinhauser@google.com \
    --cc=bp@alien8.de \
    --cc=joelaf@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=shuah@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.