Skip to content

Instantly share code, notes, and snippets.

@nielsbot
Created August 24, 2012 20:05
Show Gist options
  • Save nielsbot/3455076 to your computer and use it in GitHub Desktop.
Save nielsbot/3455076 to your computer and use it in GitHub Desktop.
Get number of threads (running) in all tasks on OS X
#import <libproc.h>
#import <stdlib.h>
#import <stdio.h>
int main( int argc, const char * argv[])
{
pid_t * pids = calloc(0x4000, 1);
int count = proc_listallpids(pids, 0x4000);
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\t%s:\n", pid, BSDInfo.pbi_name);
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 ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment