libmeminfo: Add THP field to procmem output am: f6ad0be442

Original change: https://android-review.googlesource.com/c/platform/system/memory/libmeminfo/+/1677671

Change-Id: Idb187922d3f47394e17b023b223eddf808450f5f
diff --git a/include/meminfo/meminfo.h b/include/meminfo/meminfo.h
index 28051d7..545f27d 100644
--- a/include/meminfo/meminfo.h
+++ b/include/meminfo/meminfo.h
@@ -46,6 +46,8 @@
     uint64_t shared_hugetlb;
     uint64_t private_hugetlb;
 
+    uint64_t thp;
+
     MemUsage()
         : vss(0),
           rss(0),
@@ -61,7 +63,8 @@
           shmem_pmd_mapped(0),
           file_pmd_mapped(0),
           shared_hugetlb(0),
-          private_hugetlb(0) {}
+          private_hugetlb(0),
+          thp(0) {}
 
     ~MemUsage() = default;
 
diff --git a/meminfo_private.h b/meminfo_private.h
index df5699c..e087fb8 100644
--- a/meminfo_private.h
+++ b/meminfo_private.h
@@ -24,11 +24,15 @@
 #include <meminfo/procmeminfo.h>
 #include <meminfo/sysmeminfo.h>
 
-// Macros to do per-page flag manipulation
 #define _BITS(x, offset, bits) (((x) >> (offset)) & ((1LL << (bits)) - 1))
+
+// Macros to do per-page pagemap data manipulation
 #define PAGE_PRESENT(x) (_BITS(x, 63, 1))
 #define PAGE_SWAPPED(x) (_BITS(x, 62, 1))
 #define PAGE_SHIFT(x) (_BITS(x, 55, 6))
 #define PAGE_PFN(x) (_BITS(x, 0, 55))
 #define PAGE_SWAP_OFFSET(x) (_BITS(x, 5, 50))
 #define PAGE_SWAP_TYPE(x) (_BITS(x, 0, 5))
+
+// Macros to do per-page kpageflags data manipulation
+#define KPAGEFLAG_THP(x) (_BITS(x, 22, 1))
diff --git a/procmeminfo.cpp b/procmeminfo.cpp
index c0d4362..af64f86 100644
--- a/procmeminfo.cpp
+++ b/procmeminfo.cpp
@@ -434,6 +434,10 @@
             return false;
         }
 
+        if (KPAGEFLAG_THP(cur_page_flags)) {
+            vma.usage.thp += pagesz;
+        }
+
         // skip unwanted pages from the count
         if ((cur_page_flags & pgflags_mask_) != pgflags_) continue;
 
diff --git a/tools/procmem.cpp b/tools/procmem.cpp
index b245f2a..1fe8d50 100644
--- a/tools/procmem.cpp
+++ b/tools/procmem.cpp
@@ -60,25 +60,25 @@
 
 static void print_separator(std::stringstream& ss) {
     if (show_wss) {
-        ss << ::android::base::StringPrintf("%7s  %7s  %7s  %7s  %7s  %7s  %7s  %7s  %s\n",
+        ss << ::android::base::StringPrintf("%7s  %7s  %7s  %7s  %7s  %7s  %7s  %7s  %7s  %s\n",
                                             "-------", "-------", "-------", "-------", "-------",
-                                            "-------", "-------", "-------", "");
+                                            "-------", "-------", "-------", "-------", "");
         return;
     }
-    ss << ::android::base::StringPrintf("%7s  %7s  %7s  %7s  %7s  %7s  %7s  %7s  %7s  %s\n",
+    ss << ::android::base::StringPrintf("%7s  %7s  %7s  %7s  %7s  %7s  %7s  %7s  %7s  %7s  %s\n",
                                         "-------", "-------", "-------", "-------", "-------",
-                                        "-------", "-------", "-------", "-------", "");
+                                        "-------", "-------", "-------", "-------", "-------", "");
 }
 
 static void print_header(std::stringstream& ss) {
     if (show_wss) {
-        ss << ::android::base::StringPrintf("%7s  %7s  %7s  %7s  %7s  %7s  %7s  %7s  %s\n", "WRss",
-                                            "WPss", "WUss", "WShCl", "WShDi", "WPrCl", "WPrDi",
-                                            "Flags", "Name");
-    } else {
         ss << ::android::base::StringPrintf("%7s  %7s  %7s  %7s  %7s  %7s  %7s  %7s  %7s  %s\n",
-                                            "Vss", "Rss", "Pss", "Uss", "ShCl", "ShDi", "PrCl",
-                                            "PrDi", "Flags", "Name");
+                                            "WRss", "WPss", "WUss", "WShCl", "WShDi", "WPrCl",
+                                            "WPrDi", "THP", "Flags", "Name");
+    } else {
+        ss << ::android::base::StringPrintf(
+                "%7s  %7s  %7s  %7s  %7s  %7s  %7s  %7s  %7s  %7s  %s\n", "Vss", "Rss", "Pss",
+                "Uss", "ShCl", "ShDi", "PrCl", "PrDi", "THP", "Flags", "Name");
     }
     print_separator(ss);
 }
@@ -88,11 +88,12 @@
         ss << ::android::base::StringPrintf("%6" PRIu64 "K  ", stats.vss / 1024);
     }
 
-    ss << ::android::base::StringPrintf("%6" PRIu64 "K  %6" PRIu64 "K  %6" PRIu64 "K  %6" PRIu64
-                                        "K  %6" PRIu64 "K  %6" PRIu64 "K  %6" PRIu64 "K  ",
-                                        stats.rss / 1024, stats.pss / 1024, stats.uss / 1024,
-                                        stats.shared_clean / 1024, stats.shared_dirty / 1024,
-                                        stats.private_clean / 1024, stats.private_dirty / 1024);
+    ss << ::android::base::StringPrintf(
+            "%6" PRIu64 "K  %6" PRIu64 "K  %6" PRIu64 "K  %6" PRIu64 "K  %6" PRIu64 "K  %6" PRIu64
+            "K  %6" PRIu64 "K  %6" PRIu64 "K ",
+            stats.rss / 1024, stats.pss / 1024, stats.uss / 1024, stats.shared_clean / 1024,
+            stats.shared_dirty / 1024, stats.private_clean / 1024, stats.private_dirty / 1024,
+            stats.thp / 1024);
 }
 
 static int show(const MemUsage& proc_stats, const std::vector<Vma>& maps) {