查看缓存的工具--缓存命中率--valgrind,perf

本文介绍如何使用valgrind和perf工具分析程序的缓存命中率,包括指令缓存、数据缓存和三级缓存的命中情况,以及如何通过这些数据判断程序性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看缓存命中率
运用:
有两个程序,需要比较哪个程序更好?就需要比较程序的命中率,命中率高的就选用
[root@localhost ~]# yum -y install valgrind
[root@localhost ~]# valgrind --tool=cachegrind uname #uname为程序名

==4948== Cachegrind, a cache and branch-prediction profiler
==4948== Copyright (C) 2002-2017, and GNU GPL'd, by Nicholas Nethercote et al.
==4948== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==4948== Command: uname
==4948== 
--4948-- warning: L3 cache found, using its data for the LL simulation.
Linux
==4948== 
==4948== I   refs:      202,155#指令缓存
==4948== I1  misses:      1,043
==4948== LLi misses:      1,036
==4948== I1  miss rate:    0.52%
==4948== LLi miss rate:    0.51%
==4948== 
==4948== D   refs:       71,002  (52,073 rd   + 18,929 wr)#数据缓存
==4948== D1  misses:      3,354  ( 2,685 rd   +    669 wr)
==4948== LLd misses:      2,700  ( 2,083 rd   +    617 wr)
==4948== D1  miss rate:     4.7% (   5.2%     +    3.5%  )
==4948== LLd miss rate:     3.8% (   4.0%     +    3.3%  )
==4948== 
==4948== LL refs:         4,397  ( 3,728 rd   +    669 wr) #三级缓存
==4948== LL misses:       3,736  ( 3,119 rd   +    617 wr)
==4948== LL miss rate:      1.4% (   1.2%     +    3.3%  )

缓存被表示为 L1(一级),L2(二级),LL(最后一级)
I refs:需要读取的指令数,I1 misses: 缓存未命中数,LLi 缓存未命中数,I1 miss rate:未命中率,LLi miss rate:未命中率
D refs:需要读写取的数据数量,D1 misses:缓存未命中数,LLd misses:缓存未命中数… …
另外一个工具:perf
[root@localhost ~]# yum -y install perf

[root@localhost ~]# perf stat -e cache-misses uname
Linux

 Performance counter stats for 'uname':

             4,108      cache-misses                                                

       0.000890324 seconds time elapsed

主要用valgrind

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值