Skip to content

Fix CPU hog function #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v2
- name: build
run: |
brew install autoconf automake libtool
brew install autoconf automake libtool txt2man
./autogen.sh
./configure
make
Expand All @@ -30,6 +30,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: install build dependencies
run: |
sudo apt-get update
sudo env DEBIAN_FRONTEND=noninteractive apt-get install -qy txt2man
- name: first_build
run: |
./autogen.sh
Expand Down
10 changes: 10 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ distclean-local:
rm -rf autom4te.cache
rm -f aclocal.m4 compile config.* configure depcomp INSTALL install-sh \
Makefile.in missing src/Makefile.in test-driver test/Makefile.in

man/stress.1: man/stress.txt
txt2man -t stress \
-r stress-@PACKAGE_VERSION@ \
-d "@RELEASE_DAY@" \
-s 1 \
-v "tool to impose load on and stress test systems" \
$< > $@

.PHONY: distclean-local
4 changes: 4 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
AC_PREREQ([2.69])
AC_INIT([stress], [1.0.7], [https://github.com/resurrecting-open-source-projects/stress/issues])
AC_SUBST([RELEASE_DAY], ["21 Jan 2023"], [Day of release (used in man page)])
AC_CONFIG_SRCDIR([src/stress.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE
Expand All @@ -17,6 +18,9 @@ AC_FUNC_FORK
AC_FUNC_MALLOC
AC_CHECK_FUNCS([alarm sqrt strerror])

# Checks for build helpers
AC_CHECK_PROG(TXT2MAN_CHECK, txt2man, yes)
AS_IF([test x"$TXT2MAN_CHECK" != x"yes"], [AC_MSG_ERROR([txt2man tool needed to generate man page])])
# Special functions
AC_ARG_ENABLE([static],
AS_HELP_STRING([--enable-static],
Expand Down
24 changes: 0 additions & 24 deletions man/create-man.sh

This file was deleted.

152 changes: 0 additions & 152 deletions man/stress.1

This file was deleted.

6 changes: 4 additions & 2 deletions man/stress.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ OPTIONS
-n, --dry-run Show what would have been done.
-t, --timeout <N> Timeout after N seconds. This option is ignored by -n.
--backoff <N> Wait for factor of microseconds before starting work.
-c, --cpu <N> Spawn N workers spinning on sqrt().
-c, --cpu <N> Spawn N workers spinning on pseudo-random number generation
and sqrt().
-i, --io <N> Spawn N workers spinning on sync().
-m, --vm <N> Spawn N workers spinning on malloc()/free().
--vm-bytes <B> Malloc B bytes per vm worker (default is 256MB).
Expand All @@ -39,7 +40,8 @@ OPTIONS
EXAMPLES
The simple case is that you just want to bring the system load average up to an
arbitrary value. The following forks 13 processes, each of which spins in a
tight loop calculating the sqrt() of a random number acquired with rand().
tight loop calculating the sqrt() of a random number acquired from a fixed-seed
pseudo-random number generator (xoroshiro128+).

stress -c 13

Expand Down
49 changes: 45 additions & 4 deletions src/stress.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright 2001-2010 Amos Waterland <[email protected]>
* Copyright 2021-2023 Joao Eriberto Mota Filho <[email protected]>
* Copyright 2023 Vratislav Bendel <[email protected]>
* Copyright 2025 Marcus Müller <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand All @@ -23,6 +24,7 @@
#include <errno.h>
#include <libgen.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -479,9 +481,47 @@ void worker_init(void)
int
hogcpu (void)
{
while (1)
sqrt (rand ());

/* initial state for PRNG */
static uint32_t s[] = {
4 /* chosen by fair dice roll */,
42 /* the answer */,
3 /* pi */,
0xCAFECA4E /* because who doesn't like cake? */
};
while (1) {
/* START xoroshirp128plus:
Written in 2018 by David Blackman and Sebastiano Vigna ([email protected])

To the extent possible under law, the author has dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

const uint32_t result = s[0] + s[3];
const uint32_t t = s[1] << 9;

s[2] ^= s[0];
s[3] ^= s[1];
s[1] ^= s[2];
s[0] ^= s[3];

s[2] ^= t;

s[3] = (s[3] << 11) | (s[3]>> (32-11));
/* END xoroshirp128plus */
volatile double value = sqrt ((double)(result));
}
return 0;
}

Expand Down Expand Up @@ -762,7 +802,8 @@ usage (int status)
" -n, --dry-run show what would have been done\n"
" -t, --timeout N timeout after N seconds\n"
" --backoff N wait factor of N microseconds before work starts\n"
" -c, --cpu N spawn N workers spinning on sqrt()\n"
" -c, --cpu N spawn N workers spinning on pseudo-random number generation\n"
" and sqrt()\n"
" -i, --io N spawn N workers spinning on sync()\n"
" -m, --vm N spawn N workers spinning on malloc()/free()\n"
" --vm-bytes B malloc B bytes per vm worker (default is 256MB)\n"
Expand Down