vcpustat: modify vcpu info acquirement from debugfs

Previous judgement to determine whether the vcpustat info matches the process is:
strstr(buf, pid) == buf + 1
But there is an exception that the kvm exit times may contain process pid string.
And then, we will calculate the delta between two defferent process.
So, modify this judgement codition.
This commit is contained in:
nocjj 2020-09-27 15:25:41 +08:00 committed by Euler Robot
parent 8ee1001f41
commit d8f7c63405

View File

@ -0,0 +1,54 @@
From 3e17cc136a1cb2a2ae5798448c553b16694ec408 Mon Sep 17 00:00:00 2001
From: nocjj <1250062498@qq.com>
Date: Sun, 27 Sep 2020 15:25:41 +0800
Subject: [PATCH] vcpustat: modify vcpu info acquirement from debugfs
Previous judgement to determine whether the vcpustat info matches the process is:
strstr(buf, pid) == buf + 1
But there is an exception that the kvm exit times may contain process pid string.
And then, we will calculate the delta between two defferent process.
So, modify this judgement codition.
---
src/vcpu_stat.c | 10 ++++------
1 file changed, 4 insertions(+), 6 deletions(-)
diff --git a/src/vcpu_stat.c b/src/vcpu_stat.c
index 2076563..7ec2371 100644
--- a/src/vcpu_stat.c
+++ b/src/vcpu_stat.c
@@ -15,7 +15,6 @@
#include "type.h"
#include "vcpu_stat.h"
-#define PID_STRING_SIZE 20
#define KVM_VCPU_STAT_PATH "/sys/kernel/debug/kvm/vcpu_stat"
struct file_item vcpu_stat_stab[] = {
@@ -57,12 +56,9 @@ const int vcpu_stat_size = sizeof(vcpu_stat_stab) / sizeof(struct file_item);
int get_vcpu_stat(struct domain *dom)
{
char buf[BUF_SIZE];
- char pid[PID_STRING_SIZE];
+ unsigned int pid;
FILE *fp = NULL;
- if (snprintf(pid, PID_STRING_SIZE, "%u", dom->pid) < 0) {
- return -1;
- }
fp = fopen(KVM_VCPU_STAT_PATH, "r");
if (!fp) {
return -1;
@@ -72,7 +68,9 @@ int get_vcpu_stat(struct domain *dom)
char *p_next = NULL;
int i = 0;
- if (strstr(buf, pid) == NULL) {
+ /* judge whether vcpu pid is match */
+ sscanf(buf, "%u", &pid);
+ if (pid != dom->pid) {
continue;
}
for (p = strtok_r(buf, " \t\r\n", &p_next); p && i < vcpu_stat_size;
--
2.23.0