Subject: Re: bug#72278: 29.4; Proced reports incorrect results with 16KB page size

From: Paul Eggert <[email-masked]>
Date: Wed, 24 Jul 2024 23:38:34 -0700

On 2024-07-24 22:06, Eli Zaretskii wrote:
> Paul, it sounds like we need to multiply by getpagesize() instead of
> by a fixed value of 4?

Yes, that sounds right. I installed the attached patch into Emacs master 
on Savannah, and am boldly closing the bug as fixed. We can reopen it if 
I'm wrong.

Thanks, Rahguzar, for reporting the bug.

From 42cdfe7f60ef7ae3bccb5b1e43349c3a6b54ac4a Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Wed, 24 Jul 2024 23:32:24 -0700
Subject: [PATCH] Fix process-attributes rss and pmem on GNU/Linux
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Problem reported by Rahguzar <https://bugs.gnu.org/72278>.
* src/sysdep.c (system_process_attributes):
[GNU_LINUX || CYGWIN || __ANDROID__]: When computing rss and pmem,
don’t assume pagesize is 4096; it could be greater.
---
 src/sysdep.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/sysdep.c b/src/sysdep.c
index d916a695155..3955d796ca5 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -3548,6 +3548,7 @@ procfs_ttyname (int rdev)
 }
 # endif	/* GNU_LINUX || __ANDROID__ */
 
+/* Total usable RAM in KiB.  */
 static uintmax_t
 procfs_get_total_memory (void)
 {
@@ -3737,8 +3738,13 @@ system_process_attributes (Lisp_Object pid)
 	  attrs = Fcons (Fcons (Qnice, make_fixnum (niceness)), attrs);
 	  attrs = Fcons (Fcons (Qthcount, INT_TO_INTEGER (thcount)), attrs);
 	  attrs = Fcons (Fcons (Qvsize, INT_TO_INTEGER (vsize / 1024)), attrs);
-	  attrs = Fcons (Fcons (Qrss, INT_TO_INTEGER (4 * rss)), attrs);
-	  pmem = 4.0 * 100 * rss / procfs_get_total_memory ();
+
+	  /* RSS in KiB.  */
+	  uintmax_t rssk = rss;
+	  rssk *= getpagesize () >> 10;
+
+	  attrs = Fcons (Fcons (Qrss, INT_TO_INTEGER (rssk)), attrs);
+	  pmem = 100.0 * rssk / procfs_get_total_memory ();
 	  if (pmem > 100)
 	    pmem = 100;
 	  attrs = Fcons (Fcons (Qpmem, make_float (pmem)), attrs);
-- 
2.43.0