如何检索以下处理器统计信息?

fdh*_*fdh 9 macos objective-c

我该如何检索:

i)总进程数ii)总线程数

通过"总"请注意我说的是总人数中使用 被系统所支持的总量.

我特别要求OSX上的Objective-C/C解决方案.

nie*_*bot 10

你可以使用top和刮掉你想要的东西,但我很好奇,所以我挖得更深一些,找到了/usr/include/libproc.h,并写了一些代码:

#import <libproc.h>
#import <stdlib.h>
#import <stdio.h>

int main( int argc, const char * argv[])
{
    pid_t * pids = calloc(0x1000, 1);
    int count = proc_listallpids(pids, 0x1000);

    printf("count=%u\n", count) ;

    for( int index=0; index < count; ++index)
    {
        pid_t pid = pids[ index ] ;

        struct proc_taskinfo taskInfo ;
        /*int result*/ proc_pidinfo( pid, PROC_PIDTASKINFO, 0,  & taskInfo, sizeof( taskInfo ) ) ;

        // fields of taskInfo:
//          uint64_t        pti_virtual_size;   /* virtual memory size (bytes) */
//          uint64_t        pti_resident_size;  /* resident memory size (bytes) */
//          uint64_t        pti_total_user;     /* total time */
//          uint64_t        pti_total_system;
//          uint64_t        pti_threads_user;   /* existing threads only */
//          uint64_t        pti_threads_system;
//          int32_t         pti_policy;     /* default policy for new threads */
//          int32_t         pti_faults;     /* number of page faults */
//          int32_t         pti_pageins;        /* number of actual pageins */
//          int32_t         pti_cow_faults;     /* number of copy-on-write faults */
//          int32_t         pti_messages_sent;  /* number of messages sent */
//          int32_t         pti_messages_received;  /* number of messages received */
//          int32_t         pti_syscalls_mach;  /* number of mach system calls */
//          int32_t         pti_syscalls_unix;  /* number of unix system calls */
//          int32_t         pti_csw;            /* number of context switches */
//          int32_t         pti_threadnum;      /* number of threads in the task */
//          int32_t         pti_numrunning;     /* number of running threads */
//          int32_t         pti_priority;       /* task priority*/

        printf("PID %u:\n", pid);
        printf("\t%20s\t%u\n", "number of threads", taskInfo.pti_threadnum) ;
        printf("\t%20s\t%u\n", "number running threads", taskInfo.pti_numrunning) ;

        printf("\n") ;
    }

    return EXIT_SUCCESS ;
}
Run Code Online (Sandbox Code Playgroud)

  • 我什至更晚了,也许我误解了上面 Daniel 的评论,但重点是:`result` 值 *IS* 有用;它是写入的缓冲区的大小。如果为零,则表示*发生错误*并且缓冲区未更改。就我而言,大多数(所有?)result=0 情况将 `errno` 设置为 1,即 EPERM(或:权限被拒绝)。使用 root 权限重试消除了所有 result=0 的情况。 (3认同)

小智 0

对于 Objective C,您可以使用NSTask进行系统调用来sysctl -aw获取诸如

kern.maxproc = xxxx
kern.num_threads: xxxxx
Run Code Online (Sandbox Code Playgroud)