1 | /* |
---|
2 | * Copyright (c) 2014, 2018 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Dornierstr. 4 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | |
---|
16 | #ifdef HAVE_CONFIG_H |
---|
17 | #include "config.h" |
---|
18 | #endif |
---|
19 | |
---|
20 | #include <rtems/test-info.h> |
---|
21 | #include <rtems.h> |
---|
22 | #include <rtems/score/threadimpl.h> |
---|
23 | |
---|
24 | static uint_fast32_t estimate_busy_loop_maximum( void ) |
---|
25 | { |
---|
26 | uint_fast32_t units = 0; |
---|
27 | uint_fast32_t initial = rtems_clock_get_ticks_since_boot(); |
---|
28 | |
---|
29 | while ( initial == rtems_clock_get_ticks_since_boot() ) { |
---|
30 | ++units; |
---|
31 | } |
---|
32 | |
---|
33 | return units; |
---|
34 | } |
---|
35 | |
---|
36 | static uint_fast32_t wait_for_tick_change( void ) |
---|
37 | { |
---|
38 | uint_fast32_t initial = rtems_clock_get_ticks_since_boot(); |
---|
39 | uint_fast32_t now; |
---|
40 | |
---|
41 | do { |
---|
42 | now = rtems_clock_get_ticks_since_boot(); |
---|
43 | } while ( now == initial ); |
---|
44 | |
---|
45 | return now; |
---|
46 | } |
---|
47 | |
---|
48 | /* |
---|
49 | * It is important that we use actually use the same rtems_test_busy() function |
---|
50 | * at the various places, since otherwise the obtained maximum value might be |
---|
51 | * wrong. So, the compiler must not inline this function. |
---|
52 | */ |
---|
53 | RTEMS_NO_INLINE void rtems_test_busy( uint_fast32_t count ) |
---|
54 | { |
---|
55 | uint_fast32_t i = 0; |
---|
56 | |
---|
57 | do { |
---|
58 | __asm__ volatile (""); |
---|
59 | ++i; |
---|
60 | } while ( i < count ); |
---|
61 | } |
---|
62 | |
---|
63 | uint_fast32_t rtems_test_get_one_tick_busy_count( void ) |
---|
64 | { |
---|
65 | uint_fast32_t last; |
---|
66 | uint_fast32_t now; |
---|
67 | uint_fast32_t a; |
---|
68 | uint_fast32_t b; |
---|
69 | uint_fast32_t m; |
---|
70 | |
---|
71 | /* Choose a lower bound */ |
---|
72 | a = 1; |
---|
73 | |
---|
74 | /* Estimate an upper bound */ |
---|
75 | |
---|
76 | wait_for_tick_change(); |
---|
77 | b = 2 * estimate_busy_loop_maximum(); |
---|
78 | |
---|
79 | while ( true ) { |
---|
80 | last = wait_for_tick_change(); |
---|
81 | rtems_test_busy( b ); |
---|
82 | now = rtems_clock_get_ticks_since_boot(); |
---|
83 | |
---|
84 | if ( now != last ) { |
---|
85 | break; |
---|
86 | } |
---|
87 | |
---|
88 | b *= 2; |
---|
89 | last = now; |
---|
90 | } |
---|
91 | |
---|
92 | /* Find a good value */ |
---|
93 | do { |
---|
94 | m = ( a + b ) / 2; |
---|
95 | |
---|
96 | last = wait_for_tick_change(); |
---|
97 | rtems_test_busy( m ); |
---|
98 | now = rtems_clock_get_ticks_since_boot(); |
---|
99 | |
---|
100 | if ( now != last ) { |
---|
101 | b = m; |
---|
102 | } else { |
---|
103 | a = m; |
---|
104 | } |
---|
105 | } while ( b - a > 1 ); |
---|
106 | |
---|
107 | return m; |
---|
108 | } |
---|
109 | |
---|
110 | void rtems_test_busy_cpu_usage( time_t seconds, long nanoseconds ) |
---|
111 | { |
---|
112 | Thread_Control *executing; |
---|
113 | Timestamp_Control busy; |
---|
114 | Timestamp_Control start; |
---|
115 | Timestamp_Control now; |
---|
116 | |
---|
117 | executing = _Thread_Get_executing(); |
---|
118 | _Thread_Get_CPU_time_used( executing, &start ); |
---|
119 | _Timestamp_Set( &busy, seconds, nanoseconds ); |
---|
120 | |
---|
121 | do { |
---|
122 | _Thread_Get_CPU_time_used( executing, &now ); |
---|
123 | } while ( now - start < busy ); |
---|
124 | } |
---|