Give humanize_number & format_bytes their own spots in the sun and move

from kern_subr to subr_humanize.
This commit is contained in:
pooka 2009-10-02 15:48:41 +00:00
parent 713aaa79d5
commit 68f37adaa6
3 changed files with 112 additions and 75 deletions

View File

@ -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_evcnt.c
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

View File

@ -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"
@ -1219,78 +1219,6 @@ parsedisk(char *str, int len, int defpart, dev_t *devp)
return (dv);
}
/*
* 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.
*/

108
sys/kern/subr_humanize.c Normal file
View File

@ -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);
}