1 1.1 christos /* 2 1.1 christos * tostr.h - DTrace To-String include file. 3 1.1 christos * 4 1.1 christos * $Id: tostr.h,v 1.1.1.1 2015/09/30 22:01:09 christos Exp $ 5 1.1 christos * 6 1.1 christos * COPYRIGHT: Copyright (c) 2007 Brendan Gregg. 7 1.1 christos * 8 1.1 christos * CDDL HEADER START 9 1.1 christos * 10 1.1 christos * The contents of this file are subject to the terms of the 11 1.1 christos * Common Development and Distribution License, Version 1.0 only 12 1.1 christos * (the "License"). You may not use this file except in compliance 13 1.1 christos * with the License. 14 1.1 christos * 15 1.1 christos * You can obtain a copy of the license at Docs/cddl1.txt 16 1.1 christos * or http://www.opensolaris.org/os/licensing. 17 1.1 christos * See the License for the specific language governing permissions 18 1.1 christos * and limitations under the License. 19 1.1 christos * 20 1.1 christos * CDDL HEADER END 21 1.1 christos * 22 1.1 christos * 16-Sep-2007 Brendan Gregg Created this. 23 1.1 christos */ 24 1.1 christos 25 1.1 christos /* 26 1.1 christos * NUM_TO_STR(n) - takes a number and returns a string with a prefix, 27 1.1 christos * intended to fit withen 6 chars. 28 1.1 christos * 29 1.1 christos * Input Output 30 1.1 christos * 0 0 31 1.1 christos * 1 1 32 1.1 christos * 10 10 33 1.1 christos * 999 999 34 1.1 christos * 1000 1.0K 35 1.1 christos * 1100 1.1K 36 1.1 christos * 10000 10.0K 37 1.1 christos * 999999 999.0K 38 1.1 christos * 1000000 1.0M 39 1.1 christos * 10000000 10.0M 40 1.1 christos * 999999999 999.9M 41 1.1 christos */ 42 1.1 christos #define NUM_TO_STR(n) \ 43 1.1 christos n >= 1000000 ? \ 44 1.1 christos strjoin(strjoin(strjoin(lltostr(n / 1000000), "."), \ 45 1.1 christos lltostr((n % 1000000) / 100000)), "M") : n >= 1000 ? \ 46 1.1 christos strjoin(strjoin(strjoin(lltostr(n / 1000), "."), \ 47 1.1 christos lltostr((n % 1000) / 100)), "K") : lltostr(n) 48 1.1 christos 49 1.1 christos /* 50 1.1 christos * BYTES_TO_STR(n) - takes a byte count and returns a string with a prefix, 51 1.1 christos * intended to fit withen 6 chars. 52 1.1 christos * 53 1.1 christos * Input Output 54 1.1 christos * 0 0 55 1.1 christos * 1 1 56 1.1 christos * 10 10 57 1.1 christos * 999 0.9K 58 1.1 christos * 1000 0.9K 59 1.1 christos * 1024 1.0K 60 1.1 christos * 10240 10.0K 61 1.1 christos * 1000000 976.5K 62 1.1 christos * 1048576 1.0M 63 1.1 christos * 1000000000 953.6M 64 1.1 christos */ 65 1.1 christos #define BYTES_TO_STR(n) \ 66 1.1 christos n >= 1024000 ? \ 67 1.1 christos strjoin(strjoin(strjoin(lltostr(n / 1048576), "."), \ 68 1.1 christos lltostr((n % 1048576) / 104858)), "M") : n >= 1000 ? \ 69 1.1 christos strjoin(strjoin(strjoin(lltostr(n / 1024), "."), \ 70 1.1 christos lltostr((n % 1024) / 103)), "K") : lltostr(n) 71 1.1 christos 72 1.1 christos /* 73 1.1 christos * US_TO_STR(n) - takes microseconds and returns a string with a prefix, 74 1.1 christos * intended to fit withen 6 chars. 75 1.1 christos * 76 1.1 christos * Input Output 77 1.1 christos * 0 0 78 1.1 christos * 1 1u 79 1.1 christos * 10 10u 80 1.1 christos * 999 999u 81 1.1 christos * 1000 1.0m 82 1.1 christos * 1100 1.1m 83 1.1 christos * 10000 10.0m 84 1.1 christos * 999999 999.0m 85 1.1 christos */ 86 1.1 christos #define US_TO_STR(n) \ 87 1.1 christos n == 0 ? "0" : n >= 1000 ? \ 88 1.1 christos strjoin(lltostr(n / 1000), "m") : strjoin(lltostr(n), "u") 89 1.1 christos 90