[lxc-devel] [lxcfs/master] cpu info from cpuacct.usage_percpu if cpuacct.usage_all doesn't exist

lijiaocn on Github lxc-bot at linuxcontainers.org
Thu Feb 21 06:03:39 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 313 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190220/18365bb5/attachment.bin>
-------------- next part --------------
From 8d74a7511f442a2fd6e4bb41feaa2d3b77a5a3c8 Mon Sep 17 00:00:00 2001
From: lijiaocn <lijiaocn at foxmail.com>
Date: Wed, 20 Feb 2019 19:31:32 +0800
Subject: [PATCH] cpu info from cpuacct.usage_percpu if cpuacct.usage_all
 doesn't exist

---
 bindings.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 49 insertions(+), 2 deletions(-)

diff --git a/bindings.c b/bindings.c
index 049418c..9802eb6 100644
--- a/bindings.c
+++ b/bindings.c
@@ -4046,6 +4046,47 @@ static uint64_t get_reaper_age(pid_t pid)
 	return procage;
 }
 
+/*
+ * Returns 0 on success.
+ * Just an implement of read_cpuacct_usage_all.
+ */
+static inline int read_cpuacct_usage_all_percpu(char *cg, char *cpuset, struct cpuacct_usage *cpu_usage, int cpucount, long ticks_per_sec)
+{
+	char *usage_str = NULL;
+	char *delim = " ";
+	char *token = NULL;
+	int j = 0;
+	uint64_t usage;
+	int rv;
+
+	if (!cpu_usage) {
+		return -1;
+	}
+
+	if (!cgfs_get_value("cpuacct", cg, "cpuacct.usage_percpu", &usage_str)) {
+		rv = -1;
+		goto err;
+	}
+
+	// usage_str formt: 120190269376599 151483821809073 145465919648605 14450503259981
+	token = strtok(usage_str, delim);
+	while (NULL != token && j < cpucount)  {
+		sscanf(token,"%lu", &usage);
+		cpu_usage[j].user = usage / 1000.0 / 1000 / 1000 * ticks_per_sec;
+		cpu_usage[j].system = 0;
+		token = strtok(NULL, delim);
+		j++;
+	}
+
+	rv = 0;
+
+err:
+	if (usage_str)
+		free(usage_str);
+
+	return rv;
+}
+
 /*
  * Returns 0 on success.
  * It is the caller's responsibility to free `return_usage`, unless this
@@ -4072,12 +4113,17 @@ static int read_cpuacct_usage_all(char *cg, char *cpuset, struct cpuacct_usage *
 	}
 
 	cpu_usage = malloc(sizeof(struct cpuacct_usage) * cpucount);
+	bzero(cpu_usage,sizeof(struct cpuacct_usage) * cpucount);
 	if (!cpu_usage)
 		return -ENOMEM;
 
 	if (!cgfs_get_value("cpuacct", cg, "cpuacct.usage_all", &usage_str)) {
-		rv = -1;
-		goto err;
+		if (read_cpuacct_usage_all_percpu(cg, cpuset, cpu_usage, cpucount, ticks_per_sec) != 0 ){
+			rv = -1;
+			goto err;
+		}else{
+			goto suc;
+		}
 	}
 
 	if (sscanf(usage_str, "cpu user system\n%n", &read_cnt) != 0) {
@@ -4111,6 +4157,7 @@ static int read_cpuacct_usage_all(char *cg, char *cpuset, struct cpuacct_usage *
 		j++;
 	}
 
+suc: 
 	rv = 0;
 	*return_usage = cpu_usage;
 	*size = cpucount;


More information about the lxc-devel mailing list