sbin/hammer: Cleanup blocks with a single statement
[dragonfly.git] / sbin / hammer / misc.c
1 /*
2  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $DragonFly: src/sbin/hammer/misc.c,v 1.5 2008/06/26 04:07:57 dillon Exp $
35  */
36
37 #include "hammer_util.h"
38
39 void
40 hammer_key_beg_init(hammer_base_elm_t base)
41 {
42         bzero(base, sizeof(*base));
43
44         base->localization = HAMMER_MIN_LOCALIZATION;
45         base->obj_id = HAMMER_MIN_OBJID;
46         base->key = HAMMER_MIN_KEY;
47         base->create_tid = 1;
48         base->rec_type = HAMMER_MIN_RECTYPE;
49         base->obj_type = 0;
50 }
51
52 void
53 hammer_key_end_init(hammer_base_elm_t base)
54 {
55         bzero(base, sizeof(*base));
56
57         base->localization = HAMMER_MAX_LOCALIZATION;
58         base->obj_id = HAMMER_MAX_OBJID;
59         base->key = HAMMER_MAX_KEY;
60         base->create_tid = HAMMER_MAX_TID;
61         base->rec_type = HAMMER_MAX_RECTYPE;
62         base->obj_type = 0;
63 }
64
65 int
66 getyn(void)
67 {
68         char buf[256];
69         int len;
70
71         if (fgets(buf, sizeof(buf), stdin) == NULL)
72                 return(0);
73         len = strlen(buf);
74         while (len && (buf[len-1] == '\n' || buf[len-1] == '\r'))
75                 --len;
76         buf[len] = 0;
77         if (strcmp(buf, "y") == 0 ||
78             strcmp(buf, "yes") == 0 ||
79             strcmp(buf, "Y") == 0 ||
80             strcmp(buf, "YES") == 0)
81                 return(1);
82         return(0);
83 }
84
85 const char *
86 sizetostr(off_t size)
87 {
88         static char buf[32];
89
90         if (size < 1024 / 2)
91                 snprintf(buf, sizeof(buf), "%6.2fB", (double)size);
92         else if (size < 1024 * 1024 / 2)
93                 snprintf(buf, sizeof(buf), "%6.2fKB", (double)size / 1024);
94         else if (size < 1024 * 1024 * 1024LL / 2)
95                 snprintf(buf, sizeof(buf), "%6.2fMB",
96                         (double)size / (1024 * 1024));
97         else if (size < 1024 * 1024 * 1024LL * 1024LL / 2)
98                 snprintf(buf, sizeof(buf), "%6.2fGB",
99                         (double)size / (1024 * 1024 * 1024LL));
100         else
101                 snprintf(buf, sizeof(buf), "%6.2fTB",
102                         (double)size / (1024 * 1024 * 1024LL * 1024LL));
103         return(buf);
104 }
105
106 int
107 hammer_fs_to_vol(const char *fs, struct hammer_ioc_volume_list *p)
108 {
109         struct hammer_ioc_volume_list ioc;
110         int fd;
111
112         fd = open(fs, O_RDONLY);
113         if (fd < 0) {
114                 perror("open");
115                 return(-1);
116         }
117
118         bzero(&ioc, sizeof(ioc));
119         ioc.nvols = HAMMER_MAX_VOLUMES;
120         ioc.vols = malloc(ioc.nvols * sizeof(*ioc.vols));
121         if (ioc.vols == NULL) {
122                 perror("malloc");
123                 close(fd);
124                 return(-1);
125         }
126
127         if (ioctl(fd, HAMMERIOC_LIST_VOLUMES, &ioc) < 0) {
128                 perror("ioctl");
129                 close(fd);
130                 free(ioc.vols);
131                 return(-1);
132         }
133
134         bcopy(&ioc, p, sizeof(ioc));
135         close(fd);
136
137         return(0);
138 }
139
140 int
141 hammer_fs_to_rootvol(const char *fs, char *buf, int len)
142 {
143         struct hammer_ioc_volume_list ioc;
144         int i;
145
146         if (hammer_fs_to_vol(fs, &ioc) == -1)
147                 return(-1);
148
149         for (i = 0; i < ioc.nvols; i++)
150                 if (ioc.vols[i].vol_no == HAMMER_ROOT_VOLNO) {
151                         strlcpy(buf, ioc.vols[i].device_name, len);
152                         break;
153                 }
154         assert(i != ioc.nvols);  /* root volume must exist */
155
156         free(ioc.vols);
157         return(0);
158 }
159
160 /*
161  * Functions and data structure for zone statistics
162  */
163 /*
164  * Each layer1 needs ((2^19) / 64) = 8192 uint64_t.
165  */
166 #define HAMMER_LAYER1_UINT64 8192
167 #define HAMMER_LAYER1_BYTES (HAMMER_LAYER1_UINT64 * sizeof(uint64_t))
168
169 static int *l1_max = NULL;
170 static uint64_t **l1_bits = NULL;
171
172 static __inline
173 int
174 hammer_set_layer_bits(uint64_t *bits, int i)
175 {
176         int q, r;
177
178         q = i >> 6;
179         r = i & ((1 << 6) - 1);
180
181         bits += q;
182         if (!((*bits) & ((uint64_t)1 << r))) {
183                 (*bits) |= ((uint64_t)1 << r);
184                 return(1);
185         }
186         return(0);  /* already seen this block */
187 }
188
189 static
190 void
191 hammer_extend_layer1_bits(int vol, int newsiz, int oldsiz)
192 {
193         uint64_t *p;
194
195         assert(newsiz > oldsiz);
196         assert(newsiz > 0 && oldsiz >= 0);
197
198         p = l1_bits[vol];
199         if (p == NULL)
200                 p = malloc(HAMMER_LAYER1_BYTES * newsiz);
201         else
202                 p = realloc(p, HAMMER_LAYER1_BYTES * newsiz);
203         if (p == NULL)
204                 err(1, "alloc");
205         l1_bits[vol] = p;
206
207         p += HAMMER_LAYER1_UINT64 * oldsiz;
208         bzero(p, HAMMER_LAYER1_BYTES * (newsiz - oldsiz));
209 }
210
211 struct zone_stat*
212 hammer_init_zone_stat(void)
213 {
214         return(calloc(HAMMER_MAX_ZONES, sizeof(struct zone_stat)));
215 }
216
217 struct zone_stat*
218 hammer_init_zone_stat_bits(void)
219 {
220         int i;
221
222         l1_max = calloc(HAMMER_MAX_VOLUMES, sizeof(int));
223         if (l1_max == NULL)
224                 err(1, "calloc");
225
226         l1_bits = calloc(HAMMER_MAX_VOLUMES, sizeof(uint64_t*));
227         if (l1_bits == NULL)
228                 err(1, "calloc");
229
230         for (i = 0; i < HAMMER_MAX_VOLUMES; i++) {
231                 l1_max[i] = -1;  /* +1 needs to be 0 */
232                 l1_bits[i] = NULL;
233         }
234         return(hammer_init_zone_stat());
235 }
236
237 void
238 hammer_cleanup_zone_stat(struct zone_stat *stats)
239 {
240         int i;
241
242         if (l1_bits)
243                 for (i = 0; i < HAMMER_MAX_VOLUMES; i++) {
244                         free(l1_bits[i]);
245                         l1_bits[i] = NULL;
246                 }
247
248         free(l1_bits);
249         l1_bits = NULL;
250
251         free(l1_max);
252         l1_max = NULL;
253
254         free(stats);
255 }
256
257 static
258 void
259 _hammer_add_zone_stat(struct zone_stat *stats, int zone, int bytes,
260         int new_block, int new_item)
261 {
262         struct zone_stat *sp = stats + zone;
263
264         if (new_block)
265                 sp->blocks++;
266         if (new_item)
267                 sp->items++;
268         sp->used += bytes;
269 }
270
271 void
272 hammer_add_zone_stat(struct zone_stat *stats, hammer_off_t offset, int bytes)
273 {
274         int zone, vol, i, j, new_block;
275         uint64_t *p;
276
277         offset &= ~HAMMER_BIGBLOCK_MASK64;
278         zone = HAMMER_ZONE_DECODE(offset);
279         vol = HAMMER_VOL_DECODE(offset);
280
281         offset = HAMMER_OFF_SHORT_ENCODE(offset); /* cut off volume bits */
282         i = HAMMER_BLOCKMAP_LAYER1_INDEX(offset);
283         j = HAMMER_BLOCKMAP_LAYER2_INDEX(offset);
284
285         if (i > l1_max[vol]) {
286                 assert(i < (HAMMER_BLOCKMAP_RADIX1 / HAMMER_MAX_VOLUMES));
287                 hammer_extend_layer1_bits(vol, i + 1, l1_max[vol] + 1);
288                 l1_max[vol] = i;
289         }
290
291         p = l1_bits[vol] + i * HAMMER_LAYER1_UINT64;
292         new_block = hammer_set_layer_bits(p, j);
293         _hammer_add_zone_stat(stats, zone, bytes, new_block, 1);
294 }
295
296 /*
297  * If the same layer2 is used more than once the result will be wrong.
298  */
299 void
300 hammer_add_zone_stat_layer2(struct zone_stat *stats,
301         hammer_blockmap_layer2_t layer2)
302 {
303         _hammer_add_zone_stat(stats, layer2->zone,
304                 HAMMER_BIGBLOCK_SIZE - layer2->bytes_free, 1, 0);
305 }
306
307 static __inline
308 double
309 _calc_used_percentage(int64_t blocks, int64_t used)
310 {
311         double res;
312
313         if (blocks)
314                 res = ((double)(used * 100)) / (blocks << HAMMER_BIGBLOCK_BITS);
315         else
316                 res = 0;
317         return(res);
318 }
319
320 void
321 hammer_print_zone_stat(const struct zone_stat *stats)
322 {
323         int i;
324         int64_t total_blocks = 0;
325         int64_t total_items = 0;
326         int64_t total_used = 0;
327         const struct zone_stat *p = stats;
328 #define INDENT ""
329
330         printf("HAMMER zone statistics\n");
331         printf(INDENT"zone #             "
332                 "blocks       items              used[B]             used[%%]\n");
333         for (i = 0; i < HAMMER_MAX_ZONES; i++) {
334                 printf(INDENT"zone %-2d %-10s %-12ju %-18ju %-19ju %g\n",
335                         i, zone_labels[i], p->blocks, p->items, p->used,
336                         _calc_used_percentage(p->blocks, p->used));
337                 total_blocks += p->blocks;
338                 total_items += p->items;
339                 total_used += p->used;
340                 p++;
341         }
342
343         /*
344          * Remember that zone0 is always 0% used and zone15 is
345          * always 100% used.
346          */
347         printf(INDENT"--------------------------------------------------------------------------------\n");
348         printf(INDENT"total              %-12ju %-18ju %-19ju %g\n",
349                 (uintmax_t)total_blocks,
350                 (uintmax_t)total_items,
351                 (uintmax_t)total_used,
352                 _calc_used_percentage(total_blocks, total_used));
353 }