首页 > 其他分享 >随笔-perf off-cpu 原理

随笔-perf off-cpu 原理

时间:2024-03-08 09:25:03浏览次数:21  
标签:stat sched perf switch sleep off cpu rq

off-cpu:

view
# test in centos7

echo 1 | sudo tee /proc/sys/kernel/sched_schedstats
perf record -e sched:sched_stat_sleep -e sched:sched_switch -e sched:sched_process_exit -p $(cat /var/run/cs.pid) -g -o perf.data.raw sleep 10
perf inject -v -s -i perf.data.raw -o perf.data # The step is merging sched_start and sched_switch events

perf script -F comm,pid,tid,cpu,time,period,event,ip,sym,dso,trace | awk '
    NF > 4 { exec = $1; period_ms = int($5 / 1000000) }
    NF > 1 && NF <= 4 && period_ms > 0 { print $2 }
    NF < 2 && period_ms > 0 { printf "%s\n%d\n\n", exec, period_ms }' | \
    /opt/FlameGraph/stackcollapse.pl | \
    /opt/FlameGraph/flamegraph.pl --countname=ms --title="Off-CPU Time Flame Graph" --colors=io > offcpu.svg

原理:

1、Application stack traces don't change while off-CPU.
This means we only need to measure the stack trace once, either at the beginning or end of the off-CPU period. The end is usually easier, since you're recording the time interval then anyway. Here is tracing pseudocode for measuring off-CPU time with stack traces:

on context switch finish:
    sleeptime[prev_thread_id] = timestamp
    if !sleeptime[thread_id]
        return
    delta = timestamp - sleeptime[thread_id]
    totaltime[pid, execname, user stack, kernel stack] += delta
    sleeptime[thread_id] = 0

on tracer exit:
    for each key in totaltime:
        print key
        print totaltime[key]

Q: sched_switch事件似乎已经够用了,为什么还要sched_stat_sleep呢? 因为sched_switch虽然能知道花了多少时间,但是并不清楚时间都花在哪里,而且时间包含了involuntary context switches,而我们感兴趣的由于block event而发生的voluntary context switches。怎么获取? -> sched_stat_sleep, 因为block event发生时,程序会相应的进入TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE状态,获取伪代码如下

update_stats_dequeue
    if (tsk->state & TASK_INTERRUPTIBLE)
        __schedstat_set(se->statistics.sleep_start, rq_clock(rq_of(cfs_rq)));
    if (tsk->state & TASK_UNINTERRUPTIBLE)
        __schedstat_set(se->statistics.block_start, rq_clock(rq_of(cfs_rq)));

update_stats_enqueue
    update_stats_enqueue_sleeper(cfs_rq, se); if (flags & ENQUEUE_WAKEUP)
        if (sleep_start)
            trace_sched_stat_sleep(tsk, delta);
        if (block_start)
            trace_sched_stat_iowait(tsk, delta); if (tsk->in_iowait)
            trace_sched_stat_blocked(tsk, delta);

综上:

perf inject:

通过关联以下2个tracepoint, 我们可以得到任务sleep的时长及其对应的callchain

  • sched_iowait/sleep/blocked. 获得sleep的时长
  • sched_switch. 获得调用栈
commit 26a031e136f4f8dc82c64df48cca0eb3b5d3eb4f
Author: Andrew Vagin <[email protected]>
Date:   Tue Aug 7 16:56:04 2012 +0400

    perf inject: Merge sched_stat_* and sched_switch events

    You may want to know where and how long a task is sleeping. A callchain
    may be found in sched_switch and a time slice in stat_iowait, so I add
    handler in perf inject for merging this events.

    My code saves sched_switch event for each process and when it meets
    stat_iowait, it reports the sched_switch event, because this event
    contains a correct callchain. By another words it replaces all
    stat_iowait events on proper sched_switch events.

参考:

  1. Linux perf_events Off-CPU Time Flame Graph
  2. https://perf.wiki.kernel.org/index.php/Tutorial
  3. PingCAP 工欲性能调优,必先利其器(2)- 火焰图
  4. 透过Tracepoint理解内核 - 调度器框架和性能 - J.FW的文章 - 知乎

