-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHugePageSize.c
63 lines (56 loc) · 1.31 KB
/
HugePageSize.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
static inline uint64_t
rte_str_to_size(const char *str)
{
char *endptr;
unsigned long long size;
while (isspace((int)*str))
str++;
if (*str == '-')
return 0;
errno = 0;
size = strtoull(str, &endptr, 0);
if (errno)
return 0;
if (*endptr == ' ')
endptr++; /* allow 1 space gap */
switch (*endptr){
case 'G': case 'g': size *= 1024; /* fall-through */
case 'M': case 'm': size *= 1024; /* fall-through */
case 'K': case 'k': size *= 1024; /* fall-through */
default:
break;
}
return size;
}
static uint64_t
get_default_hp_size(void)
{
const char proc_meminfo[] = "/proc/meminfo";
const char str_hugepagesz[] = "Hugepagesize:";
unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
char buffer[256];
unsigned long long size = 0;
FILE *fd = fopen(proc_meminfo, "r");
if (fd == NULL)
printf("Cannot open %s\n", proc_meminfo);
while(fgets(buffer, sizeof(buffer), fd)){
if (strncmp(buffer, str_hugepagesz, hugepagesz_len) == 0){
size = rte_str_to_size(&buffer[hugepagesz_len]);
break;
}
}
fclose(fd);
if (size == 0)
printf("Cannot get default hugepage size from %s\n", proc_meminfo);
return size;
}
int main(){
size_t hsize = get_default_hp_size();
printf("hsize:%ld\n", hsize);
return 0;
}