Home | History | Annotate | Line # | Download | only in ckckp
cleanalot_async.c revision 1.6
      1  1.6  perseant /*	$NetBSD: cleanalot_async.c,v 1.6 2006/07/21 00:29:23 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.6  perseant #include <string.h>
     43  1.1  perseant #include <unistd.h>
     44  1.1  perseant 
     45  1.1  perseant void dirname(int n, char *s)
     46  1.1  perseant {
     47  1.1  perseant 	if (n == 0) {
     48  1.1  perseant 		strcat(s, "/0");
     49  1.1  perseant 		mkdir(s);
     50  1.1  perseant 	}
     51  1.5  perseant 	while (n) {
     52  1.5  perseant 		sprintf(s + strlen(s), "/%x", n & 0xf);
     53  1.5  perseant 		n >>= 4;
     54  1.1  perseant 		mkdir(s);
     55  1.5  perseant 	}
     56  1.1  perseant }
     57  1.1  perseant 
     58  1.1  perseant /*
     59  1.1  perseant  * Write a file, creating the directory if necessary.
     60  1.1  perseant  */
     61  1.1  perseant int write_file(int gen, int n, int plex, char *buf, int size)
     62  1.1  perseant {
     63  1.5  perseant 	FILE *fp;
     64  1.5  perseant 	char s[1024], *t;
     65  1.5  perseant 	int r;
     66  1.1  perseant 
     67  1.5  perseant 	sprintf(s, "dir_%x_%x", plex, gen);
     68  1.5  perseant 	dirname(n, s);
     69  1.1  perseant 	strcat(s, ".file");
     70  1.1  perseant 
     71  1.6  perseant 	/* printf("write file %d.%d.%x: %s\n", gen, plex, n, s); */
     72  1.1  perseant 
     73  1.5  perseant 	fp = fopen(s, "wb");
     74  1.5  perseant 	if (fp == NULL)
     75  1.5  perseant 		return 0;
     76  1.4  perseant 	if (size)
     77  1.5  perseant 	r = fwrite(buf, size, 1, fp);
     78  1.4  perseant 	else
     79  1.4  perseant 		r = 1;
     80  1.5  perseant 	fclose(fp);
     81  1.1  perseant 
     82  1.5  perseant 	return r;
     83  1.1  perseant }
     84  1.1  perseant 
     85  1.1  perseant int write_dirs(int gen, int size, int plex)
     86  1.1  perseant {
     87  1.5  perseant 	int i, j, tot;
     88  1.5  perseant 	char s[1024];
     89  1.5  perseant 	char *buf;
     90  1.5  perseant 
     91  1.5  perseant 	/* Create all base dirs */
     92  1.5  perseant 	for (i = 0; i < plex; i++) {
     93  1.5  perseant 		sprintf(s, "dir_%x_%x", i, gen);
     94  1.5  perseant 		if (mkdir(s, 0700) != 0)
     95  1.5  perseant 			return 0;
     96  1.5  perseant 	}
     97  1.1  perseant 
     98  1.5  perseant 	/* Write files */
     99  1.4  perseant 	if (size) {
    100  1.5  perseant 	buf = malloc(size);
    101  1.5  perseant 	if (buf == NULL)
    102  1.5  perseant 		return 0;
    103  1.5  perseant 	}
    104  1.5  perseant 	tot = 0;
    105  1.5  perseant 	for (i = 0; ; i++) {
    106  1.5  perseant 		for (j = 0; j < plex; j++) {
    107  1.5  perseant 			if (write_file(gen, i, j, buf, size) == 0) {
    108  1.4  perseant 				if (size)
    109  1.5  perseant 				free(buf);
    110  1.5  perseant 				return tot;
    111  1.5  perseant 			}
    112  1.5  perseant 			++tot;
    113  1.5  perseant 		}
    114  1.5  perseant 	}
    115  1.5  perseant 	/* NOTREACHED */
    116  1.1  perseant }
    117  1.1  perseant 
    118  1.1  perseant int main(int argc, char **argv)
    119  1.1  perseant {
    120  1.5  perseant 	int c, i, j;
    121  1.5  perseant 	int bs = 0;
    122  1.5  perseant 	int count = 0;
    123  1.5  perseant 	int plex = 2;
    124  1.5  perseant 	char cmd[1024];
    125  1.1  perseant 
    126  1.4  perseant 	bs = -1;
    127  1.5  perseant 	while((c = getopt(argc, argv, "b:n:p:")) != -1) {
    128  1.5  perseant 		switch(c) {
    129  1.5  perseant 		    case 'b':
    130  1.5  perseant 			bs = atoi(optarg);
    131  1.5  perseant 			break;
    132  1.5  perseant 		    case 'n':
    133  1.5  perseant 			count = atoi(optarg);
    134  1.5  perseant 			break;
    135  1.5  perseant 		    case 'p':
    136  1.5  perseant 			plex = atoi(optarg);
    137  1.5  perseant 			break;
    138  1.1  perseant 		    default:
    139  1.5  perseant 			exit(1);
    140  1.5  perseant 		}
    141  1.5  perseant 	}
    142  1.5  perseant 
    143  1.5  perseant 	/*
    144  1.5  perseant 	 * Process old-style, non-flag parameters
    145  1.5  perseant 	 */
    146  1.5  perseant 	if (count == 0) {
    147  1.5  perseant 		if (argv[optind] != NULL)
    148  1.5  perseant 			count = atoi(argv[optind]);
    149  1.5  perseant 	}
    150  1.4  perseant 	if (bs < 0 && getenv("BS") != NULL)
    151  1.5  perseant 		bs = atoi(getenv("BS"));
    152  1.4  perseant 	if (bs < 0)
    153  1.5  perseant 		bs = 16384;
    154  1.5  perseant 	if (plex == 0)
    155  1.5  perseant 		plex = 2;
    156  1.5  perseant 
    157  1.5  perseant 	for (i = 0; count == 0 || i < count; i++) {
    158  1.5  perseant 		if (count)
    159  1.5  perseant 			printf("::: begin iteration %d/%d\n", i, count);
    160  1.5  perseant 		else
    161  1.5  perseant 			printf("::: begin iteration %d\n", i);
    162  1.5  perseant 
    163  1.5  perseant 		for (j = 0; ; j++) {
    164  1.5  perseant 			if ((c = write_dirs(j, bs, plex)) == 0)
    165  1.5  perseant 				break;
    166  1.5  perseant 			printf("%d: %d files of size %d\n", j, c, bs);
    167  1.5  perseant 			sprintf(cmd, "rm -rf dir_%x_%x", plex - 1, j);
    168  1.5  perseant 			system(cmd);
    169  1.1  perseant 			sync();
    170  1.5  perseant 		}
    171  1.1  perseant 		system("df -k .");
    172  1.3  perseant 		printf("remove files\n");
    173  1.3  perseant 		system("rm -rf dir_*");
    174  1.3  perseant 		system("df -k .");
    175  1.5  perseant 	}
    176  1.1  perseant }
    177