获取当前pthread的CPU使用率的Mac OS X [英] Get current pthread cpu usage Mac OS X
问题描述
我怎样才能从线程本身在Mac OS X线程的CPU时间?对于Linux,我要做的就是的getrusage(RUSAGE_THREAD,&安培; RU)
但这种方法不适用于Mac OS X
How can I get a thread's cpu time from the thread itself in Mac OS X ? For linux, what I do is getrusage(RUSAGE_THREAD, &ru)
but this solution isn't available for Mac OS X.
我碰到这个<少时href=\"http://stackoverflow.com/questions/1543157/how-can-i-find-out-how-much-memory-my-c-app-is-using-on-the-mac\">question但我不知道如何去适应它为我的目的(我不熟悉的Mac OS X的内部,我甚至不能确定的pthread线程==马赫线程)。
I came across this question but I don't know how to adapt it for my purpose (I'm not familiar with Mac OS X's internals. I am not even sure pthread thread == mach thread).
推荐答案
这是我结束了:
#include <mach/mach_init.h>
#include <mach/thread_act.h>
#include <mach/mach_port.h>
[...]
mach_port_t thread;
kern_return_t kr;
mach_msg_type_number_t count;
thread_basic_info_data_t info;
thread = mach_thread_self();
count = THREAD_BASIC_INFO_COUNT;
kr = thread_info(thread, THREAD_BASIC_INFO, (thread_info_t) &info, &count);
if (kr == KERN_SUCCESS && (info.flags & TH_FLAGS_IDLE) == 0) {
usage->utime.tv_sec = info.user_time.seconds;
usage->utime.tv_usec = info.user_time.microseconds;
usage->stime.tv_sec = info.system_time.seconds;
usage->stime.tv_usec = info.system_time.microseconds;
}
else {
// should not happen
printf("Could not retreive thread info.");
bzero(usage, sizeof(struct usage));
}
mach_port_deallocate(mach_task_self(), thread);
我得到非常不同的结果我得到什么用的getrusage(RUSAGE_THREAD,&安培; RU)
Linux下。所以我不知道这是正确的做法。
I get very different results that what i get with getrusage(RUSAGE_THREAD, &ru)
under Linux. So I'm not sure this is the right way.
这篇关于获取当前pthread的CPU使用率的Mac OS X的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!