xref: /freebsd/tools/test/stress2/misc/context2.sh (revision 4d846d26)
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# This problem was seen with WiP kernel code:
30# https://people.freebsd.org/~pho/stress/log/kostik1210.txt
31
32. ../default.cfg
33
34here=`pwd`
35cd /tmp
36sed '1,/^EOF/d' < $here/$0 > context2.c
37mycc -o context2 -Wall -Wextra -O2 context2.c -lpthread || exit 1
38rm -f context2.c
39[ -d $RUNDIR ] || mkdir -p $RUNDIR
40cd $RUNDIR
41
42daemon sh -c "(cd $here/../testcases/swap; ./swap -t 10m -i 20)" > \
43    /dev/null 2>&1
44for i in `jot 4`; do
45	/tmp/context2 &
46done
47wait
48while pgrep -q swap; do
49	pkill -9 swap
50done
51rm -f /tmp/context2
52exit 0
53EOF
54/*
55 * Inspired by lmbench-3.0-a9/src/lat_ctx.c
56 * Pass a token thru pipes to NTHREADS+1 threads in a circular list.
57 */
58
59#include <sys/types.h>
60
61#include <err.h>
62#include <errno.h>
63#include <pthread.h>
64#include <stdio.h>
65#include <stdlib.h>
66#include <string.h>
67#include <time.h>
68#include <unistd.h>
69
70#define NTHREADS 64
71#define RUNTIME 300
72
73pid_t pid[NTHREADS];
74int fds[NTHREADS+1][2];
75
76void *
77thr_routine(void *arg)
78{
79	int i;
80	int token;
81
82	i = (long)arg;
83	for (;;) {
84		if (read(fds[i][0], &token, sizeof(token)) != sizeof(token))
85			err(1, "read pipe 2");
86		token++;
87		if (write(fds[i+1][1], &token, sizeof(token)) != sizeof(token))
88			err(1, "write pipe 1");
89	}
90	return (0);
91}
92
93int
94main(void)
95{
96	pthread_t threads[NTHREADS];
97	time_t start;
98	long arg;
99	int i, r, token;
100
101	for (i = 0; i < NTHREADS + 1; i++) {
102		if (pipe(fds[i]) == -1)
103			err(1, "pipe");
104	}
105
106	for (i = 0; i < NTHREADS; i++) {
107		arg = i;
108		if ((r = pthread_create(&threads[i], NULL, thr_routine,
109		    (void *)arg)) != 0)
110			errc(1, r, "pthread_create(): %s\n", strerror(r));
111	}
112
113	start = time(NULL);
114	while (time(NULL) - start < RUNTIME) {
115		token = 0;
116		if (write(fds[0][1], &token, sizeof(token)) != sizeof(token))
117			err(1, "write pipe 2");
118		if (read(fds[NTHREADS][0], &token, sizeof(token)) !=
119		    sizeof(token))
120			err(1, "read pipe 1");
121	}
122
123	for (i = 0; i < NTHREADS; i++)
124		if ((r = pthread_cancel(threads[i])) != 0)
125			errc(1, r, "pthread_cancel(%d)", i);
126	for (i = 0; i < NTHREADS; i++)
127		if ((r = pthread_join(threads[i], NULL)) != 0)
128			errc(1, r, "pthread_join(%d)", i);
129
130        return (0);
131}
132