有谁知道如何获取应用程序的CPU使用率?这绝对是可能的,因为应用程序商店(Activity Monitor Touch)中有应用程序可以显示它.
我有一个具有以下成员函数的类:
/// caller pid
virtual pid_t Pid() const = 0;
/// physical memory size in KB
virtual uint64_t Size() const = 0;
/// resident memory for this process
virtual uint64_t Rss() const = 0;
/// cpu used by this process
virtual double PercentCpu() const = 0;
/// memory used by this process
virtual double PercentMemory() const = 0;
/// number of threads in this process
virtual int32_t Lwps() const = 0;
这个类的职责是返回有关调用者的进程信息.物理内存大小可以通过sysctl调用轻松确定,而pid是微不足道的,但除了在ps或top上调用popen并解析输出之外,其余的调用都没有找到 - 这是不可接受的.任何帮助将不胜感激.
要求:
在g ++ 4.0上编译
没有obj-c
OSX …
我需要在OS X上获得目标C/C的总CPU空闲时间吗?
如果可能的话请提供代码示例.这是我用来获取这些指标的代码.结果百分比与我在Activity Monitor中的百分比不同.所以我假设CPU时间计算不正确:
#include <sys/sysctl.h>
#include <sys/types.h>
#include <mach/mach.h>
#include <mach/processor_info.h>
#include <mach/mach_host.h>
- (void)printCPUUsage
{
processor_cpu_load_info_t cpuLoad;
mach_msg_type_number_t processorMsgCount;
natural_t processorCount;
uint64_t totalSystemTime = 0, totalUserTime = 0, totalIdleTime = 0, totalCPUTime = 0;
kern_return_t err = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &processorCount, (processor_info_array_t *)&cpuLoad, &processorMsgCount);
for (natural_t i = 0; i < processorCount; i++) {
// Calc load types and totals, with guards against 32-bit overflow
// (values are natural_t)
uint64_t system = 0, user = 0, idle = 0, …Run Code Online (Sandbox Code Playgroud) 是否有一种标准的方法可以在OS X(10.6和10.7)上获得Cocoa/Objective-C中的GPU百分比使用率?
有没有办法访问并打印出设备当前的 cpu 信息?我找不到任何东西。
var cpuInfo: processor_info_array_t = nil
var numCpuInfo: mach_msg_type_number_t = 0
var coresTotalUsage: Float = 0.0
var numCPUsU: natural_t = 0
let err = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numCPUsU, &cpuInfo, &numCpuInfo);
assert(err == KERN_SUCCESS, "Failed call to host_processor_info")
Run Code Online (Sandbox Code Playgroud)
嗨,我正在调用上面的C API host_processor_info从swift获取进程加载信息,没问题.
cpuInfo是一个inout参数(指针),返回时将指向包含该API分配的CPU信息的结构.调用者负责释放内存; 我可以很容易地从目标C那里做到这一点但是没有任何好运.我知道我可以将该调用包装成一个客观的C扩展,但我正在尝试学习swift,并且如果可能的话,希望避免使用obj-c解决方案.
在obj-c我会解除:
size_t cpuInfoSize = sizeof(integer_t) * numCpuInfo;
vm_deallocate(mach_task_self(), (vm_address_t) cpuInfo, cpuInfoSize)
Run Code Online (Sandbox Code Playgroud)
swift中的cpuInfo是一个不能转换为vm_address_t的UnsafeMutablePointer.
任何帮助表示感谢,谢谢.