[RFC,v4,6/9] KVM: selftests: Add a helper to get system default hugetlb page size
diff mbox series

Message ID 20210302125751.19080-7-wangyanan55@huawei.com
State New, archived
Headers show
Series
  • KVM: selftests: some improvement and a new test for kvm page table
Related show

Commit Message

Yanan Wang March 2, 2021, 12:57 p.m. UTC
If HUGETLB is configured in the host kernel, then we can know the system
default hugetlb page size through *cat /proc/meminfo*. Otherwise, we will
not see the information of hugetlb pages in file /proc/meminfo if it's not
configured. So add a helper to determine whether HUGETLB is configured and
then get the default page size by reading /proc/meminfo.

This helper can be useful when a program wants to use the default hugetlb
pages of the system and doesn't know the default page size.

Signed-off-by: Yanan Wang <wangyanan55@huawei.com>
---
 .../testing/selftests/kvm/include/test_util.h |  1 +
 tools/testing/selftests/kvm/lib/test_util.c   | 27 +++++++++++++++++++
 2 files changed, 28 insertions(+)

Comments

Andrew Jones March 12, 2021, 11:40 a.m. UTC | #1
On Tue, Mar 02, 2021 at 08:57:48PM +0800, Yanan Wang wrote:
> If HUGETLB is configured in the host kernel, then we can know the system
> default hugetlb page size through *cat /proc/meminfo*. Otherwise, we will
> not see the information of hugetlb pages in file /proc/meminfo if it's not
> configured. So add a helper to determine whether HUGETLB is configured and
> then get the default page size by reading /proc/meminfo.
> 
> This helper can be useful when a program wants to use the default hugetlb
> pages of the system and doesn't know the default page size.
> 
> Signed-off-by: Yanan Wang <wangyanan55@huawei.com>
> ---
>  .../testing/selftests/kvm/include/test_util.h |  1 +
>  tools/testing/selftests/kvm/lib/test_util.c   | 27 +++++++++++++++++++
>  2 files changed, 28 insertions(+)
> 
> diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h
> index ef24c76ba89a..e087174eefe5 100644
> --- a/tools/testing/selftests/kvm/include/test_util.h
> +++ b/tools/testing/selftests/kvm/include/test_util.h
> @@ -80,6 +80,7 @@ struct vm_mem_backing_src_alias {
>  
>  bool thp_configured(void);
>  size_t get_trans_hugepagesz(void);
> +size_t get_def_hugetlb_pagesz(void);
>  void backing_src_help(void);
>  enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name);
>  
> diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c
> index f2d133f76c67..80d68dbd72d2 100644
> --- a/tools/testing/selftests/kvm/lib/test_util.c
> +++ b/tools/testing/selftests/kvm/lib/test_util.c
> @@ -153,6 +153,33 @@ size_t get_trans_hugepagesz(void)
>  	return size;
>  }
>  
> +size_t get_def_hugetlb_pagesz(void)
> +{
> +	char buf[64];
> +	const char *tag = "Hugepagesize:";
> +	FILE *f;
> +
> +	f = fopen("/proc/meminfo", "r");
> +	TEST_ASSERT(f != NULL, "Error in opening /proc/meminfo: %d", errno);
> +
> +	while (fgets(buf, sizeof(buf), f) != NULL) {
> +		if (strstr(buf, tag) == buf) {
> +			fclose(f);
> +			return strtoull(buf + strlen(tag), NULL, 10) << 10;
> +		}
> +	}
> +
> +	if (feof(f)) {
> +		fclose(f);
> +		TEST_FAIL("HUGETLB is not configured in host kernel");
> +	} else {
> +		fclose(f);
> +		TEST_FAIL("Error in reading /proc/meminfo: %d", errno);
> +	}

fclose() can be factored out.

> +
> +	return 0;
> +}
> +
>  void backing_src_help(void)
>  {
>  	int i;
> -- 
> 2.23.0
>

Besides the fclose comment and the same errno comment as the previous
patch

Reviewed-by: Andrew Jones <drjones@redhat.com>
Yanan Wang March 22, 2021, 6:45 a.m. UTC | #2
On 2021/3/12 19:40, Andrew Jones wrote:
> On Tue, Mar 02, 2021 at 08:57:48PM +0800, Yanan Wang wrote:
>> If HUGETLB is configured in the host kernel, then we can know the system
>> default hugetlb page size through *cat /proc/meminfo*. Otherwise, we will
>> not see the information of hugetlb pages in file /proc/meminfo if it's not
>> configured. So add a helper to determine whether HUGETLB is configured and
>> then get the default page size by reading /proc/meminfo.
>>
>> This helper can be useful when a program wants to use the default hugetlb
>> pages of the system and doesn't know the default page size.
>>
>> Signed-off-by: Yanan Wang <wangyanan55@huawei.com>
>> ---
>>   .../testing/selftests/kvm/include/test_util.h |  1 +
>>   tools/testing/selftests/kvm/lib/test_util.c   | 27 +++++++++++++++++++
>>   2 files changed, 28 insertions(+)
>>
>> diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h
>> index ef24c76ba89a..e087174eefe5 100644
>> --- a/tools/testing/selftests/kvm/include/test_util.h
>> +++ b/tools/testing/selftests/kvm/include/test_util.h
>> @@ -80,6 +80,7 @@ struct vm_mem_backing_src_alias {
>>   
>>   bool thp_configured(void);
>>   size_t get_trans_hugepagesz(void);
>> +size_t get_def_hugetlb_pagesz(void);
>>   void backing_src_help(void);
>>   enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name);
>>   
>> diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c
>> index f2d133f76c67..80d68dbd72d2 100644
>> --- a/tools/testing/selftests/kvm/lib/test_util.c
>> +++ b/tools/testing/selftests/kvm/lib/test_util.c
>> @@ -153,6 +153,33 @@ size_t get_trans_hugepagesz(void)
>>   	return size;
>>   }
>>   
>> +size_t get_def_hugetlb_pagesz(void)
>> +{
>> +	char buf[64];
>> +	const char *tag = "Hugepagesize:";
>> +	FILE *f;
>> +
>> +	f = fopen("/proc/meminfo", "r");
>> +	TEST_ASSERT(f != NULL, "Error in opening /proc/meminfo: %d", errno);
>> +
>> +	while (fgets(buf, sizeof(buf), f) != NULL) {
>> +		if (strstr(buf, tag) == buf) {
>> +			fclose(f);
>> +			return strtoull(buf + strlen(tag), NULL, 10) << 10;
>> +		}
>> +	}
>> +
>> +	if (feof(f)) {
>> +		fclose(f);
>> +		TEST_FAIL("HUGETLB is not configured in host kernel");
>> +	} else {
>> +		fclose(f);
>> +		TEST_FAIL("Error in reading /proc/meminfo: %d", errno);
>> +	}
> fclose() can be factored out.
>
>> +
>> +	return 0;
>> +}
>> +
>>   void backing_src_help(void)
>>   {
>>   	int i;
>> -- 
>> 2.23.0
>>
> Besides the fclose comment and the same errno comment as the previous
> patch
I will fix it and add your R-b in this patch.

Thanks,
Yanan
> Reviewed-by: Andrew Jones <drjones@redhat.com>
>
> .

Patch
diff mbox series

diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h
index ef24c76ba89a..e087174eefe5 100644
--- a/tools/testing/selftests/kvm/include/test_util.h
+++ b/tools/testing/selftests/kvm/include/test_util.h
@@ -80,6 +80,7 @@  struct vm_mem_backing_src_alias {
 
 bool thp_configured(void);
 size_t get_trans_hugepagesz(void);
+size_t get_def_hugetlb_pagesz(void);
 void backing_src_help(void);
 enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name);
 
diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c
index f2d133f76c67..80d68dbd72d2 100644
--- a/tools/testing/selftests/kvm/lib/test_util.c
+++ b/tools/testing/selftests/kvm/lib/test_util.c
@@ -153,6 +153,33 @@  size_t get_trans_hugepagesz(void)
 	return size;
 }
 
+size_t get_def_hugetlb_pagesz(void)
+{
+	char buf[64];
+	const char *tag = "Hugepagesize:";
+	FILE *f;
+
+	f = fopen("/proc/meminfo", "r");
+	TEST_ASSERT(f != NULL, "Error in opening /proc/meminfo: %d", errno);
+
+	while (fgets(buf, sizeof(buf), f) != NULL) {
+		if (strstr(buf, tag) == buf) {
+			fclose(f);
+			return strtoull(buf + strlen(tag), NULL, 10) << 10;
+		}
+	}
+
+	if (feof(f)) {
+		fclose(f);
+		TEST_FAIL("HUGETLB is not configured in host kernel");
+	} else {
+		fclose(f);
+		TEST_FAIL("Error in reading /proc/meminfo: %d", errno);
+	}
+
+	return 0;
+}
+
 void backing_src_help(void)
 {
 	int i;