Created
January 31, 2019 08:33
-
-
Save brb/5369b5cfd08babb80cf2c4081dc19762 to your computer and use it in GitHub Desktop.
bpf_num_possible_cpus
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 <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <errno.h> | |
static inline unsigned int bpf_num_possible_cpus(void) | |
{ | |
static const char *fcpu = "/tmp/foo"; | |
unsigned int start, end, possible_cpus = 0; | |
char buff[128]; | |
FILE *fp; | |
int n, i, j = 0; | |
fp = fopen(fcpu, "r"); | |
if (!fp) { | |
printf("Failed to open %s: '%s'!\n", fcpu, strerror(errno)); | |
exit(1); | |
} | |
if (!fgets(buff, sizeof(buff), fp)) { | |
printf("Failed to read %s!\n", fcpu); | |
exit(1); | |
} | |
int len = strlen(buff); | |
for (i = 0; i <= len; i++) { | |
if (buff[i] == ',' || buff[i] == '\0') { | |
buff[i] = '\0'; | |
n = sscanf(&buff[j], "%u-%u", &start, &end); | |
if (n <= 0) { | |
printf("Failed to retrieve # possible CPUs!\n"); | |
exit(1); | |
} else if (n == 1) { | |
end = start; | |
} | |
possible_cpus += end - start + 1; | |
j = i + 1; | |
} | |
} | |
fclose(fp); | |
return possible_cpus; | |
} | |
int main() | |
{ | |
printf("num: %d\n", bpf_num_possible_cpus()); | |
return 0; | |
} |
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
$ echo -n "" > /tmp/foo && ./main | |
Failed to read /tmp/foo! | |
$ echo "" > /tmp/foo && ./main | |
Failed to retrieve # possible CPUs! | |
$ echo "1" > /tmp/foo && ./main | |
num: 1 | |
$ echo "1,3" > /tmp/foo && ./main | |
num: 2 | |
$ echo "0,2-3" > /tmp/foo && ./main | |
num: 3 | |
$ echo "0-1,3-4" > /tmp/foo && ./main | |
num: 4 | |
$ echo "0-3" > /tmp/foo && ./main | |
num: 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment