In iOS, how do I get the current CPU Utilization from inside an app that is running?
I'm looking for a way to get the CPU Utilization numbers like in Instruments and Activity Monitor with C/C++/Obj-C so I can do some automated testing with it. I'm looking for somewhere around a 1 second granularity, though 100 ms would be ideal. Another way to look at this question would be "How does Activity Monitor get the info it gets?".
Here's what I have found out so far:
- Many of the *nix equivalents don't seem to work with iOS
- sysctl doesn't have KERN_CPTIME
- iOS doesn't ha开发者_如何转开发ve the proc filesystem so I can't use
/proc/stat.
- This eliminates a lot of programs like vmstat and iostat
- MacOSX's sample doesn't exist in iOS.
I found out the answer. There is an undocumented API, host_processor_info() with the PROCESSOR_CPU_LOAD_INFO flavor, that can give you the number of system, user, nice, and idle ticks. You have to subtract the number of ticks from a previous call to it to get the current CPU utilization. Remember to call vm_deallocate on the array you get.
You cannot. The standard Un*x access to this information is not available within an iOS app.
精彩评论