Add a microtime() implementation that interpolates between ticks

using the cycle counter.  MP-safeness is achieved by giving each
CPU its own PCC frequency variables, and kicking the non-primary
processors via an IPI once per second.

Based on the sample code from David Mills' "A Kernel Model for
Precision Timekeeping".
This commit is contained in:
thorpej 2001-04-28 06:10:49 +00:00
parent 86d8020753
commit 53c1b3f9d7
8 changed files with 263 additions and 75 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: clock.c,v 1.29 2000/06/05 21:47:10 thorpej Exp $ */
/* $NetBSD: clock.c,v 1.30 2001/04/28 06:10:49 thorpej Exp $ */
/*
* Copyright (c) 1988 University of Utah.
@ -44,7 +44,7 @@
#include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
__KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.29 2000/06/05 21:47:10 thorpej Exp $");
__KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.30 2001/04/28 06:10:49 thorpej Exp $");
#include <sys/param.h>
#include <sys/kernel.h>
@ -76,10 +76,6 @@ __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.29 2000/06/05 21:47:10 thorpej Exp $");
#define UNIX_YEAR_OFFSET 0
#endif
#ifdef NTP
extern long time_precision; /* kern_clock.c; clock resolution in microseconds. */
#endif
struct device *clockdev;
const struct clockfns *clockfns;
int clockinitted;
@ -133,13 +129,6 @@ cpu_initclocks()
tickfix >>= (ftp - 1);
tickfixinterval = hz >> (ftp - 1);
}
#ifdef NTP
/*
* Since we don't have a "real" microtime, our actual
* precision is limited to the hardclock interrupt rate.
*/
time_precision = tick;
#endif
/*
* Establish the clock interrupt; it's a special case.

View File

@ -1,7 +1,7 @@
/* $NetBSD: interrupt.c,v 1.58 2001/04/15 23:26:05 thorpej Exp $ */
/* $NetBSD: interrupt.c,v 1.59 2001/04/28 06:10:49 thorpej Exp $ */
/*-
* Copyright (c) 2000 The NetBSD Foundation, Inc.
* Copyright (c) 2000, 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
@ -72,7 +72,7 @@
#include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
__KERNEL_RCSID(0, "$NetBSD: interrupt.c,v 1.58 2001/04/15 23:26:05 thorpej Exp $");
__KERNEL_RCSID(0, "$NetBSD: interrupt.c,v 1.59 2001/04/28 06:10:49 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@ -80,6 +80,7 @@ __KERNEL_RCSID(0, "$NetBSD: interrupt.c,v 1.58 2001/04/15 23:26:05 thorpej Exp $
#include <sys/vmmeter.h>
#include <sys/sched.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <machine/cpuvar.h>
@ -107,6 +108,7 @@ void
interrupt(unsigned long a0, unsigned long a1, unsigned long a2,
struct trapframe *framep)
{
static int microset_iter; /* call microset() once per sec. */
struct cpu_info *ci = curcpu();
struct cpu_softc *sc = ci->ci_softc;
struct proc *p;
@ -141,6 +143,22 @@ interrupt(unsigned long a0, unsigned long a1, unsigned long a2,
*/
sc->sc_evcnt_clock.ev_count++;
uvmexp.intrs++;
/*
* Update the PCC frequency for use by microtime().
*/
if (
#if defined(MULTIPROCESSOR)
CPU_IS_PRIMARY(ci) &&
#endif
microset_iter-- == 0) {
microset_iter = hz;
#if defined(MULTIPROCESSOR)
alpha_multicast_ipi(cpus_running,
ALPHA_IPI_MICROSET);
#endif
microset(ci, framep);
}
if (platform.clockintr) {
/*
* Call hardclock(). This will also call

View File

@ -1,4 +1,4 @@
/* $NetBSD: ipifuncs.c,v 1.27 2001/04/21 16:27:10 thorpej Exp $ */
/* $NetBSD: ipifuncs.c,v 1.28 2001/04/28 06:10:49 thorpej Exp $ */
/*-
* Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
@ -39,7 +39,7 @@
#include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
__KERNEL_RCSID(0, "$NetBSD: ipifuncs.c,v 1.27 2001/04/21 16:27:10 thorpej Exp $");
__KERNEL_RCSID(0, "$NetBSD: ipifuncs.c,v 1.28 2001/04/28 06:10:49 thorpej Exp $");
/*
* Interprocessor interrupt handlers.
@ -78,6 +78,7 @@ void alpha_ipi_pause(struct cpu_info *, struct trapframe *);
*/
ipifunc_t ipifuncs[ALPHA_NIPIS] = {
alpha_ipi_halt,
microset,
alpha_ipi_tbia,
alpha_ipi_tbiap,
pmap_do_tlb_shootdown,
@ -91,6 +92,7 @@ ipifunc_t ipifuncs[ALPHA_NIPIS] = {
const char *ipinames[ALPHA_NIPIS] = {
"halt ipi",
"microset ipi",
"tbia ipi",
"tbiap ipi",
"shootdown ipi",

View File

@ -1,4 +1,4 @@
/* $NetBSD: machdep.c,v 1.241 2001/04/26 03:10:44 ross Exp $ */
/* $NetBSD: machdep.c,v 1.242 2001/04/28 06:10:49 thorpej Exp $ */
/*-
* Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
@ -73,7 +73,7 @@
#include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.241 2001/04/26 03:10:44 ross Exp $");
__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.242 2001/04/28 06:10:49 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@ -1942,46 +1942,6 @@ remrunqueue(p)
sched_whichqs &= ~(1 << bit);
}
/*
* Return the best possible estimate of the time in the timeval
* to which tvp points. Unfortunately, we can't read the hardware registers.
* We guarantee that the time will be greater than the value obtained by a
* previous call.
*
* XXX PLEASE REWRITE ME TO USE THE CYCLE COUNTER AND DEAL WITH
* XXX MULTIPLE CPUs IN A SANE WAY!
*/
void
microtime(tvp)
register struct timeval *tvp;
{
static struct timeval lasttime;
static struct simplelock microtime_slock = SIMPLELOCK_INITIALIZER;
int s;
s = splclock();
simple_lock(&microtime_slock);
*tvp = time;
#ifdef notdef
tvp->tv_usec += clkread();
while (tvp->tv_usec >= 1000000) {
tvp->tv_sec++;
tvp->tv_usec -= 1000000;
}
#endif
if (tvp->tv_sec == lasttime.tv_sec &&
tvp->tv_usec <= lasttime.tv_usec &&
(tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000) {
tvp->tv_sec++;
tvp->tv_usec -= 1000000;
}
lasttime = *tvp;
simple_unlock(&microtime_slock);
splx(s);
}
/*
* Wait "n" microseconds.
*/

View File

@ -0,0 +1,207 @@
/* $NetBSD: microtime.c,v 1.1 2001/04/28 06:10:49 thorpej Exp $ */
/*-
* Copyright (c) 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/******************************************************************************
* *
* Copyright (c) David L. Mills 1993, 1994 *
* *
* Permission to use, copy, modify, and distribute this software and its *
* documentation for any purpose and without fee is hereby granted, provided *
* that the above copyright notice appears in all copies and that both the *
* copyright notice and this permission notice appear in supporting *
* documentation, and that the name University of Delaware not be used in *
* advertising or publicity pertaining to distribution of the software *
* without specific, written prior permission. The University of Delaware *
* makes no representations about the suitability this software for any *
* purpose. It is provided "as is" without express or implied warranty. *
* *
******************************************************************************/
#include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
__KERNEL_RCSID(0, "$NetBSD: microtime.c,v 1.1 2001/04/28 06:10:49 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <machine/cpu.h>
#include <machine/rpb.h>
/*
* Return the best possible estimate of the time in the timeval to which
* tvp points. The kernel logical time variable is interpolated between
* ticks by reading the PCC to determine the number of cycles since the
* last processor update, then converting the result to microseconds. In
* the case of multiprocessor systems, the interpolation is specific to
* each processor, since each processor has its own PCC.
*/
void
microtime(struct timeval *tvp)
{
static struct timeval lasttime;
static struct simplelock microtime_slock = SIMPLELOCK_INITIALIZER;
struct timeval t, st;
struct cpu_info *ci = curcpu();
long sec, usec;
int s;
s = splclock(); /* also blocks IPIs */
/* XXXSMP: not atomic */
st = time; /* read system time */
if (ci->ci_pcc_denom != 0) {
/*
* Determine the current clock time as the time at last
* microset() call, plus the PCC accumulation since then.
* This time should lie in the interval between the current
* master clock time and the time at the nexdt tick, but
* this code does not explicitly require that in the interest
* of speed. If something ugly occurs, like a settimeofday()
* call, the processors may disagree among themselves for not
* more than the interval between microset() calls. In any
* case, the following sanity checks will suppress timewarps.
*/
t = ci->ci_pcc_time;
usec = (alpha_rpcc() & 0xffffffffUL) - ci->ci_pcc_pcc;
if (usec < 0)
usec += 0x100000000L;
t.tv_usec = (usec * ci->ci_pcc_ms_delta) / ci->ci_pcc_denom;
while (t.tv_usec >= 1000000) {
t.tv_usec -= 1000000;
t.tv_sec++;
}
} else {
/*
* Can't use the PCC -- just use the system time.
*/
t = st;
}
/*
* Ordinarily, the current clock time is guaranteed to be later
* by at least one microsecond than the last time the clock was
* read. However, this rule applies only if the current time is
* within one second of the last time. Otherwise, the clock will
* (shudder) be set backward. The clock adjustment daemon or
* human equivalent is presumed to be correctly implemented and
* to set the clock backward only upon unavoidable crisis.
*/
simple_lock(&microtime_slock);
sec = lasttime.tv_sec - t.tv_sec;
usec = lasttime.tv_usec - t.tv_usec;
if (usec < 0) {
usec += 1000000;
sec--;
}
if (sec == 0) {
t.tv_usec += usec + 1;
if (t.tv_usec >= 1000000) {
t.tv_usec -= 1000000;
t.tv_sec++;
}
}
lasttime = t;
simple_unlock(&microtime_slock);
splx(s);
*tvp = t;
}
/*
* This routine is called about once per second directly by the master
* processor and via an interprocessor interrupt for other processors.
* It determines the PCC frequency of each processor relative to the
* master clock and the time this determination is made. These values
* are used by microtime() to interpolate the microseconds between
* timer interrupts. Note that we assume the kernel variables have
* been zeroed early in life.
*/
void
microset(struct cpu_info *ci, struct trapframe *framep)
{
struct timeval t;
long delta, denom;
/* Note: Clock interrupts are already blocked. */
/* XXX BLOCK MACHINE CHECKS? */
denom = ci->ci_pcc_pcc;
t = time; /* XXXSMP: not atomic */
ci->ci_pcc_pcc = alpha_rpcc() & 0xffffffffUL;
/* XXX UNBLOCK MACHINE CHECKS? */
if (ci->ci_pcc_denom == 0) {
/*
* This is our first time here on this CPU. Just
* start with reasonable initial values.
*/
ci->ci_pcc_time = t;
ci->ci_pcc_ms_delta = 1000000;
ci->ci_pcc_denom = hwrpb->rpb_cc_freq;
return;
}
denom = ci->ci_pcc_pcc - denom;
if (denom < 0)
denom += 0x100000000L;
delta = (t.tv_sec - ci->ci_pcc_time.tv_sec) * 1000000 +
(t.tv_usec - ci->ci_pcc_time.tv_usec);
ci->ci_pcc_time = t;
/*
* Make sure it's within .5 to 1.5 seconds -- otherwise,
* the time is probably be frobbed with by the timekeeper
* or the human.
*/
if (delta > 500000 && delta < 15000000) {
ci->ci_pcc_ms_delta = delta;
ci->ci_pcc_denom = denom;
} else {
#if 0
printf("microset: delta %d, resetting state\n", delta);
#endif
ci->ci_pcc_ms_delta = 1000000;
ci->ci_pcc_denom = hwrpb->rpb_cc_freq;
}
}

View File

@ -1,4 +1,4 @@
# $NetBSD: files.alpha,v 1.136 2001/04/26 03:10:45 ross Exp $
# $NetBSD: files.alpha,v 1.137 2001/04/28 06:10:50 thorpej Exp $
#
# alpha-specific configuration info
@ -487,6 +487,7 @@ file arch/alpha/alpha/ipifuncs.c multiprocessor
file arch/alpha/alpha/machdep.c
file arch/alpha/alpha/mainbus.c
file arch/alpha/alpha/mem.c
file arch/alpha/alpha/microtime.c
file arch/alpha/alpha/pmap.c
file arch/alpha/alpha/process_machdep.c
file arch/alpha/alpha/procfs_machdep.c procfs

View File

@ -1,7 +1,7 @@
/* $NetBSD: cpu.h,v 1.55 2001/04/26 03:10:46 ross Exp $ */
/* $NetBSD: cpu.h,v 1.56 2001/04/28 06:10:50 thorpej Exp $ */
/*-
* Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
* Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
@ -130,6 +130,15 @@ struct cpu_info {
u_long ci_want_resched; /* preempt current process */
u_long ci_intrdepth; /* interrupt trap depth */
struct trapframe *ci_db_regs; /* registers for debuggers */
/*
* Variables used by microtime().
*/
struct timeval ci_pcc_time;
long ci_pcc_pcc;
long ci_pcc_ms_delta;
long ci_pcc_denom;
#if defined(MULTIPROCESSOR)
__volatile u_long ci_flags; /* flags; see below */
__volatile u_long ci_ipis; /* interprocessor interrupts pending */
@ -272,6 +281,7 @@ struct rpb;
struct trapframe;
int badaddr(void *, size_t);
void microset(struct cpu_info *, struct trapframe *);
#endif /* _KERNEL */
#endif /* _ALPHA_CPU_H_ */

View File

@ -1,4 +1,4 @@
/* $NetBSD: intr.h,v 1.46 2001/04/20 22:28:58 thorpej Exp $ */
/* $NetBSD: intr.h,v 1.47 2001/04/28 06:10:50 thorpej Exp $ */
/*-
* Copyright (c) 2000, 2001 The NetBSD Foundation, Inc.
@ -158,18 +158,19 @@ _splraise(int s)
/*
* Interprocessor interrupts. In order how we want them processed.
*/
#define ALPHA_IPI_HALT 0x0000000000000001UL
#define ALPHA_IPI_TBIA 0x0000000000000002UL
#define ALPHA_IPI_TBIAP 0x0000000000000004UL
#define ALPHA_IPI_SHOOTDOWN 0x0000000000000008UL
#define ALPHA_IPI_IMB 0x0000000000000010UL
#define ALPHA_IPI_AST 0x0000000000000020UL
#define ALPHA_IPI_SYNCH_FPU 0x0000000000000040UL
#define ALPHA_IPI_DISCARD_FPU 0x0000000000000080UL
#define ALPHA_IPI_PAUSE 0x0000000000000100UL
#define ALPHA_IPI_PMAP_REACTIVATE 0x0000000000000200UL
#define ALPHA_IPI_HALT (1UL << 0)
#define ALPHA_IPI_MICROSET (1UL << 1)
#define ALPHA_IPI_TBIA (1UL << 2)
#define ALPHA_IPI_TBIAP (1UL << 3)
#define ALPHA_IPI_SHOOTDOWN (1UL << 4)
#define ALPHA_IPI_IMB (1UL << 5)
#define ALPHA_IPI_AST (1UL << 6)
#define ALPHA_IPI_SYNCH_FPU (1UL << 7)
#define ALPHA_IPI_DISCARD_FPU (1UL << 8)
#define ALPHA_IPI_PAUSE (1UL << 9)
#define ALPHA_IPI_PMAP_REACTIVATE (1UL << 10)
#define ALPHA_NIPIS 10 /* must not exceed 64 */
#define ALPHA_NIPIS 11 /* must not exceed 64 */
struct cpu_info;
struct trapframe;