sys/vfs/hammer: Use MIN/MAX macros for B-Tree node's elm base
[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         }
83         return(0);
84 }
85
86 const char *
87 sizetostr(off_t size)
88 {
89         static char buf[32];
90
91         if (size < 1024 / 2) {
92                 snprintf(buf, sizeof(buf), "%6.2fB", (double)size);
93         } else if (size < 1024 * 1024 / 2) {
94                 snprintf(buf, sizeof(buf), "%6.2fKB",
95                         (double)size / 1024);
96         } else if (size < 1024 * 1024 * 1024LL / 2) {
97                 snprintf(buf, sizeof(buf), "%6.2fMB",
98                         (double)size / (1024 * 1024));
99         } else if (size < 1024 * 1024 * 1024LL * 1024LL / 2) {
100                 snprintf(buf, sizeof(buf), "%6.2fGB",
101                         (double)size / (1024 * 1024 * 1024LL));
102         } else {
103                 snprintf(buf, sizeof(buf), "%6.2fTB",
104                         (double)size / (1024 * 1024 * 1024LL * 1024LL));
105         }
106         return(buf);
107 }
108
109 int
110 hammer_fs_to_vol(const char *fs, struct hammer_ioc_volume_list *p)
111 {
112         struct hammer_ioc_volume_list ioc;
113         int fd;
114
115         fd = open(fs, O_RDONLY);
116         if (fd < 0) {
117                 perror("open");
118                 return(-1);
119         }
120
121         bzero(&ioc, sizeof(ioc));
122         ioc.nvols = HAMMER_MAX_VOLUMES;
123         ioc.vols = malloc(ioc.nvols * sizeof(*ioc.vols));
124         if (ioc.vols == NULL) {
125                 perror("malloc");
126                 close(fd);
127                 return(-1);
128         }
129
130         if (ioctl(fd, HAMMERIOC_LIST_VOLUMES, &ioc) < 0) {
131                 perror("ioctl");
132                 close(fd);
133                 free(ioc.vols);
134                 return(-1);
135         }
136
137         bcopy(&ioc, p, sizeof(ioc));
138         close(fd);
139
140         return(0);
141 }
142
143 int
144 hammer_fs_to_rootvol(const char *fs, char *buf, int len)
145 {
146         struct hammer_ioc_volume_list ioc;
147         int i;
148
149         if (hammer_fs_to_vol(fs, &ioc) == -1)
150                 return(-1);
151
152         for (i = 0; i < ioc.nvols; i++) {
153                 if (ioc.vols[i].vol_no == HAMMER_ROOT_VOLNO) {
154                         strlcpy(buf, ioc.vols[i].device_name, len);
155                         break;
156                 }
157         }
158         assert(i != ioc.nvols);  /* root volume must exist */
159
160         free(ioc.vols);
161         return(0);
162 }
163
164 /*
165  * Functions and data structure for zone statistics
166  */
167 /*
168  * Each layer1 needs ((2^19) / 64) = 8192 uint64_t.
169  */
170 #define HAMMER_LAYER1_UINT64 8192
171 #define HAMMER_LAYER1_BYTES (HAMMER_LAYER1_UINT64 * sizeof(uint64_t))
172
173 static int *l1_max = NULL;
174 static uint64_t **l1_bits = NULL;
175
176 static __inline
177 int
178 hammer_set_layer_bits(uint64_t *bits, int i)
179 {
180         int q, r;
181
182         q = i >> 6;
183         r = i & ((1 << 6) - 1);
184
185         bits += q;
186         if (!((*bits) & ((uint64_t)1 << r))) {
187                 (*bits) |= ((uint64_t)1 << r);
188                 return(1);
189         }
190         return(0);  /* already seen this block */
191 }
192
193 static
194 void
195 hammer_extend_layer1_bits(int vol, int newsiz, int oldsiz)
196 {
197         uint64_t *p;
198
199         assert(newsiz > oldsiz);
200         assert(newsiz > 0 && oldsiz >= 0);
201
202         p = l1_bits[vol];
203         if (p == NULL)
204                 p = malloc(HAMMER_LAYER1_BYTES * newsiz);
205         else
206                 p = realloc(p, HAMMER_LAYER1_BYTES * newsiz);
207         if (p == NULL)
208                 err(1, "alloc");
209         l1_bits[vol] = p;
210
211         p += HAMMER_LAYER1_UINT64 * oldsiz;
212         bzero(p, HAMMER_LAYER1_BYTES * (newsiz - oldsiz));
213 }
214
215 struct zone_stat*
216 hammer_init_zone_stat(void)
217 {
218         return(calloc(HAMMER_MAX_ZONES, sizeof(struct zone_stat)));
219 }
220
221 struct zone_stat*
222 hammer_init_zone_stat_bits(void)
223 {
224         int i;
225
226         l1_max = calloc(HAMMER_MAX_VOLUMES, sizeof(int));
227         if (l1_max == NULL)
228                 err(1, "calloc");
229
230         l1_bits = calloc(HAMMER_MAX_VOLUMES, sizeof(uint64_t*));
231         if (l1_bits == NULL)
232                 err(1, "calloc");
233
234         for (i = 0; i < HAMMER_MAX_VOLUMES; i++) {
235                 l1_max[i] = -1;  /* +1 needs to be 0 */
236                 l1_bits[i] = NULL;
237         }
238         return(hammer_init_zone_stat());
239 }
240
241 void
242 hammer_cleanup_zone_stat(struct zone_stat *stats)
243 {
244         int i;
245
246         if (l1_bits) {
247                 for (i = 0; i < HAMMER_MAX_VOLUMES; i++) {
248                         free(l1_bits[i]);
249                         l1_bits[i] = NULL;
250                 }
251         }
252
253         free(l1_bits);
254         l1_bits = NULL;
255
256         free(l1_max);
257         l1_max = NULL;
258
259         free(stats);
260 }
261
262 static
263 void
264 _hammer_add_zone_stat(struct zone_stat *stats, int zone, int bytes,
265         int new_block, int new_item)
266 {
267         struct zone_stat *sp = stats + zone;
268
269         if (new_block)
270                 sp->blocks++;
271         if (new_item)
272                 sp->items++;
273         sp->used += bytes;
274 }
275
276 void
277 hammer_add_zone_stat(struct zone_stat *stats, hammer_off_t offset, int bytes)
278 {
279         int zone, vol, i, j, new_block;
280         uint64_t *p;
281
282         offset &= ~HAMMER_BIGBLOCK_MASK64;
283         zone = HAMMER_ZONE_DECODE(offset);
284         vol = HAMMER_VOL_DECODE(offset);
285
286         offset &= HAMMER_OFF_SHORT_MASK;  /* cut off volume bits from layer1 */
287         i = HAMMER_BLOCKMAP_LAYER1_INDEX(offset);
288         j = HAMMER_BLOCKMAP_LAYER2_INDEX(offset);
289
290         if (i > l1_max[vol]) {
291                 assert(i < (HAMMER_BLOCKMAP_RADIX1 / HAMMER_MAX_VOLUMES));
292                 hammer_extend_layer1_bits(vol, i + 1, l1_max[vol] + 1);
293                 l1_max[vol] = i;
294         }
295
296         p = l1_bits[vol] + i * HAMMER_LAYER1_UINT64;
297         new_block = hammer_set_layer_bits(p, j);
298         _hammer_add_zone_stat(stats, zone, bytes, new_block, 1);
299 }
300
301 /*
302  * If the same layer2 is used more than once the result will be wrong.
303  */
304 void
305 hammer_add_zone_stat_layer2(struct zone_stat *stats,
306         hammer_blockmap_layer2_t layer2)
307 {
308         _hammer_add_zone_stat(stats, layer2->zone,
309                 HAMMER_BIGBLOCK_SIZE - layer2->bytes_free, 1, 0);
310 }
311
312 static __inline
313 double
314 _calc_used_percentage(int64_t blocks, int64_t used)
315 {
316         double res;
317
318         if (blocks)
319                 res = ((double)(used * 100)) / (blocks << HAMMER_BIGBLOCK_BITS);
320         else
321                 res = 0;
322         return(res);
323 }
324
325 void
326 hammer_print_zone_stat(const struct zone_stat *stats)
327 {
328         int i;
329         int64_t total_blocks = 0;
330         int64_t total_items = 0;
331         int64_t total_used = 0;
332         const struct zone_stat *p = stats;
333 #define INDENT ""
334
335         printf("HAMMER zone statistics\n");
336         printf(INDENT"zone #             "
337                 "blocks       items              used[B]             used[%%]\n");
338         for (i = 0; i < HAMMER_MAX_ZONES; i++) {
339                 printf(INDENT"zone %-2d %-10s %-12ju %-18ju %-19ju %g\n",
340                         i, zone_labels[i], p->blocks, p->items, p->used,
341                         _calc_used_percentage(p->blocks, p->used));
342                 total_blocks += p->blocks;
343                 total_items += p->items;
344                 total_used += p->used;
345                 p++;
346         }
347
348         /*
349          * Remember that zone0 is always 0% used and zone15 is
350          * always 100% used.
351          */
352         printf(INDENT"--------------------------------------------------------------------------------\n");
353         printf(INDENT"total              %-12ju %-18ju %-19ju %g\n",
354                 (uintmax_t)total_blocks,
355                 (uintmax_t)total_items,
356                 (uintmax_t)total_used,
357                 _calc_used_percentage(total_blocks, total_used));
358 }