From patchwork Mon Nov 8 20:05:25 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugo Mills X-Patchwork-Id: 318522 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id oAC1I1aS023460 for ; Fri, 12 Nov 2010 01:18:03 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757363Ab0KLBR5 (ORCPT ); Thu, 11 Nov 2010 20:17:57 -0500 Received: from frost.carfax.org.uk ([212.13.194.111]:4310 "EHLO frost.carfax.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757356Ab0KLBRz (ORCPT ); Thu, 11 Nov 2010 20:17:55 -0500 X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter1.kernel.org [140.211.167.41]); Fri, 12 Nov 2010 01:18:03 +0000 (UTC) X-Greylist: delayed 1905 seconds by postgrey-1.27 at vger.kernel.org; Thu, 11 Nov 2010 20:17:54 EST Received: from intmx.carfax.org.uk ([10.0.0.5] helo=vlad.carfax.org.uk ident=Debian-exim) by frost.carfax.org.uk with esmtp (Exim 4.69) (envelope-from <6e6e59176fd2704de00d10e4f968e406c0a83bd3@carfax.org.uk>) id 1PGhmK-0006ti-0T; Fri, 12 Nov 2010 00:46:08 +0000 Received: from hrm by vlad.carfax.org.uk with local (Exim 4.72) (envelope-from <6e6e59176fd2704de00d10e4f968e406c0a83bd3@vlad.carfax.org.uk>) id 1PGhmI-0001yk-Ro; Fri, 12 Nov 2010 00:46:06 +0000 Message-Id: <6e6e59176fd2704de00d10e4f968e406c0a83bd3.1289521433.git.hugo@carfax.org.uk> In-Reply-To: References: From: Hugo Mills Date: Mon, 8 Nov 2010 20:05:25 +0000 Subject: [PATCH v2 2/3] Add --monitor option to btrfs balance progress. To: linux-btrfs@vger.kernel.org, Goffredo Baroncelli , Chris Mason X-frost.carfax.org.uk-Spam-Score: -0.0 (/) X-frost.carfax.org.uk-Spam-Report: Spam detection software, running on the system "spamd2.lon.bitfolk.com", has identified this incoming email as possible spam. The original message has been attached to this so you can view it (if it isn't spam) or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: For the impatient, this patch introduces the pot-watching --monitor option, which checks the balance progress at regular intervals, and updates a single status line with the current progress and an estimated completion time. [...] Content analysis details: (-0.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 T_RP_MATCHES_RCVD Envelope sender domain matches handover relay domain Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org diff --git a/btrfs_cmds.c b/btrfs_cmds.c index 2745d64..c681b5a 100644 --- a/btrfs_cmds.c +++ b/btrfs_cmds.c @@ -797,22 +797,108 @@ int get_balance_progress(char *path, struct btrfs_ioctl_balance_progress *bal) return err; } +const struct option progress_options[] = { + { "monitor", 0, NULL, 'm' }, + { NULL, 0, NULL, 0 } +}; + int do_balance_progress(int argc, char **argv) { char *path; int ret = 0; int err = 0; struct btrfs_ioctl_balance_progress bal; + __u64 last_completed = -1; + __u64 initial_completed = -1; + struct timeval now; + struct timeval started; + int monitor = 0; + + optind = 1; + while(1) { + int c = getopt_long(argc, argv, "m", progress_options, NULL); + if (c < 0) + break; + switch(c) { + case 'm': + monitor = 1; + break; + default: + fprintf(stderr, "Invalid arguments for balance progress\n"); + free(argv); + return 1; + } + } + + if(optind >= argc) { + fprintf(stderr, "No filesystem path given for progress\n"); + return 1; + } - path = argv[1]; + path = argv[optind]; + do { + int prs = 0; - ret = get_balance_progress(path, &bal); - if (!ret) - printf("\r%llu/%llu block groups moved, " - "%0.2f%% complete.\n", - bal.completed, - bal.expected, - (float)bal.completed/bal.expected*100.0); + ret = get_balance_progress(path, &bal); + if (ret) + break; + + if (last_completed != bal.completed) { + printf("\r%llu/%llu block groups moved, " + "%0.2f%% complete.", + bal.completed, + bal.expected, + (float)bal.completed/bal.expected*100.0); + } + + if (initial_completed != -1 + && initial_completed != bal.completed) { + ret = gettimeofday(&now, NULL); + if (ret) { + fprintf(stderr, "Can't read current time\n"); + return 22; + } + /* Seconds per block */ + float rate = (float)(now.tv_sec - started.tv_sec) + / (bal.completed - initial_completed); + int secs_remaining = rate + * (bal.expected - bal.completed); + printf(" Time remaining"); + if (secs_remaining >= 60*60*24) { + printf(" %dd", secs_remaining / (60*60*24)); + secs_remaining %= 60*60*24; + prs = 1; + } + if (prs || secs_remaining >= 60*60) { + printf(" %dh", secs_remaining / (60*60)); + secs_remaining %= 60*60; + prs = 1; + } + if (prs || secs_remaining > 60) { + printf(" %dm", secs_remaining / 60); + secs_remaining %= 60; + } + printf(" %ds\x1b[K", secs_remaining); + } + + if (last_completed != -1 && last_completed != bal.completed) { + initial_completed = bal.completed; + ret = gettimeofday(&started, NULL); + if (ret) { + fprintf(stderr, "Can't read current time\n"); + return 22; + } + } + + last_completed = bal.completed; + + if (monitor) { + fflush(stdout); + sleep(1); + } else { + printf("\n"); + } + } while(monitor); switch(ret) { case 0: diff --git a/man/btrfs.8.in b/man/btrfs.8.in index 69d8613..3f7642e 100644 --- a/man/btrfs.8.in +++ b/man/btrfs.8.in @@ -21,7 +21,7 @@ btrfs \- control a btrfs filesystem .PP \fBbtrfs\fP \fBfilesystem resize\fP\fI [+/\-][gkm]|max \fP .PP -\fBbtrfs\fP \fBbalance progress\fP \fI\fP +\fBbtrfs\fP \fBbalance progress\fP [\fB-m\fP|\fB--monitor\fP] \fI\fP .PP \fBbtrfs\fP \fBdevice scan\fP\fI [ [..]]\fP .PP @@ -150,7 +150,7 @@ Balance the chunks of the filesystem identified by \fI\fR across the devices. .TP -\fBbalance progress\fP \fI\fP +\fBbalance progress\fP [\fB-m\fP|\fB--monitor\fP] \fI\fP Report progress on the currently-running balance operation on \fI\fP. .TP