Whamcloud - gitweb
e2freefrag: Improve output to include be more like xfs_db's freesp -s
[tools/e2fsprogs.git] / misc / e2freefrag.c
1 /*
2  * e2freefrag - report filesystem free-space fragmentation
3  *
4  * Copyright (C) 2009 Sun Microsystems, Inc.
5  *
6  * Author: Rupesh Thakare <rupesh@sun.com>
7  *         Andreas Dilger <adilger@sun.com>
8  *
9  * %Begin-Header%
10  * This file may be redistributed under the terms of the GNU Public
11  * License version 2.
12  * %End-Header%
13  */
14 #include <stdio.h>
15 #ifdef HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #ifdef HAVE_STDLIB_H
19 #include <stdlib.h>
20 #endif
21 #ifdef HAVE_GETOPT_H
22 #include <getopt.h>
23 #else
24 extern char *optarg;
25 extern int optind;
26 #endif
27
28 #include "ext2fs/ext2_fs.h"
29 #include "ext2fs/ext2fs.h"
30 #include "e2freefrag.h"
31
32 void usage(const char *prog)
33 {
34         fprintf(stderr, "usage: %s [-c chunksize in kb] [-h] "
35                 "device_name\n", prog);
36         exit(1);
37 }
38
39 static int ul_log2(unsigned long arg)
40 {
41         int     l = 0;
42
43         arg >>= 1;
44         while (arg) {
45                 l++;
46                 arg >>= 1;
47         }
48         return l;
49 }
50
51 void init_chunk_info(ext2_filsys fs, struct chunk_info *info)
52 {
53         int i;
54
55         info->chunkbits = ul_log2(info->chunkbytes);
56         info->blocksize_bits = ul_log2((unsigned long)fs->blocksize);
57         info->blks_in_chunk = info->chunkbytes >> info->blocksize_bits;
58
59         info->min = ~0UL;
60         info->max = info->avg = 0;
61         info->real_free_chunks = 0;
62
63         for (i = 0; i < MAX_HIST; i++) {
64                 info->histogram.fc_chunks[i] = 0;
65                 info->histogram.fc_blocks[i] = 0;
66         }
67 }
68
69 void scan_block_bitmap(ext2_filsys fs, struct chunk_info *info)
70 {
71         unsigned long long blocks_count = fs->super->s_blocks_count;
72         unsigned long long chunks = (blocks_count + info->blks_in_chunk) >>
73                                 (info->chunkbits - info->blocksize_bits);
74         unsigned long long chunk_num;
75         unsigned long last_chunk_size = 0;
76         unsigned long long chunk_start_blk = 0;
77
78         for (chunk_num = 0; chunk_num < chunks; chunk_num++) {
79                 unsigned long long blk, num_blks;
80                 int chunk_free;
81
82                 /* Last chunk may be smaller */
83                 if (chunk_start_blk + info->blks_in_chunk > blocks_count)
84                         num_blks = blocks_count - chunk_start_blk;
85                 else
86                         num_blks = info->blks_in_chunk;
87
88                 chunk_free = 0;
89
90                 /* Initialize starting block for first chunk correctly else
91                  * there is a segfault when blocksize = 1024 in which case
92                  * block_map->start = 1 */
93                 for (blk = (chunk_num == 0 ? fs->super->s_first_data_block : 0);
94                      blk < num_blks; blk++, chunk_start_blk++) {
95                         int used = ext2fs_fast_test_block_bitmap(fs->block_map,
96                                                                chunk_start_blk);
97                         if (!used) {
98                                 last_chunk_size++;
99                                 chunk_free++;
100                         }
101
102                         if (used && last_chunk_size != 0) {
103                                 unsigned long index;
104
105                                 index = ul_log2(last_chunk_size) + 1;
106                                 info->histogram.fc_chunks[index]++;
107                                 info->histogram.fc_blocks[index] +=
108                                                         last_chunk_size;
109
110                                 if (last_chunk_size > info->max)
111                                         info->max = last_chunk_size;
112                                 if (last_chunk_size < info->min)
113                                         info->min = last_chunk_size;
114                                 info->avg += last_chunk_size;
115
116                                 info->real_free_chunks++;
117                                 last_chunk_size = 0;
118                         }
119                 }
120
121                 if (chunk_free == info->blks_in_chunk)
122                         info->free_chunks++;
123         }
124 }
125
126 errcode_t get_chunk_info(ext2_filsys fs, struct chunk_info *info)
127 {
128         unsigned long total_chunks;
129         char *unitp = "KMGTPEZY";
130         int units = 10;
131         unsigned long start = 0, end, cum;
132         int i, retval = 0;
133
134         scan_block_bitmap(fs, info);
135
136         printf("Total blocks: %u\nFree blocks: %u (%0.1f%%)\n",
137                fs->super->s_blocks_count, fs->super->s_free_blocks_count,
138                (double)fs->super->s_free_blocks_count * 100 /
139                                                 fs->super->s_blocks_count);
140
141         printf("\nChunksize: %lu bytes (%u blocks)\n",
142                info->chunkbytes, info->blks_in_chunk);
143         total_chunks = (fs->super->s_blocks_count + info->blks_in_chunk) >>
144                                 (info->chunkbits - info->blocksize_bits);
145         printf("Total chunks: %lu\nFree chunks: %lu (%0.1f%%)\n",
146                total_chunks, info->free_chunks,
147                (double)info->free_chunks * 100 / total_chunks);
148
149         /* Display chunk information in KB */
150         if (info->real_free_chunks) {
151                 info->min = (info->min * fs->blocksize) >> 10;
152                 info->max = (info->max * fs->blocksize) >> 10;
153                 info->avg = (info->avg / info->real_free_chunks *
154                              fs->blocksize) >> 10;
155         } else {
156                 info->min = 0;
157         }
158
159         printf("\nMin free chunk: %lu KB \nMax free chunk: %lu KB\n"
160                "Avg free chunk: %lu KB\n", info->min, info->max, info->avg);
161
162         printf("\nHISTOGRAM OF FREE CHUNK SIZES:\n");
163         printf("%s :  %12s  %12s  %7s\n", "Chunk Size Range", "Free chunks",
164                "Free Blocks", "Percent");
165         for (i = 0; i < MAX_HIST; i++) {
166                 end = 1 << (i + info->blocksize_bits - units);
167                 if (info->histogram.fc_chunks[i] != 0)
168                         printf("%5lu%c...%5lu%c- :  %12lu  %12lu  %6.2f%%\n",
169                                start, *unitp, end, *unitp,
170                                info->histogram.fc_chunks[i],
171                                info->histogram.fc_blocks[i],
172                                (double)info->histogram.fc_blocks[i] * 100 /
173                                fs->super->s_free_blocks_count);
174                 start = end;
175                 if (start == 1<<10) {
176                         start = 1;
177                         units += 10;
178                         unitp++;
179                 }
180         }
181
182         return retval;
183 }
184
185 void close_device(char *device_name, ext2_filsys fs)
186 {
187         int retval = ext2fs_close(fs);
188
189         if (retval)
190                 com_err(device_name, retval, "while closing the filesystem.\n");
191 }
192
193 void collect_info(ext2_filsys fs, struct chunk_info *chunk_info)
194 {
195         unsigned int retval = 0, i, free_blks;
196
197         printf("Device: %s\n", fs->device_name);
198         printf("Blocksize: %u bytes\n", fs->blocksize);
199
200         retval = ext2fs_read_block_bitmap(fs);
201         if (retval) {
202                 com_err(fs->device_name, retval, "while reading block bitmap");
203                 close_device(fs->device_name, fs);
204                 exit(1);
205         }
206
207         init_chunk_info(fs, chunk_info);
208
209         retval = get_chunk_info(fs, chunk_info);
210         if (retval) {
211                 com_err(fs->device_name, retval, "while collecting chunk info");
212                 close_device(fs->device_name, fs);
213                 exit(1);
214         }
215 }
216
217 void open_device(char *device_name, ext2_filsys *fs)
218 {
219         int retval;
220         int flag = EXT2_FLAG_FORCE;
221
222         retval = ext2fs_open(device_name, flag, 0, 0, unix_io_manager, fs);
223         if (retval) {
224                 com_err(device_name, retval, "while opening filesystem");
225                 exit(1);
226         }
227 }
228
229 int main(int argc, char *argv[])
230 {
231         struct chunk_info chunk_info = { .chunkbytes = DEFAULT_CHUNKSIZE };
232         errcode_t retval = 0;
233         ext2_filsys fs = NULL;
234         char *device_name;
235         char *progname;
236         char c, *end;
237
238         progname = argv[0];
239
240         while ((c = getopt(argc, argv, "c:h")) != EOF) {
241                 switch (c) {
242                 case 'c':
243                         chunk_info.chunkbytes = strtoull(optarg, &end, 0);
244                         if (*end != '\0') {
245                                 fprintf(stderr, "%s: bad chunk size '%s'\n",
246                                         progname, optarg);
247                                 usage(progname);
248                         }
249                         if (chunk_info.chunkbytes &
250                             (chunk_info.chunkbytes - 1)) {
251                                 fprintf(stderr, "%s: chunk size must be a "
252                                         "power of 2.", argv[0]);
253                                 usage(progname);
254                         }
255                         chunk_info.chunkbytes *= 1024;
256                         break;
257                 default:
258                         fprintf(stderr, "%s: bad option '%c'\n",
259                                 progname, c);
260                 case 'h':
261                         usage(progname);
262                         break;
263                 }
264         }
265
266         if (optind == argc) {
267                 fprintf(stderr, "%s: missing device name.\n", progname);
268                 usage(progname);
269         }
270
271         device_name = argv[optind];
272
273         open_device(device_name, &fs);
274
275         if (chunk_info.chunkbytes < fs->blocksize) {
276                 fprintf(stderr, "%s: chunksize must be greater than or equal "
277                         "to filesystem blocksize.\n", progname);
278                 exit(1);
279         }
280         collect_info(fs, &chunk_info);
281         close_device(device_name, fs);
282
283         return retval;
284 }