当前位置: 首页 > news >正文

统计程序两个点之间执行的指令数量

环境:支持perf

ubuntu安装

apt-get install linux-tools-common linux-tools-generic linux-tools-`uname -r`

android

一般自带simpleperf

分析

两个点作差, 求中间结果;

*(int*)nullptr = 0;

案例

断点 1

代码

#define SETPOINT(...)  do { *(int*)nullptr = 0; } while(0)
int main() {SETPOINT(1);int a = 0;for(int i = 0 ; i < 500; i++) a+=i;return 0;
}

执行结果

ch@ch-ubuntu:~/ch/perf_test/exe_count$ sudo perf stat -e instructions:u ./a.out 
[sudo] password for ch: 
./a.out: Segmentation faultPerformance counter stats for './a.out':100064      instructions:u                                              0.111183917 seconds time elapsed0.001217000 seconds user0.000000000 seconds sysch@ch-ubuntu:~/ch/perf_test/exe_count$ sudo perf stat -e instructions:u ./a.out 
./a.out: Segmentation faultPerformance counter stats for './a.out':100064      instructions:u                                              0.104691299 seconds time elapsed0.000875000 seconds user0.000000000 seconds sysch@ch-ubuntu:~/ch/perf_test/exe_count$ sudo perf stat -e instructions:u ./a.out 
./a.out: Segmentation faultPerformance counter stats for './a.out':100064      instructions:u                                              0.111463860 seconds time elapsed0.000931000 seconds user0.000000000 seconds sys

断点 2

代码

#define SETPOINT(...)  do { *(int*)nullptr = 0; } while(0)
int main() {int a = 0;for(int i = 0 ; i < 500; i++) a+=i;SETPOINT(2);return 0;
}

执行结果

ch@ch-ubuntu:~/ch/perf_test/exe_count$ sudo perf stat -e instructions:u ./a.out 
./a.out: Segmentation faultPerformance counter stats for './a.out':102569      instructions:u                                              0.105533002 seconds time elapsed0.000904000 seconds user0.000000000 seconds sysch@ch-ubuntu:~/ch/perf_test/exe_count$ sudo perf stat -e instructions:u ./a.out 
./a.out: Segmentation faultPerformance counter stats for './a.out':102567      instructions:u                                              0.105150980 seconds time elapsed0.000876000 seconds user0.000000000 seconds sysch@ch-ubuntu:~/ch/perf_test/exe_count$ sudo perf stat -e instructions:u ./a.out 
./a.out: Segmentation faultPerformance counter stats for './a.out':102567      instructions:u                                              0.103408851 seconds time elapsed0.000897000 seconds user0.000000000 seconds sys

结果分析

102567 - 100064 == 2503

代码分析

ch@ch-ubuntu:~/ch/perf_test/exe_count$ objdump --disassemble=main ./a.out./a.out:     file format elf64-x86-64Disassembly of section .init:Disassembly of section .plt:Disassembly of section .plt.got:Disassembly of section .text:0000000000001129 <main>:1129:	f3 0f 1e fa          	endbr64112d:	55                   	push   %rbp112e:	48 89 e5             	mov    %rsp,%rbp1131:	c7 45 f8 00 00 00 00 	movl   $0x0,-0x8(%rbp)1138:	c7 45 fc 00 00 00 00 	movl   $0x0,-0x4(%rbp)113f:	eb 0a                	jmp    114b <main+0x22>1141:	8b 45 fc             	mov    -0x4(%rbp),%eax1144:	01 45 f8             	add    %eax,-0x8(%rbp)1147:	83 45 fc 01          	addl   $0x1,-0x4(%rbp)114b:	81 7d fc f3 01 00 00 	cmpl   $0x1f3,-0x4(%rbp)1152:	7e ed                	jle    1141 <main+0x18>1154:	b8 00 00 00 00       	mov    $0x0,%eax1159:	c7 00 00 00 00 00    	movl   $0x0,(%rax)115f:	b8 00 00 00 00       	mov    $0x0,%eax1164:	5d                   	pop    %rbp1165:	c3                   	ret

可以看到1141 - 1152一共有5条指令, 5 * 500差不多2500, perf统计有些许误差属于正常现象;

结论

可以两个点之间的执行共执行了2500条指令; 然后根据指令数量和执行环境推断理论执行性能;
不适用于和多种硬件打交道的环境; 而且不同的syscall耗时不一样;

http://www.lryc.cn/news/142450.html

相关文章:

  • 时序预测 | MATLAB实现基于TSO-XGBoost金枪鱼算法优化XGBoost的时间序列预测(多指标评价)
  • java- ConcurrentHashMap 并发
  • java练习8.100m小球落地
  • Android JNI系列详解之生成指定CPU的库文件
  • Leetcode每日一题:1448. 统计二叉树中好节点的数目
  • 华为OD七日集训第2期 - 按算法分类,由易到难,循序渐进,玩转OD(文末送书)
  • 3d max插件CG MAGIC中的蜂窝材质功能可提升效率吗?
  • 一句话木马攻击复现:揭示黑客入侵的实战过程
  • 【游戏开发教程】Unity Cinemachine快速上手,详细案例讲解(虚拟相机系统 | 新发出品 | 良心教程)
  • 当图像宽高为奇数时,如何计算 I420 格式的uv分量大小
  • 结构型模式-代理模式
  • SpringBoot+Redis BitMap 实现签到与统计功能
  • P5739 【深基7.例7】计算阶乘
  • scikit-learn中OneHotEncoder用法
  • linux操作系统的权限的深入学习(未完)
  • C 连接MySQL8
  • 福利之舞:员工的心跳与企业的平衡术
  • MyBatis动态语句且如何实现模糊查询及resultType与resultMap的区别---详细介绍
  • 麒麟OS国产系统身份证阅读器web网页开发使用操作流程
  • 前端面试:【事件处理】探索事件流、委托与事件对象
  • c语言函数指针使用例子
  • 云计算技术应用专业实训室建设方案
  • C语言学习之共用体(union)的运用
  • Sentinel 控制台(集群流控管理)
  • PMP P-08 Communication Management
  • matlab中判断数据的奇偶性(mod函数、rem函数)
  • Redis使用
  • #systemverilog# 之 event region 和 timeslot 仿真调度(七)Active/NBA 咋跳转的?
  • 回归预测 | MATLAB实现SSA-ELM麻雀搜索算法优化极限学习机多输入单输出回归预测(多指标,多图)
  • LION AI 大模型落地,首搭星纪元 ES