Module Name: src Committed By: pooka Date: Fri Oct 2 15:48:42 UTC 2009
Modified Files: src/sys/conf: files src/sys/kern: kern_subr.c Added Files: src/sys/kern: subr_humanize.c Log Message: Give humanize_number & format_bytes their own spots in the sun and move from kern_subr to subr_humanize. To generate a diff of this commit: cvs rdiff -u -r1.958 -r1.959 src/sys/conf/files cvs rdiff -u -r1.200 -r1.201 src/sys/kern/kern_subr.c cvs rdiff -u -r0 -r1.1 src/sys/kern/subr_humanize.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/conf/files diff -u src/sys/conf/files:1.958 src/sys/conf/files:1.959 --- src/sys/conf/files:1.958 Wed Sep 30 20:44:49 2009 +++ src/sys/conf/files Fri Oct 2 15:48:41 2009 @@ -1,4 +1,4 @@ -# $NetBSD: files,v 1.958 2009/09/30 20:44:49 jmcneill Exp $ +# $NetBSD: files,v 1.959 2009/10/02 15:48:41 pooka Exp $ # @(#)files.newconf 7.5 (Berkeley) 5/10/93 version 20090313 @@ -1483,6 +1483,7 @@ file kern/subr_exec_fd.c file kern/subr_extent.c file kern/subr_hash.c +file kern/subr_humanize.c file kern/subr_kmem.c file kern/subr_kobj.c file kern/subr_lockdebug.c Index: src/sys/kern/kern_subr.c diff -u src/sys/kern/kern_subr.c:1.200 src/sys/kern/kern_subr.c:1.201 --- src/sys/kern/kern_subr.c:1.200 Fri Sep 25 19:21:09 2009 +++ src/sys/kern/kern_subr.c Fri Oct 2 15:48:41 2009 @@ -1,4 +1,4 @@ -/* $NetBSD: kern_subr.c,v 1.200 2009/09/25 19:21:09 dyoung Exp $ */ +/* $NetBSD: kern_subr.c,v 1.201 2009/10/02 15:48:41 pooka Exp $ */ /*- * Copyright (c) 1997, 1998, 1999, 2002, 2007, 2008 The NetBSD Foundation, Inc. @@ -79,7 +79,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: kern_subr.c,v 1.200 2009/09/25 19:21:09 dyoung Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_subr.c,v 1.201 2009/10/02 15:48:41 pooka Exp $"); #include "opt_ddb.h" #include "opt_md.h" @@ -1220,78 +1220,6 @@ } /* - * snprintf() `bytes' into `buf', reformatting it so that the number, - * plus a possible `x' + suffix extension) fits into len bytes (including - * the terminating NUL). - * Returns the number of bytes stored in buf, or -1 if there was a problem. - * E.g, given a len of 9 and a suffix of `B': - * bytes result - * ----- ------ - * 99999 `99999 B' - * 100000 `97 kB' - * 66715648 `65152 kB' - * 252215296 `240 MB' - */ -int -humanize_number(char *buf, size_t len, uint64_t bytes, const char *suffix, - int divisor) -{ - /* prefixes are: (none), kilo, Mega, Giga, Tera, Peta, Exa */ - const char *prefixes; - int r; - uint64_t umax; - size_t i, suffixlen; - - if (buf == NULL || suffix == NULL) - return (-1); - if (len > 0) - buf[0] = '\0'; - suffixlen = strlen(suffix); - /* check if enough room for `x y' + suffix + `\0' */ - if (len < 4 + suffixlen) - return (-1); - - if (divisor == 1024) { - /* - * binary multiplies - * XXX IEC 60027-2 recommends Ki, Mi, Gi... - */ - prefixes = " KMGTPE"; - } else - prefixes = " kMGTPE"; /* SI for decimal multiplies */ - - umax = 1; - for (i = 0; i < len - suffixlen - 3; i++) { - umax *= 10; - if (umax > bytes) - break; - } - for (i = 0; bytes >= umax && prefixes[i + 1]; i++) - bytes /= divisor; - - r = snprintf(buf, len, "%qu%s%c%s", (unsigned long long)bytes, - i == 0 ? "" : " ", prefixes[i], suffix); - - return (r); -} - -int -format_bytes(char *buf, size_t len, uint64_t bytes) -{ - int rv; - size_t nlen; - - rv = humanize_number(buf, len, bytes, "B", 1024); - if (rv != -1) { - /* nuke the trailing ` B' if it exists */ - nlen = strlen(buf) - 2; - if (strcmp(&buf[nlen], " B") == 0) - buf[nlen] = '\0'; - } - return (rv); -} - -/* * Return true if system call tracing is enabled for the specified process. */ bool Added files: Index: src/sys/kern/subr_humanize.c diff -u /dev/null src/sys/kern/subr_humanize.c:1.1 --- /dev/null Fri Oct 2 15:48:42 2009 +++ src/sys/kern/subr_humanize.c Fri Oct 2 15:48:41 2009 @@ -0,0 +1,108 @@ +/* $NetBSD: subr_humanize.c,v 1.1 2009/10/02 15:48:41 pooka Exp $ */ + +/*- + * Copyright (c) 1997, 1998, 1999, 2002, 2007, 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Luke Mewburn. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__KERNEL_RCSID(0, "$NetBSD: subr_humanize.c,v 1.1 2009/10/02 15:48:41 pooka Exp $"); + +#include <sys/types.h> +#include <sys/systm.h> + +/* + * snprintf() `bytes' into `buf', reformatting it so that the number, + * plus a possible `x' + suffix extension) fits into len bytes (including + * the terminating NUL). + * Returns the number of bytes stored in buf, or -1 if there was a problem. + * E.g, given a len of 9 and a suffix of `B': + * bytes result + * ----- ------ + * 99999 `99999 B' + * 100000 `97 kB' + * 66715648 `65152 kB' + * 252215296 `240 MB' + */ +int +humanize_number(char *buf, size_t len, uint64_t bytes, const char *suffix, + int divisor) +{ + /* prefixes are: (none), kilo, Mega, Giga, Tera, Peta, Exa */ + const char *prefixes; + int r; + uint64_t umax; + size_t i, suffixlen; + + if (buf == NULL || suffix == NULL) + return (-1); + if (len > 0) + buf[0] = '\0'; + suffixlen = strlen(suffix); + /* check if enough room for `x y' + suffix + `\0' */ + if (len < 4 + suffixlen) + return (-1); + + if (divisor == 1024) { + /* + * binary multiplies + * XXX IEC 60027-2 recommends Ki, Mi, Gi... + */ + prefixes = " KMGTPE"; + } else + prefixes = " kMGTPE"; /* SI for decimal multiplies */ + + umax = 1; + for (i = 0; i < len - suffixlen - 3; i++) { + umax *= 10; + if (umax > bytes) + break; + } + for (i = 0; bytes >= umax && prefixes[i + 1]; i++) + bytes /= divisor; + + r = snprintf(buf, len, "%qu%s%c%s", (unsigned long long)bytes, + i == 0 ? "" : " ", prefixes[i], suffix); + + return (r); +} + +int +format_bytes(char *buf, size_t len, uint64_t bytes) +{ + int rv; + size_t nlen; + + rv = humanize_number(buf, len, bytes, "B", 1024); + if (rv != -1) { + /* nuke the trailing ` B' if it exists */ + nlen = strlen(buf) - 2; + if (strcmp(&buf[nlen], " B") == 0) + buf[nlen] = '\0'; + } + return (rv); +}