grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Higher resolution timers


From: Colin D Bennett
Subject: Re: Higher resolution timers
Date: Tue, 20 May 2008 10:46:46 -0700

On Mon, 12 May 2008 09:33:54 -0700
Colin D Bennett <address@hidden> wrote:

> I think that using the TSC (w/ RDTSC instruction) and calibrating it
> with a quick 2-3 RTC tick loop at startup might be the easiest option.

Just wondered if something like this would be a practical way to go:

/*******************************************************************/
/* How many ticks to use for calibration loop. (>= 1) */
#define CALIBRATION_TICKS 2

/* Output: Number of TSC cycles per second. */
grub_uint64_t tsc_cycles_per_second;

void grub_timer_tsc_calibrate(void)
{
        /* Calibrate to real time (relative, not absolute). */
        grub_uint64_t start_tsc;
        grub_uint64_t end_tsc;
        grub_uint32_t initial_tick;
        grub_uint32_t start_tick;
        grub_uint32_t end_tick;

        /* Wait for the start of the next tick;
           we'll base out timing off this edge. */
        initial_tick = grub_get_rtc();
        do 
        {
                start_tick = grub_get_rtc();
        }
        while (start_tick == initial_tick);
        start_tsc = rdtsc();

        /* Wait for the start of the next tick.  This will
           be the end of the 1-tick period. */
        do 
        {
                end_tick = grub_get_rtc();
        }
        while (end_tick - start_tick < CALIBRATION_TICKS);
        end_tsc = rdtsc();

        tsc_cycles_per_second = 
                (end_tsc - start_tsc) / (end_tick - start_tick) 
                * GRUB_TICKS_PER_SEC;
}
/*******************************************************************/


Colin

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]