]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - misc/e2freefrag.c
e2freefrag: Do not print chunk-related information by default
[thirdparty/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->blocksize_bits = ul_log2((unsigned long)fs->blocksize);
56 if (info->chunkbytes) {
57 info->chunkbits = ul_log2(info->chunkbytes);
58 info->blks_in_chunk = info->chunkbytes >> info->blocksize_bits;
59 } else {
60 info->chunkbits = ul_log2(DEFAULT_CHUNKSIZE);
61 info->blks_in_chunk = DEFAULT_CHUNKSIZE >> info->blocksize_bits;
62 }
63
64 info->min = ~0UL;
65 info->max = info->avg = 0;
66 info->real_free_chunks = 0;
67
68 for (i = 0; i < MAX_HIST; i++) {
69 info->histogram.fc_chunks[i] = 0;
70 info->histogram.fc_blocks[i] = 0;
71 }
72 }
73
74 void scan_block_bitmap(ext2_filsys fs, struct chunk_info *info)
75 {
76 unsigned long long blocks_count = fs->super->s_blocks_count;
77 unsigned long long chunks = (blocks_count + info->blks_in_chunk) >>
78 (info->chunkbits - info->blocksize_bits);
79 unsigned long long chunk_num;
80 unsigned long last_chunk_size = 0;
81 unsigned long long chunk_start_blk = 0;
82
83 for (chunk_num = 0; chunk_num < chunks; chunk_num++) {
84 unsigned long long blk, num_blks;
85 int chunk_free;
86
87 /* Last chunk may be smaller */
88 if (chunk_start_blk + info->blks_in_chunk > blocks_count)
89 num_blks = blocks_count - chunk_start_blk;
90 else
91 num_blks = info->blks_in_chunk;
92
93 chunk_free = 0;
94
95 /* Initialize starting block for first chunk correctly else
96 * there is a segfault when blocksize = 1024 in which case
97 * block_map->start = 1 */
98 for (blk = (chunk_num == 0 ? fs->super->s_first_data_block : 0);
99 blk < num_blks; blk++, chunk_start_blk++) {
100 int used = ext2fs_fast_test_block_bitmap(fs->block_map,
101 chunk_start_blk);
102 if (!used) {
103 last_chunk_size++;
104 chunk_free++;
105 }
106
107 if (used && last_chunk_size != 0) {
108 unsigned long index;
109
110 index = ul_log2(last_chunk_size) + 1;
111 info->histogram.fc_chunks[index]++;
112 info->histogram.fc_blocks[index] +=
113 last_chunk_size;
114
115 if (last_chunk_size > info->max)
116 info->max = last_chunk_size;
117 if (last_chunk_size < info->min)
118 info->min = last_chunk_size;
119 info->avg += last_chunk_size;
120
121 info->real_free_chunks++;
122 last_chunk_size = 0;
123 }
124 }
125
126 if (chunk_free == info->blks_in_chunk)
127 info->free_chunks++;
128 }
129 }
130
131 errcode_t get_chunk_info(ext2_filsys fs, struct chunk_info *info)
132 {
133 unsigned long total_chunks;
134 char *unitp = "KMGTPEZY";
135 int units = 10;
136 unsigned long start = 0, end, cum;
137 int i, retval = 0;
138
139 scan_block_bitmap(fs, info);
140
141 printf("Total blocks: %u\nFree blocks: %u (%0.1f%%)\n",
142 fs->super->s_blocks_count, fs->super->s_free_blocks_count,
143 (double)fs->super->s_free_blocks_count * 100 /
144 fs->super->s_blocks_count);
145
146 if (info->chunkbytes) {
147 printf("\nChunksize: %lu bytes (%u blocks)\n",
148 info->chunkbytes, info->blks_in_chunk);
149 total_chunks = (fs->super->s_blocks_count +
150 info->blks_in_chunk) >>
151 (info->chunkbits - info->blocksize_bits);
152 printf("Total chunks: %lu\nFree chunks: %lu (%0.1f%%)\n",
153 total_chunks, info->free_chunks,
154 (double)info->free_chunks * 100 / total_chunks);
155 }
156
157 /* Display chunk information in KB */
158 if (info->real_free_chunks) {
159 info->min = (info->min * fs->blocksize) >> 10;
160 info->max = (info->max * fs->blocksize) >> 10;
161 info->avg = (info->avg / info->real_free_chunks *
162 fs->blocksize) >> 10;
163 } else {
164 info->min = 0;
165 }
166
167 printf("\nMin. free extent: %lu KB \nMax. free extent: %lu KB\n"
168 "Avg. free extent: %lu KB\n", info->min, info->max, info->avg);
169
170 printf("\nHISTOGRAM OF FREE EXTENT SIZES:\n");
171 printf("%s : %12s %12s %7s\n", "Extent Size Range", "Free extents",
172 "Free Blocks", "Percent");
173 for (i = 0; i < MAX_HIST; i++) {
174 end = 1 << (i + info->blocksize_bits - units);
175 if (info->histogram.fc_chunks[i] != 0)
176 printf("%5lu%c...%5lu%c- : %12lu %12lu %6.2f%%\n",
177 start, *unitp, end, *unitp,
178 info->histogram.fc_chunks[i],
179 info->histogram.fc_blocks[i],
180 (double)info->histogram.fc_blocks[i] * 100 /
181 fs->super->s_free_blocks_count);
182 start = end;
183 if (start == 1<<10) {
184 start = 1;
185 units += 10;
186 unitp++;
187 }
188 }
189
190 return retval;
191 }
192
193 void close_device(char *device_name, ext2_filsys fs)
194 {
195 int retval = ext2fs_close(fs);
196
197 if (retval)
198 com_err(device_name, retval, "while closing the filesystem.\n");
199 }
200
201 void collect_info(ext2_filsys fs, struct chunk_info *chunk_info)
202 {
203 unsigned int retval = 0, i, free_blks;
204
205 printf("Device: %s\n", fs->device_name);
206 printf("Blocksize: %u bytes\n", fs->blocksize);
207
208 retval = ext2fs_read_block_bitmap(fs);
209 if (retval) {
210 com_err(fs->device_name, retval, "while reading block bitmap");
211 close_device(fs->device_name, fs);
212 exit(1);
213 }
214
215 init_chunk_info(fs, chunk_info);
216
217 retval = get_chunk_info(fs, chunk_info);
218 if (retval) {
219 com_err(fs->device_name, retval, "while collecting chunk info");
220 close_device(fs->device_name, fs);
221 exit(1);
222 }
223 }
224
225 void open_device(char *device_name, ext2_filsys *fs)
226 {
227 int retval;
228 int flag = EXT2_FLAG_FORCE;
229
230 retval = ext2fs_open(device_name, flag, 0, 0, unix_io_manager, fs);
231 if (retval) {
232 com_err(device_name, retval, "while opening filesystem");
233 exit(1);
234 }
235 }
236
237 int main(int argc, char *argv[])
238 {
239 struct chunk_info chunk_info = { };
240 errcode_t retval = 0;
241 ext2_filsys fs = NULL;
242 char *device_name;
243 char *progname;
244 char c, *end;
245
246 add_error_table(&et_ext2_error_table);
247 progname = argv[0];
248
249 while ((c = getopt(argc, argv, "c:h")) != EOF) {
250 switch (c) {
251 case 'c':
252 chunk_info.chunkbytes = strtoull(optarg, &end, 0);
253 if (*end != '\0') {
254 fprintf(stderr, "%s: bad chunk size '%s'\n",
255 progname, optarg);
256 usage(progname);
257 }
258 if (chunk_info.chunkbytes &
259 (chunk_info.chunkbytes - 1)) {
260 fprintf(stderr, "%s: chunk size must be a "
261 "power of 2.\n", argv[0]);
262 usage(progname);
263 }
264 chunk_info.chunkbytes *= 1024;
265 break;
266 default:
267 fprintf(stderr, "%s: bad option '%c'\n",
268 progname, c);
269 case 'h':
270 usage(progname);
271 break;
272 }
273 }
274
275 if (optind == argc) {
276 fprintf(stderr, "%s: missing device name.\n", progname);
277 usage(progname);
278 }
279
280 device_name = argv[optind];
281
282 open_device(device_name, &fs);
283
284 if (chunk_info.chunkbytes && (chunk_info.chunkbytes < fs->blocksize)) {
285 fprintf(stderr, "%s: chunksize must be greater than or equal "
286 "to filesystem blocksize.\n", progname);
287 exit(1);
288 }
289 collect_info(fs, &chunk_info);
290 close_device(device_name, fs);
291
292 return retval;
293 }