diff --git a/bin/pt-mysql-summary b/bin/pt-mysql-summary index eb506ff6..b0b2db9a 100755 --- a/bin/pt-mysql-summary +++ b/bin/pt-mysql-summary @@ -576,22 +576,19 @@ shorten() { local div="${3:-1024}" echo "$num" | awk -v prec="$prec" -v div="$div" ' - { - size = 4; - val = $1; - - unit = val >= 1099511627776 ? "T" : val >= 1073741824 ? "G" : val >= 1048576 ? "M" : val >= 1024 ? "k" : ""; - - while ( int(val) && !(val % 1024) ) { - val /= 1024; + { + num = $1; + unit = num >= 1125899906842624 ? "P" \ + : num >= 1099511627776 ? "T" \ + : num >= 1073741824 ? "G" \ + : num >= 1048576 ? "M" \ + : num >= 1024 ? "k" \ + : ""; + while ( num >= div ) { + num /= div; + } + printf "%.*f%s", prec, num, unit; } - - while ( val > 1000 ) { - val /= div; - } - - printf "%.*f%s", prec, val, unit; - } ' } @@ -2854,7 +2851,7 @@ Replace C with the name of any tool. =head1 AUTHORS -Baron Schwartz, Brian Fraser, and Daniel Nichter. +Baron Schwartz, Brian Fraser, and Daniel Nichter =head1 ABOUT PERCONA TOOLKIT diff --git a/bin/pt-summary b/bin/pt-summary index c46f57a3..09415584 100755 --- a/bin/pt-summary +++ b/bin/pt-summary @@ -713,22 +713,19 @@ shorten() { local div="${3:-1024}" echo "$num" | awk -v prec="$prec" -v div="$div" ' - { - size = 4; - val = $1; - - unit = val >= 1099511627776 ? "T" : val >= 1073741824 ? "G" : val >= 1048576 ? "M" : val >= 1024 ? "k" : ""; - - while ( int(val) && !(val % 1024) ) { - val /= 1024; + { + num = $1; + unit = num >= 1125899906842624 ? "P" \ + : num >= 1099511627776 ? "T" \ + : num >= 1073741824 ? "G" \ + : num >= 1048576 ? "M" \ + : num >= 1024 ? "k" \ + : ""; + while ( num >= div ) { + num /= div; + } + printf "%.*f%s", prec, num, unit; } - - while ( val > 1000 ) { - val /= div; - } - - printf "%.*f%s", prec, val, unit; - } ' } @@ -2527,7 +2524,7 @@ Replace C with the name of any tool. =head1 AUTHORS -Baron Schwartz and Kevin van Zonneveld (http://kevin.vanzonneveld.net) +Baron Schwartz, Kevin van Zonneveld, and Brian Fraser =head1 ABOUT PERCONA TOOLKIT diff --git a/lib/bash/report_formatting.sh b/lib/bash/report_formatting.sh index 0cbf7b9f..76386317 100644 --- a/lib/bash/report_formatting.sh +++ b/lib/bash/report_formatting.sh @@ -82,29 +82,34 @@ name_val () { # Sub: shorten # Shorten a value in bytes to another representation. -# shorten() { local num="$1" local prec="${2:-2}" local div="${3:-1024}" + # By default Mebibytes (MiB), Gigibytes (GiB), etc. are used because + # that's what MySQL uses. This may create odd output for values like + # 1500M * 2 (bug 937793) because the base unit is MiB but the code + # see 1,572,864,000 * 2 = 3,145,728,000 which is > 1 GiB so it uses + # GiB as the unit, resulting in 2.9G instead of 3.0G that the user + # might expect to see. There's no easy way to determine that + # 3,145,728,000 was actually a multiple of MiB and not some weird GiB + # value to begin with like 3.145G. The Perl lib Transformers::shorten() + # uses MiB, GiB, etc. too. echo "$num" | awk -v prec="$prec" -v div="$div" ' - { - size = 4; - val = $1; - - unit = val >= 1099511627776 ? "T" : val >= 1073741824 ? "G" : val >= 1048576 ? "M" : val >= 1024 ? "k" : ""; - - while ( int(val) && !(val % 1024) ) { - val /= 1024; + { + num = $1; + unit = num >= 1125899906842624 ? "P" \ + : num >= 1099511627776 ? "T" \ + : num >= 1073741824 ? "G" \ + : num >= 1048576 ? "M" \ + : num >= 1024 ? "k" \ + : ""; + while ( num >= div ) { + num /= div; + } + printf "%.*f%s", prec, num, unit; } - - while ( val > 1000 ) { - val /= div; - } - - printf "%.*f%s", prec, val, unit; - } ' } diff --git a/t/lib/bash/report_formatting.sh b/t/lib/bash/report_formatting.sh index 0d594958..373371a7 100644 --- a/t/lib/bash/report_formatting.sh +++ b/t/lib/bash/report_formatting.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -plan 19 +plan 20 . "$LIB_DIR/report_formatting.sh" @@ -12,12 +12,12 @@ is \ is \ "$(shorten 3145728000 1)" \ "2.9G" \ - "10485760, 1 precision, default divisor => 2.9G" + "3145728000, 1 precision, default divisor => 2.9G" is \ "$(shorten 3145728000 1 1000)" \ - "3.0G" \ - "Opt-in to the 2.1 behavior works" + "3.1G" \ + "3145728000, 1 precision, divisor 1000 => 3.1G" is \ "$(shorten 0 0)" \ @@ -31,8 +31,8 @@ is \ is \ "$(shorten 1572864000 1 1000)" \ - "1.5G" \ - "1572864000, 1 precision, divisor 1000 => 1.5G" + "1.6G" \ + "1572864000, 1 precision, divisor 1000 => 1.6G" is \ "$(shorten 364 0)" \ @@ -59,6 +59,10 @@ is \ "6.5T" \ "6492100000006, 1 precision, divisor 1000 => 6.5T" +is "$(shorten 1059586048 1)" \ + "1010.5M" \ + "1059586048 => 1010.5M (bug 993436)" + # section is \ @@ -74,7 +78,7 @@ is \ is \ "$(section "A_B_C")" \ "# A#B#C#######################################################" \ - "..but it does replace everything after and including the first underscore with #s" + "replace extra underscores with #s" # name_val diff --git a/t/lib/bash/report_system_info.sh b/t/lib/bash/report_system_info.sh index f58139e2..a20068a2 100644 --- a/t/lib/bash/report_system_info.sh +++ b/t/lib/bash/report_system_info.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -plan 48 +plan 49 . "$LIB_DIR/alt_cmds.sh" . "$LIB_DIR/log_warn_die.sh" @@ -827,6 +827,18 @@ EOF parse_free_minus_b "$TMPDIR/in" > "$TMPDIR/got" no_diff "$TMPDIR/got" "$TMPDIR/expected" "parse_free_minus_b" +# Bug 993436: Memory: Total reports M when it should say G +cat < "$TMPDIR/expected" + Total | 1010.5M + Free | 784.4M + Used | physical = 226.1M, swap allocated = 2.0G, swap used = 0.0, virtual = 226.1M + Buffers | 48.8M + Caches | 122.2M + Dirty | 152 kB +EOF +parse_free_minus_b "$T_DIR/pt-summary/samples/Linux/002/memory" > "$TMPDIR/got" +no_diff "$TMPDIR/got" "$TMPDIR/expected" "parse_free_minus_b (bug 993436)" + # parse_filesystems cat < $TMPDIR/expected diff --git a/t/pt-summary/samples/Linux/output_002.txt b/t/pt-summary/samples/Linux/output_002.txt index 9faee741..55c34910 100644 --- a/t/pt-summary/samples/Linux/output_002.txt +++ b/t/pt-summary/samples/Linux/output_002.txt @@ -14,7 +14,7 @@ Architecture | CPU = 32-bit, OS = 32-bit Models | 1xQEMU Virtual CPU version 0.14.1 Caches | 1x4096 KB # Memory ##################################################### - Total | 1.0M + Total | 1010.5M Free | 784.4M Used | physical = 226.1M, swap allocated = 2.0G, swap used = 0.0, virtual = 226.1M Buffers | 48.8M diff --git a/t/pt-summary/samples/Linux/output_003.txt b/t/pt-summary/samples/Linux/output_003.txt index be42106b..9c927b67 100644 --- a/t/pt-summary/samples/Linux/output_003.txt +++ b/t/pt-summary/samples/Linux/output_003.txt @@ -14,7 +14,7 @@ Architecture | CPU = 32-bit, OS = 32-bit Models | 1xQEMU Virtual CPU version 0.14.1 Caches | 1x4096 KB # Memory ##################################################### - Total | 1.0M + Total | 1010.5M Free | 784.7M Used | physical = 225.8M, swap allocated = 2.0G, swap used = 0.0, virtual = 225.8M Buffers | 48.8M diff --git a/util/test-bash-functions b/util/test-bash-functions index bd155777..b689f9f0 100755 --- a/util/test-bash-functions +++ b/util/test-bash-functions @@ -32,6 +32,7 @@ cd "$cwd" BIN_DIR="$BRANCH/bin"; LIB_DIR="$BRANCH/lib/bash"; +T_DIR="$BRANCH/t"; T_LIB_DIR="$BRANCH/t/lib"; SANDBOX_VERSION="$($BRANCH/sandbox/test-env version)"