Skip to contentSkip to conversation

how to get cpu_khz?

DISCUSSION John Weber 4 participants 4 messages Sep 17, 2001 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

John Weber

author
#1 lore raw
Quoted 8 lines · David Fries

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.

/*

 *  $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 <[email protected]>
* Small changes by David Sterba <[email protected]> *
*/

#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);
}
#2 lore raw
In reply to a message not in the archive
Quoted 10 lines · John Weber
Quoted 8 lines · David Fries

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?

Quoted 2 lines

I don't know of any official way of doing this, but here's some code (written by aa) that accomplishes this.

For a user space program you could just read /proc/cpuinfo, I'm actually writing a kernel driver, maybe I wasn't clear enough. I'm just frustrated because the variable I'm after, cpu_khz is already calculated at boot time (that's where /proc/cpuinfo gets its data) and that variable doesn't appear to be exported to the rest of the kernel, either that or I'm just missing something, which I would rather be the case at this point.

Signature · 4 lines
		+---------------------------------+
		|      David Fries                |
		|      [email protected]        |
		+---------------------------------+
#3 lore raw
Quoted 14 lines · David Fries
Quoted 10 lines · John Weber
Quoted 8 lines · David Fries

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?

Quoted 2 lines

I don't know of any official way of doing this, but here's some code (written by aa) that accomplishes this.

Quoted 8 lines

For a user space program you could just read /proc/cpuinfo, I'm actually writing a kernel driver, maybe I wasn't clear enough. I'm just frustrated because the variable I'm after, cpu_khz is already calculated at boot time (that's where /proc/cpuinfo gets its data) and that variable doesn't appear to be exported to the rest of the kernel, either that or I'm just missing something, which I would rather be the case at this point.

Ask Alan to export it by default. If no-go, export it in your configuration.

Cheers,
Dick Johnson

Penguin : Linux version 2.4.1 on an i686 machine (799.53 BogoMips).

I was going to compile a list of innovations that could be attributed to Microsoft. Once I realized that Ctrl-Alt-Del was handled in the BIOS, I found that there aren't any.

#4 lore raw
Quoted 8 lines
Quoted 5 lines

calculated at boot time (that's where /proc/cpuinfo gets its data) and that variable doesn't appear to be exported to the rest of the kernel, either that or I'm just missing something, which I would rather be the case at this point.

Ask Alan to export it by default. If no-go, export it in your configuration.

Processor speed from cpu_khz is not a constant, it varies per processor on split multiplier SMP and it varies on certain kinds of power management. For delays we have udelay(). That has a chance of supporting speed changes