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