1235368Sgnn/* 2235368Sgnn * tostr.h - DTrace To-String include file. 3235368Sgnn * 4235368Sgnn * $Id: tostr.h 36 2007-09-15 06:51:18Z brendan $ 5235368Sgnn * 6235368Sgnn * COPYRIGHT: Copyright (c) 2007 Brendan Gregg. 7235368Sgnn * 8235368Sgnn * CDDL HEADER START 9235368Sgnn * 10235368Sgnn * The contents of this file are subject to the terms of the 11235368Sgnn * Common Development and Distribution License, Version 1.0 only 12235368Sgnn * (the "License"). You may not use this file except in compliance 13235368Sgnn * with the License. 14235368Sgnn * 15235368Sgnn * You can obtain a copy of the license at Docs/cddl1.txt 16235368Sgnn * or http://www.opensolaris.org/os/licensing. 17235368Sgnn * See the License for the specific language governing permissions 18235368Sgnn * and limitations under the License. 19235368Sgnn * 20235368Sgnn * CDDL HEADER END 21235368Sgnn * 22235368Sgnn * 16-Sep-2007 Brendan Gregg Created this. 23235368Sgnn */ 24235368Sgnn 25235368Sgnn/* 26235368Sgnn * NUM_TO_STR(n) - takes a number and returns a string with a prefix, 27235368Sgnn * intended to fit withen 6 chars. 28235368Sgnn * 29235368Sgnn * Input Output 30235368Sgnn * 0 0 31235368Sgnn * 1 1 32235368Sgnn * 10 10 33235368Sgnn * 999 999 34235368Sgnn * 1000 1.0K 35235368Sgnn * 1100 1.1K 36235368Sgnn * 10000 10.0K 37235368Sgnn * 999999 999.0K 38235368Sgnn * 1000000 1.0M 39235368Sgnn * 10000000 10.0M 40235368Sgnn * 999999999 999.9M 41235368Sgnn */ 42235368Sgnn#define NUM_TO_STR(n) \ 43235368Sgnn n >= 1000000 ? \ 44235368Sgnn strjoin(strjoin(strjoin(lltostr(n / 1000000), "."), \ 45235368Sgnn lltostr((n % 1000000) / 100000)), "M") : n >= 1000 ? \ 46235368Sgnn strjoin(strjoin(strjoin(lltostr(n / 1000), "."), \ 47235368Sgnn lltostr((n % 1000) / 100)), "K") : lltostr(n) 48235368Sgnn 49235368Sgnn/* 50235368Sgnn * BYTES_TO_STR(n) - takes a byte count and returns a string with a prefix, 51235368Sgnn * intended to fit withen 6 chars. 52235368Sgnn * 53235368Sgnn * Input Output 54235368Sgnn * 0 0 55235368Sgnn * 1 1 56235368Sgnn * 10 10 57235368Sgnn * 999 0.9K 58235368Sgnn * 1000 0.9K 59235368Sgnn * 1024 1.0K 60235368Sgnn * 10240 10.0K 61235368Sgnn * 1000000 976.5K 62235368Sgnn * 1048576 1.0M 63235368Sgnn * 1000000000 953.6M 64235368Sgnn */ 65235368Sgnn#define BYTES_TO_STR(n) \ 66235368Sgnn n >= 1024000 ? \ 67235368Sgnn strjoin(strjoin(strjoin(lltostr(n / 1048576), "."), \ 68235368Sgnn lltostr((n % 1048576) / 104858)), "M") : n >= 1000 ? \ 69235368Sgnn strjoin(strjoin(strjoin(lltostr(n / 1024), "."), \ 70235368Sgnn lltostr((n % 1024) / 103)), "K") : lltostr(n) 71235368Sgnn 72235368Sgnn/* 73235368Sgnn * US_TO_STR(n) - takes microseconds and returns a string with a prefix, 74235368Sgnn * intended to fit withen 6 chars. 75235368Sgnn * 76235368Sgnn * Input Output 77235368Sgnn * 0 0 78235368Sgnn * 1 1u 79235368Sgnn * 10 10u 80235368Sgnn * 999 999u 81235368Sgnn * 1000 1.0m 82235368Sgnn * 1100 1.1m 83235368Sgnn * 10000 10.0m 84235368Sgnn * 999999 999.0m 85235368Sgnn */ 86235368Sgnn#define US_TO_STR(n) \ 87235368Sgnn n == 0 ? "0" : n >= 1000 ? \ 88235368Sgnn strjoin(lltostr(n / 1000), "m") : strjoin(lltostr(n), "u") 89235368Sgnn 90