From owner-freebsd-hackers@FreeBSD.ORG Sat Aug 4 07:36:01 2007 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 4EF4F16A420 for ; Sat, 4 Aug 2007 07:36:01 +0000 (UTC) (envelope-from artifact.one@googlemail.com) Received: from wx-out-0506.google.com (wx-out-0506.google.com [66.249.82.239]) by mx1.freebsd.org (Postfix) with ESMTP id 2C78B13C4CC for ; Sat, 4 Aug 2007 07:36:01 +0000 (UTC) (envelope-from artifact.one@googlemail.com) Received: by wx-out-0506.google.com with SMTP id i29so764911wxd for ; Sat, 04 Aug 2007 00:36:00 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=googlemail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=YFa9mVwqI00+6pTil2n7HB3f4suE7WeR8p/aDuECFz5pTGi0rz+X9w7oAYEc9I5L9tzwyCg6oTAfWlVZnyl8fFqOr3bWorzznQzBFklBmtgTygNaQKf1eJvJJ2bMegush66tYBZuPlwGGw6t6LAxkdrb/zMe3fuAxVKumuI/fJI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=beta; h=received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=I4XXSzOm7kmHO1l8gETIAmPkNsQDaTYcGC8eveRlcqPuugq7d3yxJtU7EtC4g1/1RjJdUDjcDCDKwTfzDDPagWxhOU2QnJlbYMMn2TXmt7x75AWMa40gjEF7J94DtK/dfx+wdbsAZZ+chNzsjS7uQotwDPFLdB+Vo1mxPB+d82Y= Received: by 10.90.34.3 with SMTP id h3mr3732028agh.1186211413027; Sat, 04 Aug 2007 00:10:13 -0700 (PDT) Received: by 10.90.51.1 with HTTP; Sat, 4 Aug 2007 00:10:13 -0700 (PDT) Message-ID: <8e96a0b90708040010r3980d5ffj709e16058740955a@mail.gmail.com> Date: Sat, 4 Aug 2007 08:10:13 +0100 From: "mal content" To: freebsd-hackers@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Subject: CPU activity as percentage. X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 04 Aug 2007 07:36:01 -0000 Hello. I'm trying to write a function sys_cpu_percent() that returns the current cpu usage as a percentage. I currently have this: double sys_cpu_percent() { long cp_time[CPUSTATES]; double used; double total; size_t len = sizeof(cp_time); if (sysctlbyname("kern.cp_time", cp_time, &len, 0, 0) < 0) return 0; used = cp_time[CP_USER] + cp_time[CP_NICE] + cp_time[CP_SYS] + cp_time[CP_INTR]; total = cp_time[CP_USER] + cp_time[CP_NICE] + cp_time[CP_SYS] + cp_time[CP_INTR] + cp_time[CP_IDLE]; return (used / total) * 100; } However the function always returns ~9%, even when running a cpu intensive task in the background. Am I missing something obvious here? Is there a better way of doing this? My system is uniprocessor, if that makes any difference. thanks, MC