Skip to content

Commit 93c8e92

Browse files
andreaskurthGregAC
authored andcommitted
[sw/simple_system] Declare pcount_enable function as inline
The `pcount_enable` function can be used to selectively enable performance counters only for a specific code segment. For example: ```c pcount_enable(0); pcount_reset(); pcount_enable(1); /* code to be measured */ pcount_enable(0); ``` The `pcount_enable` function consists of a single CSR instruction, so the overhead and thus the impact on the measurement is potentially low. When the function is called, however, many instructions have to be executed in addition to the single CSR instruction, which influences measurements. This commit moves the `pcount_enable` function to the `simple_system_common.h` header file and declares it as `inline` (and `static`, to prevent link-time collisions when the header file is included in multiple compilation units). This helps the compiler inline the function even without LTO. Signed-off-by: Andreas Kurth <[email protected]>
1 parent 2c8ee8c commit 93c8e92

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

examples/sw/simple_system/common/simple_system_common.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,6 @@ void pcount_reset() {
102102
"csrw mhpmcounter31h, x0\n");
103103
}
104104

105-
void pcount_enable(int enable) {
106-
// Note cycle is disabled with everything else
107-
unsigned int inhibit_val = enable ? 0x0 : 0xFFFFFFFF;
108-
// CSR 0x320 was called `mucounteren` in the privileged spec v1.9.1, it was
109-
// then dropped in v1.10, and then re-added in v1.11 with the name
110-
// `mcountinhibit`. Unfortunately, the version of binutils we use only allows
111-
// the old name, and LLVM only supports the new name (though this is changed
112-
// on trunk to support both), so we use the numeric value here for maximum
113-
// compatibility.
114-
asm volatile("csrw 0x320, %0\n" : : "r"(inhibit_val));
115-
}
116-
117105
unsigned int get_mepc() {
118106
uint32_t result;
119107
__asm__ volatile("csrr %0, mepc;" : "=r"(result));

examples/sw/simple_system/common/simple_system_common.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,17 @@ void sim_halt();
4848
*
4949
* @param enable if non-zero enables, otherwise disables
5050
*/
51-
void pcount_enable(int enable);
51+
static inline void pcount_enable(int enable) {
52+
// Note cycle is disabled with everything else
53+
unsigned int inhibit_val = enable ? 0x0 : 0xFFFFFFFF;
54+
// CSR 0x320 was called `mucounteren` in the privileged spec v1.9.1, it was
55+
// then dropped in v1.10, and then re-added in v1.11 with the name
56+
// `mcountinhibit`. Unfortunately, the version of binutils we use only allows
57+
// the old name, and LLVM only supports the new name (though this is changed
58+
// on trunk to support both), so we use the numeric value here for maximum
59+
// compatibility.
60+
asm volatile("csrw 0x320, %0\n" : : "r"(inhibit_val));
61+
}
5262

5363
/**
5464
* Resets all performance counters. This effects mcycle and minstret as well

0 commit comments

Comments
 (0)