1N/A/*
1N/A * tostr.h - DTrace To-String include file.
1N/A *
1N/A * $Id: tostr.h 36 2007-09-15 06:51:18Z brendan $
1N/A *
1N/A * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
1N/A *
1N/A * CDDL HEADER START
1N/A *
1N/A * The contents of this file are subject to the terms of the
1N/A * Common Development and Distribution License, Version 1.0 only
1N/A * (the "License"). You may not use this file except in compliance
1N/A * with the License.
1N/A *
1N/A * You can obtain a copy of the license at Docs/cddl1.txt
1N/A * or http://www.opensolaris.org/os/licensing.
1N/A * See the License for the specific language governing permissions
1N/A * and limitations under the License.
1N/A *
1N/A * CDDL HEADER END
1N/A *
1N/A * 16-Sep-2007 Brendan Gregg Created this.
1N/A */
1N/A
1N/A/*
1N/A * NUM_TO_STR(n) - takes a number and returns a string with a prefix,
1N/A * intended to fit withen 6 chars.
1N/A *
1N/A * Input Output
1N/A * 0 0
1N/A * 1 1
1N/A * 10 10
1N/A * 999 999
1N/A * 1000 1.0K
1N/A * 1100 1.1K
1N/A * 10000 10.0K
1N/A * 999999 999.0K
1N/A * 1000000 1.0M
1N/A * 10000000 10.0M
1N/A * 999999999 999.9M
1N/A */
1N/A#define NUM_TO_STR(n) \
1N/A n >= 1000000 ? \
1N/A strjoin(strjoin(strjoin(lltostr(n / 1000000), "."), \
1N/A lltostr((n % 1000000) / 100000)), "M") : n >= 1000 ? \
1N/A strjoin(strjoin(strjoin(lltostr(n / 1000), "."), \
1N/A lltostr((n % 1000) / 100)), "K") : lltostr(n)
1N/A
1N/A/*
1N/A * BYTES_TO_STR(n) - takes a byte count and returns a string with a prefix,
1N/A * intended to fit withen 6 chars.
1N/A *
1N/A * Input Output
1N/A * 0 0
1N/A * 1 1
1N/A * 10 10
1N/A * 999 0.9K
1N/A * 1000 0.9K
1N/A * 1024 1.0K
1N/A * 10240 10.0K
1N/A * 1000000 976.5K
1N/A * 1048576 1.0M
1N/A * 1000000000 953.6M
1N/A */
1N/A#define BYTES_TO_STR(n) \
1N/A n >= 1024000 ? \
1N/A strjoin(strjoin(strjoin(lltostr(n / 1048576), "."), \
1N/A lltostr((n % 1048576) / 104858)), "M") : n >= 1000 ? \
1N/A strjoin(strjoin(strjoin(lltostr(n / 1024), "."), \
1N/A lltostr((n % 1024) / 103)), "K") : lltostr(n)
1N/A
1N/A/*
1N/A * US_TO_STR(n) - takes microseconds and returns a string with a prefix,
1N/A * intended to fit withen 6 chars.
1N/A *
1N/A * Input Output
1N/A * 0 0
1N/A * 1 1u
1N/A * 10 10u
1N/A * 999 999u
1N/A * 1000 1.0m
1N/A * 1100 1.1m
1N/A * 10000 10.0m
1N/A * 999999 999.0m
1N/A */
1N/A#define US_TO_STR(n) \
1N/A n == 0 ? "0" : n >= 1000 ? \
1N/A strjoin(lltostr(n / 1000), "m") : strjoin(lltostr(n), "u")
1N/A