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> --- tools/testing/selftests/kvm/Makefile | 6 ++-- .../testing/selftests/kvm/include/test_util.h | 2 ++ ...m_binary_stats_test.c => kvm_stats_test.c} | 31 +++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) rename tools/testing/selftests/kvm/{kvm_binary_stats_test.c => kvm_stats_test.c} (88%) diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 5832f510a16c..673e1f6d20f4 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -82,7 +82,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 @@ -94,7 +94,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 @@ -104,7 +104,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..4b73e7d5ac0f 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" @@ -179,6 +180,7 @@ static void vcpu_stats_test(struct kvm_vm *vm, int vcpu_id) #define DEFAULT_NUM_VM 4 #define DEFAULT_NUM_VCPU 4 +#define INT_MAX_LEN 10 /* * Usage: kvm_bin_form_stats [#vm] [#vcpu] @@ -230,8 +232,37 @@ 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; + + len = strlen(KVM_DEBUGFS_PATH) + 2 * INT_MAX_LEN + 3; + vm_dir_path = malloc(len); + TEST_ASSERT(vm_dir_path, "Allocate memory for VM directory path"); + vcpu_dir_path = malloc(len + INT_MAX_LEN + 6); + 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), "VCPU 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