From: John Weber <john@worldwideweber.org>
To: linux-kernel@vger.kernel.org
Subject: Re: how to get cpu_khz?
Date: Mon, 17 Sep 2001 15:11:26 -0400 [thread overview]
Message-ID: <3BA64ADE.999E1402@worldwideweber.org> (raw)
In-Reply-To: <fa.ginsptv.1gg45b5@ifi.uio.no>
[-- Attachment #1: Type: text/plain, Size: 619 bytes --]
David Fries wrote:
>
> I'm using the TSC of the Pentium processors to get some precise timing
> delays for writing to a eeprom (bit banging bus operations), and it
> works just fine, but the cpu_khz variable isn't exported to a kernel
> module, so I hardcoded in my module. It works fine for that one
> system, but obviously I don't want to hard code it for the general
> case. I guess I could write my own routine to figure out what the
> cpu_khz is, but it is already done, so how do I get access to it?
I don't know of any official way of doing this, but here's some
code (written by aa) that accomplishes this.
[-- Attachment #2: MHz.c --]
[-- Type: text/plain, Size: 1277 bytes --]
/*
* $Id: MHz.c,v 1.4 2001/05/21 18:58:01 davej Exp $
* This file is part of x86info.
* (C) 2001 Dave Jones.
*
* Licensed under the terms of the GNU GPL License version 2.
*
* Estimate CPU MHz routine by Andrea Arcangeli <andrea@suse.de>
* Small changes by David Sterba <sterd9am@ss1000.ms.mff.cuni.cz>
*
*/
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#include <unistd.h>
__inline__ unsigned long long int rdtsc()
{
unsigned long long int x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
return x;
}
void estimate_MHz()
{
struct timezone tz;
struct timeval tvstart, tvstop;
unsigned long long int cycles[2]; /* gotta be 64 bit */
unsigned int microseconds; /* total time taken */
memset(&tz, 0, sizeof(tz));
/* get this function in cached memory */
gettimeofday(&tvstart, &tz);
cycles[0] = rdtsc();
gettimeofday(&tvstart, &tz);
/* we don't trust that this is any specific length of time */
usleep(1000000);
cycles[1] = rdtsc();
gettimeofday(&tvstop, &tz);
microseconds = ((tvstop.tv_sec-tvstart.tv_sec)*1000000) +
(tvstop.tv_usec-tvstart.tv_usec);
printf("%lldMHz processor (estimate).\n",
(cycles[1]-cycles[0])/microseconds);
}
int main(void)
{
while (1) {
estimate_MHz();
}
return (0);
}
next parent reply other threads:[~2001-09-17 19:13 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <fa.ginsptv.1gg45b5@ifi.uio.no>
2001-09-17 19:11 ` John Weber [this message]
2001-09-17 19:51 ` how to get cpu_khz? David Fries
2001-09-17 19:58 ` Richard B. Johnson
2001-09-17 21:26 ` Alan Cox
2001-09-17 16:43 David Fries
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3BA64ADE.999E1402@worldwideweber.org \
--to=john@worldwideweber.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).