xref: /linux/tools/testing/selftests/rcutorture/bin/cpus2use.sh (revision a1c3be890440a1769ed6f822376a3e3ab0d42994)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0+
3#
4# Get an estimate of how CPU-hoggy to be.
5#
6# Usage: cpus2use.sh
7#
8# Copyright (C) IBM Corporation, 2013
9#
10# Authors: Paul E. McKenney <paulmck@linux.ibm.com>
11
12if test -n "$TORTURE_ALLOTED_CPUS"
13then
14	echo $TORTURE_ALLOTED_CPUS
15	exit 0
16fi
17ncpus=`grep '^processor' /proc/cpuinfo | wc -l`
18if mpstat -V > /dev/null 2>&1
19then
20	idlecpus=`mpstat | tail -1 | \
21		awk -v ncpus=$ncpus '{ print ncpus * ($7 + $NF) / 100 }'`
22else
23	# No mpstat command, so use all available CPUs.
24	echo The mpstat command is not available, so greedily using all CPUs.
25	idlecpus=$ncpus
26fi
27awk -v ncpus=$ncpus -v idlecpus=$idlecpus < /dev/null '
28BEGIN {
29	cpus2use = idlecpus;
30	if (cpus2use < 1)
31		cpus2use = 1;
32	if (cpus2use < ncpus / 10)
33		cpus2use = ncpus / 10;
34	if (cpus2use == int(cpus2use))
35		cpus2use = int(cpus2use)
36	else
37		cpus2use = int(cpus2use) + 1
38	print cpus2use;
39}'
40
41