-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add mpstat-like command for mac os x
- Loading branch information
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
cpu_usage |
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
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 |
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
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_. | ||
|
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
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); | ||
} | ||
} |