How to get the value of huge page size?

天大地大妈咪最大 提交于 2020-01-24 13:08:08

问题


I am looking to get the value of huge page size directly from my C code without to run a bash command.

From bash i can do this

grep pse /proc/cpuinfo    > /dev/null && echo '2M huge page size are supported'
grep pdpe1gb /proc/cpuinfo> /dev/null && echo '1G huge page size are supported'

Secondly how to use mmap with 1G huge page size ?

thanks

Update

snippet code

#include <stdio.h>
#include <limits.h>
#include <hugetlbfs.h>

int main(void){
    long result1 = gethugepagesize(); 
    printf( "%d\n", result1 );
    long result2 = gethugepagesizes( NULL, 0);
    printf( "%d\n", result2 );
    long result3 = getpagesizes( NULL, 0);
    printf( "%d\n", result3 );
    printf("%d\n", PF_LINUX_HUGETLB);
    return 0;
}

Output

2097152
1
2
1048576

here gethugepagesize return 2 Mb what about 1Gb huge page ?


回答1:


Try this out.

#include <hugetlbfs.h>
int getpagesizes(long pagesizes[], int n_elem); 



回答2:


Since I have insufficient reputation points to post a comment, I'll post this in an answer.

I modified your code:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <hugetlbfs.h>

void dump_page_sizes_arr( int (getter_fn)(long pagesizes[], int n_elem), int elem ) {

    int elem_alloc, i;
    long *pagesizes = NULL;

    if( elem <= 0 ) return;

    pagesizes = calloc( elem, sizeof(long) );
    if( pagesizes == NULL ) return;

    elem_alloc = getter_fn( pagesizes, elem );
    if ( elem_alloc != elem ) goto stop;

    for( i=0; i<elem_alloc; i++ ) printf( " %ld\n", pagesizes[i] );

stop:
    free( pagesizes );
}

int main(void){

    long result1 = gethugepagesize();
    printf( "huge page size = %ld\n", result1 );

    int result2 = gethugepagesizes( NULL, 0 );
    printf( "huge page sizes [%d] =\n", result2 );
    dump_page_sizes_arr( gethugepagesizes, result2 );

    int result3 = getpagesizes( NULL, 0 );
    printf( "page sizes [%d] =\n", result3 );
    dump_page_sizes_arr( getpagesizes, result3 );

    printf( "PF_LINUX_HUGETLB = %d\n", PF_LINUX_HUGETLB );

    return 0;
}

And it seems I am getting very similar results on my system (Mint 17.3 x64 + 2 GiB RAM):

huge page size = 2097152
huge page sizes [1] =
 2097152
page sizes [2] =
 4096
 2097152
PF_LINUX_HUGETLB = 1048576

So to answer your question:

what about 1Gb huge page ?

... it seems your system doesn't support it.



来源:https://stackoverflow.com/questions/17225200/how-to-get-the-value-of-huge-page-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!