From owner-freebsd-questions@FreeBSD.ORG Sun Sep 9 22:32:10 2007 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 1D32416A41A for ; Sun, 9 Sep 2007 22:32:10 +0000 (UTC) (envelope-from erikt@midgard.homeip.net) Received: from ch-smtp01.sth.basefarm.net (ch-smtp01.sth.basefarm.net [80.76.149.212]) by mx1.freebsd.org (Postfix) with ESMTP id 88F5013C48E for ; Sun, 9 Sep 2007 22:32:09 +0000 (UTC) (envelope-from erikt@midgard.homeip.net) Received: from c83-253-31-60.bredband.comhem.se ([83.253.31.60]:59925 helo=falcon.midgard.homeip.net) by ch-smtp01.sth.basefarm.net with esmtp (Exim 4.66) (envelope-from ) id 1IUVK2-0006hE-5P for freebsd-questions@freebsd.org; Mon, 10 Sep 2007 00:32:08 +0200 Received: (qmail 80605 invoked from network); 10 Sep 2007 00:32:03 +0200 Received: from owl.midgard.homeip.net (10.1.5.7) by falcon.midgard.homeip.net with ESMTP; 10 Sep 2007 00:32:03 +0200 Received: (qmail 51179 invoked by uid 1001); 10 Sep 2007 00:32:03 +0200 Date: Mon, 10 Sep 2007 00:32:03 +0200 From: Erik Trulsson To: freebsd-questions@freebsd.org Message-ID: <20070909223203.GA50980@owl.midgard.homeip.net> Mail-Followup-To: freebsd-questions@freebsd.org References: <20070909205025.GA40102@glitch.rwxrwxrwx.net> <20070909205759.GA35519@owl.midgard.homeip.net> <20070909220445.GA40462@glitch.rwxrwxrwx.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20070909220445.GA40462@glitch.rwxrwxrwx.net> User-Agent: Mutt/1.5.16 (2007-06-09) X-Originating-IP: 83.253.31.60 X-Scan-Result: No virus found in message 1IUVK2-0006hE-5P. X-Scan-Signature: ch-smtp01.sth.basefarm.net 1IUVK2-0006hE-5P d1b2bc3887aa6c0407d5ad69405651a5 Subject: Re: Getting the CPU frequency in C X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 09 Sep 2007 22:32:10 -0000 On Mon, Sep 10, 2007 at 12:04:45AM +0200, Martin Tournoij wrote: > On Sun 09 Sep 2007 22:09, Erik Trulsson wrote: > > On Sun, Sep 09, 2007 at 10:50:25PM +0200, Martin Tournoij wrote: > > > I'm trying to get the CPU frequency in C: > > > > > > #include > > > #include > > > #include > > > #include > > > #include > > > #include > > > > > > int main() > > > { > > > int mib[2]; > > > size_t size; > > > struct clockinfo clockrate; > > > > > > mib[0] = CTL_KERN; > > > mib[1] = KERN_CLOCKRATE; > > > size = sizeof clockrate; > > > sysctl(mib, 2, &clockrate, &size, NULL, 0); > > > > > > fprintf(stdout, "hz: %i\n", clockrate.hz); > > > fprintf(stdout, "tick: %i\n", clockrate.tick); > > > fprintf(stdout, "spare: %i\n", clockrate.spare); > > > fprintf(stdout, "stathz: %i\n", clockrate.stathz); > > > fprintf(stdout, "profhz: %i\n", clockrate.profhz); > > > > > > return 0; > > > } > > > > > > I tried to run this on two machines (one machine with hw.clockrate: 1378 and > > > the other 797) and it outputs the same on both: > > > hz: 1000 > > > tick: 1000 > > > spare: 0 > > > stathz: 133 > > > profhz: 666 > > > > > > The profhz value suggest the devil is at work :D although it's probably a some > > > stupid mistake on my part :/ Can anyone help? > > > > None of the kern.clockrate entries has any particular relationship with the > > CPU clock frequency, so it is not unexpected that you would get the same > > output from both machines. > > > > I think looking at hw.clockrate is the most portable you can get. > > If your CPU is using Cool'n'Quiet or the Intel equivalent you can also > > look at dev.cpu.N.freq for the current frequency. > > I got confused because they both have the same name ... do'h > > hw.clockrate doesn't seem to available through C(?), Of course it is. Using sysctlbyname(3) to access it works fine: #include #include #include int main() { size_t size; int clockrate; size = sizeof clockrate; sysctlbyname("hw.clockrate", &clockrate, &size, NULL, 0); fprintf(stdout, "hz: %i\n", clockrate); return 0; } hw.clockrate does however only seem to exist on i386 and amd64 architectures so if you are running on something else you will have to find some alternative solution. (Parsing the dmesg(8) output?) > exec-ing sysctl > hw.clockrate would work, but that's not very elegant... > > dev.cpu.0.freq doesn't seem to exists on my (Athlon) CPU, it > does on my PIII CPU though. It partly depends on the CPU, and mostly on the BIOS if the cpufreq(4) kernel module will be activated (assuming it has been loaded in the first place of course.) -- Erik Trulsson ertr1013@student.uu.se