Skip to contentSkip to conversation

How to measure time accurately.

DISCUSSION krishna 6 participants 8 messages Mar 28, 2005 – Mar 30, 2005

lore
Share & tools
lore

krishna

author
#0 lore raw

Hi All,

Can any one tell me how to measure time accurately for a block of C code in device drivers. For example, If I want to measure the time duration of firmware download.

Regards,
Krishna Chaitanya

#1 lore raw
Quoted 5 lines · krishna

Hi All,

Can any one tell me how to measure time accurately for a block of C code in device drivers.
For example, If I want to measure the time duration of firmware download.

rdtsc()

#2 lore raw
In reply to krishna
Quoted 4 lines · krishna

Hi All,

Can any one tell me how to measure time accurately for a block of C code in device drivers. For example, If I want to measure the time duration of firmware download.

Most cpus have some way of getting at a counter or decrementer of various frequencies. Usually it requires low-level hardware knowledge and often it needs assembly code.

On ppc you'd use the mftbu/mftbl instructions, as suggested by Lee on x86 you'd use the rdtsc instruction.

Chris

#3 lore raw
Quoted 13 lines · Chris Friesen

Date: Mon, 28 Mar 2005 23:07:14 -0600
From: Chris Friesen <[email protected]>
To: krishna <[email protected]>
Cc: Linux Kernel <[email protected]>
Subject: Re: How to measure time accurately.

Quoted 5 lines · krishna

Hi All,

Can any one tell me how to measure time accurately for a block of C code in device drivers.
For example, If I want to measure the time duration of firmware download.

Quoted 8 lines

Most cpus have some way of getting at a counter or decrementer of various frequencies. Usually it requires low-level hardware knowledge and often it needs assembly code.

On ppc you'd use the mftbu/mftbl instructions, as suggested by Lee on x86 you'd use the rdtsc instruction.

In some cases you can simply count jiffies - depending on how accurate you need to time things I'd say that often something like this is adequate :

unsigned long start, time_spent;

start = jiffies;
/* do stuff */
time_spent = jiffies - start;
printk("stuff took %d jiffies (%d seconds)\n", time_spent, time_spent/HZ);

Signature · 1 line
Jesper Juhl
#4 lore raw
Quoted 3 lines

In some cases you can simply count jiffies - depending on how accurate you need to time things I'd say that often something like this is adequate :

These "some cases" exclude this one:
If interrupts are disabled, a jiffy might be missed. Take care.

If you are on UP and want to measure within
- a tick [when using preempt]
- kernel space [no preempt]
disabled interrupts should usually not be the case.

Jan Engelhardt

Signature · 1 line
No TOFU for me, please.
#5 lore raw
In reply to Chris Friesen
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Chris" == Chris Friesen <[email protected]> writes:

Quoted 5 lines · Chris> krishna wrote:
Quoted 5 lines

Hi All,

Can any one tell me how to measure time accurately for a block of C code in device drivers. For example, If I want to measure the time duration of firmware download.

Chris> Most cpus have some way of getting at a counter or decrementer Chris> of various frequencies. Usually it requires low-level hardware Chris> knowledge and often it needs assembly code.

As a device driver is inside the linux kernel (unless you're writein a user-mode device driver :-)) you can use the getcycles() macro that's defined for most architectures. It provides a snapshot of the cycle-counter.

Caveats:
1. If you're running with power management, the cycle counter ticks at a variable rate.
2. If you're on a multiprocessor, the cycle counters of different processors need not be synchronised.

Signature · 2 lines
Dr Peter Chubb  http://www.gelato.unsw.edu.au  peterc AT gelato.unsw.edu.au
The technical we do immediately,  the political takes *forever*
#6 lore raw
Quoted 10 lines · Peter Chubb
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Chris" == Chris Friesen <[email protected]> writes:

Chris> Most cpus have some way of getting at a counter or decrementer Chris> of various frequencies. Usually it requires low-level hardware Chris> knowledge and often it needs assembly code.

As a device driver is inside the linux kernel (unless you're writein a user-mode device driver :-)) you can use the getcycles() macro that's defined for most architectures. It provides a snapshot of the cycle-counter.

For ppc this only gives 32-bit values, which overflow every 129 seconds on my G5. Depending on how long you're trying to time, this could be a problem.

Chris

#7 lore raw
Quoted 3 lines

For ppc this only gives 32-bit values, which overflow every 129 seconds on my G5. Depending on how long you're trying to time, this could be a problem.

Just take an extra measure to "record" overflows (2^32-1 => 0) and you're set.

Jan Engelhardt

Signature · 1 line
No TOFU for me, please.