Skip to content

Instantly share code, notes, and snippets.

@HalCanary
Last active August 20, 2021 16:39
Show Gist options
  • Save HalCanary/5b8ac8fbbd1e02482acdd9ae17f8e2d5 to your computer and use it in GitHub Desktop.
Save HalCanary/5b8ac8fbbd1e02482acdd9ae17f8e2d5 to your computer and use it in GitHub Desktop.
cpufrequency.c
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/sysctl.h>
int main() {
static const char* NAMES[3] = {
"hw.cpufrequency",
"hw.cpufrequency_min",
"hw.cpufrequency_max",
};
for (int i = 0; i < 3; ++i) {
int64_t value = -1;
size_t len = sizeof(value);
printf("%s: ", NAMES[i]);
if (0 == sysctlbyname(NAMES[i], &value, &len, NULL, 0)) {
printf("%lld\n", (long long)value);
} else if (ENOENT == errno) {
printf("error: 'unknown value'\n");
} else {
printf("error: '%s'\n", strerror(errno));
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment