iOS 和Mac 线程的 CPU 使用率
有谁知道是否可以获得应用程序中特定线程、进程或某些代码的 cpu 使用情况? 如果你看一下 AUGraph,它有一个返回平均 CPU 使用率的函数。他们是怎么做到的?
Does anyone know if it is possible to get the cpu usage for a specific thread, process, or some code in the application?
If you look at AUGraph it has a function which returns average cpu usage. How do they do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这是否也适用于 iOS,但对于 OS X,您可以通过访问 Mach/BSD 子系统来获取所需的信息。基本上,您可以获得给定进程的线程列表,然后对所有线程的使用情况求和以获得进程的 CPU 使用情况。如果只需要一个线程cpu使用情况,则不需要求和。
它并不像人们希望的那样简单和直接,但它就是这样(此代码基于 Amit Singh“Mac OS X 内部结构”):
我不知道是否有更好的方法来做到这一点,可能是这样,但这对我有用。
I am not sure that this also applies to iOS, but for OS X, you can get the info you need by accessing the Mach/BSD subsystem. Basically you get the list of threads for a given process, then sum all threads' usage to get the process cpu usage. If you need only a thread cpu usage, you don't need to sum up.
It is not as easy and straightforward as one might desire, but here it is (this code is based on Amit Singh "Mac OS X internals"):
I don't know if there is a better way to do that, possibly so, but this one has worked for me.
编辑:抱歉——误读了你的问题。这并不显示应用程序中特定线程/进程的用法,但确实执行了 AUGraph 的操作。
只需使用
getloadavg()
:示例输出:
因此,这些报告的值是百分比。要这样查看它们:
示例输出:
更多来自 Apple 手册页 此处。
EDIT: Apologies--misread your question. This doesn't show usage for a specific thread/process in an app, but does do what AUGraph does.
Just use
getloadavg()
:Sample output:
So, those reported values are percentages. To see them as such:
Sample output from this:
More from the Apple man page here.