Home | History | Annotate | Line # | Download | only in ckckp
cleanalot_async.c revision 1.2
      1 /*	$NetBSD: cleanalot_async.c,v 1.2 2006/04/27 22:37:54 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         r = fwrite(buf, size, 1, fp);
     76         fclose(fp);
     77 
     78         return r;
     79 }
     80 
     81 int write_dirs(int gen, int size, int plex)
     82 {
     83         int i, j, tot;
     84         char s[1024];
     85         char *buf;
     86 
     87         /* Create all base dirs */
     88         for (i = 0; i < plex; i++) {
     89                 sprintf(s, "dir_%x_%x", i, gen);
     90                 if (mkdir(s, 0700) != 0)
     91                         return 0;
     92         }
     93 
     94         /* Write files */
     95         buf = malloc(size);
     96         if (buf == NULL)
     97                 return 0;
     98         tot = 0;
     99         for (i = 0; ; i++) {
    100                 for (j = 0; j < plex; j++) {
    101                         if (write_file(gen, i, j, buf, size) == 0) {
    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         while((c = getopt(argc, argv, "b:n:p:")) != -1) {
    120                 switch(c) {
    121                     case 'b':
    122                             bs = atoi(optarg);
    123                             break;
    124                     case 'n':
    125                             count = atoi(optarg);
    126                             break;
    127                     case 'p':
    128                             plex = atoi(optarg);
    129                             break;
    130 		    default:
    131                             exit(1);
    132                 }
    133         }
    134 
    135         /*
    136          * Process old-style, non-flag parameters
    137          */
    138         if (count == 0) {
    139                 if (argv[optind] != NULL)
    140                         count = atoi(argv[optind]);
    141         }
    142         if (bs == 0 && getenv("BS") != NULL)
    143                 bs = atoi(getenv("BS"));
    144         if (bs == 0)
    145                 bs = 16384;
    146         if (plex == 0)
    147                 plex = 2;
    148 
    149         for (i = 0; count == 0 || i < count; i++) {
    150                 if (count)
    151                         printf("::: begin iteration %d/%d\n", i, count);
    152                 else
    153                         printf("::: begin iteration %d\n", i);
    154 
    155                 for (j = 0; ; j++) {
    156                         if ((c = write_dirs(j, bs, plex)) == 0)
    157                                 break;
    158                 	printf("%d: %d files of size %d\n", j, c, bs);
    159                         sprintf(cmd, "rm -rf dir_%x_%x", plex - 1, j);
    160                         system(cmd);
    161 			sync();
    162                 }
    163                 printf("%d files of size %d\n", j * plex, bs);
    164 		system("df -k .");
    165         }
    166 }
    167