Blob Blame History Raw
From 5cd8f63b62394840de818328d275d7f20efe79a7 Mon Sep 17 00:00:00 2001
Message-Id: <5cd8f63b62394840de818328d275d7f20efe79a7.1587532692.git.kamalesh@linux.vnet.ibm.com>
In-Reply-To: <cover.1587532692.git.kamalesh@linux.vnet.ibm.com>
References: <cover.1587532692.git.kamalesh@linux.vnet.ibm.com>
From: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
Date: Tue, 21 Apr 2020 07:10:54 -0500
Subject: [PATCH V4 02/14] lparstat: Remove ppc64_cpu tool dependency
To: powerpc-utils-devel@googlegroups.com
Cc: Tyrel Datwyler <tyreld@linux.ibm.com>,
    Nathan Lynch <nathanl@linux.ibm.com>,
    Naveen N . Rao <naveen.n.rao@linux.vnet.ibm.com>,
    Gautham R . Shenoy <ego@linux.vnet.ibm.com>,
    Vasant Hegde <hegdevasant@linux.vnet.ibm.com>

get_smt_mode(), calls ppc64_cpu tool to get the current SMT mode, this
method of probing has its disadvantages like tightly coupling lparstat
with ppc64_cpu to mention one.  Use, the internal library to fetch this
information.

Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
---
 Makefile.am    |  3 ++-
 src/lparstat.c | 54 ++++++++++++++++++++++++++++++++++----------------
 src/lparstat.h |  2 +-
 3 files changed, 40 insertions(+), 19 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 1f0b4f6..15f8bcc 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -70,7 +70,8 @@ src_nvram_LDADD = -lz @LIBDL@
 
 src_lsprop_SOURCES = src/lsprop.c $(pseries_platform_SOURCES)
 
-src_lparstat_SOURCES = src/lparstat.c src/lparstat.h $(pseries_platform_SOURCES)
+src_lparstat_SOURCES = src/lparstat.c src/lparstat.h $(pseries_platform_SOURCES) \
+		       $(cpu_info_helpers_SOURCES)
 
 src_ppc64_cpu_SOURCES = src/ppc64_cpu.c $(pseries_platform_SOURCES) $(cpu_info_helpers_SOURCES)
 src_ppc64_cpu_LDADD = -lpthread
diff --git a/src/lparstat.c b/src/lparstat.c
index ffb2cfa..b7cb2d2 100644
--- a/src/lparstat.c
+++ b/src/lparstat.c
@@ -31,6 +31,7 @@
 #include <sys/time.h>
 #include "lparstat.h"
 #include "pseries_platform.h"
+#include "cpu_info_helpers.h"
 
 #define LPARCFG_FILE	"/proc/ppc64/lparcfg"
 #define SE_NOT_FOUND	"???"
@@ -38,6 +39,10 @@
 
 static bool o_legacy = false;
 
+static int threads_per_cpu;
+static int cpus_in_system;
+static int threads_in_system;
+
 struct sysentry *get_sysentry(char *name)
 {
 	struct sysentry *se = &system_data[0];
@@ -73,6 +78,16 @@ void get_sysdata(char *name, char **descr, char *value)
 	*descr = se->descr;
 }
 
+static int is_smt_capable(void)
+{
+	return __is_smt_capable(threads_in_system);
+}
+
+static int parse_smt_state(void)
+{
+	return __do_smt(false, cpus_in_system, threads_per_cpu, false);
+}
+
 void get_time()
 {
 	struct timeval t;
@@ -522,32 +537,23 @@ void get_mem_total(struct sysentry *se, char *buf)
 
 void get_smt_mode(struct sysentry *se, char *buf)
 {
-	FILE *f;
-	char line[128];
-	char *cmd = "/usr/sbin/ppc64_cpu --smt";
-	char *first_line;
+	int smt_state = 0;
 
-	f = popen(cmd, "r");
-	if (!f) {
-		fprintf(stderr, "Failed to execute %s\n", cmd);
+	if (!is_smt_capable()) {
+		sprintf(buf, "1");
 		return;
 	}
 
-	first_line = fgets(line, 128, f);
-	pclose(f);
-
-	if (!first_line) {
-		fprintf(stderr, "Could not read output of %s\n", cmd);
+	smt_state = parse_smt_state();
+	if (smt_state < 0) {
+		fprintf(stderr, "Failed to get smt state\n");
 		return;
 	}
 
-	/* The output is either "SMT=x" or "SMT is off", we can cheat
-	 * by looking at line[8] for an 'f'.
-	 */
-	if (line[8] == 'f')
+	if (smt_state == 1)
 		sprintf(buf, "Off");
 	else
-		sprintf(buf, "%c", line[4]);
+		sprintf(buf, "%d", smt_state);
 }
 
 long long get_cpu_time_diff()
@@ -574,6 +580,19 @@ void get_cpu_stat(struct sysentry *se, char *buf)
 	sprintf(buf, "%.2f", percent);
 }
 
+void init_sysinfo(void)
+{
+	int rc = 0;
+
+	/* probe one time system cpu information */
+	rc = get_cpu_info(&threads_per_cpu, &cpus_in_system,
+			  &threads_in_system);
+	if (rc) {
+		fprintf(stderr, "Failed to capture system CPUs information\n");
+		exit(rc);
+	}
+}
+
 void init_sysdata(void)
 {
 	get_time();
@@ -746,6 +765,7 @@ int main(int argc, char *argv[])
 	if (optind < argc)
 		count = atoi(argv[optind++]);
 
+	init_sysinfo();
 	init_sysdata();
 
 	if (i_option)
diff --git a/src/lparstat.h b/src/lparstat.h
index 3aee192..ae84caf 100644
--- a/src/lparstat.h
+++ b/src/lparstat.h
@@ -184,7 +184,7 @@ struct sysentry system_data[] = {
 	 .descr = "Online Memory",
 	 .get = &get_mem_total},
 
-	/* ppc64_cpu --smt */
+	/* smt mode, cpu_info_helpers::__do_smt() */
 	{.name = "smt_state",
 	 .descr = "SMT",
 	 .get = &get_smt_mode},
-- 
2.25.3