Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:50

0001 /*
0002  *  This file implements a benchmark timer using the count/compare
0003  *  CP0 registers.
0004  *
0005  *  Copyright (c) 2005 by Cogent Computer Systems
0006  *  Written by Jay Monkman <jtm@lopingdog.com>
0007  *
0008  *  The license and distribution terms for this file may be
0009  *  found in the file LICENSE in this distribution or at
0010  *  http://www.rtems.org/license/LICENSE.
0011  */
0012 
0013 #include <assert.h>
0014 
0015 #include <bsp.h>
0016 #include <rtems/btimer.h>
0017 
0018 bool benchmark_timer_find_average_overhead;
0019 uint32_t tstart;
0020 
0021 void benchmark_timer_initialize(void)
0022 {
0023     __asm__ volatile ("mfc0 %0, $9\n" : "=r" (tstart));
0024     /* tick time in picooseconds */
0025 }
0026 
0027 #define AVG_OVERHEAD      0  /* It typically takes N instructions */
0028                              /*     to start/stop the timer. */
0029 #define LEAST_VALID       1  /* Don't trust a value lower than this */
0030                              /* tx39 simulator can count instructions. :) */
0031 
0032 benchmark_timer_t benchmark_timer_read(void)
0033 {
0034   uint32_t  total;
0035   uint32_t  cnt;
0036 
0037   __asm__ volatile ("mfc0 %0, $9\n" : "=r" (cnt));
0038 
0039   total = cnt - tstart;
0040   total = (total * 1000) / 396; /* convert to nanoseconds */
0041 
0042 
0043   if ( benchmark_timer_find_average_overhead == true )
0044     return total;          /* in one microsecond units */
0045 
0046   if ( total < LEAST_VALID )
0047     return 0;            /* below timer resolution */
0048 
0049   return total - AVG_OVERHEAD;
0050 }
0051 
0052 void benchmark_timer_disable_subtracting_average_overhead(
0053   bool find_flag
0054 )
0055 {
0056   benchmark_timer_find_average_overhead = find_flag;
0057 }