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