1 | /* timer.c |
---|
2 | * |
---|
3 | * This file manages the benchmark timer used by the RTEMS Timing Test |
---|
4 | * Suite. Each measured time period is demarcated by calls to |
---|
5 | * Timer_initialize() and Read_timer(). Read_timer() usually returns |
---|
6 | * the number of microseconds since Timer_initialize() exitted. |
---|
7 | * |
---|
8 | * NOTE: It is important that the timer start/stop overhead be |
---|
9 | * determined when porting or modifying this code. |
---|
10 | * |
---|
11 | * COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994. |
---|
12 | * On-Line Applications Research Corporation (OAR). |
---|
13 | * All rights assigned to U.S. Government, 1994. |
---|
14 | * |
---|
15 | * This material may be reproduced by or for the U.S. Government pursuant |
---|
16 | * to the copyright license under the clause at DFARS 252.227-7013. This |
---|
17 | * notice must appear in all copies of this file and its derivatives. |
---|
18 | * |
---|
19 | * $Id$ |
---|
20 | */ |
---|
21 | |
---|
22 | #ifndef lint |
---|
23 | static char _sccsid[] = "@(#)timer.c 05/07/96 1.4\n"; |
---|
24 | #endif |
---|
25 | |
---|
26 | #include <rtems.h> |
---|
27 | |
---|
28 | #define CLOCKS_PER_MICROSECOND ( CPU_CLOCK_RATE_MHZ ) |
---|
29 | #define TIMER_MAX_VALUE 0xffffffff |
---|
30 | |
---|
31 | static unsigned32 read_timer( void ) |
---|
32 | { |
---|
33 | return 0; /* do something with real hardware here */ |
---|
34 | } |
---|
35 | |
---|
36 | static rtems_boolean Timer_driver_Find_average_overhead; |
---|
37 | static unsigned32 Timer_initial_value = 0; |
---|
38 | |
---|
39 | void Timer_initialize( void ) |
---|
40 | { |
---|
41 | Timer_initial_value = read_timer(); |
---|
42 | |
---|
43 | /* |
---|
44 | * Somehow start the timer |
---|
45 | */ |
---|
46 | |
---|
47 | /* Timer is always running */ |
---|
48 | } |
---|
49 | |
---|
50 | /* |
---|
51 | * The following controls the behavior of Read_timer(). |
---|
52 | * |
---|
53 | * AVG_OVEREHAD is the overhead for starting and stopping the timer. It |
---|
54 | * is usually deducted from the number returned. |
---|
55 | * |
---|
56 | * LEAST_VALID is the lowest number this routine should trust. Numbers |
---|
57 | * below this are "noise" and zero is returned. |
---|
58 | */ |
---|
59 | |
---|
60 | #define AVG_OVERHEAD 8 /* It typically takes X.X microseconds */ |
---|
61 | /* (Y countdowns) to start/stop the timer. */ |
---|
62 | /* This value is in cycles. */ |
---|
63 | #define LEAST_VALID 1 /* Don't trust a clicks value lower than this */ |
---|
64 | |
---|
65 | int Read_timer( void ) |
---|
66 | { |
---|
67 | unsigned64 clicks; |
---|
68 | unsigned32 total; |
---|
69 | |
---|
70 | /* |
---|
71 | * Read the timer and see how many clicks it has been since we started. |
---|
72 | */ |
---|
73 | |
---|
74 | clicks = read_timer(); /* XXX: read some HW here */ |
---|
75 | if (clicks < Timer_initial_value) |
---|
76 | { |
---|
77 | clicks += TIMER_MAX_VALUE; |
---|
78 | } |
---|
79 | clicks -= Timer_initial_value; |
---|
80 | |
---|
81 | /* |
---|
82 | * Total is calculated by taking into account the number of timer overflow |
---|
83 | * interrupts since the timer was initialized and clicks since the last |
---|
84 | * interrupts. |
---|
85 | */ |
---|
86 | #if 0 /* leave total in number of cycles */ |
---|
87 | total = clicks / CLOCKS_PER_MICROSECOND; |
---|
88 | #else |
---|
89 | total = clicks; |
---|
90 | #endif |
---|
91 | |
---|
92 | if ( Timer_driver_Find_average_overhead == 1 ) |
---|
93 | return total; /* in # cycles units */ |
---|
94 | else { |
---|
95 | if ( total < LEAST_VALID ) |
---|
96 | return 0; /* below timer resolution */ |
---|
97 | /* |
---|
98 | * leave total in cycles |
---|
99 | */ |
---|
100 | return (total - AVG_OVERHEAD); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | /* |
---|
105 | * Empty function call used in loops to measure basic cost of looping |
---|
106 | * in Timing Test Suite. |
---|
107 | */ |
---|
108 | |
---|
109 | rtems_status_code Empty_function( void ) |
---|
110 | { |
---|
111 | return RTEMS_SUCCESSFUL; |
---|
112 | } |
---|
113 | |
---|
114 | void Set_find_average_overhead( |
---|
115 | rtems_boolean find_flag |
---|
116 | ) |
---|
117 | { |
---|
118 | Timer_driver_Find_average_overhead = find_flag; |
---|
119 | } |
---|
120 | |
---|