如何获取多线程嵌入式应用程序中发生的上下文切换的详细信息?

发布于 2025-01-15 15:11:54 字数 188 浏览 0 评论 0原文

我正在尝试在嵌入式设备上分析 C++ 应用程序。使用 Vtune,我发现该应用程序正在启动数百个线程,其中大多数线程仅在总时间的一小部分内处于活动状态。

我想获取正在发生的上下文切换的详细信息(最好是在某种时间线视图中)。我还没有遇到一个可以显示接触开关信息的工具。是否有某种分析器可以提供此功能?或者通过其他方式获取此信息?

谢谢。

I am trying to profile a C++ application on an embedded device. Using Vtune, I found out that the app is launching hundreds of threads, among which most are active for only small percentage of the total time.

I want to get a details of the context switches that are happening (preferably in some kind of a timeline view). I have yet to come across a tool that can show the contact switch information. Is there some kind of profiler that provides this? Or some other way to get this info?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

任性一次 2025-01-22 15:11:55

在 Linux 上,您可以使用 cat /proc/{PID}/status 来获取有关线程、volunteer_ctxt_switches 和 nonvolunter_ctxt_switches 的一些信息

,例如,

uname@hostname:/$ cat /proc/1357/status
Name:   avahi-daemon
Umask:  0022
State:  S (sleeping)
Tgid:   1357
Ngid:   0
Pid:    1357
PPid:   1
TracerPid:  0
Uid:    107 107 107 107
Gid:    114 114 114 114
FDSize: 128
Groups: 114 
NStgid: 1357
NSpid:  1357
NSpgid: 1357
NSsid:  1357
VmPeak:    10500 kB
VmSize:     8288 kB
VmLck:         0 kB
VmPin:         0 kB
VmHWM:      3664 kB
VmRSS:      2700 kB
RssAnon:         328 kB
RssFile:        2372 kB
RssShmem:          0 kB
VmData:      468 kB
VmStk:       132 kB
VmExe:        92 kB
VmLib:      3720 kB
VmPTE:        52 kB
VmSwap:        0 kB
HugetlbPages:          0 kB
CoreDumping:    0
Threads:    1
SigQ:   0/31668
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000001000
SigCgt: 0000000180004203
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: 0000003fffffffff
CapAmb: 0000000000000000
NoNewPrivs: 0
Seccomp:    0
Speculation_Store_Bypass:   thread vulnerable
Cpus_allowed:   ffffffff,ffffffff,ffffffff,ffffffff
Cpus_allowed_list:  0-127
Mems_allowed:   00000000,00000001
Mems_allowed_list:  0
voluntary_ctxt_switches:    1610
nonvoluntary_ctxt_switches: 25

On Linux you could use cat /proc/{PID}/status, to get some information on threads, voluntary_ctxt_switches and nonvoluntary_ctxt_switches

for example,

uname@hostname:/$ cat /proc/1357/status
Name:   avahi-daemon
Umask:  0022
State:  S (sleeping)
Tgid:   1357
Ngid:   0
Pid:    1357
PPid:   1
TracerPid:  0
Uid:    107 107 107 107
Gid:    114 114 114 114
FDSize: 128
Groups: 114 
NStgid: 1357
NSpid:  1357
NSpgid: 1357
NSsid:  1357
VmPeak:    10500 kB
VmSize:     8288 kB
VmLck:         0 kB
VmPin:         0 kB
VmHWM:      3664 kB
VmRSS:      2700 kB
RssAnon:         328 kB
RssFile:        2372 kB
RssShmem:          0 kB
VmData:      468 kB
VmStk:       132 kB
VmExe:        92 kB
VmLib:      3720 kB
VmPTE:        52 kB
VmSwap:        0 kB
HugetlbPages:          0 kB
CoreDumping:    0
Threads:    1
SigQ:   0/31668
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000001000
SigCgt: 0000000180004203
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: 0000003fffffffff
CapAmb: 0000000000000000
NoNewPrivs: 0
Seccomp:    0
Speculation_Store_Bypass:   thread vulnerable
Cpus_allowed:   ffffffff,ffffffff,ffffffff,ffffffff
Cpus_allowed_list:  0-127
Mems_allowed:   00000000,00000001
Mems_allowed_list:  0
voluntary_ctxt_switches:    1610
nonvoluntary_ctxt_switches: 25

郁金香雨 2025-01-22 15:11:55

这个答案是针对 Linux 操作系统的。如果您指定您正在使用的操作系统,那就太好了,否则您可能会得到不需要的解决方案。

如果您有 Linux Perf 事件,则可以使用 perf timechart recordperf timechart 获取应用程序中上下文切换的可视时间线。如果记录的持续时间较长,则可能需要一段时间来处理结果。

如果您想知道程序的哪些部分是罪魁祸首,也许最好使用 perf record -e context-switch --call-graph XXX 来在发生上下文切换时对回溯进行采样。查看 perf 手册以了解命令行选项的更多详细信息。收集一些跟踪数据后,您可以使用性能报告将其可视化。我相信 Intel VTune 仍然能够打开 perf 跟踪,但您需要将文件从默认的 perf.data 重命名为以 .perf 扩展名结尾的文件名。

This answer is specific for the Linux OS. It would be good if you specify what OS you are using because otherwise you may get the solution you don't need.

If you have Linux Perf events, you can get a visual timeline of the context switches in your application using perf timechart record and perf timechart. If the duration of the record is large it may take a while to process the result.

If you want to know what parts of your program are the culprit, maybe it would be better to use perf record -e context-switch --call-graph XXX to sample the backtrace when a context switch happens. Look into the perf manual to see more details of the command line options. Once you collect some trace data, you can visualise it with perf report. I believe Intel VTune is still able to open perf traces, but you need to rename the files from the default perf.data to a file name ending with .perf extension.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文