From patchwork Thu Oct 21 19:01:36 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugo Mills X-Patchwork-Id: 271981 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 o9LJBXIU029982 for ; Thu, 21 Oct 2010 19:11:34 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757847Ab0JUTL2 (ORCPT ); Thu, 21 Oct 2010 15:11:28 -0400 Received: from frost.carfax.org.uk ([212.13.194.111]:1518 "EHLO frost.carfax.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754772Ab0JUTLX (ORCPT ); Thu, 21 Oct 2010 15:11:23 -0400 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 ) id 1P90Xp-0004IN-Kl for linux-btrfs@vger.kernel.org; Thu, 21 Oct 2010 19:11:21 +0000 Received: from hrm by vlad.carfax.org.uk with local (Exim 4.69) (envelope-from ) id 1P90Xp-0002jq-Dd for linux-btrfs@vger.kernel.org; Thu, 21 Oct 2010 20:11:21 +0100 Message-Id: <20101021190202.711591083@carfax.org.uk> References: <20101021190135.757086134@carfax.org.uk> User-Agent: quilt/0.46-1 Date: Thu, 21 Oct 2010 20:01:36 +0100 From: Hugo Mills To: linux-btrfs@vger.kernel.org Cc: Hugo Mills Subject: [patch v2 1/4] Update pretty-printer for different systems of counting multiples. Content-Disposition: inline; filename=raw-numbers X-frost.carfax.org.uk-Spam-Score: -0.0 (/) X-frost.carfax.org.uk-Spam-Report: Spam detection software, running on the system "spamd1.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: Make the pretty-printer for data sizes capable of printing in ISO (powers of 10^3), binary (powers of 2^10) or raw (a simple byte count). Signed-off-by: Hugo Mills --- btrfs-show.c | 7 ++++--- btrfs_cmds.c | 13 ++++++++----- mkfs.c | 3 ++- utils.c | 48 +++++++++++++++++++++++++++++++++ utils.h | 7 ++++++- 5 files changed, 53 insertions(+), 25 deletions(-) [...] 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 X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter1.kernel.org [140.211.167.41]); Thu, 21 Oct 2010 19:11:34 +0000 (UTC) Index: btrfs-progs-unstable/btrfs-show.c =================================================================== --- btrfs-progs-unstable.orig/btrfs-show.c 2010-10-09 15:39:09.000000000 +0100 +++ btrfs-progs-unstable/btrfs-show.c 2010-10-20 19:20:02.000000000 +0100 @@ -69,7 +69,8 @@ else printf("Label: none "); - super_bytes_used = pretty_sizes(device->super_bytes_used); + super_bytes_used = pretty_sizes(device->super_bytes_used, + PRETTY_SIZE_RAW); total = device->total_devs; printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf, @@ -81,8 +82,8 @@ char *total_bytes; char *bytes_used; device = list_entry(cur, struct btrfs_device, dev_list); - total_bytes = pretty_sizes(device->total_bytes); - bytes_used = pretty_sizes(device->bytes_used); + total_bytes = pretty_sizes(device->total_bytes, PRETTY_SIZE_RAW); + bytes_used = pretty_sizes(device->bytes_used, PRETTY_SIZE_RAW); printf("\tdevid %4llu size %s used %s path %s\n", (unsigned long long)device->devid, total_bytes, bytes_used, device->name); Index: btrfs-progs-unstable/btrfs_cmds.c =================================================================== --- btrfs-progs-unstable.orig/btrfs_cmds.c 2010-10-09 15:39:09.000000000 +0100 +++ btrfs-progs-unstable/btrfs_cmds.c 2010-10-20 19:19:20.000000000 +0100 @@ -634,7 +634,8 @@ else printf("Label: none "); - super_bytes_used = pretty_sizes(device->super_bytes_used); + super_bytes_used = pretty_sizes(device->super_bytes_used, + PRETTY_SIZE_RAW); total = device->total_devs; printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf, @@ -646,8 +647,8 @@ char *total_bytes; char *bytes_used; device = list_entry(cur, struct btrfs_device, dev_list); - total_bytes = pretty_sizes(device->total_bytes); - bytes_used = pretty_sizes(device->bytes_used); + total_bytes = pretty_sizes(device->total_bytes, PRETTY_SIZE_RAW); + bytes_used = pretty_sizes(device->bytes_used, PRETTY_SIZE_RAW); printf("\tdevid %4llu size %s used %s path %s\n", (unsigned long long)device->devid, total_bytes, bytes_used, device->name); @@ -913,8 +914,10 @@ written += 8; } - total_bytes = pretty_sizes(sargs->spaces[i].total_bytes); - used_bytes = pretty_sizes(sargs->spaces[i].used_bytes); + total_bytes = pretty_sizes(sargs->spaces[i].total_bytes, + PRETTY_SIZE_RAW); + used_bytes = pretty_sizes(sargs->spaces[i].used_bytes, + PRETTY_SIZE_RAW); printf("%s: total=%s, used=%s\n", description, total_bytes, used_bytes); } Index: btrfs-progs-unstable/mkfs.c =================================================================== --- btrfs-progs-unstable.orig/mkfs.c 2010-10-09 15:39:09.000000000 +0100 +++ btrfs-progs-unstable/mkfs.c 2010-10-17 19:35:08.000000000 +0100 @@ -524,7 +524,8 @@ printf("fs created label %s on %s\n\tnodesize %u leafsize %u " "sectorsize %u size %s\n", label, first_file, nodesize, leafsize, sectorsize, - pretty_sizes(btrfs_super_total_bytes(&root->fs_info->super_copy))); + pretty_sizes(btrfs_super_total_bytes(&root->fs_info->super_copy), + PRETTY_SIZE_BINARY)); printf("%s\n", BTRFS_BUILD_VERSION); btrfs_commit_transaction(trans, root); Index: btrfs-progs-unstable/utils.c =================================================================== --- btrfs-progs-unstable.orig/utils.c 2010-10-09 15:39:09.000000000 +0100 +++ btrfs-progs-unstable/utils.c 2010-10-17 19:35:08.000000000 +0100 @@ -966,30 +966,48 @@ return ret; } -static char *size_strs[] = { "", "KB", "MB", "GB", "TB", +static char *bin_size_strs[] = { "", "KiB", "MiB", "GiB", "TiB", + "PiB", "EiB", "ZiB", "YiB"}; +static char *iso_size_strs[] = { "", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; -char *pretty_sizes(u64 size) +char *pretty_sizes(u64 size, int format) { int num_divs = 0; u64 last_size = size; u64 fract_size = size; float fraction; char *pretty; + int divisor = 1024; + char** size_strs = bin_size_strs; - while(size > 0) { - fract_size = last_size; - last_size = size; - size /= 1024; - num_divs++; + if(format == PRETTY_SIZE_RAW) { + pretty = malloc(21); + sprintf(pretty, "%llu", size); + } else { + if(format == PRETTY_SIZE_ISO) { + divisor = 1000; + size_strs = iso_size_strs; + } else if(format == PRETTY_SIZE_BINARY) { + divisor = 1024; + size_strs = bin_size_strs; + } + + while(size > 0) { + fract_size = last_size; + last_size = size; + size /= divisor; + num_divs++; + } + if (num_divs == 0) + num_divs = 1; + if (num_divs > ARRAY_SIZE(bin_size_strs)) + return NULL; + + fraction = (float)fract_size / divisor; + pretty = malloc(16); + sprintf(pretty, "%.2f%s", fraction, size_strs[num_divs-1]); } - if (num_divs == 0) - num_divs = 1; - if (num_divs > ARRAY_SIZE(size_strs)) - return NULL; - - fraction = (float)fract_size / 1024; - pretty = malloc(16); - sprintf(pretty, "%.2f%s", fraction, size_strs[num_divs-1]); + return pretty; } Index: btrfs-progs-unstable/utils.h =================================================================== --- btrfs-progs-unstable.orig/utils.h 2010-10-09 15:39:09.000000000 +0100 +++ btrfs-progs-unstable/utils.h 2010-10-17 19:35:08.000000000 +0100 @@ -21,6 +21,11 @@ #define BTRFS_MKFS_SYSTEM_GROUP_SIZE (4 * 1024 * 1024) +/* Constants for pretty_size() format parameter */ +#define PRETTY_SIZE_RAW 0 +#define PRETTY_SIZE_ISO 1 +#define PRETTY_SIZE_BINARY 2 + int make_btrfs(int fd, const char *device, const char *label, u64 blocks[6], u64 num_bytes, u32 nodesize, u32 leafsize, u32 sectorsize, u32 stripesize); @@ -39,5 +44,5 @@ int check_mounted(const char *devicename); int btrfs_device_already_in_root(struct btrfs_root *root, int fd, int super_offset); -char *pretty_sizes(u64 size); +char *pretty_sizes(u64 size, int format); #endif