xref: /freebsd/tools/test/stress2/misc/sched.sh (revision 8e9740b62e950b40dcf7dbe729855be14450d40d)
1#!/bin/sh
2
3#
4# Copyright (c) 2014 EMC Corp.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
30
31# Show scheduler fairness for ULE vs. 4BSD.
32
33. ../default.cfg
34
35here=`pwd`
36cd /tmp
37sed '1,/^EOF/d' < $here/$0 > sched.c
38mycc -o sched -Wall -Wextra -O0 sched.c || exit 1
39rm -f sched.c
40cd $here
41
42mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
43mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
44
45mdconfig -a -t swap -s 1g -u $mdstart || exit 1
46bsdlabel -w md$mdstart auto
47newfs $newfs_flags md${mdstart}$part > /dev/null
48mount /dev/md${mdstart}$part $mntpoint
49chmod 777 $mntpoint
50
51cpus=`sysctl hw.ncpu | sed 's/.*: //'`
52uname -v
53(cd $mntpoint; /tmp/sched $((cpus + 1))) > /dev/null 2>&1 &
54sleep 30
55export LANG=C
56top -U nobody -d 1 | grep nobody | awk '{print $11}' | sed 's/%//' |
57    ministat -A -w 73 | tail -1 | awk '{if ($NF > 1.0) exit 1}' ||
58{ echo Broken; top -U nobody -d 1 | grep nobody; }
59killall sched
60wait
61
62for i in `jot 3`; do
63	echo "run #$i"
64	(cd $mntpoint; /tmp/sched $((cpus + 1)))
65done
66
67while mount | grep $mntpoint | grep -q /dev/md; do
68	umount $mntpoint || sleep 1
69done
70rm -f /tmp/sched
71mdconfig -d -u $mdstart
72exit
73EOF
74#include <sys/param.h>
75#include <sys/stat.h>
76#include <sys/time.h>
77#include <sys/wait.h>
78
79#include <err.h>
80#include <errno.h>
81#include <fcntl.h>
82#include <fts.h>
83#include <libutil.h>
84#include <pwd.h>
85#include <sched.h>
86#include <stdint.h>
87#include <stdio.h>
88#include <stdlib.h>
89#include <string.h>
90#include <unistd.h>
91
92#define N 100 * 1024 * 1024
93
94double r;
95int parallel;
96
97void
98work(void)
99{
100	struct passwd *pw;
101	struct timespec start, finish;
102	double d1, d2;
103	int i, j;
104	volatile char *cp;
105
106	while (access("rendezvous", R_OK) != 0)
107		usleep(1);
108
109	if ((pw = getpwnam("nobody")) == NULL)
110		err(1, "no such user: nobody");
111	if (setgroups(1, &pw->pw_gid) ||
112	    setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
113	    seteuid(pw->pw_uid) || setuid(pw->pw_uid))
114		err(1, "Can't drop privileges to \"nobody\"");
115	endpwent();
116
117	d1 = d2 = 0;
118	cp = malloc(N);
119	clock_gettime(CLOCK_REALTIME_PRECISE, &start);
120	for (i = 0; i < 1; i++) {
121		for (j = 0; j < INT_MAX; j++) {
122			d1 = d1 + 1.0 / j;
123			d2 = d1 + 0.8 / j;
124			if (j % 1000 == 0) {
125				cp[arc4random() % N] = j % 255;
126			}
127		}
128	}
129	r = d1 + d2;
130	clock_gettime(CLOCK_REALTIME_PRECISE, &finish);
131	timespecsub(&finish, &start, &finish);
132#if defined(DEBUG)
133	fprintf(stderr, "Elapsed time for pid %d: %.4f\n", getpid(),
134	    finish.tv_sec + (double)finish.tv_nsec / 1e9);
135#endif
136
137	_exit(0);
138}
139
140int
141main(int argc, char **argv)
142{
143	int fd, i;
144
145	if (argc == 2)
146		parallel = atoi(argv[1]);
147	else
148		errx(1, "Usage: %s <cpus>", argv[0]);
149
150	for (i = 0; i < parallel; i++) {
151		if (fork() == 0)
152			work();
153	}
154	if ((fd = open("rendezvous", O_CREAT, 0644)) == -1)
155		err(1, "open()");
156	close(fd);
157	for (i = 0; i < parallel; i++)
158		wait(NULL);
159
160	return (0);
161}
162