Path: blob/main/tools/test/stress2/misc/context2.sh
39537 views
#!/bin/sh12#3# Copyright (c) 2016 EMC Corp.4# All rights reserved.5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions8# are met:9# 1. Redistributions of source code must retain the above copyright10# notice, this list of conditions and the following disclaimer.11# 2. Redistributions in binary form must reproduce the above copyright12# notice, this list of conditions and the following disclaimer in the13# documentation and/or other materials provided with the distribution.14#15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25# SUCH DAMAGE.26#2728# This problem was seen with WiP kernel code:29# https://people.freebsd.org/~pho/stress/log/kostik1210.txt3031. ../default.cfg3233here=`pwd`34cd /tmp35sed '1,/^EOF/d' < $here/$0 > context2.c36mycc -o context2 -Wall -Wextra -O2 context2.c -lpthread || exit 137rm -f context2.c38[ -d $RUNDIR ] || mkdir -p $RUNDIR39cd $RUNDIR4041daemon sh -c "(cd $here/../testcases/swap; ./swap -t 10m -i 20)" > \42/dev/null 2>&143for i in `jot 4`; do44/tmp/context2 &45done46wait47while pgrep -q swap; do48pkill -9 swap49done50rm -f /tmp/context251exit 052EOF53/*54* Inspired by lmbench-3.0-a9/src/lat_ctx.c55* Pass a token thru pipes to NTHREADS+1 threads in a circular list.56*/5758#include <sys/types.h>5960#include <err.h>61#include <errno.h>62#include <pthread.h>63#include <stdio.h>64#include <stdlib.h>65#include <string.h>66#include <time.h>67#include <unistd.h>6869#define NTHREADS 6470#define RUNTIME 3007172pid_t pid[NTHREADS];73int fds[NTHREADS+1][2];7475void *76thr_routine(void *arg)77{78int i;79int token;8081i = (long)arg;82for (;;) {83if (read(fds[i][0], &token, sizeof(token)) != sizeof(token))84err(1, "read pipe 2");85token++;86if (write(fds[i+1][1], &token, sizeof(token)) != sizeof(token))87err(1, "write pipe 1");88}89return (0);90}9192int93main(void)94{95pthread_t threads[NTHREADS];96time_t start;97long arg;98int i, r, token;99100for (i = 0; i < NTHREADS + 1; i++) {101if (pipe(fds[i]) == -1)102err(1, "pipe");103}104105for (i = 0; i < NTHREADS; i++) {106arg = i;107if ((r = pthread_create(&threads[i], NULL, thr_routine,108(void *)arg)) != 0)109errc(1, r, "pthread_create(): %s\n", strerror(r));110}111112start = time(NULL);113while (time(NULL) - start < RUNTIME) {114token = 0;115if (write(fds[0][1], &token, sizeof(token)) != sizeof(token))116err(1, "write pipe 2");117if (read(fds[NTHREADS][0], &token, sizeof(token)) !=118sizeof(token))119err(1, "read pipe 1");120}121122for (i = 0; i < NTHREADS; i++)123if ((r = pthread_cancel(threads[i])) != 0)124errc(1, r, "pthread_cancel(%d)", i);125for (i = 0; i < NTHREADS; i++)126if ((r = pthread_join(threads[i], NULL)) != 0)127errc(1, r, "pthread_join(%d)", i);128129return (0);130}131132133