Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 15 Jun 2004 23:32:01 -0500
From:      Dan Nelson <dnelson@allantgroup.com>
To:        CAVELIER =?utf-8?Q?Gr=C3=A9gory?= <g_cavelier@yahoo.fr>
Cc:        questions@freebsd.org
Subject:   Re: CPU frequency
Message-ID:  <20040616043201.GN62945@dan.emsphone.com>
In-Reply-To: <40CFB701.9000303@yahoo.fr>
References:  <40CFB701.9000303@yahoo.fr>

next in thread | previous in thread | raw e-mail | index | archive | help
In the last episode (Jun 16), CAVELIER Grgory said:
> How can I get the CPU frequency from a C program ???
> Under Linux, I used the /proc filesystem but how can I do this with 
> FreeBSD (I have version 5.2.1)

Try 

#include <stdio.h>
#include <stdlib.h>
#include <sys/sysctl.h>
#include <sys/types.h>
int main(void)
{
	long mhz;
	size_t mhzsize = sizeof(mhz);

	if (sysctlbyname("hw.clockrate", &mhz, &mhzsize, NULL, 0))
	{
		perror("cannot get MHz");
		return 1;
	}

	printf("%ld\n", mhz);
	return 0;
}


You could also use machdep.tsc_freq to get the exact clock rate in Hz.

-- 
	Dan Nelson
	dnelson@allantgroup.com



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20040616043201.GN62945>