From patchwork Sun Oct 17 18:26:44 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: hugo-lkml@carfax.org.uk X-Patchwork-Id: 260411 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 o9HIRP2R024374 for ; Sun, 17 Oct 2010 18:27:26 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932497Ab0JQS1V (ORCPT ); Sun, 17 Oct 2010 14:27:21 -0400 Received: from frost.carfax.org.uk ([212.13.194.111]:2218 "EHLO frost.carfax.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932381Ab0JQS1P (ORCPT ); Sun, 17 Oct 2010 14:27:15 -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 1P7Xww-0001o8-2F for linux-btrfs@vger.kernel.org; Sun, 17 Oct 2010 18:27:14 +0000 Received: from hrm by vlad.carfax.org.uk with local (Exim 4.69) (envelope-from ) id 1P7Xwv-0003DF-M3; Sun, 17 Oct 2010 19:27:13 +0100 Message-Id: <20101017182706.200881422@carfax.org.uk> User-Agent: quilt/0.48-1 Date: Sun, 17 Oct 2010 19:26:44 +0100 From: hugo-lkml@carfax.org.uk To: linux-btrfs@vger.kernel.org Cc: Hugo Mills Subject: [patch 1/4] Update pretty-printer for different multiple systems. References: <20101017182643.297900488@carfax.org.uk> 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). We default to binary sizes, maintaining the original behaviour, but print (e.g.) MiB to indicate that it's a power of 2^10. [...] 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]); Sun, 17 Oct 2010 18:27:26 +0000 (UTC) Index: btrfs-progs-unstable/btrfs-show.c =================================================================== --- btrfs-progs-unstable.orig/btrfs-show.c 2010-10-17 18:13:29.000000000 +0100 +++ btrfs-progs-unstable/btrfs-show.c 2010-10-17 19:00:34.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_BINARY); 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_BINARY); + bytes_used = pretty_sizes(device->bytes_used, PRETTY_SIZE_BINARY); 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-17 18:13:29.000000000 +0100 +++ btrfs-progs-unstable/btrfs_cmds.c 2010-10-17 19:00:39.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_BINARY); 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_BINARY); + bytes_used = pretty_sizes(device->bytes_used, PRETTY_SIZE_BINARY); 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_BINARY); + used_bytes = pretty_sizes(sargs->spaces[i].used_bytes, + PRETTY_SIZE_BINARY); 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-17 18:13:29.000000000 +0100 +++ btrfs-progs-unstable/mkfs.c 2010-10-17 18:15: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-17 18:13:29.000000000 +0100 +++ btrfs-progs-unstable/utils.c 2010-10-17 18:44:13.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-17 18:13:29.000000000 +0100 +++ btrfs-progs-unstable/utils.h 2010-10-17 18:15: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