Skip to content

Commit

Permalink
add mpstat-like command for mac os x
Browse files Browse the repository at this point in the history
  • Loading branch information
jclulow committed Jan 28, 2013
1 parent c3c94f5 commit 27c7948
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions macosx/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpu_usage
12 changes: 12 additions & 0 deletions macosx/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@


CC=gcc


all: cpu_usage

cpu_usage: cpu_usage.c
$(CC) -o $@ $<

clean:
rm -f cpu_usage
9 changes: 9 additions & 0 deletions macosx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Mac OS X Tools

## cpu_usage

Mac OS X lacks, perhaps unsurprisingly, a command line tool to emit
the sort of data one expects from _mpstat(1)_. This tool seeks to
print CPU Usage figures for each core in the format expected by
_heatmap_.

89 changes: 89 additions & 0 deletions macosx/cpu_usage.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@


#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include <mach/mach.h>
#include <mach/processor_info.h>
#include <mach/mach_host.h>
#include <time.h>

typedef struct cpu_load {
int cl_system;
int cl_user;
int cl_nice;
int cl_idle;
} cpu_load_t;

int cpu_count = -1;
cpu_load_t *cpu_load = NULL;

void
update_cpu_load()
{
kern_return_t error;
natural_t nmpu;
processor_info_array_t info;
mach_msg_type_number_t cnt;
int infosz;
int i;
int firstrun = 0;

error = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO,
&nmpu, &info, &cnt);
if (error != KERN_SUCCESS) {
mach_error("update_cpu_load1", error);
exit(1);
}

if (cpu_load == NULL) {
cpu_count = nmpu;
cpu_load = calloc(nmpu, sizeof (cpu_load_t));
firstrun = 1;
}

infosz = cnt / nmpu;

for (i = 0; i < nmpu; i++) {
cpu_load_t newload;
newload.cl_system = info[CPU_STATE_SYSTEM + i * infosz];
newload.cl_user = info[CPU_STATE_USER + i * infosz];
newload.cl_nice = info[CPU_STATE_NICE + i * infosz];
newload.cl_idle = info[CPU_STATE_IDLE + i * infosz];
if (!firstrun) {
int delta_system = newload.cl_system -
cpu_load[i].cl_system;
int delta_user = newload.cl_user -
cpu_load[i].cl_user;
int delta_nice = newload.cl_nice -
cpu_load[i].cl_nice;
int delta_idle = newload.cl_idle -
cpu_load[i].cl_idle;

int used = delta_system + delta_user + delta_nice;
int percent = 100 * used / (used + delta_idle);

fprintf(stdout, "%d ", percent);
}
memcpy(&cpu_load[i], &newload, sizeof (cpu_load_t));
}
if (!firstrun)
fprintf(stdout, "\n");

vm_deallocate(mach_task_self(), (vm_address_t)info, cnt);
}

int
main(int argc, char **argv)
{
setvbuf(stdout, NULL, _IOLBF, 0);

for (;;) {
update_cpu_load();
sleep(1);
}
}

0 comments on commit 27c7948

Please sign in to comment.