Skip to content

Instantly share code, notes, and snippets.

@oxnz
Last active August 29, 2015 13:56
Show Gist options
  • Save oxnz/8845352 to your computer and use it in GitHub Desktop.
Save oxnz/8845352 to your computer and use it in GitHub Desktop.
darwin system utility
#include <stdio.h>
#include <sys/sysctl.h>
#include <mach/host_info.h>
#include <mach/mach_host.h>
#include <mach/task_info.h>
#include <mach/task.h>
void usage(int verbose) {
printf("Usage:\n");
if (verbose > 2)
printf("Verbose\n");
}
void outMemSiz(const char *label, size_t siz) {
const char *suffix[] = {
"B",
"KB",
"MB",
"GB",
"TB",
"PB",
"EB"};
int p = 0;
double cnt = siz;
while (cnt > 1024) {
++p;
cnt /= 1024;
}
printf("%s:\t\t%8.2lf %s\n", label, cnt, suffix[p]);
}
int main(int argc, char* argv[]) {
size_t pagesize;
size_t len = sizeof(pagesize);
int mib[6] = {0};
mib[0] = CTL_HW;
mib[1] = HW_PAGESIZE;
if (sysctl(mib, 2, &pagesize, &len, NULL, 0) < 0) {
perror("sysctl");
goto errproc;
}
mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
vm_statistics_data_t vmstat;
//ref: /usr/include/mach/vm_statistics.h
if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmstat,
&count) != KERN_SUCCESS) {
perror("host_statistics");
goto errproc;
}
size_t total = vmstat.wire_count + vmstat.active_count +
vmstat.inactive_count + vmstat.free_count;
size_t wired = vmstat.wire_count;
size_t active = vmstat.active_count;
size_t inactive = vmstat.inactive_count;
size_t free_ = vmstat.free_count;
task_basic_info_64_data_t info;
unsigned siz = sizeof(info);
task_info(mach_task_self(), TASK_BASIC_INFO_64, (task_info_t)&info, &siz);
// output
outMemSiz("total", total * pagesize);
outMemSiz("wired", wired * pagesize);
outMemSiz("active", active * pagesize);
outMemSiz("inactive", inactive * pagesize);
outMemSiz("free", free_ * pagesize);
outMemSiz("task", info.resident_size);
return 0;
errproc:
return 1;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define PROGNAME "perror"
void help(void) {
fprintf(stderr, "Usage: %s [options] <errcode>\n"
"Options:\n"
" -a --all print all error code with error messages\n"
" -h --help show this help message and exit\n"
" <errcode> print the error message specified by errcode\n",
PROGNAME
);
}
int main(int argc, char* argv[]) {
if (argc == 2 && strncmp(argv[1], "-h", 2)) {
if (!strncmp(argv[1], "-a", 2)) {
for (int i = 0; i < sys_nerr; ++i) {
fprintf(stdout, "sys error code %3d: %s\n", i, strerror(i));
}
} else {
int ecode = strtol(argv[1], NULL, 10);
if (!ecode) {
if (errno == EINVAL) {
fprintf(stderr, "invalid number: %s\n", argv[1]);
} else if (errno == ERANGE) {
fprintf(stderr, "number out of range: %s\n", argv[1]);
}
goto ERRPROC;
} else if (ecode < 0 || ecode > sys_nerr) {
fprintf(stderr, "errcode out of range: %3d(0~%3d)\n",
ecode, sys_nerr);
goto ERRPROC;
}
fprintf(stdout, "sys error code %3d: %s\n",
ecode, strerror(ecode));
}
} else {
help();
}
return argc != 2;
ERRPROC:
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment