简体   繁体   English

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

[英]How to get the value of huge page size?

I am looking to get the value of huge page size directly from my C code without to run a bash command. 我希望直接从我的C代码中获取巨大页面大小的值,而无需运行bash命令。

From bash i can do this 从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'

Secondly how to use mmap with 1G huge page size ? 其次,如何在1G巨大的页面大小中使用mmap?

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 ? 这里gethugepagesize返回2 Mb 1Gb的大页面怎么办?

Try this out. 试试看

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

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): 看来我的系统得到了非常相似的结果(薄荷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 ? 1Gb的大页面怎么样?

... it seems your system doesn't support it. ...看来您的系统不支持它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM