From nobody@FreeBSD.org  Wed Apr 23 09:30:40 2014
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1])
	(using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by hub.freebsd.org (Postfix) with ESMTPS id 7A30BEB4
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 23 Apr 2014 09:30:40 +0000 (UTC)
Received: from cgiserv.freebsd.org (cgiserv.freebsd.org [IPv6:2001:1900:2254:206a::50:4])
	(using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
	(Client did not present a certificate)
	by mx1.freebsd.org (Postfix) with ESMTPS id 66F0818AE
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 23 Apr 2014 09:30:40 +0000 (UTC)
Received: from cgiserv.freebsd.org ([127.0.1.6])
	by cgiserv.freebsd.org (8.14.8/8.14.8) with ESMTP id s3N9UeQg091369
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 23 Apr 2014 09:30:40 GMT
	(envelope-from nobody@cgiserv.freebsd.org)
Received: (from nobody@localhost)
	by cgiserv.freebsd.org (8.14.8/8.14.8/Submit) id s3N9UWV2088429;
	Wed, 23 Apr 2014 09:30:32 GMT
	(envelope-from nobody)
Message-Id: <201404230930.s3N9UWV2088429@cgiserv.freebsd.org>
Date: Wed, 23 Apr 2014 09:30:32 GMT
From: "Ivan A. Kosarev" <ikosarev@accesssoftek.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: sysctl(KERN_PROC_VMMAP) takes too long
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         188911
>Category:       kern
>Synopsis:       [libc] sysctl(KERN_PROC_VMMAP) takes too long
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Apr 23 09:40:00 UTC 2014
>Closed-Date:    
>Last-Modified:  Thu May 15 13:20:00 UTC 2014
>Originator:     Ivan A. Kosarev
>Release:        10.0
>Organization:
Access Softek, Inc
>Environment:
FreeBSD fbsd10.unicals.com 10.0-RELEASE FreeBSD 10.0-RELEASE #0 r260789: Thu Jan 16 22:34:59 UTC 2014     root@snap.freebsd.org:/usr/obj/usr/src/sys/GENERIC  amd64
>Description:
With a mmap() call with a large 'size' parameter a subsequent
sysctl(KERN_PROC_VMMAP) call takes too long to perform.
>How-To-Repeat:

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

#include <unistd.h>
#include <dlfcn.h>
#include <fcntl.h>

#include <sys/sysctl.h>
#include <sys/user.h>
#include <sys/mman.h>


int main(void)
{
    int mib[4];
    size_t size;
    int err;
    void *p;

    printf("#1\n");

    p = mmap((void*) 0x3ffffffff000, 0x80000001000,
        PROT_READ | PROT_WRITE,
        MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
        -1, 0);
    assert(p != MAP_FAILED);

    printf("#2\n");

    mib[0] = CTL_KERN;
    mib[1] = KERN_PROC;
    mib[2] = KERN_PROC_VMMAP;
    mib[3] = getpid();

    size = 0;
    err = sysctl(mib, 4, NULL, &size, NULL, 0);  /* takes about 40 seconds */
    assert(err == 0);

    printf("#3\n");

    return EXIT_SUCCESS;
}
>Fix:

According to kib@ this is because we compute rss accurately which means we have to visit every page
in range. I dont think you need rss. To confirm this theory, can you try this patch?

Index: sys/kern/kern_proc.c
===================================================================
--- sys/kern/kern_proc.c        (revision 265931)
+++ sys/kern/kern_proc.c        (working copy)
@@ -2182,7 +2182,7 @@ kern_proc_vmmap_out(struct proc *p, struct sbuf *s
                }
                kve->kve_resident = 0;
                addr = entry->start;
-               while (addr < entry->end) {
+               while (0 && addr < entry->end) {
                        locked_pa = 0;
                        mincoreinfo = pmap_mincore(map->pmap, addr, &locked_pa);
                        if (locked_pa != 0)

This should disable the rss computation.


>Release-Note:
>Audit-Trail:

From: Ed Maste <emaste@freebsd.org>
To: bug-followup@FreeBSD.org, ikosarev@accesssoftek.com, 
	Viktor Kutuzov <vkutuzov@accesssoftek.com>
Cc:  
Subject: Re: kern/188911: [libc] sysctl(KERN_PROC_VMMAP) takes too long
Date: Mon, 12 May 2014 22:33:41 -0400

 Yes, this is the cause of the slowdown; it takes a long time to
 iterate over 8TB 4K at a time. I confirmed by commenting out the loop,
 as in the proof of concept patch.

From: "Ivan A. Kosarev" <ik@unicals.com>
To: bug-followup@FreeBSD.org, ikosarev@accesssoftek.com
Cc: Ed Maste <emaste@freebsd.org>, 
 Viktor Kutuzov <vkutuzov@accesssoftek.com>
Subject: Re: kern/188911: [libc] sysctl(KERN_PROC_VMMAP) takes too long
Date: Thu, 15 May 2014 17:10:46 +0400

 Yes, for our purposes we don't need to care about resident pages so if 
 we could somehow avoid executing the loop, that would solve the problem. 
 Thanks.
>Unformatted:
