Path: blob/main/tools/test/stress2/misc/context.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. ../default.cfg2930here=`pwd`31cd /tmp32sed '1,/^EOF/d' < $here/$0 > context.c33mycc -o context -Wall -Wextra -O2 -g context.c || exit 134rm -f context.c35[ -d $RUNDIR ] || mkdir -p $RUNDIR36cd $RUNDIR3738daemon sh -c "(cd $here/../testcases/swap; ./swap -t 10m -i 20)" > \39/dev/null 2>&140for i in `jot 4`; do41/tmp/context &42pids="$pids $!"43done44s=045for i in $pids; do46wait $i47[ $? -ne 0 ] && s=$((s + 1))48done49while pgrep -q swap; do50pkill -9 swap51done52rm -f /tmp/context53exit $s54EOF55/*56* Inspired by lmbench-3.0-a9/src/lat_ctx.c57* Pass a token thru pipes to CHILDREN+1 processes in a circular list58*/5960#include <sys/types.h>6162#include <err.h>63#include <errno.h>64#include <signal.h>65#include <stdio.h>66#include <stdlib.h>67#include <time.h>68#include <unistd.h>6970#define CHILDREN 6471#define RUNTIME 3007273int fds[CHILDREN +1 ][2];74pid_t pid[CHILDREN];7576void77handler(int s __unused)78{79_exit(0);80}8182int83main(void)84{85time_t start;86int i, j;87int token;8889for (i = 0; i < CHILDREN + 1; i++) {90if (pipe(fds[i]) == -1)91err(1, "pipe");92}9394signal(SIGHUP, handler);95start = time(NULL);96for (i = 0; i < CHILDREN; i++) {97pid[i] = fork();98if (pid[i] == -1) {99perror("fork");100exit(2);101}102103if (pid[i] == 0) { /* child */104for (;;) {105if (read(fds[i][0], &token, sizeof(token))106!= sizeof(token))107err(1, "read pipe 2");108if (write(fds[i+1][1], &token, sizeof(token))109!= sizeof(token))110err(1, "write pipe 1");111}112}113114} /* parent */115116for (j = 0; time(NULL) - start < RUNTIME; j++) {117token = j;118if (write(fds[0][1], &token, sizeof(token)) != sizeof(token))119err(1, "write pipe 2");120if (read(fds[CHILDREN][0], &token, sizeof(token))121!= sizeof(token))122err(1, "read pipe 1");123}124125for (i = 0; i < CHILDREN; i++)126kill(pid[i], SIGHUP);127128return (0);129}130131132