Include a new heap summary line in the SIGQUIT output.
Looks like this:
Heap: 87% free, 4MB/32MB; 6327 objects
While I'm here, fix another long-standing TODO to make PrettySize have the
usual google3 behavior. (I took the specific thresholds from Chromium.)
Also distinguish between the more general "Dump" member functions and the
specific SIGQUIT-related ones by consistently calling the latter DumpForSigQuit.
Change-Id: I76e783adc18dd089bac9b348f53dc9860a0fe4b9
diff --git a/src/utils.cc b/src/utils.cc
index 70d15bf..ea50073 100644
--- a/src/utils.cc
+++ b/src/utils.cc
@@ -345,16 +345,26 @@
return result;
}
-std::string PrettySize(size_t size_in_bytes) {
- if ((size_in_bytes / GB) * GB == size_in_bytes) {
- return StringPrintf("%zdGB", size_in_bytes / GB);
- } else if ((size_in_bytes / MB) * MB == size_in_bytes) {
- return StringPrintf("%zdMB", size_in_bytes / MB);
- } else if ((size_in_bytes / KB) * KB == size_in_bytes) {
- return StringPrintf("%zdKB", size_in_bytes / KB);
- } else {
- return StringPrintf("%zdB", size_in_bytes);
+std::string PrettySize(size_t byte_count) {
+ // The byte thresholds at which we display amounts. A byte count is displayed
+ // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1].
+ static const size_t kUnitThresholds[] = {
+ 0, // B up to...
+ 3*1024, // KB up to...
+ 2*1024*1024, // MB up to...
+ 1024*1024*1024 // GB from here.
+ };
+ static const size_t kBytesPerUnit[] = { 1, KB, MB, GB };
+ static const char* const kUnitStrings[] = { "B", "KB", "MB", "GB" };
+
+ int i = arraysize(kUnitThresholds);
+ while (--i > 0) {
+ if (byte_count >= kUnitThresholds[i]) {
+ break;
+ }
}
+
+ return StringPrintf("%zd%s", byte_count / kBytesPerUnit[i], kUnitStrings[i]);
}
std::string PrettyDuration(uint64_t nano_duration) {