Skip to contentSkip to conversation

kvm/stats: Update kvm stats to clear on write to their debugfs entry

PATCH Applied Suraj Jitindar Singh 4 participants 7 messages Oct 14, 2016 – Oct 18, 2016 KVM

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm/stats: Update kvm stats to clear on write to their debugfs entry

Various kvm vm and vcpu stats are provided via debugfs entries. Currently there is no way to reset these stats back to zero for the system except by stopping all vms.

Add the ability to clear (reset back to zero) these stats on a per stat basis by writing to the debugfs files. The stats are just reset to zero irrespective of what is actually written to the file.

---
 virt/kvm/kvm_main.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 60 insertions(+), 6 deletions(-)
virt/kvm/kvm_main.c +60 -6
index 81dfc73..141fc03 100644
@@ -595,7 +595,7 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd)
 		stat_data->kvm = kvm;
 		stat_data->offset = p->offset;
 		kvm->debugfs_stat_data[p - debugfs_entries] = stat_data;
-		if (!debugfs_create_file(p->name, 0444,
+		if (!debugfs_create_file(p->name, 0644,
 					 kvm->debugfs_dentry,
 					 stat_data,
 					 stat_fops_per_vm[p->kind]))
@@ -3658,11 +3658,20 @@ static int vm_stat_get_per_vm(void *data, u64 *val)
 	return 0;
 }
 
+static int vm_stat_clear_per_vm(void *data, u64 val)
+{
+	struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
+
+	*(ulong *)((void *)stat_data->kvm + stat_data->offset) = 0;
+
+	return 0;
+}
+
 static int vm_stat_get_per_vm_open(struct inode *inode, struct file *file)
 {
 	__simple_attr_check_format("%llu\n", 0ull);
 	return kvm_debugfs_open(inode, file, vm_stat_get_per_vm,
-				NULL, "%llu\n");
+				vm_stat_clear_per_vm, "%llu\n");
 }
 
 static const struct file_operations vm_stat_get_per_vm_fops = {
@@ -3688,11 +3697,23 @@ static int vcpu_stat_get_per_vm(void *data, u64 *val)
 	return 0;
 }
 
+static int vcpu_stat_clear_per_vm(void *data, u64 val)
+{
+	int i;
+	struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
+	struct kvm_vcpu *vcpu;
+
+	kvm_for_each_vcpu(i, vcpu, stat_data->kvm)
+		*(u64 *)((void *)vcpu + stat_data->offset) = 0;
+
+	return 0;
+}
+
 static int vcpu_stat_get_per_vm_open(struct inode *inode, struct file *file)
 {
 	__simple_attr_check_format("%llu\n", 0ull);
 	return kvm_debugfs_open(inode, file, vcpu_stat_get_per_vm,
-				 NULL, "%llu\n");
+				 vcpu_stat_clear_per_vm, "%llu\n");
 }
 
 static const struct file_operations vcpu_stat_get_per_vm_fops = {
@@ -3727,7 +3748,23 @@ static int vm_stat_get(void *_offset, u64 *val)
 	return 0;
 }
 
-DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
+static int vm_stat_clear(void *_offset, u64 val)
+{
+	unsigned offset = (long)_offset;
+	struct kvm *kvm;
+	struct kvm_stat_data stat_tmp = {.offset = offset};
+
+	spin_lock(&kvm_lock);
+	list_for_each_entry(kvm, &vm_list, vm_list) {
+		stat_tmp.kvm = kvm;
+		vm_stat_clear_per_vm((void *)&stat_tmp, 0);
+	}
+	spin_unlock(&kvm_lock);
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, vm_stat_clear, "%llu\n");
 
 static int vcpu_stat_get(void *_offset, u64 *val)
 {
@@ -3747,7 +3784,24 @@ static int vcpu_stat_get(void *_offset, u64 *val)
 	return 0;
 }
 
-DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
+static int vcpu_stat_clear(void *_offset, u64 val)
+{
+	unsigned offset = (long)_offset;
+	struct kvm *kvm;
+	struct kvm_stat_data stat_tmp = {.offset = offset};
+
+	spin_lock(&kvm_lock);
+	list_for_each_entry(kvm, &vm_list, vm_list) {
+		stat_tmp.kvm = kvm;
+		vcpu_stat_clear_per_vm((void *)&stat_tmp, 0);
+	}
+	spin_unlock(&kvm_lock);
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, vcpu_stat_clear,
+			"%llu\n");
 
 static const struct file_operations *stat_fops[] = {
 	[KVM_STAT_VCPU] = &vcpu_stat_fops,
@@ -3765,7 +3819,7 @@ static int kvm_init_debug(void)
 
 	kvm_debugfs_num_entries = 0;
 	for (p = debugfs_entries; p->name; ++p, kvm_debugfs_num_entries++) {
-		if (!debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
+		if (!debugfs_create_file(p->name, 0644, kvm_debugfs_dir,
 					 (void *)(long)p->offset,
 					 stat_fops[p->kind]))
 			goto out_dir;
Signature · 1 line
2.5.5
#1 lore raw

2016-10-14 17:02+1100, Suraj Jitindar Singh:

Quoted 3 lines

Various kvm vm and vcpu stats are provided via debugfs entries. Currently there is no way to reset these stats back to zero for the system except by stopping all vms.

It is not really resetting, just a special case of current behavior -- top-level stats are a sum of all existing VMs/VCPUs, so we would see 0 if they all had 0.

Quoted 2 lines

Add the ability to clear (reset back to zero) these stats on a per stat basis by writing to the debugfs files.

I can see this being used before a test, but the userspace could also read the value instead of writing and subtract it from the value after the test to get a similar result.

The userspace-only solution is insufficient?

When and what do you zero?

Quoted 2 lines

The stats are just reset to zero
irrespective of what is actually written to the file.

Sounds good. Setting the val would be as simple, but there shouldn't be a reason to and it is a debug interface, so we can change it anytime.

Quoted 2 lines

---

I have applied it to kvm/queue for the moment, thanks.

#2 lore raw
Quoted 23 lines · Radim Krčmář

2016-10-14 17:02+1100, Suraj Jitindar Singh:

Quoted 3 lines

Various kvm vm and vcpu stats are provided via debugfs entries. Currently there is no way to reset these stats back to zero for the system except by stopping all vms.

It is not really resetting, just a special case of current behavior -- top-level stats are a sum of all existing VMs/VCPUs, so we would see 0 if they all had 0.

Quoted 2 lines

Add the ability to clear (reset back to zero) these stats on a per stat basis by writing to the debugfs files.

I can see this being used before a test, but the userspace could also read the value instead of writing and subtract it from the value after the test to get a similar result.

The userspace-only solution is insufficient?

When and what do you zero?

Quoted 2 lines

The stats are just reset to zero
irrespective of what is actually written to the file.

Quoted 7 lines

Sounds good. Setting the val would be as simple, but there shouldn't be a reason to and it is a debug interface, so we can change it anytime.

Quoted 2 lines

---

I have applied it to kvm/queue for the moment, thanks.

FWIW, I was thinking about another change: provide the accumulated values of the counters of all VMs that have been destroyed (basically on vm_destroy read the values and add them on top of the "forever" counters.
Did not find the time to do something like this.

#3 lore raw
Quoted 40 lines · Christian Borntraeger
Quoted 39 lines · Radim Krčmář

2016-10-14 17:02+1100, Suraj Jitindar Singh:

Quoted 5 lines

Various kvm vm and vcpu stats are provided via debugfs entries. Currently there is no way to reset these stats back to zero for the system
except by stopping all vms.

It is not really resetting, just a special case of current behavior --
top-level stats are a sum of all existing VMs/VCPUs, so we would see 0
if they all had 0.

Quoted 4 lines

Add the ability to clear (reset back to zero) these stats on a per stat
basis by writing to the debugfs files.

I can see this being used before a test, but the userspace could also
read the value instead of writing and subtract it from the value after
the test to get a similar result.

The userspace-only solution is insufficient?

When and what do you zero?

Quoted 4 lines

                                       The stats are just reset to zero
irrespective of what is actually written to the file.

Sounds good.  Setting the val would be as simple, but there shouldn't be
a reason to and it is a debug interface, so we can change it anytime.

Quoted 3 lines

---

Quoted 7 lines
Quoted 1 line

I have applied it to kvm/queue for the moment, thanks.

FWIW, I was thinking about another change: provide the accumulated values of
the counters of all VMs that have been destroyed (basically on vm_destroy read
the values and add them on top of the "forever" counters. Did not find the time to do something like this.

This doesn't seem like it would be particularly hard to implement, I'm just not sure of the use case.

Quoted 3 lines
#4 lore raw
In reply to Radim Krčmář
Quoted 11 lines · Radim Krčmář

2016-10-14 17:02+1100, Suraj Jitindar Singh:

Quoted 5 lines

Various kvm vm and vcpu stats are provided via debugfs entries. Currently there is no way to reset these stats back to zero for the system
except by stopping all vms.

It is not really resetting, just a special case of current behavior --
top-level stats are a sum of all existing VMs/VCPUs, so we would see 0
if they all had 0.

Yeah I worded that poorly

Quoted 9 lines
Quoted 5 lines

Add the ability to clear (reset back to zero) these stats on a per stat
basis by writing to the debugfs files.

I can see this being used before a test, but the userspace could also read the value instead of writing and subtract it from the value after
the test to get a similar result.

True

Quoted 4 lines

The userspace-only solution is insufficient?

When and what do you zero?

My use case was just reading these stats as a way to verify that I was actually hitting certain code paths that increment certain stats. It was beneficial to be able to reset them to zero just to make it easier to discern when and by how much they increased.

While it would be trivial to put something together which tracked the change from some baseline value I don't see any reason to not have the functionality to reset them back to zero as well.

Quoted 5 lines
Quoted 5 lines

                                       The stats are just reset to zero
irrespective of what is actually written to the file.

Quoted 8 lines

Sounds good.  Setting the val would be as simple, but there shouldn't be
a reason to and it is a debug interface, so we can change it anytime.

Quoted 3 lines

---

I have applied it to kvm/queue for the moment, thanks.

Thanks,
Suraj

maintainer
#5 lore raw
In reply to Suraj Jitindar Singh

----- Original Message -----

Quoted 6 lines

From: "Suraj Jitindar Singh" <[email protected]>
To: [email protected]
Cc: [email protected], [email protected], "Suraj Jitindar Singh" <[email protected]>
Sent: Friday, October 14, 2016 8:02:42 AM
Subject: [PATCH] kvm/stats: Update kvm stats to clear on write to their debugfs entry

Quoted 7 lines

Various kvm vm and vcpu stats are provided via debugfs entries. Currently there is no way to reset these stats back to zero for the system except by stopping all vms.

Add the ability to clear (reset back to zero) these stats on a per stat basis by writing to the debugfs files. The stats are just reset to zero irrespective of what is actually written to the file.

Even though this is a debug interface, people will still rely on underspecified APIs. Please make it fail with EINVAL unless 0 is written. Apart from this, it's a good idea.

Paolo

Quoted 131 lines
---
 virt/kvm/kvm_main.c | 66
 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 60 insertions(+), 6 deletions(-)
virt/kvm/kvm_main.c +60 -7
index 81dfc73..141fc03 100644
@@ -595,7 +595,7 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd)
 		stat_data->kvm = kvm;
 		stat_data->offset = p->offset;
 		kvm->debugfs_stat_data[p - debugfs_entries] = stat_data;
-		if (!debugfs_create_file(p->name, 0444,
+		if (!debugfs_create_file(p->name, 0644,
 					 kvm->debugfs_dentry,
 					 stat_data,
 					 stat_fops_per_vm[p->kind]))
@@ -3658,11 +3658,20 @@ static int vm_stat_get_per_vm(void *data, u64 *val)
 	return 0;
 }
 
+static int vm_stat_clear_per_vm(void *data, u64 val)
+{
+	struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
+
+	*(ulong *)((void *)stat_data->kvm + stat_data->offset) = 0;
+
+	return 0;
+}
+
 static int vm_stat_get_per_vm_open(struct inode *inode, struct file *file)
 {
 	__simple_attr_check_format("%llu\n", 0ull);
 	return kvm_debugfs_open(inode, file, vm_stat_get_per_vm,
-				NULL, "%llu\n");
+				vm_stat_clear_per_vm, "%llu\n");
 }
 
 static const struct file_operations vm_stat_get_per_vm_fops = {
@@ -3688,11 +3697,23 @@ static int vcpu_stat_get_per_vm(void *data, u64 *val)
 	return 0;
 }
 
+static int vcpu_stat_clear_per_vm(void *data, u64 val)
+{
+	int i;
+	struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
+	struct kvm_vcpu *vcpu;
+
+	kvm_for_each_vcpu(i, vcpu, stat_data->kvm)
+		*(u64 *)((void *)vcpu + stat_data->offset) = 0;
+
+	return 0;
+}
+
 static int vcpu_stat_get_per_vm_open(struct inode *inode, struct file *file)
 {
 	__simple_attr_check_format("%llu\n", 0ull);
 	return kvm_debugfs_open(inode, file, vcpu_stat_get_per_vm,
-				 NULL, "%llu\n");
+				 vcpu_stat_clear_per_vm, "%llu\n");
 }
 
 static const struct file_operations vcpu_stat_get_per_vm_fops = {
@@ -3727,7 +3748,23 @@ static int vm_stat_get(void *_offset, u64 *val)
 	return 0;
 }
 
-DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
+static int vm_stat_clear(void *_offset, u64 val)
+{
+	unsigned offset = (long)_offset;
+	struct kvm *kvm;
+	struct kvm_stat_data stat_tmp = {.offset = offset};
+
+	spin_lock(&kvm_lock);
+	list_for_each_entry(kvm, &vm_list, vm_list) {
+		stat_tmp.kvm = kvm;
+		vm_stat_clear_per_vm((void *)&stat_tmp, 0);
+	}
+	spin_unlock(&kvm_lock);
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, vm_stat_clear, "%llu\n");
 
 static int vcpu_stat_get(void *_offset, u64 *val)
 {
@@ -3747,7 +3784,24 @@ static int vcpu_stat_get(void *_offset, u64 *val)
 	return 0;
 }
 
-DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
+static int vcpu_stat_clear(void *_offset, u64 val)
+{
+	unsigned offset = (long)_offset;
+	struct kvm *kvm;
+	struct kvm_stat_data stat_tmp = {.offset = offset};
+
+	spin_lock(&kvm_lock);
+	list_for_each_entry(kvm, &vm_list, vm_list) {
+		stat_tmp.kvm = kvm;
+		vcpu_stat_clear_per_vm((void *)&stat_tmp, 0);
+	}
+	spin_unlock(&kvm_lock);
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, vcpu_stat_clear,
+			"%llu\n");
 
 static const struct file_operations *stat_fops[] = {
 	[KVM_STAT_VCPU] = &vcpu_stat_fops,
@@ -3765,7 +3819,7 @@ static int kvm_init_debug(void)
 
 	kvm_debugfs_num_entries = 0;
 	for (p = debugfs_entries; p->name; ++p, kvm_debugfs_num_entries++) {
-		if (!debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
+		if (!debugfs_create_file(p->name, 0644, kvm_debugfs_dir,
 					 (void *)(long)p->offset,
 					 stat_fops[p->kind]))
 			goto out_dir;
--

2.5.5

#6 lore raw
Quoted 16 lines · Paolo Bonzini

----- Original Message -----

Quoted 14 lines

From: "Suraj Jitindar Singh" <[email protected]>
To: [email protected]
Cc: [email protected], [email protected], "Suraj Jitindar Singh" <[email protected]>
Sent: Friday, October 14, 2016 8:02:42 AM
Subject: [PATCH] kvm/stats: Update kvm stats to clear on write to their debugfs entry

Various kvm vm and vcpu stats are provided via debugfs entries. Currently there is no way to reset these stats back to zero for the system
except by stopping all vms.

Quoted 8 lines
Quoted 5 lines

Add the ability to clear (reset back to zero) these stats on a per stat
basis by writing to the debugfs files. The stats are just reset to zero
irrespective of what is actually written to the file.

Even though this is a debug interface, people will still rely on underspecified APIs.  Please make it fail with EINVAL unless 0 is written.  Apart from this, it's a good idea.

Ok, I'll update this and send a V2

Quoted 155 lines

Paolo

Quoted 152 lines

---
 virt/kvm/kvm_main.c | 66
 ++++++++++++++++++++++++++++++++++++++++++++++++-----

 1 file changed, 60 insertions(+), 6 deletions(-)
virt/kvm/kvm_main.c +0 -0
index 81dfc73..141fc03 100644
@@ -595,7 +595,7 @@ static int kvm_create_vm_debugfs(struct kvm
 *kvm, int fd)
  		stat_data->kvm = kvm;
  		stat_data->offset = p->offset;

  kvm->debugfs_stat_data[p - debugfs_entries] = stat_data;

virt/kvm/kvm_main.c +1 -1
-		if (!debugfs_create_file(p->name, 0444,
+		if (!debugfs_create_file(p->name, 0644,

   kvm->debugfs_dentry, stat_data, stat_fops_per_vm[p-

Quoted 1 line

kind]))

virt/kvm/kvm_main.c +0 -0
@@ -3658,11 +3658,20 @@ static int vm_stat_get_per_vm(void *data,
 u64 *val)
  	return 0;
  }

virt/kvm/kvm_main.c +3 -0
+static int vm_stat_clear_per_vm(void *data, u64 val)
+{
+	struct kvm_stat_data *stat_data = (struct kvm_stat_data

*)data;

virt/kvm/kvm_main.c +2 -0
+
+	*(ulong *)((void *)stat_data->kvm + stat_data->offset) =

0;

virt/kvm/kvm_main.c +4 -0
+
+	return 0;
+}
+

 static int vm_stat_get_per_vm_open(struct inode *inode, struct file *file)
 {
  __simple_attr_check_format("%llu\n", 0ull); return kvm_debugfs_open(inode, file, vm_stat_get_per_vm,

virt/kvm/kvm_main.c +1 -1
-				NULL, "%llu\n");
+				vm_stat_clear_per_vm, "%llu\n");

 }

 static const struct file_operations vm_stat_get_per_vm_fops = {

virt/kvm/kvm_main.c +0 -0
@@ -3688,11 +3697,23 @@ static int vcpu_stat_get_per_vm(void *data,
 u64 *val)
  	return 0;
  }

virt/kvm/kvm_main.c +4 -0
+static int vcpu_stat_clear_per_vm(void *data, u64 val)
+{
+	int i;
+	struct kvm_stat_data *stat_data = (struct kvm_stat_data

*)data;

virt/kvm/kvm_main.c +8 -0
+	struct kvm_vcpu *vcpu;
+
+	kvm_for_each_vcpu(i, vcpu, stat_data->kvm)
+		*(u64 *)((void *)vcpu + stat_data->offset) = 0;
+
+	return 0;
+}
+

 static int vcpu_stat_get_per_vm_open(struct inode *inode, struct file *file)
 {
  __simple_attr_check_format("%llu\n", 0ull); return kvm_debugfs_open(inode, file, vcpu_stat_get_per_vm,

virt/kvm/kvm_main.c +1 -1
-				 NULL, "%llu\n");
+				 vcpu_stat_clear_per_vm,

"%llu\n");
 }

 static const struct file_operations vcpu_stat_get_per_vm_fops = {

virt/kvm/kvm_main.c +0 -0
@@ -3727,7 +3748,23 @@ static int vm_stat_get(void *_offset, u64
 *val)
  	return 0;
  }


-DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");

virt/kvm/kvm_main.c +17 -0
+static int vm_stat_clear(void *_offset, u64 val)
+{
+	unsigned offset = (long)_offset;
+	struct kvm *kvm;
+	struct kvm_stat_data stat_tmp = {.offset = offset};
+
+	spin_lock(&kvm_lock);
+	list_for_each_entry(kvm, &vm_list, vm_list) {
+		stat_tmp.kvm = kvm;
+		vm_stat_clear_per_vm((void *)&stat_tmp, 0);
+	}
+	spin_unlock(&kvm_lock);
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, vm_stat_clear,

"%llu\n");

 static int vcpu_stat_get(void *_offset, u64 *val) {

virt/kvm/kvm_main.c +0 -0
@@ -3747,7 +3784,24 @@ static int vcpu_stat_get(void *_offset, u64
 *val)
  	return 0;
  }


-DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");

virt/kvm/kvm_main.c +17 -0
+static int vcpu_stat_clear(void *_offset, u64 val)
+{
+	unsigned offset = (long)_offset;
+	struct kvm *kvm;
+	struct kvm_stat_data stat_tmp = {.offset = offset};
+
+	spin_lock(&kvm_lock);
+	list_for_each_entry(kvm, &vm_list, vm_list) {
+		stat_tmp.kvm = kvm;
+		vcpu_stat_clear_per_vm((void *)&stat_tmp, 0);
+	}
+	spin_unlock(&kvm_lock);
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get,

vcpu_stat_clear,

virt/kvm/kvm_main.c +1 -0
+			"%llu\n");


 static const struct file_operations *stat_fops[] = { [KVM_STAT_VCPU] = &vcpu_stat_fops,

virt/kvm/kvm_main.c +0 -0
@@ -3765,7 +3819,7 @@ static int kvm_init_debug(void)


  kvm_debugfs_num_entries = 0;
  for (p = debugfs_entries; p->name; ++p,
kvm_debugfs_num_entries++) {

virt/kvm/kvm_main.c +0 -1
-		if (!debugfs_create_file(p->name, 0444,

kvm_debugfs_dir,

virt/kvm/kvm_main.c +1 -0
+		if (!debugfs_create_file(p->name, 0644,

kvm_debugfs_dir,
   (void *)(long)p->offset, stat_fops[p->kind])) goto out_dir;
--
2.5.5