Subject: bin/23717: ps(1) report negative memory usage on host with memory >2GB
To: None <gnats-bugs@gnats.NetBSD.org>
From: None <njoly@pasteur.fr>
List: netbsd-bugs
Date: 12/11/2003 12:56:19
>Number:         23717
>Category:       bin
>Synopsis:       ps(1) report negative memory usage on host with memory >2GB
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Dec 11 11:57:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     Nicolas Joly
>Release:        NetBSD 1.6ZG (-current 20031209)
>Organization:
	Institut Pasteur
>Environment:
System: NetBSD calimero.sis.pasteur.fr 1.6ZG NetBSD 1.6ZG (CALIMERO) #3: Tue Dec 9 10:43:23 CET 2003 njoly@calimero.sis.pasteur.fr:/local/src/NetBSD/obj/amd64/sys/arch/amd64/compile/CALIMERO amd64
Architecture: x86_64
Machine: amd64
>Description:
On hosts with more than 2GB of memory, ps(1) will report negative
percentage of real memory used :

njoly@calimero [~]> ps ux
USER    PID %CPU %MEM VSZ  RSS TT STAT STARTED    TIME COMMAND
njoly   599  0.0 -1.1 548  704 ?? S     9:19AM 0:01.25 sshd: njoly@ttyp0 
njoly 19115  0.0 -1.1 548  704 ?? S    12:14PM 0:00.37 sshd: njoly@ttyp1 
njoly 24773  0.0 -3.2 548 2104 ?? S     8:04AM 0:01.73 sshd: njoly@ttyp2 
njoly   595  0.0 -3.0 292 1968 p0 Ss    9:19AM 0:01.46 -zsh 
njoly 20793  0.0 -1.1  76  728 p0 R+    9:13AM 0:00.00 ps ux 
njoly 21239  0.0 -2.8 228 1856 p1 Ss+  12:14PM 0:00.08 -zsh 
njoly 20624  0.0 -2.6 836 1748 p2 S+    9:07AM 0:00.08 vi /tmp/p23544 
njoly 23257  0.0 -3.2 284 2128 p2 Ss    8:04AM 0:00.02 -zsh 
njoly 23544  0.0 -1.4 204  952 p2 S+    9:07AM 0:00.00 /bin/sh /usr/bin/send-pr

ps(1), to calculate the memory precentage, needs the amount of physical
memory and queries it with sysctl(3) + HW_PHYSMEM. But on hosts with large
amount of memory (>2GB), this value becomes negative (integer overflow),
leading to invalid memory usage values.

njoly@calimero [~]> sysctl -a | grep physmem
hw.physmem = -67571712
hw.physmem64 = 4227395584

>How-To-Repeat:
use `ps ux' command on hosts with more than 2GB memory.
>Fix:
I made this quick patch locally to restore the expected behaviour.
It use HW_PHYSMEM64 instead of HW_PHYSMEM.

Index: bin/ps/nlist.c
===================================================================
RCS file: /cvsroot/src/bin/ps/nlist.c,v
retrieving revision 1.20
diff -u -r1.20 nlist.c
--- bin/ps/nlist.c      2003/08/07 09:05:24     1.20
+++ bin/ps/nlist.c      2003/12/11 10:06:50
@@ -153,13 +153,14 @@
        int mib[2];
        size_t size;
        fixpt_t xccpu;
+       u_quad_t q;
 
        nlistread = 1;
        mib[0] = CTL_HW;
-       mib[1] = HW_PHYSMEM;
-       size = sizeof(mempages);
-       if (sysctl(mib, 2, &mempages, &size, NULL, 0) == 0)
-               mempages /= getpagesize();
+       mib[1] = HW_PHYSMEM64;
+       size = sizeof(q);
+       if (sysctl(mib, 2, &q, &size, NULL, 0) == 0)
+               mempages = q / getpagesize();
        else
                mempages = 0;

 
>Release-Note:
>Audit-Trail:
>Unformatted: