From owner-freebsd-hackers@FreeBSD.ORG Sat Feb 12 20:16:57 2005 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 50A9116A4CE for ; Sat, 12 Feb 2005 20:16:57 +0000 (GMT) Received: from dan.emsphone.com (dan.emsphone.com [199.67.51.101]) by mx1.FreeBSD.org (Postfix) with ESMTP id BAD6643D64 for ; Sat, 12 Feb 2005 20:16:56 +0000 (GMT) (envelope-from dan@dan.emsphone.com) Received: (from dan@localhost) by dan.emsphone.com (8.13.1/8.13.1) id j1CKGpqf054922; Sat, 12 Feb 2005 14:16:51 -0600 (CST) (envelope-from dan) Date: Sat, 12 Feb 2005 14:16:51 -0600 From: Dan Nelson To: Nimnod Message-ID: <20050212201651.GE49626@dan.emsphone.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-OS: FreeBSD 5.3-STABLE X-message-flag: Outlook Error User-Agent: Mutt/1.5.7i cc: freebsd-hackers@freebsd.org Subject: Re: how to calculate CPU utilization? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 12 Feb 2005 20:16:57 -0000 In the last episode (Feb 12), Nimnod said: > i am looking for how to accurately calculate the CPU utilization per > process. i have looked through the top sources, and found out it's > using the kvm lib, which isn't suitable for my purpose. Why not? You don't need to be setuid root to call kvm functions; when possible they fetch their info via sysctl. Neither top nor ps are setuid. > is there a way to accurately calculate %CPU based on information from > the struct proc ? it does carry info on proc time (p_runtime), > system, user and interrupt ticks, and previous system, user and int > times in usec, but i just can't make the right equation to get the > appropriate values. first, i attempted to divide the process runtime > by system uptime, which seemed ok, but obviously this is not the way. > could anyone please help? You need to take two samples separated by some delay (1 or 5 seconds maybe), then take their difference and divide by the delay. That will give you the cpu usage over that period. -- Dan Nelson dnelson@allantgroup.com