1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| @ 内存访问性能监控 .section .data access_counter: .word 0 cache_miss_counter: .word 0 .section .text .global memory_performance_monitor
memory_performance_monitor: @ 启用性能监控单元(PMU) @ 使能用户模式访问PMU mrc p15, 0, r0, c9, c14, 0 @ PMUSERENR orr r0, r0, #1 mcr p15, 0, r0, c9, c14, 0 @ 使能性能计数器 mrc p15, 0, r0, c9, c12, 0 @ PMCR orr r0, r0, #1 @ 使能所有计数器 orr r0, r0, #2 @ 重置所有计数器 mcr p15, 0, r0, c9, c12, 0 @ 配置事件计数器0:L1数据缓存缺失 mov r0, #0 mcr p15, 0, r0, c9, c12, 5 @ 选择计数器0 mov r0, #0x03 @ L1数据缓存缺失事件 mcr p15, 0, r0, c9, c13, 1 @ 设置事件类型 @ 配置事件计数器1:内存访问 mov r0, #1 mcr p15, 0, r0, c9, c12, 5 mov r0, #0x19 @ 内存访问事件 mcr p15, 0, r0, c9, c13, 1 @ 使能计数器 mov r0, #0x80000003 @ 使能周期计数器和事件计数器0,1 mcr p15, 0, r0, c9, c12, 1 bx lr
@ 读取性能计数器 .global read_performance_counters
read_performance_counters: @ 读取周期计数器 mrc p15, 0, r0, c9, c13, 0 @ PMCCNTR @ 读取事件计数器0(缓存缺失) mov r1, #0 mcr p15, 0, r1, c9, c12, 5 mrc p15, 0, r1, c9, c13, 2 @ 读取事件计数器1(内存访问) mov r2, #1 mcr p15, 0, r2, c9, c12, 5 mrc p15, 0, r2, c9, c13, 2 @ 计算缓存缺失率 @ miss_rate = cache_misses / memory_accesses cmp r2, #0 beq no_accesses @ 简单的百分比计算(miss_rate * 100) mov r3, #100 mul r1, r1, r3 udiv r3, r1, r2 @ 缓存缺失率(百分比) no_accesses: bx lr
|