Re: How to find out how many other processes share VM with $PID?

Fengguang Wu
2007-08-28T00:10:21Z

On Mon, Aug 27, 2007 at 02:26:50PM +0100, Denys Vlasenko wrote:
> On Monday 27 August 2007 13:13, Fengguang Wu wrote:
> > Hi Denys,
> >
> > On Mon, Aug 27, 2007 at 12:56:31PM +0100, Denys Vlasenko wrote:
> > > Hi,
> > >
> > > I was a bit frustrated by bad quality of memory usage info
> > > from top and ps, and decided to write my own utility.
> > >
> > > One problem I don't know how to solve is how to avoid counting
> > > twice (or more) memory used by processes which share VM
> > > (by use of CLONE_VM flage to sys_clone).
> > >
> > > I know how to detect and correctly account for threads
> > > (processes created with CLONE_THREAD), but how to detect non-threads
> > > with shared VM?
> >
> > There is a nice LWN article on this issue:
> > ELC: How much memory are applications really using?
> > http://lwn.net/Articles/230975/
> >
> > Another helpful patch could be:
> > maps: PSS(proportional set size) accounting in smaps
> > http://lkml.org/lkml/2007/8/19/23
>
> Thanks a lot, very useful pages indeed.
>
> However they still don't explain how I can avoid counting memory
> twice for /proc/PID1 and /proc/PID2 when PID2 is a child of PID1,
> created with CLONE_VM.
>
> The example: I allocate 1234k, dirty it, then clone with CLONE_VM.
> I will seemingly have two processes, each using 1234k, _privately_
> (i.e., pages are not shown as shared in smaps) -
> which is technically correct, pages are not shared with other VMs,
> but they ARE shared by means of these two processes having the same VM!
>
> How userspace tools can figure out that these processes have shared VM?
>
> IOW: do we need "VMsharecount: N" in addition to "Threads: N"
> in /proc/PID/status?

A full solution would require two parameters, i.e. VmUsers/VmMagic.

But please make sure the new lines won't break important tools like
ps/top/pmaps/...

A quick test shows that only ps will parse /proc/<pid>/status:

strace -e open ps
strace -e open top
strace -e open pmap $$