linux-api.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Adrian Hunter <adrian.hunter-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
To: Peter Zijlstra <peterz-wEGCiKHe2LqWVfeAwA7xHQ@public.gmane.org>,
	Pawel Moll <pawel.moll-5wv7dgnIgG8@public.gmane.org>
Cc: ajh mls <ajhmls-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>,
	Richard Cochran
	<richardcochran-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>,
	Steven Rostedt <rostedt-nx8X9YLhiw1AfugRpC6u6w@public.gmane.org>,
	Ingo Molnar <mingo-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>,
	Paul Mackerras <paulus-eUNUBHrolfbYtjvyW6yDsg@public.gmane.org>,
	Arnaldo Carvalho de Melo
	<acme-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>,
	John Stultz <john.stultz-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>,
	Masami Hiramatsu
	<masami.hiramatsu.pt-FCd8Q96Dh0JBDgjK7y7TUQ@public.gmane.org>,
	Christopher Covington
	<cov-sgV2jX0FEOL9JmXXK+q4OQ@public.gmane.org>,
	Namhyung Kim <namhyung-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>,
	David Ahern <dsahern-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>,
	Thomas Gleixner <tglx-hfZtesqFncYOwBW4kG4KsQ@public.gmane.org>,
	Tomeu Vizoso <tomeu-XCtybt49RKsYaV1qd6yewg@public.gmane.org>,
	"linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org"
	<linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>,
	"linux-api-u79uwXL29TY76Z2rM5mHXA@public.gmane.org"
	<linux-api-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>
Subject: Re: [PATCH v5] perf: Use monotonic clock as a source for timestamps
Date: Thu, 12 Feb 2015 12:04:54 +0200	[thread overview]
Message-ID: <54DC7AC6.5010605@intel.com> (raw)
In-Reply-To: <20150211161256.GH2896-IIpfhp3q70z/8w/KjCw3T+5/BudmfyzbbVWyRVo5IupeoWH0uzbU5w@public.gmane.org>

On 11/02/15 18:12, Peter Zijlstra wrote:
> 
> How about something like the below? I _think_ it should mostly work for
> x86, where the tsc is a 64bit wide cycle counter.

It would have to be based on CLOCK_MONOTONIC_RAW not CLOCK_MONOTONIC and you
would have to check the clocksource is TSC.

Why is CLOCK_MONOTONIC preferred anyway - I would have thought any
adjustment would skew performance timings?

