Last active
August 20, 2021 16:39
-
-
Save HalCanary/5b8ac8fbbd1e02482acdd9ae17f8e2d5 to your computer and use it in GitHub Desktop.
cpufrequency.c
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 <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