2

我希望直接从我的 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 大页面呢?

4

2 回答 2

3

试试这个。

#include <hugetlbfs.h>
int getpagesizes(long pagesizes[], int n_elem); 
于 2013-06-20T23:20:44.650 回答
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 大页面呢?

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

于 2016-01-23T11:11:13.433 回答