1797a0b63SMatthew Dillon /*
2797a0b63SMatthew Dillon * Copyright (c) 2009 The DragonFly Project. All rights reserved.
3797a0b63SMatthew Dillon *
4797a0b63SMatthew Dillon * This code is derived from software contributed to The DragonFly Project
5797a0b63SMatthew Dillon * by Matthew Dillon <dillon@backplane.com>
6797a0b63SMatthew Dillon *
7797a0b63SMatthew Dillon * Redistribution and use in source and binary forms, with or without
8797a0b63SMatthew Dillon * modification, are permitted provided that the following conditions
9797a0b63SMatthew Dillon * are met:
10797a0b63SMatthew Dillon *
11797a0b63SMatthew Dillon * 1. Redistributions of source code must retain the above copyright
12797a0b63SMatthew Dillon * notice, this list of conditions and the following disclaimer.
13797a0b63SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
14797a0b63SMatthew Dillon * notice, this list of conditions and the following disclaimer in
15797a0b63SMatthew Dillon * the documentation and/or other materials provided with the
16797a0b63SMatthew Dillon * distribution.
17797a0b63SMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its
18797a0b63SMatthew Dillon * contributors may be used to endorse or promote products derived
19797a0b63SMatthew Dillon * from this software without specific, prior written permission.
20797a0b63SMatthew Dillon *
21797a0b63SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22797a0b63SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23797a0b63SMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24797a0b63SMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25797a0b63SMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26797a0b63SMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27797a0b63SMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28797a0b63SMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29797a0b63SMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30797a0b63SMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31797a0b63SMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32797a0b63SMatthew Dillon * SUCH DAMAGE.
33797a0b63SMatthew Dillon */
34797a0b63SMatthew Dillon
35797a0b63SMatthew Dillon #include "hammer.h"
36797a0b63SMatthew Dillon
37797a0b63SMatthew Dillon static void rebalance_usage(int exit_code);
38797a0b63SMatthew Dillon
39797a0b63SMatthew Dillon /*
403550e0f5SThomas Nikolajsen * rebalance <filesystem> [saturation_percentage] (default 85%)
41797a0b63SMatthew Dillon */
42797a0b63SMatthew Dillon void
hammer_cmd_rebalance(char ** av,int ac)43797a0b63SMatthew Dillon hammer_cmd_rebalance(char **av, int ac)
44797a0b63SMatthew Dillon {
45797a0b63SMatthew Dillon struct hammer_ioc_rebalance rebal;
46797a0b63SMatthew Dillon const char *filesystem;
47797a0b63SMatthew Dillon int fd;
48797a0b63SMatthew Dillon int perc;
49797a0b63SMatthew Dillon
50797a0b63SMatthew Dillon if (TimeoutOpt > 0)
51797a0b63SMatthew Dillon alarm(TimeoutOpt);
52797a0b63SMatthew Dillon
53797a0b63SMatthew Dillon bzero(&rebal, sizeof(rebal));
54797a0b63SMatthew Dillon
55797a0b63SMatthew Dillon rebal.key_beg.localization = HAMMER_MIN_LOCALIZATION;
56797a0b63SMatthew Dillon rebal.key_beg.obj_id = HAMMER_MIN_OBJID;
57797a0b63SMatthew Dillon hammer_get_cycle(&rebal.key_beg, NULL);
58797a0b63SMatthew Dillon
59797a0b63SMatthew Dillon rebal.key_end.localization = HAMMER_MAX_LOCALIZATION;
60797a0b63SMatthew Dillon rebal.key_end.obj_id = HAMMER_MAX_OBJID;
615e1e1454STomohiro Kusumi rebal.allpfs = AllPFS;
62797a0b63SMatthew Dillon
63*88cdee70STomohiro Kusumi if (ac == 0) {
64797a0b63SMatthew Dillon rebalance_usage(1);
65*88cdee70STomohiro Kusumi /* not reached */
66*88cdee70STomohiro Kusumi }
67797a0b63SMatthew Dillon filesystem = av[0];
68797a0b63SMatthew Dillon if (ac == 1) {
69953bfaa2SAntonio Huete Jimenez perc = 85;
70797a0b63SMatthew Dillon } else {
71797a0b63SMatthew Dillon perc = strtol(av[1], NULL, 0);
72*88cdee70STomohiro Kusumi if (perc < 50 || perc > 100) {
73797a0b63SMatthew Dillon rebalance_usage(1);
74*88cdee70STomohiro Kusumi /* not reached */
75*88cdee70STomohiro Kusumi }
76797a0b63SMatthew Dillon }
77953bfaa2SAntonio Huete Jimenez rebal.saturation = HAMMER_BTREE_INT_ELMS * perc / 100;
78953bfaa2SAntonio Huete Jimenez
79a276dc6bSMatthew Dillon printf("rebalance start %016jx:%04x\n",
80a276dc6bSMatthew Dillon (uintmax_t)rebal.key_beg.obj_id,
81797a0b63SMatthew Dillon rebal.key_beg.localization);
82797a0b63SMatthew Dillon
83797a0b63SMatthew Dillon fd = open(filesystem, O_RDONLY);
84052fd72bSTomohiro Kusumi if (fd < 0) {
85797a0b63SMatthew Dillon err(1, "Unable to open %s", filesystem);
86052fd72bSTomohiro Kusumi /* not reached */
87052fd72bSTomohiro Kusumi }
88797a0b63SMatthew Dillon RunningIoctl = 1;
89797a0b63SMatthew Dillon if (ioctl(fd, HAMMERIOC_REBALANCE, &rebal) < 0) {
90797a0b63SMatthew Dillon printf("Rebalance %s failed: %s\n",
91797a0b63SMatthew Dillon filesystem, strerror(errno));
92797a0b63SMatthew Dillon } else if (rebal.head.flags & HAMMER_IOC_HEAD_INTR) {
93a276dc6bSMatthew Dillon printf("Rebalance %s interrupted by timer at %016jx:%04x\n",
94797a0b63SMatthew Dillon filesystem,
95a276dc6bSMatthew Dillon (uintmax_t)rebal.key_cur.obj_id,
96797a0b63SMatthew Dillon rebal.key_cur.localization);
9752e2f1b5STomohiro Kusumi if (CyclePath)
98797a0b63SMatthew Dillon hammer_set_cycle(&rebal.key_cur, 0);
99797a0b63SMatthew Dillon } else {
100797a0b63SMatthew Dillon if (CyclePath)
101797a0b63SMatthew Dillon hammer_reset_cycle();
102797a0b63SMatthew Dillon printf("Rebalance %s succeeded\n", filesystem);
103797a0b63SMatthew Dillon }
104797a0b63SMatthew Dillon RunningIoctl = 0;
105797a0b63SMatthew Dillon close(fd);
106797a0b63SMatthew Dillon printf("Rebalance:\n"
10778c4be83STomohiro Kusumi " %jd B-Tree nodes scanned\n"
10878c4be83STomohiro Kusumi " %jd B-Tree nodes deleted\n"
109a276dc6bSMatthew Dillon " %jd collision retries\n"
11078c4be83STomohiro Kusumi " %jd B-Tree nodes rebalanced\n",
111a276dc6bSMatthew Dillon (intmax_t)rebal.stat_ncount,
112a276dc6bSMatthew Dillon (intmax_t)rebal.stat_deletions,
113a276dc6bSMatthew Dillon (intmax_t)rebal.stat_collisions,
114a276dc6bSMatthew Dillon (intmax_t)rebal.stat_nrebal
115797a0b63SMatthew Dillon );
116797a0b63SMatthew Dillon }
117797a0b63SMatthew Dillon
118797a0b63SMatthew Dillon static
119797a0b63SMatthew Dillon void
rebalance_usage(int exit_code)120797a0b63SMatthew Dillon rebalance_usage(int exit_code)
121797a0b63SMatthew Dillon {
122797a0b63SMatthew Dillon fprintf(stderr,
123f6532f03SThomas Nikolajsen "hammer rebalance <filesystem> [saturation_percentage]\n"
1243550e0f5SThomas Nikolajsen "saturation_percentage is 50%%-100%%, default is 85%%.\n");
125797a0b63SMatthew Dillon exit(exit_code);
126797a0b63SMatthew Dillon }
127