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

From: Stefan Kangas <[email-masked]>
Date: Thu, 25 Jul 2024 02:55:32 -0700

Paul Eggert <[email-masked]> writes:

> 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 ();

Thanks, that looks like an improvement.

On Linux, page sizes can vary per process ("multiple page size
support").  Should we bother with that?

I also note that the man page for getpagesize says this:

    This call first appeared in 4.2BSD.  SVr4, 4.4BSD, SUSv2.  In
    SUSv2 the getpagesize() call was labeled LEGACY, and it was
    removed in POSIX.1-2001.

>  	  if (pmem > 100)
>  	    pmem = 100;
>  	  attrs = Fcons (Fcons (Qpmem, make_float (pmem)), attrs);
> --
> 2.43.0