> 
> I suppose we should extend the perf userpage time data with
> time_last_cycle and time_mask if/when we want to make this work on
> something with a short counter.
> 
> Of course, at that time we also need to somehow deal with that counter
> wrapping, its hardly practical to go iterate all possible userpg
> instances from a timer handler.
> 
> 
> ---
>  Documentation/kernel-parameters.txt |  9 +++++++
>  arch/x86/kernel/cpu/perf_event.c    | 44 ++++++++++++++++++++++++---------
>  include/linux/perf_event.h          |  6 +++++
>  kernel/events/core.c                | 49 ++++++++++++++++++++++++++++++++++---
>  kernel/time/timekeeping.c           | 30 +++++++++++++++++++++++
>  5 files changed, 123 insertions(+), 15 deletions(-)
> 
> diff --git a/Documentation/kernel-parameters.txt b/Documentation/kernel-parameters.txt
> index 176d4fe4f076..52255676b6e2 100644
> --- a/Documentation/kernel-parameters.txt
> +++ b/Documentation/kernel-parameters.txt
> @@ -91,6 +91,7 @@ the beginning of each description states the restrictions within which a
>  	NUMA	NUMA support is enabled.
>  	NFS	Appropriate NFS support is enabled.
>  	OSS	OSS sound support is enabled.
> +	PERF	Performance events and counters support is enabled.
>  	PV_OPS	A paravirtualized kernel is enabled.
>  	PARIDE	The ParIDE (parallel port IDE) subsystem is enabled.
>  	PARISC	The PA-RISC architecture is enabled.
> @@ -2796,6 +2797,14 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
>  			allocator.  This parameter is primarily	for debugging
>  			and performance comparison.
>  
> +	perf_use_local_clock
> +			[PERF]
> +			Use local_clock() as a source for perf timestamps
> +			generation. This was be the default behaviour and
> +			this parameter can be used to maintain backward
> +			compatibility or on older hardware with expensive
> +			monotonic clock source.
> +
>  	pf.		[PARIDE]
>  			See Documentation/blockdev/paride.txt.
>  
> diff --git a/arch/x86/kernel/cpu/perf_event.c b/arch/x86/kernel/cpu/perf_event.c
> index b71a7f86d68a..436a66632f76 100644
> --- a/arch/x86/kernel/cpu/perf_event.c
> +++ b/arch/x86/kernel/cpu/perf_event.c
> @@ -1952,6 +1952,35 @@ static struct pmu pmu = {
>  	.flush_branch_stack	= x86_pmu_flush_branch_stack,
>  };
>  
> +static void local_clock_user_time(struct perf_event_mmap_page *userpg, u64 now)
> +{
> +	data = cyc2ns_read_begin();
> +
> +	userpg->cap_user_time = 1;
> +	userpg->time_mult = data->cyc2ns_mul;
> +	userpg->time_shift = data->cyc2ns_shift;
> +	userpg->time_offset = data->cyc2ns_offset - now;
> +
> +	userpg->cap_user_time_zero = 1;
> +	userpg->time_zero = data->cyc2ns_offset;
> +
> +	cyc2ns_read_end(data);
> +}
> +
> +extern void notrace __ktime_get_mono_fast(u64 *offset, u32 *mult, u16 *shift);
> +
> +static void ktime_fast_mono_user_time(struct perf_event_mmap_page *userpg, u64 now)
> +{
> +	userpg->cap_user_time = 1;
> +	userpg->cap_user_time_zero = 1;
> +
> +	__ktime_get_mono_fast(&userpg->time_zero,
> +			      &userpg->time_mult,
> +			      &userpg->time_shift);
> +
> +	userpg->offset = userpg->time_zero - now;
> +}
> +
>  void arch_perf_update_userpage(struct perf_event *event,
>  			       struct perf_event_mmap_page *userpg, u64 now)
>  {
> @@ -1966,17 +1995,10 @@ void arch_perf_update_userpage(struct perf_event *event,
>  	if (!sched_clock_stable())
>  		return;
>  
> -	data = cyc2ns_read_begin();
> -
> -	userpg->cap_user_time = 1;
> -	userpg->time_mult = data->cyc2ns_mul;
> -	userpg->time_shift = data->cyc2ns_shift;
> -	userpg->time_offset = data->cyc2ns_offset - now;
> -
> -	userpg->cap_user_time_zero = 1;
> -	userpg->time_zero = data->cyc2ns_offset;
> -
> -	cyc2ns_read_end(data);
> +	if (static_key_false(&perf_use_local_clock_key))
> +		local_clock_user_time(userpg, now);
> +	else
> +		ktime_fast_mono_user_time(userpg, now);
>  }
>  
>  /*
> diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
> index 33262004c310..1d61f968113a 100644
> --- a/include/linux/perf_event.h
> +++ b/include/linux/perf_event.h
> @@ -561,6 +561,12 @@ extern void perf_pmu_enable(struct pmu *pmu);
>  extern int perf_event_task_disable(void);
>  extern int perf_event_task_enable(void);
>  extern int perf_event_refresh(struct perf_event *event, int refresh);
> +
> +extern struct static_key perf_use_local_clock_key = STATIC_KEY_INIT_FALSE;
> +extern void __weak
> +arch_perf_update_userpage(struct perf_event *event,
> +			  struct perf_event_mmap_page *userpg, u64 now);
> +
>  extern void perf_event_update_userpage(struct perf_event *event);
>  extern int perf_event_release_kernel(struct perf_event *event);
>  extern struct perf_event *
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 13209a90b751..7bad385103ea 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -42,6 +42,8 @@
>  #include <linux/module.h>
>  #include <linux/mman.h>
>  #include <linux/compat.h>
> +#include <linux/sysctl.h>
> +#include <linux/jump_label.h>
>  
>  #include "internal.h"
>  
> @@ -322,9 +324,43 @@ extern __weak const char *perf_pmu_name(void)
>  	return "pmu";
>  }
>  
> +struct static_key perf_use_local_clock_key = STATIC_KEY_INIT_FALSE;
> +static bool perf_use_local_clock_param __initdata;
> +static int __init perf_use_local_clock_setup(char *__unused)
> +{
> +	perf_use_local_clock_param = true;
> +	return 1;
> +}
> +__setup("perf_use_local_clock", perf_use_local_clock_setup);
> +
> +static int sysctl_perf_sample_time_clk_id = CLOCK_MONOTONIC;
> +
> +static struct ctl_table perf_sample_time_kern_table[] = {
> +	{
> +		.procname       = "perf_sample_time_clk_id",
> +		.data           = &sysctl_perf_sample_time_clk_id,
> +		.maxlen         = sizeof(int),
> +		.mode           = 0444,
> +		.proc_handler   = proc_dointvec,
> +	},
> +	{}
> +};
> +
> +static struct ctl_table perf_sample_time_root_table[] = {
> +	{
> +		.procname	= "kernel",
> +		.mode		= 0555,
> +		.child		= perf_sample_time_kern_table,
> +	},
> +	{}
> +};
> +
>  static inline u64 perf_clock(void)
>  {
> -	return local_clock();
> +	if (static_key_false(&perf_use_local_clock_key))
> +		return local_clock();
> +	else
> +		return ktime_get_mono_fast_ns();
>  }
>  
>  static inline struct perf_cpu_context *
> @@ -4101,8 +4137,8 @@ static void perf_event_init_userpage(struct perf_event *event)
>  	rcu_read_unlock();
>  }
>  
> -void __weak arch_perf_update_userpage(
> -	struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
> +void __weak arch_perf_update_userpage(struct perf_event *event,
> +				      struct perf_event_mmap_page *userpg, u64 now)
>  {
>  }
>  
> @@ -4487,7 +4523,7 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
>  	if (vma->vm_flags & VM_WRITE)
>  		flags |= RING_BUFFER_WRITABLE;
>  
> -	rb = rb_alloc(nr_pages, 
> +	rb = rb_alloc(nr_pages,
>  		event->attr.watermark ? event->attr.wakeup_watermark : 0,
>  		event->cpu, flags);
>  
> @@ -8516,6 +8552,11 @@ void __init perf_event_init(void)
>  	 */
>  	BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
>  		     != 1024);
> +
> +	if (perf_use_local_clock_param)
> +		static_key_slow_inc(&perf_use_local_clock_key);
> +	else
> +		register_sysctl_table(perf_sample_time_root_table);
>  }
>  
>  static int __init perf_event_sysfs_init(void)
> diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
> index b124af259800..37bed5931a91 100644
> --- a/kernel/time/timekeeping.c
> +++ b/kernel/time/timekeeping.c
> @@ -334,6 +334,36 @@ u64 notrace ktime_get_mono_fast_ns(void)
>  }
>  EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns);
>  
> +void notrace __ktime_get_mono_fast(u64 *offset, u32 *mult, u16 *shift)
> +{
> +	struct tk_read_base *tkr;
> +	unsigned int seq;
> +	cycle_t cycle_now, delta;
> +	u64 nsecs, now;
> +
> +	do {
> +		seq = raw_read_seqcount(&tk_fast_mono.seq);
> +		tkr = tk_fast_mono.base + (seq & 0x01);
> +
> +		cycle_now = tkr->read(tkr->clock);
> +		delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask);
> +
> +		nsec = delta * tkr->mult + tkr->xtime_nsec;
> +		nsec >>= tkr->shift;
> +		nsec += arch_gettimeoffset();
> +
> +		now = ktime_to_ns(tkr->base_mono) + nsec;
> +
> +		*mult = tkr->mult;
> +		*shift = tkr->shift;
> +
> +		nsec = mul_u64_u32_shr(cycle_now, tkr->mult, tkr->shift);
> +
> +		*offset = now - nsec;
> +
> +	} while (read_seqcount_retry(&tk_fast_mono.seq, seq));
> +}
> +
>  #ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
>  
>  static inline void update_vsyscall(struct timekeeper *tk)
> 
> 

  parent reply	other threads:[~2015-02-12 10:04 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-06 16:51 [PATCH v4 0/3] perf: User/kernel time correlation and event generation Pawel Moll
2014-11-06 16:51 ` [PATCH v4 1/3] perf: Use monotonic clock as a source for timestamps Pawel Moll
2015-01-05 13:00   ` Peter Zijlstra
     [not found]     ` <20150105130035.GP30905-ndre7Fmf5hadTX5a5knrm8zTDFooKrT+cvkQGrU6aU0@public.gmane.org>
2015-01-21 15:52       ` Pawel Moll
     [not found]         ` <1421855543.14076.68.camel-5wv7dgnIgG8@public.gmane.org>
2015-01-21 19:48           ` Pawel Moll
     [not found]             ` <1421869684.14076.105.camel-5wv7dgnIgG8@public.gmane.org>
2015-01-21 20:07               ` Pawel Moll
     [not found]   ` <1415292718-19785-2-git-send-email-pawel.moll-5wv7dgnIgG8@public.gmane.org>
2014-11-27 15:05     ` Pawel Moll
     [not found]       ` <1417100750.4371.1.camel-5wv7dgnIgG8@public.gmane.org>
2014-12-11 13:39         ` Pawel Moll
     [not found]           ` <1418305153.4037.1.camel-5wv7dgnIgG8@public.gmane.org>
2015-01-05 13:01             ` Peter Zijlstra
2015-01-21 15:47               ` Pawel Moll
2015-01-16 12:41     ` Adrian Hunter
2015-01-21 20:27     ` [PATCH v5] " Pawel Moll
     [not found]       ` <1421872037-12559-1-git-send-email-pawel.moll-5wv7dgnIgG8@public.gmane.org>
2015-02-02 16:52         ` Pawel Moll
     [not found]           ` <CAN+dfcT_6zZZ4oeyngUE5N0Wtx2B9CvXsfU71m+cuyXpq2KBdw@mail.gmail.com>
     [not found]             ` <CAN+dfcT_6zZZ4oeyngUE5N0Wtx2B9CvXsfU71m+cuyXpq2KBdw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-02-03  9:20               ` Pawel Moll
     [not found]                 ` <1422955245.4944.26.camel-5wv7dgnIgG8@public.gmane.org>
2015-02-11 16:12                   ` Peter Zijlstra
     [not found]                     ` <20150211161256.GH2896-IIpfhp3q70z/8w/KjCw3T+5/BudmfyzbbVWyRVo5IupeoWH0uzbU5w@public.gmane.org>
2015-02-12 10:04                       ` Adrian Hunter [this message]
     [not found]                         ` <54DC7AC6.5010605-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2015-02-12 10:28                           ` Peter Zijlstra
     [not found]                             ` <20150212102814.GK2896-IIpfhp3q70z/8w/KjCw3T+5/BudmfyzbbVWyRVo5IupeoWH0uzbU5w@public.gmane.org>
2015-02-12 15:38                               ` Peter Zijlstra
     [not found]                                 ` <20150212153808.GR24151-ndre7Fmf5hadTX5a5knrm8zTDFooKrT+cvkQGrU6aU0@public.gmane.org>
2015-02-13  0:25                                   ` John Stultz
2015-02-13  7:07                               ` Adrian Hunter
     [not found] ` <1415292718-19785-1-git-send-email-pawel.moll-5wv7dgnIgG8@public.gmane.org>
2014-11-06 16:51   ` [PATCH v4 2/3] perf: Userspace event Pawel Moll
     [not found]     ` <1415292718-19785-3-git-send-email-pawel.moll-5wv7dgnIgG8@public.gmane.org>
2015-01-05 13:12       ` Peter Zijlstra
     [not found]         ` <20150105131237.GR30905-ndre7Fmf5hadTX5a5knrm8zTDFooKrT+cvkQGrU6aU0@public.gmane.org>
2015-01-21 16:01           ` Pawel Moll
2014-11-06 16:51 ` [PATCH v4 3/3] perf: Sample additional clock value Pawel Moll
     [not found]   ` <1415292718-19785-4-git-send-email-pawel.moll-5wv7dgnIgG8@public.gmane.org>
2015-01-05 13:45     ` Peter Zijlstra
2015-01-05 19:17       ` John Stultz
     [not found]       ` <20150105134514.GS30905-ndre7Fmf5hadTX5a5knrm8zTDFooKrT+cvkQGrU6aU0@public.gmane.org>
2015-01-21 17:12         ` Pawel Moll
     [not found]           ` <1421860365.14076.91.camel-5wv7dgnIgG8@public.gmane.org>
2015-01-21 17:44             ` John Stultz
     [not found]               ` <CALAqxLV6ggCO81ntWuYuDjrqNeMePkZBpq92G9-iwMHm7ONuFg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-01-21 17:54                 ` Pawel Moll
     [not found]                   ` <1421862883.14076.99.camel-5wv7dgnIgG8@public.gmane.org>
2015-01-21 18:05                     ` John Stultz
2015-01-23 17:06         ` Pawel Moll
     [not found]           ` <1422032767.14076.151.camel-5wv7dgnIgG8@public.gmane.org>
2015-01-23 18:05             ` David Ahern

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=54DC7AC6.5010605@intel.com \
    --to=adrian.hunter-ral2jqcrhueavxtiumwx3w@public.gmane.org \
    --cc=acme-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
    --cc=ajhmls-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    --cc=cov-sgV2jX0FEOL9JmXXK+q4OQ@public.gmane.org \
    --cc=dsahern-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    --cc=john.stultz-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org \
    --cc=linux-api-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=masami.hiramatsu.pt-FCd8Q96Dh0JBDgjK7y7TUQ@public.gmane.org \
    --cc=mingo-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org \
    --cc=namhyung-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
    --cc=paulus-eUNUBHrolfbYtjvyW6yDsg@public.gmane.org \
    --cc=pawel.moll-5wv7dgnIgG8@public.gmane.org \
    --cc=peterz-wEGCiKHe2LqWVfeAwA7xHQ@public.gmane.org \
    --cc=richardcochran-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    --cc=rostedt-nx8X9YLhiw1AfugRpC6u6w@public.gmane.org \
    --cc=tglx-hfZtesqFncYOwBW4kG4KsQ@public.gmane.org \
    --cc=tomeu-XCtybt49RKsYaV1qd6yewg@public.gmane.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;
as well as URLs for NNTP newsgroup(s).