2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
34 * $DragonFly: src/sbin/hammer/misc.c,v 1.5 2008/06/26 04:07:57 dillon Exp $
40 * (taken from /usr/src/sys/vfs/hammer/hammer_btree.c)
42 * Compare two B-Tree elements, return -N, 0, or +N (e.g. similar to strcmp).
44 * Note that for this particular function a return value of -1, 0, or +1
45 * can denote a match if delete_tid is otherwise discounted. A delete_tid
46 * of zero is considered to be 'infinity' in comparisons.
48 * See also hammer_rec_rb_compare() and hammer_rec_cmp() in hammer_object.c.
51 hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2)
53 if (key1->localization < key2->localization)
55 if (key1->localization > key2->localization)
58 if (key1->obj_id < key2->obj_id)
60 if (key1->obj_id > key2->obj_id)
63 if (key1->rec_type < key2->rec_type)
65 if (key1->rec_type > key2->rec_type)
68 if (key1->key < key2->key)
70 if (key1->key > key2->key)
73 if (key1->create_tid == 0) {
74 if (key2->create_tid == 0)
78 if (key2->create_tid == 0)
80 if (key1->create_tid < key2->create_tid)
82 if (key1->create_tid > key2->create_tid)
88 hammer_key_beg_init(hammer_base_elm_t base)
90 bzero(base, sizeof(*base));
92 base->localization = HAMMER_MIN_LOCALIZATION;
93 base->obj_id = HAMMER_MIN_OBJID;
94 base->key = HAMMER_MIN_KEY;
96 base->rec_type = HAMMER_MIN_RECTYPE;
100 hammer_key_end_init(hammer_base_elm_t base)
102 bzero(base, sizeof(*base));
104 base->localization = HAMMER_MAX_LOCALIZATION;
105 base->obj_id = HAMMER_MAX_OBJID;
106 base->key = HAMMER_MAX_KEY;
107 base->create_tid = HAMMER_MAX_TID;
108 base->rec_type = HAMMER_MAX_RECTYPE;
112 hammer_crc_test_leaf(void *data, hammer_btree_leaf_elm_t leaf)
116 if (leaf->data_len == 0) {
119 switch(leaf->base.rec_type) {
120 case HAMMER_RECTYPE_INODE:
121 if (leaf->data_len != sizeof(struct hammer_inode_data))
123 crc = crc32(data, HAMMER_INODE_CRCSIZE);
126 crc = crc32(data, leaf->data_len);
130 return (leaf->data_crc == crc);
139 if (fgets(buf, sizeof(buf), stdin) == NULL)
142 while (len && (buf[len-1] == '\n' || buf[len-1] == '\r'))
145 if (strcmp(buf, "y") == 0 ||
146 strcmp(buf, "yes") == 0 ||
147 strcmp(buf, "Y") == 0 ||
148 strcmp(buf, "YES") == 0) {
155 sizetostr(off_t size)
159 if (size < 1024 / 2) {
160 snprintf(buf, sizeof(buf), "%6.2f", (double)size);
161 } else if (size < 1024 * 1024 / 2) {
162 snprintf(buf, sizeof(buf), "%6.2fKB",
163 (double)size / 1024);
164 } else if (size < 1024 * 1024 * 1024LL / 2) {
165 snprintf(buf, sizeof(buf), "%6.2fMB",
166 (double)size / (1024 * 1024));
167 } else if (size < 1024 * 1024 * 1024LL * 1024LL / 2) {
168 snprintf(buf, sizeof(buf), "%6.2fGB",
169 (double)size / (1024 * 1024 * 1024LL));
171 snprintf(buf, sizeof(buf), "%6.2fTB",
172 (double)size / (1024 * 1024 * 1024LL * 1024LL));
178 hammer_fs_to_vol(const char *fs, struct hammer_ioc_volume_list *p)
180 struct hammer_ioc_volume_list ioc;
183 fd = open(fs, O_RDONLY);
189 bzero(&ioc, sizeof(ioc));
190 ioc.nvols = HAMMER_MAX_VOLUMES;
191 ioc.vols = malloc(ioc.nvols * sizeof(*ioc.vols));
192 if (ioc.vols == NULL) {
198 if (ioctl(fd, HAMMERIOC_LIST_VOLUMES, &ioc) < 0) {
205 bcopy(&ioc, p, sizeof(ioc));
212 hammer_fs_to_rootvol(const char *fs, char *buf, int len)
214 struct hammer_ioc_volume_list ioc;
217 if (hammer_fs_to_vol(fs, &ioc) == -1)
220 for (i = 0; i < ioc.nvols; i++) {
221 if (ioc.vols[i].vol_no == HAMMER_ROOT_VOLNO) {
222 strlcpy(buf, ioc.vols[i].device_name, len);
226 assert(i != ioc.nvols); /* root volume must exist */
233 * Functions and data structure for zone statistics
236 * Each layer1 needs ((2^19) / 64) = 8192 uint64_t.
238 #define HAMMER_LAYER1_UINT64 8192
239 #define HAMMER_LAYER1_BYTES (HAMMER_LAYER1_UINT64 * sizeof(uint64_t))
241 static int *l1_max = NULL;
242 static uint64_t **l1_bits = NULL;
246 hammer_set_layer_bits(uint64_t *bits, int i)
251 r = i & ((1 << 6) - 1);
254 if (!((*bits) & ((uint64_t)1 << r))) {
255 (*bits) |= ((uint64_t)1 << r);
258 return(0); /* already seen this block */
263 hammer_extend_layer1_bits(int vol, int newsiz, int oldsiz)
267 assert(newsiz > oldsiz);
268 assert(newsiz > 0 && oldsiz >= 0);
272 p = malloc(HAMMER_LAYER1_BYTES * newsiz);
274 p = realloc(p, HAMMER_LAYER1_BYTES * newsiz);
279 p += HAMMER_LAYER1_UINT64 * oldsiz;
280 bzero(p, HAMMER_LAYER1_BYTES * (newsiz - oldsiz));
284 hammer_init_zone_stat(void)
286 return(calloc(HAMMER_MAX_ZONES, sizeof(struct zone_stat)));
290 hammer_init_zone_stat_bits(void)
294 l1_max = calloc(HAMMER_MAX_VOLUMES, sizeof(int));
298 l1_bits = calloc(HAMMER_MAX_VOLUMES, sizeof(uint64_t*));
302 for (i = 0; i < HAMMER_MAX_VOLUMES; i++) {
303 l1_max[i] = -1; /* +1 needs to be 0 */
306 return(hammer_init_zone_stat());
310 hammer_cleanup_zone_stat(struct zone_stat *stats)
315 for (i = 0; i < HAMMER_MAX_VOLUMES; i++) {
332 _hammer_add_zone_stat(struct zone_stat *stats, int zone,
333 hammer_off_t bytes, int new_block, int new_item)
335 struct zone_stat *sp = stats + zone;
345 hammer_add_zone_stat(struct zone_stat *stats, hammer_off_t offset,
348 int zone, vol, i, j, new_block;
351 offset &= ~HAMMER_BIGBLOCK_MASK64;
352 zone = HAMMER_ZONE_DECODE(offset);
353 vol = HAMMER_VOL_DECODE(offset);
355 offset &= HAMMER_OFF_SHORT_MASK; /* cut off volume bits from layer1 */
356 i = HAMMER_BLOCKMAP_LAYER1_INDEX(offset);
357 j = HAMMER_BLOCKMAP_LAYER2_INDEX(offset);
359 if (i > l1_max[vol]) {
360 assert(i < 1024); /* no >1024 layer1 per volume */
361 hammer_extend_layer1_bits(vol, i + 1, l1_max[vol] + 1);
365 p = l1_bits[vol] + i * HAMMER_LAYER1_UINT64;
366 new_block = hammer_set_layer_bits(p, j);
367 _hammer_add_zone_stat(stats, zone, bytes, new_block, 1);
371 * If the same layer2 is used more than once the result will be wrong.
374 hammer_add_zone_stat_layer2(struct zone_stat *stats,
375 struct hammer_blockmap_layer2 *layer2)
377 _hammer_add_zone_stat(stats, layer2->zone,
378 HAMMER_BIGBLOCK_SIZE - layer2->bytes_free, 1, 0);
383 _calc_used_percentage(hammer_off_t blocks, hammer_off_t used)
388 res = ((double)(used * 100)) / (blocks << HAMMER_BIGBLOCK_BITS);
395 hammer_print_zone_stat(const struct zone_stat *stats)
398 hammer_off_t total_blocks = 0;
399 hammer_off_t total_items = 0;
400 hammer_off_t total_used = 0;
401 const struct zone_stat *p = stats;
403 printf("HAMMER zone statistics\n");
404 printf("\tzone # blocks items used[B] used[%%]\n");
406 for (i = 0; i < HAMMER_MAX_ZONES; i++) {
407 printf("\tzone %-2d %-12ju %-18ju %-19ju %g\n",
408 i, p->blocks, p->items, p->used,
409 _calc_used_percentage(p->blocks, p->used));
410 total_blocks += p->blocks;
411 total_items += p->items;
412 total_used += p->used;
417 * Remember that zone0 is always 0% used and zone15 is
420 printf("\t----------------------------------------------------------------------\n");
421 printf("\ttotal %-12ju %-18ju %-19ju %g\n",
422 (uintmax_t)total_blocks,
423 (uintmax_t)total_items,
424 (uintmax_t)total_used,
425 _calc_used_percentage(total_blocks, total_used));