]> git.baikalelectronics.ru Git - kernel.git/commitdiff
KVM: selftests: Cache binary stats metadata for duration of test
authorBen Gardon <bgardon@google.com>
Mon, 13 Jun 2022 21:25:23 +0000 (21:25 +0000)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 24 Jun 2022 08:51:51 +0000 (04:51 -0400)
In order to improve performance across multiple reads of VM stats, cache
the stats metadata in the VM struct.

Signed-off-by: Ben Gardon <bgardon@google.com>
Message-Id: <20220613212523.3436117-11-bgardon@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
tools/testing/selftests/kvm/include/kvm_util_base.h
tools/testing/selftests/kvm/lib/kvm_util.c

index fd2b52850b29f52112c309bc73b64000ec7cc8d1..b78e3c7a2566e595fd2df3e7079595b465572084 100644 (file)
@@ -84,6 +84,11 @@ struct kvm_vm {
        vm_vaddr_t idt;
        vm_vaddr_t handlers;
        uint32_t dirty_ring_size;
+
+       /* Cache of information for binary stats interface */
+       int stats_fd;
+       struct kvm_stats_header stats_header;
+       struct kvm_stats_desc *stats_desc;
 };
 
 
index 5a0fd368503f81cb16703642e71ece9181692b50..768f3bce0161c204c23f3ae766423f242693fb81 100644 (file)
@@ -547,6 +547,12 @@ void kvm_vm_free(struct kvm_vm *vmp)
        if (vmp == NULL)
                return;
 
+       /* Free cached stats metadata and close FD */
+       if (vmp->stats_fd) {
+               free(vmp->stats_desc);
+               close(vmp->stats_fd);
+       }
+
        /* Free userspace_mem_regions. */
        hash_for_each_safe(vmp->regions.slot_hash, ctr, node, region, slot_node)
                __vm_mem_region_delete(vmp, region, false);
@@ -1935,32 +1941,28 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header,
 void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data,
                   size_t max_elements)
 {
-       struct kvm_stats_desc *stats_desc;
-       struct kvm_stats_header header;
        struct kvm_stats_desc *desc;
        size_t size_desc;
-       int stats_fd;
        int i;
 
-       stats_fd = vm_get_stats_fd(vm);
-
-       read_stats_header(stats_fd, &header);
-
-       stats_desc = read_stats_descriptors(stats_fd, &header);
+       if (!vm->stats_fd) {
+               vm->stats_fd = vm_get_stats_fd(vm);
+               read_stats_header(vm->stats_fd, &vm->stats_header);
+               vm->stats_desc = read_stats_descriptors(vm->stats_fd,
+                                                       &vm->stats_header);
+       }
 
-       size_desc = get_stats_descriptor_size(&header);
+       size_desc = get_stats_descriptor_size(&vm->stats_header);
 
-       for (i = 0; i < header.num_desc; ++i) {
-               desc = (void *)stats_desc + (i * size_desc);
+       for (i = 0; i < vm->stats_header.num_desc; ++i) {
+               desc = (void *)vm->stats_desc + (i * size_desc);
 
                if (strcmp(desc->name, stat_name))
                        continue;
 
-               read_stat_data(stats_fd, &header, desc, data, max_elements);
+               read_stat_data(vm->stats_fd, &vm->stats_header, desc,
+                              data, max_elements);
 
                break;
        }
-
-       free(stats_desc);
-       close(stats_fd);
 }