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