标签:stat,sched,perf,switch,sleep,off,cpu,rq
From: https://www.cnblogs.com/stellar-liyanbin/p/17834526.html

相关文章

  • Docker之CPU和内存磁盘设置(八)
    前言Docker基本掌握使用方式,但是在限制其资源方面还没有接触。本篇就简单介绍一下有关CPU、内存等配置。让每个容器的资源分配更加合理,也能够避免一些容器遇到内存泄漏,CPU过载等问题影响其他容器的正常运行。一、配置文件Docker中,配置文件可以用来调整守护进程的行为,包......
  • CPU可视化:开启计算机智慧之源的新篇章
    在数字化飞速发展的今天,中央处理器(CPU)作为计算机的心脏,其复杂度和重要性不言而喻。 中央处理器,这个小小的芯片,却承载着计算机运行的所有指令和数据处理任务。它的内部构造复杂而精密,每一个晶体管、每一个电路都像是精心编织的密码,共同构成了计算机的智慧之源。 然而对于大多......
  • scalene python cpu&gpu 性能分析工具
    scalene使用一个pythoncpu&gpu性能分析工具,同时也支持内存的分析,同时还提供了基于ai的智能优化推荐包含的一些特性cli支持多种输出格式包含了一个web-gui基于ai的智能提示参考使用安装pipinstallscalene包含的cliusage:scalene......
  • Office办公软件官方下载2021最新版
    Office2021是Microsoft推出的一套办公软件套件,包含Word、Excel、PowerPoint、Outlook等常用应用程序。与Office2019相比,Office2021在以下方面进行了改进:新增功能:实时协作:支持多人同时编辑同一个文档、工作表或演示文稿,并能看到彼此的更改。改进的搜索和导航:......
  • 苹果电脑最新Office 2021 Mac 下载安装
    Office2021Mac版是微软公司为macOS操作系统推出的办公软件套装,包含Word、Excel、PowerPoint、Outlook等常用办公应用程序。主要特点:经典的Office体验:熟悉的界面和功能,让您可以轻松上手。强大的功能:满足您的各种办公需求,例如文档编辑、数据分析、演示文稿制作、......
  • Office 2021 专业增强版下载密钥
    MicrosoftOffice2021专业增强版是一套功能强大的办公软件套装,适用于个人和企业用户。它包含以下应用程序:Word:用于创建、编辑和格式化文本文档Excel:用于创建、编辑和分析电子表格PowerPoint:用于创建、编辑和演示演示文稿Outlook:用于管理电子邮件、日历、联系人和其他任务......
  • P1894 [USACO4.2] 完美的牛栏The Perfect Stall
    原题链接题解二分图匹配,总的来说就是如果我的位子没人霸占,那我就坐,如果没人霸占,那我尝试着让他滚蛋如果一个位子经历过两次滚蛋,说明别人确实没位子坐了,人家确实需要这个位子,那我就换一个位子code#include<bits/stdc++.h>usingnamespacestd;intbelong[205]={0};intvis[......
  • R语言质量控制图、质量管理研究分析采购订单数量、CPU时间、纸厂产出、钢板数据可视化
    全文链接:https://tecdat.cn/?p=35288原文出处:拓端数据部落公众号在当今信息时代,数据的收集和分析变得至关重要,特别是在质量管理和生产过程控制方面。控制图作为一种统计工具,广泛应用于监控过程变化、识别异常和改进生产效率。本文针对SAS启动时间、纸厂产出、钢板、采购订单数量......
  • Ubuntu22.04中的LibreOffice如何设置中文
    LibreOffice版本Version:7.3.7.2/LibreOfficeCommunityBuildID:30(Build:2)设置方法打开LibreOffice,进入软件的操作界面中。在工具栏中点击Tools,然后在下拉选项中找到Options进入到设置的界面。在Options的页面中,找到Language的选项。这个选项就是语言设置。......
  • pageoffice6动态生成Excel文件
    转载:动态生成Excel文件#动态生成Excel文件查看本示例演示效果本示例关键代码的编写位置Vue+Springboot注意本文中展示的代码均为关键代码,复制粘贴到您的项目中,按照实际的情况,例如文档路径,用户名等做适当修改即可使用。使用com.zhuozhengsoft.pageoffice.excelwriter命名空......