简体   繁体   English

如何获取在Linux上已执行程序的内核数和CPU负载

[英]How to get number of cores a program has executed on in Linux and CPU load

I need to find out how many CPU cores a given process has executed on during a certain time eg 1 second or so? 我需要找出给定进程在一定时间(例如1秒左右)内执行了多少个CPU内核? This has to be done from a C program (or possibly C++). 这必须通过C程序(或可能的C ++)完成。 I know that perf presents the number of cpu-migrations . 我知道perf表示cpu-migrations的数量。

I need to know the number of CPU core swaps and also which cores the process has used. 我需要知道CPU核心交换的数量,以及进程使用的核心。 I assume that I first have to get which threads the process executes in and then to see what cores the threads run on. 我假设我首先必须获取进程在哪个线程中执行,然后查看线程在哪些内核上运行。

The information I get is to be used to calculate the CPU load for a process. 我获得的信息将用于计算进程的CPU负载。 When running in only one CPU core I know how to do and have a working solution. 当仅在一个CPU内核中运行时,我知道该怎么做并拥有有效的解决方案。 However, handling several CPU cores that a process consisting of several threads that run in one or several CPU cores at different times makes it harder. 但是,处理多个CPU内核会使一个进程由多个线程组成,这些进程在不同的时间在一个或多个CPU内核中运行,这使它变得更加困难。 Is there any solution or hint to solution anyone here could point at? 这里有没有人可以指出的解决方案或提示?

I don't have a definitive answer, but here is what I found. 我没有确切的答案,但这是我所发现的。 I use stress -c 1 -t 1 (runs a sqrt+loop process during one second). 我使用了压力-c 1 -t 1(在一秒钟内运行sqrt + loop过程)。

I think this answers your very first question (find out how many migrations happen during the execution of your process). 我认为这回答了您的第一个问题(找出在执行过程中发生了多少次迁移)。

$ perf stat -e migrations stress -c 1 -t 1
stress: info: [4159] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
stress: info: [4159] successful run completed in 1s
 Performance counter stats for 'stress -c 1 -t 1':
                 2      migrations

Next I found an interesting tracepoint: 接下来,我找到了一个有趣的跟踪点:

perf stat -e sched:sched_migrate_task stress -c 1 -t 1

and it happens that sched_migrate_task has a dest_cpu argument which I don't know how to trace. 碰巧sched_migrate_task有一个dest_cpu参数 ,我不知道该如何跟踪。

Try out the tool kernelshark . 试用工具kernelshark。 It provides better visual representation of multi-core system, affinity and many more features that can help in information gathering. 它提供了更好的多核系统可视化表示,亲和力以及许多有助于收集信息的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM