From: Jim Warner <james.warner@comcast.net>
Date: Wed, 13 Apr 2016 05:00:00 +0000 (-0500)
Subject: top: exploit new linux-4.5 resident memory enhancement
X-Git-Tag: v3.3.12~38
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6306050a658b06bec15fa8e2a121183b1c18f85f;p=procps-ng

top: exploit new linux-4.5 resident memory enhancement

Since support already exists in the newlib branch this
represents an equivalent master branch implementation,
and this commit message is shared with 2 more patches.

Beginning with linux-4.5, the following new fields are
being added under that /proc/<pid>/status pseudo file:
 . RssAnon - size of resident anonymous memory
 . RssFile - size of resident file mappings
 . RssShmem - size of resident shared memory

p.s. Locked resident memory support was also added but
isn't directly related to the kernel 4.5 enhancements.

p.p.s. Archlinux, Debian-stretch and Fedora-23 already
are currently using a 4.5 linux kernel (as of 6/2/16).

Signed-off-by: Jim Warner <james.warner@comcast.net>
---

diff --git a/top/top.c b/top/top.c
index e355747f..09e2f0ad 100644
--- a/top/top.c
+++ b/top/top.c
@@ -284,6 +284,10 @@ SCB_NUMx(PID, tid)
 SCB_NUMx(PPD, ppid)
 SCB_NUMx(PRI, priority)
 SCB_NUM1(RES, vm_rss)                  // also serves MEM !
+SCB_NUM1(RZA, vm_rss_anon)
+SCB_NUM1(RZF, vm_rss_file)
+SCB_NUM1(RZL, vm_lock)
+SCB_NUM1(RZS, vm_rss_shared)
 SCB_STRX(SGD, supgid)
 SCB_STRS(SGN, supgrp)
 SCB_NUM1(SHR, share)
@@ -1776,7 +1780,18 @@ static FLD_t Fieldstab[] = {
    {    10,     -1,  A_right,  SF(NS4),  L_NS      }, // PIDNS
    {    10,     -1,  A_right,  SF(NS5),  L_NS      }, // USERNS
    {    10,     -1,  A_right,  SF(NS6),  L_NS      }, // UTSNS
-   {     8,     -1,  A_left,   SF(LXC),  L_LXC     }
+   {     8,     -1,  A_left,   SF(LXC),  L_LXC     },
+#ifndef NOBOOST_MEMS
+   {     6,  SK_Kb,  A_right,  SF(RZA),  L_status  },
+   {     6,  SK_Kb,  A_right,  SF(RZF),  L_status  },
+   {     6,  SK_Kb,  A_right,  SF(RZL),  L_status  },
+   {     6,  SK_Kb,  A_right,  SF(RZS),  L_status  }
+#else
+   {     4,  SK_Kb,  A_right,  SF(RZA),  L_status  },
+   {     4,  SK_Kb,  A_right,  SF(RZF),  L_status  },
+   {     4,  SK_Kb,  A_right,  SF(RZL),  L_status  },
+   {     4,  SK_Kb,  A_right,  SF(RZS),  L_status  }
+#endif
  #undef SF
  #undef A_left
  #undef A_right
@@ -2316,7 +2331,9 @@ static void zap_fieldstab (void) {
    Fieldstab[EU_VRT].scale = Fieldstab[EU_SWP].scale
       = Fieldstab[EU_RES].scale = Fieldstab[EU_COD].scale
       = Fieldstab[EU_DAT].scale = Fieldstab[EU_SHR].scale
-      = Fieldstab[EU_USE].scale = Rc.task_mscale;
+      = Fieldstab[EU_USE].scale = Fieldstab[EU_RZA].scale
+      = Fieldstab[EU_RZF].scale = Fieldstab[EU_RZL].scale
+      = Fieldstab[EU_RZS].scale = Rc.task_mscale;
 
    // lastly, ensure we've got proper column headers...
    calibrate_fields();
@@ -5426,6 +5443,18 @@ static const char *task_show (const WIN_t *q, const proc_t *p) {
          case EU_RES:
             cp = scale_mem(S, p->vm_rss, W, Jn);
             break;
+         case EU_RZA:
+            cp = scale_mem(S, p->vm_rss_anon, W, Jn);
+            break;
+         case EU_RZF:
+            cp = scale_mem(S, p->vm_rss_file, W, Jn);
+            break;
+         case EU_RZL:
+            cp = scale_mem(S, p->vm_lock, W, Jn);
+            break;
+         case EU_RZS:
+            cp = scale_mem(S, p->vm_rss_shared, W, Jn);
+            break;
          case EU_SGD:
             makeVAR(p->supgid);
             break;
diff --git a/top/top.h b/top/top.h
index 789b9b30..ec3438eb 100644
--- a/top/top.h
+++ b/top/top.h
@@ -206,6 +206,7 @@ enum pflag {
    EU_USE,
    EU_NS1, EU_NS2, EU_NS3, EU_NS4, EU_NS5, EU_NS6,
    EU_LXC,
+   EU_RZA, EU_RZF, EU_RZL, EU_RZS,
 #ifdef USE_X_COLHDR
    // not really pflags, used with tbl indexing
    EU_MAXPFLGS
diff --git a/top/top_nls.c b/top/top_nls.c
index 93e5619c..1ac239fa 100644
--- a/top/top_nls.c
+++ b/top/top_nls.c
@@ -280,6 +280,18 @@ static void build_two_nlstabs (void) {
 /* Translation Hint: maximum 'LXC' = 7 */
    Head_nlstab[EU_LXC] = _("LXC");
    Desc_nlstab[EU_LXC] = _("LXC container name");
+/* Translation Hint: maximum 'RSan' = 4 */
+   Head_nlstab[EU_RZA] = _("RSan");
+   Desc_nlstab[EU_RZA] = _("RES Anonymous (KiB)");
+/* Translation Hint: maximum 'RSfd' = 4 */
+   Head_nlstab[EU_RZF] = _("RSfd");
+   Desc_nlstab[EU_RZF] = _("RES File-based (KiB)");
+/* Translation Hint: maximum 'RSlk' = 4 */
+   Head_nlstab[EU_RZL] = _("RSlk");
+   Desc_nlstab[EU_RZL] = _("RES Locked (KiB)");
+/* Translation Hint: maximum 'RSsh' = 4 */
+   Head_nlstab[EU_RZS] = _("RSsh");
+   Desc_nlstab[EU_RZS] = _("RES Shared (KiB)");
 }