xref: /netbsd-src/regress/sys/fs/lfs/ckckp/cleanalot_async.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: cleanalot_async.c,v 1.7 2008/04/28 20:23:06 martin Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Konrad E. Schroder <perseant@hhhh.org>.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <signal.h>
35 #include <string.h>
36 #include <unistd.h>
37 
38 void dirname(int n, char *s)
39 {
40 	if (n == 0) {
41 		strcat(s, "/0");
42 		mkdir(s);
43 	}
44 	while (n) {
45 		sprintf(s + strlen(s), "/%x", n & 0xf);
46 		n >>= 4;
47 		mkdir(s);
48 	}
49 }
50 
51 /*
52  * Write a file, creating the directory if necessary.
53  */
54 int write_file(int gen, int n, int plex, char *buf, int size)
55 {
56 	FILE *fp;
57 	char s[1024], *t;
58 	int r;
59 
60 	sprintf(s, "dir_%x_%x", plex, gen);
61 	dirname(n, s);
62 	strcat(s, ".file");
63 
64 	/* printf("write file %d.%d.%x: %s\n", gen, plex, n, s); */
65 
66 	fp = fopen(s, "wb");
67 	if (fp == NULL)
68 		return 0;
69 	if (size)
70 	r = fwrite(buf, size, 1, fp);
71 	else
72 		r = 1;
73 	fclose(fp);
74 
75 	return r;
76 }
77 
78 int write_dirs(int gen, int size, int plex)
79 {
80 	int i, j, tot;
81 	char s[1024];
82 	char *buf;
83 
84 	/* Create all base dirs */
85 	for (i = 0; i < plex; i++) {
86 		sprintf(s, "dir_%x_%x", i, gen);
87 		if (mkdir(s, 0700) != 0)
88 			return 0;
89 	}
90 
91 	/* Write files */
92 	if (size) {
93 	buf = malloc(size);
94 	if (buf == NULL)
95 		return 0;
96 	}
97 	tot = 0;
98 	for (i = 0; ; i++) {
99 		for (j = 0; j < plex; j++) {
100 			if (write_file(gen, i, j, buf, size) == 0) {
101 				if (size)
102 				free(buf);
103 				return tot;
104 			}
105 			++tot;
106 		}
107 	}
108 	/* NOTREACHED */
109 }
110 
111 int main(int argc, char **argv)
112 {
113 	int c, i, j;
114 	int bs = 0;
115 	int count = 0;
116 	int plex = 2;
117 	char cmd[1024];
118 
119 	bs = -1;
120 	while((c = getopt(argc, argv, "b:n:p:")) != -1) {
121 		switch(c) {
122 		    case 'b':
123 			bs = atoi(optarg);
124 			break;
125 		    case 'n':
126 			count = atoi(optarg);
127 			break;
128 		    case 'p':
129 			plex = atoi(optarg);
130 			break;
131 		    default:
132 			exit(1);
133 		}
134 	}
135 
136 	/*
137 	 * Process old-style, non-flag parameters
138 	 */
139 	if (count == 0) {
140 		if (argv[optind] != NULL)
141 			count = atoi(argv[optind]);
142 	}
143 	if (bs < 0 && getenv("BS") != NULL)
144 		bs = atoi(getenv("BS"));
145 	if (bs < 0)
146 		bs = 16384;
147 	if (plex == 0)
148 		plex = 2;
149 
150 	for (i = 0; count == 0 || i < count; i++) {
151 		if (count)
152 			printf("::: begin iteration %d/%d\n", i, count);
153 		else
154 			printf("::: begin iteration %d\n", i);
155 
156 		for (j = 0; ; j++) {
157 			if ((c = write_dirs(j, bs, plex)) == 0)
158 				break;
159 			printf("%d: %d files of size %d\n", j, c, bs);
160 			sprintf(cmd, "rm -rf dir_%x_%x", plex - 1, j);
161 			system(cmd);
162 			sync();
163 		}
164 		system("df -k .");
165 		printf("remove files\n");
166 		system("rm -rf dir_*");
167 		system("df -k .");
168 	}
169 }
170