如何获得巨大的页面大小的价值?

时间:2013-06-20 22:56:58

标签: c linux-kernel mmap huge-pages

我希望直接从我的C代码中获取大页面大小的值,而无需运行bash命令。

从bash我可以做到这一点

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'

其次如何使用1G巨大页面大小的mmap?

感谢

更新

摘录代码

#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;
}

输出

2097152
1
2
1048576

这里gethugepagesize返回2 Mb 1Gb大页面怎么样?

2 个答案:

答案 0 :(得分:3)

试试这个。

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

答案 1 :(得分:1)

由于我没有足够的声誉点发表评论,我会在答案中发布。

我修改了你的代码:

#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;
}

我的系统似乎得到了非常相似的结果(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

所以回答你的问题:

  

1Gb大页面怎么样?

......您的系统似乎不支持它。