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
46newfs $newfs_flags md$mdstart > /dev/null
47mount /dev/md$mdstart $mntpoint
48chmod 777 $mntpoint
49
50cpus=`sysctl hw.ncpu | sed 's/.*: //'`
51uname -v
52(cd $mntpoint; /tmp/sched $((cpus + 1))) > /dev/null 2>&1 &
53sleep 30
54export LANG=C
55top -U nobody -d 1 | grep nobody | awk '{print $11}' | sed 's/%//' |
56    ministat -A -w 73 | tail -1 | awk '{if ($NF > 1.0) exit 1}' ||
57{ echo Broken; top -U nobody -d 1 | grep nobody; }
58killall sched
59wait
60
61for i in `jot 3`; do
62	echo "run #$i"
63	(cd $mntpoint; /tmp/sched $((cpus + 1)))
64done
65
66while mount | grep $mntpoint | grep -q /dev/md; do
67	umount $mntpoint || sleep 1
68done
69rm -f /tmp/sched
70mdconfig -d -u $mdstart
71exit
72EOF
73#include <sys/param.h>
74#include <sys/stat.h>
75#include <sys/time.h>
76#include <sys/wait.h>
77
78#include <err.h>
79#include <errno.h>
80#include <fcntl.h>
81#include <fts.h>
82#include <libutil.h>
83#include <pwd.h>
84#include <sched.h>
85#include <stdint.h>
86#include <stdio.h>
87#include <stdlib.h>
88#include <string.h>
89#include <unistd.h>
90
91#define N 100 * 1024 * 1024
92
93double r;
94int parallel;
95
96void
97work(void)
98{
99	struct passwd *pw;
100	struct timespec start, finish;
101	double d1, d2;
102	int i, j;
103	volatile char *cp;
104
105	while (access("rendezvous", R_OK) != 0)
106		usleep(1);
107
108	if ((pw = getpwnam("nobody")) == NULL)
109		err(1, "no such user: nobody");
110	if (setgroups(1, &pw->pw_gid) ||
111	    setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
112	    seteuid(pw->pw_uid) || setuid(pw->pw_uid))
113		err(1, "Can't drop privileges to \"nobody\"");
114	endpwent();
115
116	d1 = d2 = 0;
117	cp = malloc(N);
118	clock_gettime(CLOCK_REALTIME_PRECISE, &start);
119	for (i = 0; i < 1; i++) {
120		for (j = 0; j < INT_MAX; j++) {
121			d1 = d1 + 1.0 / j;
122			d2 = d1 + 0.8 / j;
123			if (j % 1000 == 0) {
124				cp[arc4random() % N] = j % 255;
125			}
126		}
127	}
128	r = d1 + d2;
129	clock_gettime(CLOCK_REALTIME_PRECISE, &finish);
130	timespecsub(&finish, &start, &finish);
131#if defined(DEBUG)
132	fprintf(stderr, "Elapsed time for pid %d: %.4f\n", getpid(),
133	    finish.tv_sec + (double)finish.tv_nsec / 1e9);
134#endif
135
136	_exit(0);
137}
138
139int
140main(int argc, char **argv)
141{
142	int fd, i;
143
144	if (argc == 2)
145		parallel = atoi(argv[1]);
146	else
147		errx(1, "Usage: %s <cpus>", argv[0]);
148
149	for (i = 0; i < parallel; i++) {
150		if (fork() == 0)
151			work();
152	}
153	if ((fd = open("rendezvous", O_CREAT, 0644)) == -1)
154		err(1, "open()");
155	close(fd);
156	for (i = 0; i < parallel; i++)
157		wait(NULL);
158
159	return (0);
160}
161