From owner-freebsd-hackers@freebsd.org Wed Jul 6 22:13:07 2016 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 6CA42B75D3C for ; Wed, 6 Jul 2016 22:13:07 +0000 (UTC) (envelope-from yuri@rawbw.com) Received: from shell1.rawbw.com (shell1.rawbw.com [198.144.192.42]) by mx1.freebsd.org (Postfix) with ESMTP id 5E655160A for ; Wed, 6 Jul 2016 22:13:07 +0000 (UTC) (envelope-from yuri@rawbw.com) Received: from yuri.doctorlan.com (c-24-5-143-190.hsd1.ca.comcast.net [24.5.143.190]) (authenticated bits=0) by shell1.rawbw.com (8.15.1/8.15.1) with ESMTPSA id u66MD0oo015679 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NO) for ; Wed, 6 Jul 2016 15:13:01 -0700 (PDT) (envelope-from yuri@rawbw.com) X-Authentication-Warning: shell1.rawbw.com: Host c-24-5-143-190.hsd1.ca.comcast.net [24.5.143.190] claimed to be yuri.doctorlan.com From: Yuri Subject: Why kinfo_getvmmap is sometimes so expensive? To: Freebsd hackers list Message-ID: Date: Wed, 6 Jul 2016 15:12:59 -0700 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:45.0) Gecko/20100101 Thunderbird/45.1.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 06 Jul 2016 22:13:07 -0000 The function getProcessSizeBytes, calculating the total size of the process, runs once per second. I have two processes of the same kind, but with the different run history. Process #1 didn't do much work, its total size is 1.5 GB, google perftools library says that it currently has 1.2GB allocated. Process #2 did a lot of work, its total size is 6.9 GB, but most of the used memory was freed, and google perftools library also says that it currently has only 1.2GB allocated. Both processes have about 140 lines in /proc//map. What bothers me is that getProcessSizeBytes run once per second makes process #1 to consume ~0.5% CPU, and process #2 to consume ~14% CPU. When I stop running getProcessSizeBytes, CPU times of both processes go to zero. Obviously, google perftools doesn't unmap the memory, and the totals of block sizes in /proc//map is much higher for process #2 with about the same block count. But why does this cause 14% of CPU consumption? And why another, similar process that goes through about the same number of blocks only has 0.5% CPU consumption? uint64_t getProcessSizeBytes() { int i, cnt = 0; struct kinfo_vmentry *kvm0, *kvm; m_uint64_t memSz = 0; kvm0 = ::kinfo_getvmmap(::getpid(), &cnt); for (i = 0, kvm = kvm0; ikve_end-kvm->kve_start); free(kvm0); return (memSz); } FreeBSD 10.3 Yuri