public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* How to measure time accurately.
@ 2005-03-28  3:28 krishna
  2005-03-29  3:37 ` Lee Revell
  2005-03-29  5:07 ` Chris Friesen
  0 siblings, 2 replies; 8+ messages in thread
From: krishna @ 2005-03-28  3:28 UTC (permalink / raw)
  To: Linux Kernel

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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to measure time accurately.
  2005-03-28  3:28 How to measure time accurately krishna
@ 2005-03-29  3:37 ` Lee Revell
  2005-03-29  5:07 ` Chris Friesen
  1 sibling, 0 replies; 8+ messages in thread
From: Lee Revell @ 2005-03-29  3:37 UTC (permalink / raw)
  To: krishna; +Cc: Linux Kernel

On Mon, 2005-03-28 at 08:58 +0530, krishna wrote:
> 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()




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to measure time accurately.
  2005-03-28  3:28 How to measure time accurately krishna
  2005-03-29  3:37 ` Lee Revell
@ 2005-03-29  5:07 ` Chris Friesen
  2005-03-29  9:10   ` Jesper Juhl
  2005-03-29 23:32   ` Peter Chubb
  1 sibling, 2 replies; 8+ messages in thread
From: Chris Friesen @ 2005-03-29  5:07 UTC (permalink / raw)
  To: krishna; +Cc: Linux Kernel

krishna wrote:
> 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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to measure time accurately.
  2005-03-29  5:07 ` Chris Friesen
@ 2005-03-29  9:10   ` Jesper Juhl
  2005-03-29  9:25     ` Jan Engelhardt
  2005-03-29 23:32   ` Peter Chubb
  1 sibling, 1 reply; 8+ messages in thread
From: Jesper Juhl @ 2005-03-29  9:10 UTC (permalink / raw)
  To: Chris Friesen; +Cc: krishna, Linux Kernel

On Mon, 28 Mar 2005, Chris Friesen wrote:

> Date: Mon, 28 Mar 2005 23:07:14 -0600
> From: Chris Friesen <cfriesen@nortel.com>
> To: krishna <krishna.c@globaledgesoft.com>
> Cc: Linux Kernel <linux-kernel@vger.kernel.org>
> Subject: Re: How to measure time accurately.
> 
> krishna wrote:
> > 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.
> 

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);


-- 
Jesper Juhl


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to measure time accurately.
  2005-03-29  9:10   ` Jesper Juhl
@ 2005-03-29  9:25     ` Jan Engelhardt
  0 siblings, 0 replies; 8+ messages in thread
From: Jan Engelhardt @ 2005-03-29  9:25 UTC (permalink / raw)
  To: Jesper Juhl; +Cc: Chris Friesen, krishna, Linux Kernel


>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
-- 
No TOFU for me, please.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to measure time accurately.
  2005-03-29  5:07 ` Chris Friesen
  2005-03-29  9:10   ` Jesper Juhl
@ 2005-03-29 23:32   ` Peter Chubb
  2005-03-30  0:24     ` Chris Friesen
  1 sibling, 1 reply; 8+ messages in thread
From: Peter Chubb @ 2005-03-29 23:32 UTC (permalink / raw)
  To: Chris Friesen; +Cc: krishna, Linux Kernel

>>>>> "Chris" == Chris Friesen <cfriesen@nortel.com> writes:

Chris> krishna wrote:
>> 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.
-- 
Dr Peter Chubb  http://www.gelato.unsw.edu.au  peterc AT gelato.unsw.edu.au
The technical we do immediately,  the political takes *forever*

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to measure time accurately.
  2005-03-29 23:32   ` Peter Chubb
@ 2005-03-30  0:24     ` Chris Friesen
  2005-03-30  9:46       ` Jan Engelhardt
  0 siblings, 1 reply; 8+ messages in thread
From: Chris Friesen @ 2005-03-30  0:24 UTC (permalink / raw)
  To: Peter Chubb; +Cc: krishna, Linux Kernel

Peter Chubb wrote:
>>>>>>"Chris" == Chris Friesen <cfriesen@nortel.com> 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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to measure time accurately.
  2005-03-30  0:24     ` Chris Friesen
@ 2005-03-30  9:46       ` Jan Engelhardt
  0 siblings, 0 replies; 8+ messages in thread
From: Jan Engelhardt @ 2005-03-30  9:46 UTC (permalink / raw)
  To: Chris Friesen; +Cc: Peter Chubb, krishna, Linux Kernel


> 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
-- 
No TOFU for me, please.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2005-03-30  9:47 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-03-28  3:28 How to measure time accurately krishna
2005-03-29  3:37 ` Lee Revell
2005-03-29  5:07 ` Chris Friesen
2005-03-29  9:10   ` Jesper Juhl
2005-03-29  9:25     ` Jan Engelhardt
2005-03-29 23:32   ` Peter Chubb
2005-03-30  0:24     ` Chris Friesen
2005-03-30  9:46       ` Jan Engelhardt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox