Task to busy one CPU 100% for a period of time?

Ian Smith smithi at nimnet.asn.au
Wed Jan 13 13:58:04 UTC 2016


On Tue, 12 Jan 2016 13:02:26 -0800, darwinsurvivor at gmail.com wrote:

 > Have you looked at the sysutils/stress utility? It may do what you need.
 > 
 > 
 > ~Doug

I have now, thanks :)  It works well, given I've only tested the -c CPU 
load option so far.  If I'd found it sooner, I might not have bothered 
compiling Kevin's little program and writing the below script to do 
about the same thing with it .. but I'm grateful for the exercise!

 > On Tue, Jan 12, 2016 at 10:36 AM, Ian Smith <smithi at nimnet.asn.au> wrote:
 > > On Mon, 11 Jan 2016 15:38:32 -0500, kpneal at pobox.com wrote:
[..]
 > >  > cc -c main.c
 > >  > cc -c dummy.c
 > >  > cc -o load1 main.o dummy.o
 > >  >
 > >  > One invocation of this program should consume an entire CPU and therefore
 > >  > raise the load average by 1.00. Run as many as you like.
 > >  >
 > >  > (The reason for the two compilations is to avoid having any compiler
 > >  > optimize away the for loop. Just to be safe.)
 > >
 > > Thankyou Kevin.  Works a treat, so far tested 8 at once, loadavg = 8.00
 > >
 > > I'll follow up hopefully tomorrow with results of a sh script to run a
 > > given number of instances for a given time, needing a bit more testing.

So here; both this and stress give equivalent results with 2 to 8 tasks; 
over 4 I'm running into overheating (~90C), acpi_thermal kicking in, CPU 
slowing apparently - but it is summer, and still ~28C|82F at midnight!

#!/bin/sh
# 12/1/16 add2load.sh thanks Kevin Neal <kpneal at pobox.com> for load1 C code
me=`basename $0`
[ ! "$1" ] && echo "usage: $me tasks(1-12) [seconds (dflt 930)]" && exit 1
tasks=0; [ $1 -ge 1 -a $1 -le 12 ] && tasks=$1
[ $tasks -eq 0 ] && echo "$me tasks ($1) must be 1..12" && exit 1
[ "$2" ] && secs=$2 || secs=930		# default 15.5m
ok=''; [ $secs -ge 20 -a $secs -le 1800 ] && ok=y
[ ! "$ok" ] && echo "$me seconds ($secs) must be 20..1800" && exit 1
echo "`date` $me running $tasks load1 tasks for ${secs}s"
bgpids=''
done=0; trap "done=1; sleep=0" int quit term
while [ $done -eq 0 ]; do
	while [ $tasks -gt 0 ]; do
		~/bin/load1 &
		bgpids="$bgpids $!"
	tasks=$((tasks-1))
	done
	sleep $secs
	done=1
done
kill $bgpids || echo "$me: error killing pids $bgpids"
echo "`date` $me done"
trap - int quit term
exit 0

cheers, Ian


More information about the freebsd-questions mailing list