Skip to content

Commit 29ee9aa

Browse files
g2gpsxiaoxiang781216
authored andcommitted
arch/risc-v/common: provide architecture specific perfmon bindings.
Provides two implementations: - CSR_CYCLE: Cores which implement hardware performance monitoring. - CSR_TIME: Uses the machine time registers. Using the up_perf_xx bindings directory is more efficient than performing a nanosecond conversion on every gettime event.
1 parent fe4526c commit 29ee9aa

File tree

6 files changed

+177
-0
lines changed

6 files changed

+177
-0
lines changed

arch/risc-v/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ config LITEX_CORE_VEXRISCV_SMP
668668
select ARCH_NEED_ADDRENV_MAPPING
669669
select ARCH_HAVE_S_MODE
670670
select ARCH_HAVE_ELF_EXECUTABLE
671+
select ARCH_HAVE_PERF_EVENTS
671672

672673
endchoice # LITEX Core Selection
673674

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/****************************************************************************
2+
* arch/risc-v/src/common/riscv_perf_cycle.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this args for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <nuttx/clock.h>
26+
#include <nuttx/config.h>
27+
28+
#include <stdint.h>
29+
#include <time.h>
30+
31+
#include "riscv_internal.h"
32+
33+
#ifdef CONFIG_ARCH_PERF_EVENTS
34+
35+
/****************************************************************************
36+
* Private Data
37+
****************************************************************************/
38+
39+
static unsigned long g_cycle_frequency = ULONG_MAX;
40+
41+
/****************************************************************************
42+
* Public Functions
43+
****************************************************************************/
44+
45+
/****************************************************************************
46+
* Name: up_perf_init
47+
****************************************************************************/
48+
49+
void up_perf_init(void *arg)
50+
{
51+
g_cycle_frequency = (unsigned long)(uintptr_t)arg;
52+
}
53+
54+
/****************************************************************************
55+
* Name: up_perf_gettime
56+
****************************************************************************/
57+
58+
unsigned long up_perf_gettime(void)
59+
{
60+
return READ_CSR(CSR_CYCLE);
61+
}
62+
63+
/****************************************************************************
64+
* Name: up_perf_getfreq
65+
****************************************************************************/
66+
67+
unsigned long up_perf_getfreq(void)
68+
{
69+
return g_cycle_frequency;
70+
}
71+
72+
/****************************************************************************
73+
* Name: up_perf_convert
74+
****************************************************************************/
75+
76+
void up_perf_convert(unsigned long elapsed, struct timespec *ts)
77+
{
78+
DEBUGASSERT(g_cycle_frequency && (g_cycle_frequency != ULONG_MAX));
79+
80+
ts->tv_sec = elapsed / g_cycle_frequency;
81+
elapsed -= ts->tv_sec * g_cycle_frequency;
82+
ts->tv_nsec = elapsed * (NSEC_PER_SEC / g_cycle_frequency);
83+
}
84+
#endif
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/****************************************************************************
2+
* arch/risc-v/src/common/riscv_perf_time.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this args for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <nuttx/clock.h>
26+
#include <nuttx/config.h>
27+
28+
#include <stdint.h>
29+
#include <time.h>
30+
31+
#include "riscv_internal.h"
32+
33+
#ifdef CONFIG_ARCH_PERF_EVENTS
34+
35+
/****************************************************************************
36+
* Private Data
37+
****************************************************************************/
38+
39+
static unsigned long g_clock_frequency = ULONG_MAX;
40+
41+
/****************************************************************************
42+
* Public Functions
43+
****************************************************************************/
44+
45+
/****************************************************************************
46+
* Name: up_perf_init
47+
****************************************************************************/
48+
49+
void up_perf_init(void *arg)
50+
{
51+
g_clock_frequency = (unsigned long)(uintptr_t)arg;
52+
}
53+
54+
/****************************************************************************
55+
* Name: up_perf_gettime
56+
****************************************************************************/
57+
58+
unsigned long up_perf_gettime(void)
59+
{
60+
return READ_CSR(CSR_TIME);
61+
}
62+
63+
/****************************************************************************
64+
* Name: up_perf_getfreq
65+
****************************************************************************/
66+
67+
unsigned long up_perf_getfreq(void)
68+
{
69+
return g_clock_frequency;
70+
}
71+
72+
/****************************************************************************
73+
* Name: up_perf_convert
74+
****************************************************************************/
75+
76+
void up_perf_convert(unsigned long elapsed, struct timespec *ts)
77+
{
78+
DEBUGASSERT(g_clock_frequency && (g_clock_frequency != ULONG_MAX));
79+
80+
ts->tv_sec = elapsed / g_clock_frequency;
81+
elapsed -= ts->tv_sec * g_clock_frequency;
82+
ts->tv_nsec = elapsed * (NSEC_PER_SEC / g_clock_frequency);
83+
}
84+
#endif

arch/risc-v/src/litex/Make.defs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ CHIP_ASRCS += litex_cache.S
4040
ifeq ($(CONFIG_SCHED_TICKLESS),y)
4141
ifeq ($(CONFIG_SCHED_TICKLESS_ALARM),y)
4242
CHIP_CSRCS += litex_arch_alarm.c
43+
CHIP_CSRCS += riscv_perf_time.c
4344
else
4445
CHIP_CSRCS += litex_tickless.c
4546
endif

arch/risc-v/src/litex/litex_arch_alarm.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,9 @@ void up_timer_initialize(void)
5252

5353
DEBUGASSERT(lower);
5454

55+
#ifdef CONFIG_ARCH_HAVE_PERF_EVENTS
56+
up_perf_init((void *)CONFIG_LITEX_SYS_CORE_FREQ_HZ);
57+
#endif
58+
5559
up_alarm_set_lowerhalf(lower);
5660
}

boards/risc-v/litex/arty_a7/configs/knsh-tickless/defconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ CONFIG_NSH_FILE_APPS=y
6464
CONFIG_NSH_READLINE=y
6565
CONFIG_ONESHOT=y
6666
CONFIG_PATH_INITIAL="/system/bin"
67+
CONFIG_PERF_OVERFLOW_CORRECTION=y
6768
CONFIG_RAM_SIZE=4194304
6869
CONFIG_RAM_START=0x40400000
6970
CONFIG_RAW_BINARY=y
7071
CONFIG_READLINE_CMD_HISTORY=y
7172
CONFIG_RR_INTERVAL=200
7273
CONFIG_SCHED_HAVE_PARENT=y
74+
CONFIG_SCHED_IRQMONITOR=y
7375
CONFIG_SCHED_LPWORK=y
7476
CONFIG_SCHED_TICKLESS=y
7577
CONFIG_SCHED_WAITPID=y
@@ -81,6 +83,7 @@ CONFIG_SYSLOG_TIMESTAMP=y
8183
CONFIG_SYSTEM_CLE=y
8284
CONFIG_SYSTEM_NSH=y
8385
CONFIG_SYSTEM_NSH_PROGNAME="init"
86+
CONFIG_SYSTEM_TIME64=y
8487
CONFIG_TESTING_GETPRIME=y
8588
CONFIG_UART0_RXBUFSIZE=128
8689
CONFIG_UART0_SERIAL_CONSOLE=y

0 commit comments

Comments
 (0)