From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752337AbdHKX2t (ORCPT ); Fri, 11 Aug 2017 19:28:49 -0400 Received: from mga09.intel.com ([134.134.136.24]:25982 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753744AbdHKX0m (ORCPT ); Fri, 11 Aug 2017 19:26:42 -0400 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.41,360,1498546800"; d="scan'208";a="118177247" From: Andi Kleen To: acme@kernel.org Cc: jolsa@kernel.org, linux-kernel@vger.kernel.org, Andi Kleen Subject: [PATCH v2 07/19] perf, tools: Add utility function to detect SMT status Date: Fri, 11 Aug 2017 16:26:22 -0700 Message-Id: <20170811232634.30465-7-andi@firstfloor.org> X-Mailer: git-send-email 2.9.4 In-Reply-To: <20170811232634.30465-1-andi@firstfloor.org> References: <20170811232634.30465-1-andi@firstfloor.org> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andi Kleen Add an smt_on() function to return if SMT is enabled or disabled. Used in the next patch. Signed-off-by: Andi Kleen --- tools/perf/util/Build | 1 + tools/perf/util/smt.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/smt.h | 6 ++++++ 3 files changed, 51 insertions(+) create mode 100644 tools/perf/util/smt.c create mode 100644 tools/perf/util/smt.h diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 8d49a989f193..94518c1bf8b6 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -22,6 +22,7 @@ libperf-y += rbtree.o libperf-y += libstring.o libperf-y += bitmap.o libperf-y += hweight.o +libperf-y += smt.o libperf-y += quote.o libperf-y += strbuf.o libperf-y += string.o diff --git a/tools/perf/util/smt.c b/tools/perf/util/smt.c new file mode 100644 index 000000000000..453f6f6f29f3 --- /dev/null +++ b/tools/perf/util/smt.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include "api/fs/fs.h" +#include "smt.h" + +int smt_on(void) +{ + static bool cached; + static int cached_result; + int cpu; + int ncpu; + + if (cached) + return cached_result; + + ncpu = sysconf(_SC_NPROCESSORS_CONF); + for (cpu = 0; cpu < ncpu; cpu++) { + unsigned long long siblings; + char *str; + size_t strlen; + char fn[256]; + + snprintf(fn, sizeof fn, + "devices/system/cpu/cpu%d/topology/thread_siblings", + cpu); + if (sysfs__read_str(fn, &str, &strlen) < 0) + continue; + /* Entry is hex, but does not have 0x, so need custom parser */ + siblings = strtoull(str, NULL, 16); + free(str); + if (hweight64(siblings) > 1) { + cached_result = 1; + cached = true; + break; + } + } + if (!cached) { + cached_result = 0; + cached = true; + } + return cached_result; +} diff --git a/tools/perf/util/smt.h b/tools/perf/util/smt.h new file mode 100644 index 000000000000..b8414b7bcbc8 --- /dev/null +++ b/tools/perf/util/smt.h @@ -0,0 +1,6 @@ +#ifndef SMT_H +#define SMT_H 1 + +int smt_on(void); + +#endif -- 2.9.4