Accessing TSC Register


 
Thread Tools Search this Thread
Top Forums Programming Accessing TSC Register
# 1  
Old 02-10-2010
Accessing TSC Register

I'm accessing the TSC register, and using usleep() function to calculate my clock frequency. Basically, I check the current clock, delay for 1 second, and then check the clock again to discover how many clock ticks occur in a second.

The only problem is - my result turns out to be a ridiculous (and inconsistent) number. It always very close to one of the two following numbers.
18446744072127.2 Mhz or 2712.6 Mhz

My question - Is this a result of having a multi-core processor? Or is my code/logic just plain wrong? I included the relevant sections of the code below.


Code:
typedef unsigned long long int UINT64;

UINT64 startTSC = 0;
UINT64 stopTSC = 0;
UINT64 cycleCnt = 0;

// Access TSC register to get current value
__asm__ volatile(".byte 0x0f,0x31" : "=A" (startTSC));
// Delay for 1*10^6 us (1 second)
usleep(1000000);
// Access TSC register to get new value
__asm__ volatile(".byte 0x0f,0x31" : "=A" (stopTSC));

// Calculate cycles that happened over the full second
cycleCnt = stopTSC-startTSC;


Last edited by pludi; 02-10-2010 at 09:38 AM.. Reason: code tags, please...
# 2  
Old 02-10-2010
TSC is in fact munged by dual or quad core systems, and also by power-saving features that change the CPU clock rate. In any event I'm not convinced your inline assembly is working, being raw hex there. I've found some source here with perhaps a better implementation:
Code:
/*
 * get_tsc()
 */
static unsigned long long get_tsc(void)
{
	unsigned long long tsc;
	__asm__ __volatile__ ("rdtsc" : "=A" (tsc));
	return tsc;
}

You may need to change 'long long' to just 'long' on 64-bit systems. Or better yet, #include <stdint.h> and use uint64_t for code that should work on both...
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Forum Support Area for Unregistered Users & Account Problems

Cannot able to Register!

Hi Admin, I am unable to register . Request you to help me in getting my registration done. Name: Raghavendra Kulkarni Mail id : <removed> Thanking you in advance for your support. (1 Reply)
Discussion started by: Unregistered
1 Replies

2. Forum Support Area for Unregistered Users & Account Problems

Can't register

I am in China. China is in the blocked country list. I can't register. And I don't have a static ip. Can u help me to fix it. I want to register with following information: Username:liuchengzhang email:<removed> Thanks! (1 Reply)
Discussion started by: liuchengzhang
1 Replies

3. Forum Support Area for Unregistered Users & Account Problems

I can't register

hi,admin: i come from china,my country is in the blocked list.i want to register a account,can you help me? infomation: username: zhengzfand email:<removed> (1 Reply)
Discussion started by: Unregistered
1 Replies

4. Forum Support Area for Unregistered Users & Account Problems

I can't register

Hello admin, I come from Vietnam, it's in blocked country list. Can you help me to register I want to register with following information: Username: bojankikrick Email: <removed> Thanks very much! (1 Reply)
Discussion started by: Unregistered
1 Replies

5. Forum Support Area for Unregistered Users & Account Problems

Can not register

I need help from the scripting threads and want to register. I tried it several times, but I was all the time rejected, because of your spam policy. I do what you have written, but I wasn't still able to register. (1 Reply)
Discussion started by: ScAr
1 Replies

6. Forum Support Area for Unregistered Users & Account Problems

Cant register

Registration denied. Sorry, The UNIX and Linux Forums runs an active policy of not allowing spammers. Please contact us via by posting in this forum if you believe this is in error What's with this? Tried 2 email accounts in case your forum blocks either. (1 Reply)
Discussion started by: Diagonal
1 Replies

7. Forum Support Area for Unregistered Users & Account Problems

Not able to register

I tried my best to register myself on unix.com but i am not lucky, can you pls give me instruction. (1 Reply)
Discussion started by: Kuldeep_Sngh333
1 Replies

8. Forum Support Area for Unregistered Users & Account Problems

Can't register

I tried registering, but no matter what email address I provide, it says not a valid address and not the same in confirm. I tried many times, but no go. I thought I had already registered as Sully but even that didn't like my email address for password info. Of course, that could be someone else... (2 Replies)
Discussion started by: Not registered
2 Replies
Login or Register to Ask a Question