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

linux perf 环境部署和基本测试(基于Ubuntu20.04)

1,linux 安装perf

sudo apt-ge install linux-tools-common

sudo apt-get install linux-tools-$(uname -r) linux-tools-generic -y

2 补充安装

sudo apt-get  install python3-q-text-as-data 

3,perf常用命令

lark@ubuntu:~$ perf

 usage: perf [--version] [--help] [OPTIONS] COMMAND [ARGS]

 The most commonly used perf commands are:
   annotate        Read perf.data (created by perf record) and display annotated code
   archive         Create archive with object files with build-ids found in perf.data file
   bench           General framework for benchmark suites
   buildid-cache   Manage build-id cache.
   buildid-list    List the buildids in a perf.data file
   c2c             Shared Data C2C/HITM Analyzer.
   config          Get and set variables in a configuration file.
   daemon          Run record sessions on background
   data            Data file related processing
   diff            Read perf.data files and display the differential profile
   evlist          List the event names in a perf.data file
   ftrace          simple wrapper for kernel's ftrace functionality
   inject          Filter to augment the events stream with additional information
   iostat          Show I/O performance metrics
   kallsyms        Searches running kernel for symbols
   kmem            Tool to trace/measure kernel memory properties
   kvm             Tool to trace/measure kvm guest os
   list            List all symbolic event types
   lock            Analyze lock events
   mem             Profile memory accesses
   record          Run a command and record its profile into perf.data
   report          Read perf.data (created by perf record) and display the profile
   sched           Tool to trace/measure scheduler properties (latencies)
   script          Read perf.data (created by perf record) and display trace output
   stat            Run a command and gather performance counter statistics
   test            Runs sanity tests.
   timechart       Tool to visualize total system behavior during a workload
   top             System profiling tool.
   version         display the version of perf binary
   probe           Define new dynamic tracepoints
   trace           strace inspired tool

 See 'perf help COMMAND' for more information on a specific command.

举例代码main.c

#include <stdio.h>
#include <stdlib.h>void long_test()
{int i, j;while(1){;}
}void foo2()
{int i;for (i = 0; i < 100; i++) long_test();
}void foo1()
{int i;for (i = 0; i < 1000; i++) long_test();
}int main(void) {foo1();foo2();
}

编译:gcc main.c -o main

运行 :./main

4, perf top命令 实时查看进程CPU和调用堆栈

 lark@ubuntu:~$ sudo perf top -a

5,构建火焰图

下载库安装

git clone https://github.com/brendangregg/FlameGraph.git

 执行文件生成火焰图

ps -axf | grep main
sudo perf record -p 2915  -g -- sleep 10 //采样10s//采样的数据画成火焰图
sudo perf script -i perf.data &> perf.unfold
sudo ./FlameGraph/stackcollapse-perf.pl perf.unfold &> perf.folded
sudo ./FlameGraph/flamegraph.pl perf.folded > perf.svg

google浏览器打开 火焰图。

perf record 命令可以统计每个调用栈出现的百分比

lark@ubuntu:~/test$ sudo perf report -n --stdio
# To display the perf.data header info, please use --header/--header-only options.
#
#
# Total Lost Samples: 0
#
# Samples: 20K of event 'cpu-clock:pppH'
# Event count (approx.): 5069000000
#
# Children      Self       Samples  Command  Shared Object      Symbol                             
# ........  ........  ............  .......  .................  ...................................
#
   100.00%     0.00%             0  main     libc-2.31.so       [.] __libc_start_main
            |
            ---__libc_start_main
               main
               foo1
               long_test

   100.00%     0.00%             0  main     main               [.] main
            |
            ---main
               foo1
 

上面可以看到main->foo1的栈占用率 100%。

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

相关文章:

  • 【网络面试篇】HTTP(1)(笔记)——状态码、字段、GET、POST、缓存
  • HTML 基础标签——分组标签 <div>、<span> 和基础语义容器
  • SS928V100 ISP常见问题列表
  • AI写诗:自动版大唐宫体诗
  • Java复习31(PTA)
  • 【Linux系列】Linux 系统中的软连接管理
  • @layer(级联层)
  • nginx代理websocket服务
  • 第二十七章 Vue异步更新之$nextTick
  • 【51 Pandas+Pyecharts | 深圳市共享单车数据分析可视化】
  • 【Clikhouse 探秘】ClickHouse 物化视图:加速大数据分析的新利器
  • 线程相关题(线程池、线程使用、核心线程数的设置)
  • 2181、合并零之间的节点
  • powerlaw:用于分析幂律分布的Python库
  • 工作管理实战指南:利用Jira、Confluence等Atlassian工具打破信息孤岛,增强团队协作【含免费指南】
  • JAVA语言多态和动态语言实现原理
  • 阿里云-防火墙设置不当导致ssh无法连接
  • 使用WebAssembly优化Web应用性能
  • 软件测试模型
  • 动态规划——两个数组的dp问题
  • 视频QoE测量学习笔记(二)
  • RSA算法详解:原理与应用
  • YOLOv6-4.0部分代码阅读笔记-effidehead_fuseab.py
  • 特朗普概念股DJT股票分析:为美国大选“黑天鹅事件”做好准备
  • 【MySQL】 运维篇—故障排除与性能调优:常见故障的排查与解决
  • Android R S T U版本如何在下拉栏菜单增加基本截图功能
  • C#二叉树原理及二叉搜索树代码实现
  • .eslintrc.js 的解释
  • 确保企业架构与业务的一致性与合规性:数字化转型中的关键要素与战略实施
  • goframe开发一个企业网站 前端界面 拆分界面7