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