added writesize
[tridge/junkcode.git] / writefiles.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <fcntl.h>
5 #include <errno.h>
6 #include <time.h>
7 #include <sys/time.h>
8
9
10 static struct timeval tp1,tp2;
11 static size_t block_size = 64 * 1024;
12 static int osync;
13
14 static void start_timer()
15 {
16         gettimeofday(&tp1,NULL);
17 }
18
19 static double end_timer()
20 {
21         gettimeofday(&tp2,NULL);
22         return((tp2.tv_sec - tp1.tv_sec) + 
23                (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
24 }
25
26
27 static void write_file(char *fname)
28 {
29         int fd;
30         static double total, thisrun;
31         int n;
32         char *buf;
33
34         buf = malloc(block_size);
35
36         if (!buf) {
37                 printf("Malloc of %d failed\n", (int)block_size);
38                 exit(1);
39         }
40
41         fd = open(fname, O_WRONLY|(osync?O_SYNC:0));
42         if (fd == -1) {
43                 perror(fname);
44                 free(buf);
45                 return;
46         }
47
48         while ((n = write(fd, buf, block_size)) > 0) {
49                 total += n;
50                 thisrun += n;
51                 if (end_timer() >= 1.0) {
52                         time_t t = time(NULL);
53                         printf("%6d MB    %.3f MB/sec  %s", 
54                                (int)(total/1.0e6),
55                                (thisrun*1.0e-6)/end_timer(),
56                                ctime(&t));
57                         start_timer();
58                         thisrun = 0;
59                 }
60         }
61
62         free(buf);
63         close(fd);
64 }
65
66
67 static void usage(void)
68 {
69         printf("\n" \
70 "writefiles - writes to a list of files, showing throughput\n" \
71 "\n" \
72 "Usage: writefiles [options] <files>\n" \
73 "\n" \
74 "Options:\n" \
75 "    -B size        set the block size in bytes\n" \
76 "    -s             sync open\n" \
77 "\n" \
78 "WARNING: writefiles is a destructive test!\n" \
79 "\n" \
80 "");
81 }
82
83
84 int main(int argc, char *argv[])
85 {
86         int i;
87         extern char *optarg;
88         extern int optind;
89         int c;
90
91         while ((c = getopt(argc, argv, "B:hs")) != -1) {
92                 switch (c) {
93                 case 'B':
94                         block_size = strtol(optarg, NULL, 0);
95                         break;
96                 case 's':
97                         osync = 1;
98                         break;
99                 case 'h':
100                 default:
101                         usage();
102                         exit(1);
103                 }
104         }
105
106         argc -= optind;
107         argv += optind;
108
109         if (argc == 0) {
110                 usage();
111                 exit(1);
112         }
113
114
115         start_timer();
116
117         while (1) {
118                 for (i=0; i<argc; i++) {
119                         write_file(argv[i]);
120                 }
121         }
122
123         return 0;
124 }
125