mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Redha Gouicem <redha.gouicem@gmail.com>
To: unlisted-recipients:; (no To-header on input)
Cc: Redha Gouicem <redha.gouicem@gmail.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	Juergen Gross <jgross@suse.com>,
	Vincenzo Frascino <vincenzo.frascino@arm.com>,
	Michael Kelley <mikelley@microsoft.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Krzysztof Piecuch <piecuch@protonmail.com>,
	Martin Molnar <martin.molnar.programming@gmail.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH] x86/tsc: export tsc_khz to sysfs
Date: Thu, 31 Dec 2020 13:28:18 +0100	[thread overview]
Message-ID: <20201231122819.13980-1-redha.gouicem@gmail.com> (raw)

Export the frequency of the tsc clock to user space. This is particularly
useful for benchmarking purposes because it allows to convert tsc cycles
into time. The value is available at:
     /sys/devices/system/cpu/tsc_khz

Signed-off-by: Redha Gouicem <redha.gouicem@gmail.com>
---
 arch/x86/kernel/tsc.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c
index 49d925043171..6ea991516d08 100644
--- a/arch/x86/kernel/tsc.c
+++ b/arch/x86/kernel/tsc.c
@@ -14,6 +14,7 @@
 #include <linux/percpu.h>
 #include <linux/timex.h>
 #include <linux/static_key.h>
+#include <linux/cpu.h>
 
 #include <asm/hpet.h>
 #include <asm/timer.h>
@@ -1412,6 +1413,15 @@ static int __init init_tsc_clocksource(void)
  */
 device_initcall(init_tsc_clocksource);
 
+/* sysfs file to export tsc_khz */
+static ssize_t tsc_khz_show(struct kobject *kobj,
+			    struct kobj_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%u\n", tsc_khz);
+}
+
+struct kobj_attribute tsc_khz_attr = __ATTR_RO(tsc_khz);
+
 static bool __init determine_cpu_tsc_frequencies(bool early)
 {
 	/* Make sure that cpu and tsc are not already calibrated */
@@ -1530,6 +1540,19 @@ void __init tsc_init(void)
 	detect_art();
 }
 
+static int __init tsc_khz_sysfs_init(void)
+{
+	int ret = sysfs_create_file(&cpu_subsys.dev_root->kobj, &tsc_khz_attr.attr);
+
+	if (!ret)
+		pr_info("tsc_khz exported in sysfs\n");
+	else
+		pr_warn("tsc_khz failed to be exported in sysfs\n");
+
+	return ret;
+}
+late_initcall(tsc_khz_sysfs_init);
+
 #ifdef CONFIG_SMP
 /*
  * If we have a constant TSC and are using the TSC for the delay loop,
-- 
2.30.0


                 reply	other threads:[~2020-12-31 12:29 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201231122819.13980-1-redha.gouicem@gmail.com \
    --to=redha.gouicem@gmail.com \
    --cc=bp@alien8.de \
    --cc=hpa@zytor.com \
    --cc=jgross@suse.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.molnar.programming@gmail.com \
    --cc=mikelley@microsoft.com \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=piecuch@protonmail.com \
    --cc=tglx@linutronix.de \
    --cc=vincenzo.frascino@arm.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®