Along with testing the binary interface for KVM statistics, it's good to do a small sanity check of the KVM debugfs interface. So, add a test case to the existing kvm_binary_stats_test.c to check that KVM debugfs contains the correct directory entries for the test VMs and test VCPUs. Also, rename the file to kvm_stats_test.c. Signed-off-by: Krish Sadhukhan <krish.sadhukhan@xxxxxxxxxx> Reviewed-by: Liam Merwick <liam.merwick@xxxxxxxxxx> --- tools/testing/selftests/kvm/.gitignore | 2 +- tools/testing/selftests/kvm/Makefile | 6 ++-- .../testing/selftests/kvm/include/test_util.h | 2 ++ ...m_binary_stats_test.c => kvm_stats_test.c} | 32 +++++++++++++++++++ 4 files changed, 38 insertions(+), 4 deletions(-) rename tools/testing/selftests/kvm/{kvm_binary_stats_test.c => kvm_stats_test.c} (88%) diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore index 36896d251977..4953ac735e19 100644 --- a/tools/testing/selftests/kvm/.gitignore +++ b/tools/testing/selftests/kvm/.gitignore @@ -50,4 +50,4 @@ /memslot_perf_test /set_memory_region_test /steal_time -/kvm_binary_stats_test +/kvm_stats_test diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index c103873531e0..d44fa28bc56d 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -83,7 +83,7 @@ TEST_GEN_PROGS_x86_64 += memslot_modification_stress_test TEST_GEN_PROGS_x86_64 += memslot_perf_test TEST_GEN_PROGS_x86_64 += set_memory_region_test TEST_GEN_PROGS_x86_64 += steal_time -TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test +TEST_GEN_PROGS_x86_64 += kvm_stats_test TEST_GEN_PROGS_aarch64 += aarch64/debug-exceptions TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list @@ -95,7 +95,7 @@ TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus TEST_GEN_PROGS_aarch64 += kvm_page_table_test TEST_GEN_PROGS_aarch64 += set_memory_region_test TEST_GEN_PROGS_aarch64 += steal_time -TEST_GEN_PROGS_aarch64 += kvm_binary_stats_test +TEST_GEN_PROGS_aarch64 += kvm_stats_test TEST_GEN_PROGS_s390x = s390x/memop TEST_GEN_PROGS_s390x += s390x/resets @@ -105,7 +105,7 @@ TEST_GEN_PROGS_s390x += dirty_log_test TEST_GEN_PROGS_s390x += kvm_create_max_vcpus TEST_GEN_PROGS_s390x += kvm_page_table_test TEST_GEN_PROGS_s390x += set_memory_region_test -TEST_GEN_PROGS_s390x += kvm_binary_stats_test +TEST_GEN_PROGS_s390x += kvm_stats_test TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M)) LIBKVM += $(LIBKVM_$(UNAME_M)) diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h index d79be15dd3d2..812be7b67c2d 100644 --- a/tools/testing/selftests/kvm/include/test_util.h +++ b/tools/testing/selftests/kvm/include/test_util.h @@ -59,6 +59,8 @@ void test_assert(bool exp, const char *exp_str, #define TEST_FAIL(fmt, ...) \ TEST_ASSERT(false, fmt, ##__VA_ARGS__) +#define KVM_DEBUGFS_PATH "/sys/kernel/debug/kvm" + size_t parse_size(const char *size); int64_t timespec_to_ns(struct timespec ts); diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/testing/selftests/kvm/kvm_stats_test.c similarity index 88% rename from tools/testing/selftests/kvm/kvm_binary_stats_test.c rename to tools/testing/selftests/kvm/kvm_stats_test.c index 5906bbc08483..ce0a8387eb42 100644 --- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c +++ b/tools/testing/selftests/kvm/kvm_stats_test.c @@ -13,6 +13,7 @@ #include <stdlib.h> #include <string.h> #include <errno.h> +#include <sys/stat.h> #include "test_util.h" @@ -230,8 +231,39 @@ int main(int argc, char *argv[]) vcpu_stats_test(vms[i], j); } + /* + * Check debugfs directory for every VM and VCPU + */ + struct stat buf; + int len; + char *vm_dir_path = NULL; + char *vcpu_dir_path = NULL; + +#define INT_MAX_LEN 10 + + len = strlen(KVM_DEBUGFS_PATH) + 2 * INT_MAX_LEN + 2; + vm_dir_path = malloc(len); + TEST_ASSERT(vm_dir_path, "Allocate memory for VM directory path"); + vcpu_dir_path = malloc(len + INT_MAX_LEN + 5); + TEST_ASSERT(vm_dir_path, "Allocate memory for VCPU directory path"); + for (i = 0; i < max_vm; ++i) { + sprintf(vm_dir_path, "%s/%d-%d", KVM_DEBUGFS_PATH, getpid(), + vm_get_fd(vms[i])); + stat(vm_dir_path, &buf); + TEST_ASSERT(S_ISDIR(buf.st_mode), "VM directory %s does not " + "exist", vm_dir_path); + for (j = 0; j < max_vcpu; ++j) { + sprintf(vcpu_dir_path, "%s/vcpu%d", vm_dir_path, j); + stat(vcpu_dir_path, &buf); + TEST_ASSERT(S_ISDIR(buf.st_mode), "CPU directory %s " + "does not exist", vcpu_dir_path); + } + } + for (i = 0; i < max_vm; ++i) kvm_vm_free(vms[i]); free(vms); + free(vm_dir_path); + free(vcpu_dir_path); return 0; } -- 2.27.0