Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757485AbcJPTIb (ORCPT ); Sun, 16 Oct 2016 15:08:31 -0400 Received: from Galois.linutronix.de ([146.0.238.70]:35219 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754199AbcJPTIW (ORCPT ); Sun, 16 Oct 2016 15:08:22 -0400 From: Sebastian Andrzej Siewior To: Arnaldo Carvalho de Melo Cc: Peter Zijlstra , Ingo Molnar , linux-kernel@vger.kernel.org, Davidlohr Bueso , Sebastian Andrzej Siewior Subject: [PATCH 2/2] perf bench futex: add NUMA support Date: Sun, 16 Oct 2016 21:08:03 +0200 Message-Id: <20161016190803.3392-2-bigeasy@linutronix.de> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20161016190803.3392-1-bigeasy@linutronix.de> References: <20161016190803.3392-1-bigeasy@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 5770 Lines: 194 By default the application uses malloc() and all available CPUs. This patch introduces NUMA support which means: - memory is allocated node local via numa_alloc_local() - all CPUs of the specified NUMA node are used. This is also true if the number of threads set is greater than the number of CPUs available on this node. Signed-off-by: Sebastian Andrzej Siewior --- tools/perf/bench/Build | 4 ++ tools/perf/bench/futex-hash.c | 87 ++++++++++++++++++++++++++++++++++++++-= ---- 2 files changed, 81 insertions(+), 10 deletions(-) diff --git a/tools/perf/bench/Build b/tools/perf/bench/Build index 60bf11943047..9e6e518d7d62 100644 --- a/tools/perf/bench/Build +++ b/tools/perf/bench/Build @@ -1,3 +1,7 @@ +ifdef CONFIG_NUMA +CFLAGS_futex-hash.o +=3D -DCONFIG_NUMA=3D1 +endif + perf-y +=3D sched-messaging.o perf-y +=3D sched-pipe.o perf-y +=3D mem-functions.o diff --git a/tools/perf/bench/futex-hash.c b/tools/perf/bench/futex-hash.c index d9e5e80bb4d0..8db4a5bd6a4e 100644 --- a/tools/perf/bench/futex-hash.c +++ b/tools/perf/bench/futex-hash.c @@ -25,6 +25,9 @@ =20 #include #include +#ifdef CONFIG_NUMA +#include +#endif =20 static unsigned int nthreads =3D 0; static unsigned int nsecs =3D 10; @@ -32,6 +35,7 @@ static unsigned int nsecs =3D 10; static unsigned int nfutexes =3D 1024; static bool fshared =3D false, done =3D false, silent =3D false; static int futex_flag =3D 0; +static int numa_node =3D -1; =20 struct timeval start, end, runtime; static pthread_mutex_t thread_lock; @@ -55,9 +59,28 @@ static const struct option options[] =3D { OPT_UINTEGER('f', "futexes", &nfutexes, "Specify amount of futexes per th= reads"), OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data= /details"), OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of pr= ivate ones"), +#ifdef CONFIG_NUMA + OPT_INTEGER( 'n', "numa", &numa_node, "Specify the NUMA node"), +#endif OPT_END() }; =20 +#ifndef CONFIG_NUMA +static int numa_run_on_node(int node __maybe_unused) { return 0; } +static int numa_node_of_cpu(int node __maybe_unused) { return 0; } +static void *numa_alloc_local(size_t size) { return malloc(size); } +static void numa_free(void *p, size_t size __maybe_unused) { return free(p= ); } +#endif + +static bool cpu_is_local(int cpu) +{ + if (numa_node < 0) + return true; + if (numa_node_of_cpu(cpu) =3D=3D numa_node) + return true; + return false; +} + static const char * const bench_futex_hash_usage[] =3D { "perf bench futex hash ", NULL @@ -123,6 +146,8 @@ int bench_futex_hash(int argc, const char **argv, unsigned int i, ncpus; pthread_attr_t thread_attr; struct worker *worker =3D NULL; + char *node_str =3D NULL; + unsigned int cpunum; =20 argc =3D parse_options(argc, argv, options, bench_futex_hash_usage, 0); if (argc) { @@ -136,18 +161,50 @@ int bench_futex_hash(int argc, const char **argv, act.sa_sigaction =3D toggle_done; sigaction(SIGINT, &act, NULL); =20 - if (!nthreads) /* default to the number of CPUs */ - nthreads =3D ncpus; + if (!nthreads) { + /* default to the number of CPUs per NUMA node */ + if (numa_node < 0) { + nthreads =3D ncpus; + } else { + for (i =3D 0; i < ncpus; i++) { + if (cpu_is_local(i)) + nthreads++; + } + if (!nthreads) + err(EXIT_FAILURE, "No online CPUs for this node"); + } + } else { + int cpu_available =3D 0; =20 - worker =3D calloc(nthreads, sizeof(*worker)); + for (i =3D 0; i < ncpus && !cpu_available; i++) { + if (cpu_is_local(i)) + cpu_available =3D 1; + } + if (!cpu_available) + err(EXIT_FAILURE, "No online CPUs for this node"); + } + + if (numa_node >=3D 0) { + ret =3D numa_run_on_node(numa_node); + if (ret < 0) + err(EXIT_FAILURE, "numa_run_on_node"); + ret =3D asprintf(&node_str, " on node %d", numa_node); + if (ret < 0) + err(EXIT_FAILURE, "numa_node, asprintf"); + } + + worker =3D numa_alloc_local(nthreads * sizeof(*worker)); if (!worker) goto errmem; =20 if (!fshared) futex_flag =3D FUTEX_PRIVATE_FLAG; =20 - printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futex= es for %d secs.\n\n", - getpid(), nthreads, nfutexes, fshared ? "shared":"private", nsecs); + printf("Run summary [PID %d]: %d threads%s, each operating on %d [%s] fut= exes for %d secs.\n\n", + getpid(), nthreads, + node_str ? : "", + nfutexes, fshared ? "shared":"private", + nsecs); =20 init_stats(&throughput_stats); pthread_mutex_init(&thread_lock, NULL); @@ -157,14 +214,24 @@ int bench_futex_hash(int argc, const char **argv, threads_starting =3D nthreads; pthread_attr_init(&thread_attr); gettimeofday(&start, NULL); - for (i =3D 0; i < nthreads; i++) { + for (cpunum =3D 0, i =3D 0; i < nthreads; i++, cpunum++) { + + do { + if (cpu_is_local(cpunum)) + break; + cpunum++; + if (cpunum > ncpus) + cpunum =3D 0; + } while (1); + worker[i].tid =3D i; - worker[i].futex =3D calloc(nfutexes, sizeof(*worker[i].futex)); + worker[i].futex =3D numa_alloc_local(nfutexes * + sizeof(*worker[i].futex)); if (!worker[i].futex) goto errmem; =20 CPU_ZERO(&cpu); - CPU_SET(i % ncpus, &cpu); + CPU_SET(cpunum % ncpus, &cpu); =20 ret =3D pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cp= u); if (ret) @@ -211,12 +278,12 @@ int bench_futex_hash(int argc, const char **argv, &worker[i].futex[nfutexes-1], t); } =20 - free(worker[i].futex); + numa_free(worker[i].futex, nfutexes * sizeof(*worker[i].futex)); } =20 print_summary(); =20 - free(worker); + numa_free(worker, nthreads * sizeof(*worker)); return ret; errmem: err(EXIT_FAILURE, "calloc"); --=20 2.9.3