xref: /freebsd/tools/test/stress2/misc/thr3.sh (revision 8b959dd6a3921c35395bef4a6d7ad2426a3bd88e)
1#!/bin/sh
2
3#
4# Copyright (c) 2016 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# Demonstrate VM leakage. Not seen on FreeBSD HEAD.
30
31. ../default.cfg
32
33here=`pwd`
34cd /tmp
35sed '1,/^EOF/d' < $here/$0 > thr3.c
36mycc -o thr3 -Wall -Wextra -O0 thr3.c -lpthread || exit 1
37rm -f thr3.c
38
39./thr3 &
40pid=$!
41
42log=/tmp/$0.$$
43trap "rm -f $log" EXIT INT
44r=`ps -Ovsz -p $pid | head -1`
45echo "        $r" > $log
46export max=0
47export n=0
48while true; do
49	sleep 30
50	vsz=`ps -Ovsz -p $pid | tail -1 | awk '{print $2}'`
51	[ -z "$vsz" -o "$vsz" = VSZ ] && break
52	if [ $vsz -gt $max ]; then
53		max=$vsz
54		r=`ps -Ovsz -p $pid | tail -1`
55		echo "`date '+%T'` $r"
56		n=$((n + 1))
57	fi
58done >> $log 2>&1
59[ $n -gt 1 ] && cat $log
60wait
61rm -f thr3
62[ $n -gt 1 ] && exit 1 || exit 0
63EOF
64#include <sys/types.h>
65#include <sys/time.h>
66
67#include <err.h>
68#include <fcntl.h>
69#include <pthread.h>
70#include <stdio.h>
71#include <stdlib.h>
72#include <unistd.h>
73
74#define NTHREADS 256
75#define RUNTIME (3 * 60)
76
77static void *
78thr_routine(void *arg __unused)
79{
80	getuid();
81	return (NULL);
82}
83
84int
85main(void)
86{
87	pthread_t threads[NTHREADS];
88	time_t start;
89	int i, r;
90
91	start = time(NULL);
92	while (time(NULL) - start < RUNTIME) {
93		for (i = 0; i < NTHREADS; i++)
94			if ((r = pthread_create(&threads[i], NULL,
95			    thr_routine, NULL)) != 0)
96				errc(1, r, "pthread_create()");
97
98		for (i = 0; i < NTHREADS; i++)
99			if ((r = pthread_join(threads[i], NULL)) != 0)
100				errc(1, r, "pthread_join(%d)", i);
101	}
102
103	return (0);
104}
105