Created
July 9, 2011 14:36
-
-
Save mikbe/1073620 to your computer and use it in GitHub Desktop.
Sample task info for current task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <mach/mach_init.h> | |
#include <mach/mach_port.h> | |
#include <mach/task_info.h> | |
#include <mach/thread_act.h> | |
#include <mach/vm_map.h> | |
#include <mach/task.h> | |
#include <sys/types.h> | |
#include <sys/time.h> | |
#include <sys/proc.h> | |
#include <sys/ptrace.h> | |
#include <sys/sysctl.h> | |
#include <sys/vnode.h> | |
#include <math.h> | |
#include <time.h> | |
#include <signal.h> | |
#include <errno.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
task_t get_task(pid_t pid_, task_t *task) | |
{ | |
int result; | |
if((result = task_for_pid(mach_task_self(), pid_, task)) != KERN_SUCCESS) | |
return result; | |
return 0; | |
} | |
int sample(pid_t pid_, double *user_time, double *system_time, double *percent) | |
{ | |
task_t task; | |
int error = get_task(pid_, &task); | |
if (error < 0) | |
return task; | |
struct task_basic_info t_info; | |
thread_array_t th_array; | |
uint t_info_count = TASK_BASIC_INFO_COUNT, th_count; | |
size_t i; | |
double my_user_time = 0, my_system_time = 0, my_percent = 0; | |
if ((error = task_info(task, TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count)) != KERN_SUCCESS) | |
{ | |
return (error * -1); | |
} | |
if ((error = task_threads(task, &th_array, &th_count)) != KERN_SUCCESS) | |
{ | |
return error; | |
} | |
// sum time for live threads | |
for (i = 0; i < th_count; i++) | |
{ | |
double th_user_time, th_system_time, th_percent; | |
// if ((error = AGGetMachThreadCPUUsage(th_array[i], &th_user_time, &th_system_time, &th_percent)) != KERN_SUCCESS) | |
// break; | |
struct thread_basic_info th_info; | |
mach_msg_type_number_t th_info_count = THREAD_BASIC_INFO_COUNT; | |
if ((error = thread_info(th_array[i], THREAD_BASIC_INFO, | |
(thread_info_t)&th_info, &th_info_count)) != KERN_SUCCESS) | |
{ | |
return error; | |
} | |
th_user_time = th_info.user_time.seconds + th_info.user_time.microseconds / 1e6; | |
th_system_time = th_info.system_time.seconds + th_info.system_time.microseconds / 1e6; | |
th_percent = (double)th_info.cpu_usage / TH_USAGE_SCALE; | |
my_user_time += th_user_time; | |
my_system_time += th_system_time; | |
my_percent += th_percent; | |
} | |
// destroy thread array | |
for (i = 0; i < th_count; i++) | |
{ | |
mach_port_deallocate(mach_task_self(), th_array[i]); | |
} | |
vm_deallocate(mach_task_self(), (vm_address_t)th_array, sizeof(thread_t) * th_count); | |
// check last error | |
if (error != KERN_SUCCESS) | |
{ | |
return error; | |
} | |
// add time for dead threads | |
my_user_time += t_info.user_time.seconds + t_info.user_time.microseconds / 1e6; | |
my_system_time += t_info.system_time.seconds + t_info.system_time.microseconds / 1e6; | |
if (user_time != NULL) *user_time = my_user_time; | |
if (system_time != NULL) *system_time = my_system_time; | |
if (percent != NULL) *percent = my_percent; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment