简体   繁体   English

如何知道进程正在使用哪个处理器?

[英]How to know which processor is being used by a process?

In a multithreaded environment, I want to exactly know which processor is being used by my process.在多线程环境中,我想确切地知道我的进程正在使用哪个处理器。

I looked into the source code of top and htop.我查看了top和htop的源代码。 The problem with the top/htop is that it gets the total time (from /proc/stat and /proc/[pid]/stat ) and divides it by the number of CPUs irrespective of the load that is distributed among the other processors. top/htop 的问题在于它获取总时间(来自/proc/stat/proc/[pid]/stat )并将其除以 CPU 的数量,而不管在其他处理器之间分配的负载如何。

If the total %CPU is 350% theoretically we know at least 4 processors are being used.如果理论上总 %CPU 为 350%,我们知道至少使用了 4 个处理器。 The top is generalizing this and doesn't "accurately" quantify the per processor usage.上面是概括这一点,并没有“准确地”量化每个处理器的使用情况。

350% can imply in different ways: 350% 可以以不同的方式暗示:

Processor 1: 100%
Processor 2: 75%
Processor 3: 75%
Processor 4: 100%

Processor 1: 75%
Processor 2: 100%
Processor 3: 100%
Processor 4: 75%

The /proc/[pid]/stat gives the last processor used but doesn't which processors are being used by a process. /proc/[pid]/stat给出了最后使用的处理器,但没有给出进程正在使用的处理器。

Any idea how to accurately capture which processor is being by the process?知道如何准确捕获进程正在使用的处理器吗?

My end goal is to find out how efficiently my algorithm is using the machine.我的最终目标是找出我的算法使用机器的效率。

As said in the comments of your question, there's no point in asking the system where your process is executed as the scheduler can change it before you even get the reply.正如您在问题的评论中所说,询问系统您的进程在哪里执行是没有意义的,因为调度程序可以在您得到答复之前对其进行更改。

In a Posix environment you can use pthread_setaffinity_np() to force your code to only execute on certain threads/cores although I'm not sure that will be too helpful to you find out the efficiency of your algorithm.在 Posix 环境中,您可以使用pthread_setaffinity_np()强制您的代码仅在某些线程/内核上执行,尽管我不确定这对您了解算法的效率是否有帮助。

When I want to profile a multi-threaded algorithm like that I'd probably use something like gnome-system-monitor or the KDE system monitor (gotop would work too) to print out CPU usage graphs so I can compare the system load before and after starting my program, it can at least help find out if there's 1 thread completely left alone, not very granular but for the first pass it's enough.当我想分析这样的多线程算法时,我可能会使用类似 gnome-system-monitor 或 KDE 系统监视器(gotop 也可以)来打印 CPU 使用率图,以便我可以比较之前的系统负载和启动我的程序后,它至少可以帮助确定是否有 1 个线程完全独​​立,不是很细化,但对于第一遍就足够了。

Once I'm too close to 100%/the program is too fast to see whether I'm using all available resources or not, I'd usually do something like一旦我太接近 100%/程序太快而无法查看我是否正在使用所有可用资源,我通常会做类似的事情

auto timeBefore = std::chrono::high_resolution_clock::now();
my_algo();
auto timeAfter = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> completeTime = timeAfter-timeBefore;
std::cout << "Took: " << completeTime.count() << "s" << std::endl;

so I can do some benchmarking cause after all, execution time is usually more important than having all cores pinned to 100%所以我可以做一些基准测试,因为执行时间通常比将所有内核固定到 100% 更重要

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

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