xref: /freebsd/tools/test/stress2/misc/tar.sh (revision d7d962ead0b6e5e8a39202d0590022082bf5bfb6)
1#!/bin/sh
2
3#
4# Copyright (c) 2015 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# tar x simulator
30# Create 100.000 files of size 64k; a total of 6GB
31
32. ../default.cfg
33
34dir=/tmp
35odir=`pwd`
36cd $dir
37sed '1,/^EOF/d' < $odir/$0 > $dir/tar.c
38mycc -o tar -Wall -Wextra -O0 -g tar.c || exit 1
39rm -f tar.c
40cd $odir
41
42export LANG=C
43wdir=`dirname $diskimage`
44rm -rf $wdir/tar.tmp
45[ `df -k $wdir | tail -1 | awk '{print $4}'` -lt \
46    $((6 * 1024 * 1024)) ] &&
47    echo "Not enough disk space on $wdir." && exit 0
48
49for i in `jot $(sysctl -n hw.ncpu)`; do
50	/tmp/tar $wdir | ministat -n | tail -2 &
51	sleep 3
52done
53wait
54
55rm -rf /tmp/tar $wdir/tar.tmp
56exit 0
57
58EOF
59#include <sys/stat.h>
60
61#include <err.h>
62#include <errno.h>
63#include <fcntl.h>
64#include <stdio.h>
65#include <time.h>
66#include <unistd.h>
67
68#define RUNTIME 1200
69
70char buf[64*1024];
71
72int
73main(int argc, char *argv[])
74{
75	struct timeval start, stop, diff;
76	time_t stime;
77	uint64_t mx, usec;
78	int fd, i, j, k;
79	char path[128];
80
81	if (argc != 2)
82		errx(1, "Usage: %s <path>", argv[0]);
83	mx = 0;
84	stime = time(NULL);
85	snprintf(path, sizeof(path), "%s/tar.tmp", argv[1]);
86	mkdir(path, 0777);
87	for (i = 0; i < 100; i++) {
88		snprintf(path, sizeof(path), "%s/tar.tmp/d%d", argv[1], i);
89		if (mkdir(path, 0777) == -1)
90			if (errno != EEXIST)
91				err(1, "mkdir(%s)", path);
92		for (j = 0; j < 100; j++) {
93			snprintf(path, sizeof(path), "%s/tar.tmp/d%d/d%d",
94			    argv[1], i, j);
95			if (mkdir(path, 0777) == -1)
96				if (errno != EEXIST)
97					err(1, "mkdir(%s)", path);
98		}
99	}
100	for (i = 0; i < 100; i++) {
101		for (j = 0; j < 100; j++) {
102			for (k = 0; k < 10; k++) {
103				snprintf(path, sizeof(path),
104				    "%s/tar.tmp/d%d/d%d/f%d", argv[1], i, j, k);
105				gettimeofday(&start, NULL);
106				unlink(path);
107				if ((fd = open(path, O_RDWR | O_CREAT, 0644))
108				    == -1)
109					err(1, "open(%s)", path);
110				write(fd, buf, sizeof(buf));
111				close(fd);
112				gettimeofday(&stop, NULL);
113				timersub(&stop, &start, &diff);
114				usec  = ((uint64_t)1000000 *
115				    diff.tv_sec + diff.tv_usec);
116				if (usec > mx)
117					mx = usec;
118				fprintf(stdout, "%.3f %s\n",
119				    (double)usec / 1000000, path);
120			}
121		}
122		if (time(NULL) - stime > RUNTIME) {
123			fprintf(stderr, "Timed out at %s.\n", path);
124			break;
125		}
126	}
127	if (mx > 5000000) {
128//		fprintf(stderr, "%.3f %s : FAIL\n", (double)mx / 1000000, path);
129		return (1);
130	} else
131		return (0);
132}
133