* Re: Help needed with timers on DEC 4000/710
1995-06-04 4:00 Help needed with timers on DEC 4000/710 J Ramanujam
@ 1995-06-05 4:00 ` Dirk Grunwald
0 siblings, 0 replies; 2+ messages in thread
From: Dirk Grunwald @ 1995-06-05 4:00 UTC (permalink / raw)
[ I sent this by mail and then realized others would want it too ]
The alpha has a cycle counter. It's a 32 bit counter, overflows in
2^32 cycles. This is about 15 seconds. If you're timing things with
resolution less than 15 seconds, it'll work fine.
The following code calculates the time (in microseconds) given a value
from the PCC. Given the code segments below, to time code, simply do:
unsigned long start, stop;
start = fixed_rpcc();
do_your_stuff();
stop = fixed_rpcc();
printf("Time was %f microseconds\n", rpcc_delta(start, stop);
-----------------------------------------------------------------------------
#include <sys/sysinfo.h>
#include <machine/hal_sysinfo.h>
#include <machine/rpb.h>
static double scale = -1;
unsigned long
fix_rpcc(unsigned long rpcc)
{
unsigned long offset = (rpcc >> 32) & 0xffffffff;
unsigned long cycles = (rpcc & 0xffffffff);
return offset + cycles;
}
double
rpcc_delta(unsigned long ul_start, unsigned long ul_stop)
{
if ( scale == -1 ) {
int err;
double freq;
struct rpb rpbbuf;
err = getsysinfo(GSI_GET_HWRPB, &rpbbuf, sizeof(rpbbuf));
if (err < 0) {
freq = 233;
} else {
freq = rpbbuf.rpb_counter;
}
/*
* Scale is the 1 over the clock frequency, in microseconds
*/
scale = (1000.0 * 1000.0)/(double) freq;
}
if (ul_stop < ul_start ) {
unsigned long delta;
delta = 0xffffffff - ul_start;
delta += ul_stop;
ul_start = 0;
ul_stop = delta;
}
return (ul_stop - ul_start) * scale;
}
-----------------------------------------------------------------------------
The following (assembly) routine reads the cycle counter
-----------------------------------------------------------------------------
.ugen
.verstamp 3 11
.text
.align 4
.file 2 "rpcc.c"
.globl fixed_rpcc
.loc 2 12
# 12 {
.ent fixed_rpcc 2
fixed_rpcc:
.option O2
# ldgp $gp, 0($27)
.frame $sp, 0, $26, 0
.prologue 1
.loc 2 12
.loc 2 13
# 13 unsigned long rpcc = r;
.loc 2 14
# 14 unsigned long offset = (rpcc >> 32) & 0xffffffff;
.loc 2 15
# 15 unsigned long cycles = (rpcc & 0xffffffff);
.loc 2 16
# 16 return offset + cycles;
#
# Register 16 is $a0. Load the rpcc into this register (i.e., it was
# not passed to us)
#
rpcc $16
extll $16, 4, $1
and $16, 4294967295, $2
addq $1, $2, $0
.livereg 0xFC7F0002,0x3FC00000
ret $31, ($26), 1
.end fixed_rpcc
-----------------------------------------------------------------------------
^ permalink raw reply [flat|nested] 2+ messages in thread