diff --git a/bin/pt-mysql-summary b/bin/pt-mysql-summary index eb506ff6..7356cd14 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; - } ' } diff --git a/bin/pt-summary b/bin/pt-summary index 63bac1ae..23152e0c 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" ' - { - num = $1; - - unit = num >= 1125899906842624 ? "P" \ - : num >= 1099511627776 ? "T" \ - : num >= 1073741824 ? "G" \ - : num >= 1048576 ? "M" \ - : num >= 1024 ? "k" \ - : ""; - - while ( num >= div ) { - num /= div; + { + 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; } - - printf "%.*f%s", prec, num, unit; - } ' } diff --git a/lib/bash/report_formatting.sh b/lib/bash/report_formatting.sh index 8542ece6..76386317 100644 --- a/lib/bash/report_formatting.sh +++ b/lib/bash/report_formatting.sh @@ -87,23 +87,29 @@ shorten() { 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" ' - { - num = $1; - - unit = num >= 1125899906842624 ? "P" \ - : num >= 1099511627776 ? "T" \ - : num >= 1073741824 ? "G" \ - : num >= 1048576 ? "M" \ - : num >= 1024 ? "k" \ - : ""; - - while ( num >= div ) { - num /= div; + { + 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; } - - printf "%.*f%s", prec, num, unit; - } ' } diff --git a/t/lib/bash/report_formatting.sh b/t/lib/bash/report_formatting.sh index 8974646d..373371a7 100644 --- a/t/lib/bash/report_formatting.sh +++ b/t/lib/bash/report_formatting.sh @@ -78,7 +78,7 @@ is \ is \ "$(section "A_B_C")" \ "# A#B#C#######################################################" \ - "replace everything after and including first underscore with #s" + "replace extra underscores with #s" # name_val