[RFC PATCH 16/22 -v2] add get_monotonic_cycles

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: LKML <linux-kernel@...>
Cc: Ingo Molnar <mingo@...>, Linus Torvalds <torvalds@...>, Andrew Morton <akpm@...>, Peter Zijlstra <a.p.zijlstra@...>, Christoph Hellwig <hch@...>, Mathieu Desnoyers <mathieu.desnoyers@...>, Gregory Haskins <ghaskins@...>, Arnaldo Carvalho de Melo <acme@...>, Thomas Gleixner <tglx@...>, Tim Bird <tim.bird@...>, Sam Ravnborg <sam@...>, Frank Ch. Eigler <fche@...>, Steven Rostedt <srostedt@...>
Date: Wednesday, January 9, 2008 - 7:29 pm

The latency tracer needs a way to get an accurate time
without grabbing any locks. Locks themselves might call
the latency tracer and cause at best a slow down.

This patch adds get_monotonic_cycles that returns cycles
from a reliable clock source in a monotonic fashion.

Signed-off-by: Steven Rostedt <srostedt@redhat.com>
---
 include/linux/clocksource.h |    3 ++
 kernel/time/timekeeping.c   |   48 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)

Index: linux-compile-i386.git/kernel/time/timekeeping.c
===================================================================
--- linux-compile-i386.git.orig/kernel/time/timekeeping.c	2008-01-09 14:27:26.000000000 -0500
+++ linux-compile-i386.git/kernel/time/timekeeping.c	2008-01-09 14:34:40.000000000 -0500
@@ -103,6 +103,54 @@ static inline void __get_realtime_clock_
 	timespec_add_ns(ts, nsecs);
 }
 
+cycle_t notrace get_monotonic_cycles(void)
+{
+	cycle_t cycle_now, cycle_delta, cycle_raw, cycle_last;
+
+	do {
+		/*
+		 * cycle_raw and cycle_last can change on
+		 * another CPU and we need the delta calculation
+		 * of cycle_now and cycle_last happen atomic, as well
+		 * as the adding to cycle_raw. We don't need to grab
+		 * any locks, we just keep trying until get all the
+		 * calculations together in one state.
+		 *
+		 * In fact, we __cant__ grab any locks. This
+		 * function is called from the latency_tracer which can
+		 * be called anywhere. To grab any locks (including
+		 * seq_locks) we risk putting ourselves into a deadlock.
+		 */
+		cycle_raw = clock->cycle_raw;
+		cycle_last = clock->cycle_last;
+
+		/* read clocksource: */
+		cycle_now = clocksource_read(clock);
+
+		/* calculate the delta since the last update_wall_time: */
+		cycle_delta = (cycle_now - cycle_last) & clock->mask;
+
+	} while (cycle_raw != clock->cycle_raw ||
+		 cycle_last != clock->cycle_last);
+
+	return cycle_raw + cycle_delta;
+}
+
+unsigned long notrace cycles_to_usecs(cycle_t cycles)
+{
+	u64 ret = cyc2ns(clock, cycles);
+
+	ret += NSEC_PER_USEC/2; /* For rounding in do_div() */
+	do_div(ret, NSEC_PER_USEC);
+
+	return ret;
+}
+
+cycle_t notrace usecs_to_cycles(unsigned long usecs)
+{
+	return ns2cyc(clock, (u64)usecs * 1000);
+}
+
 /**
  * getnstimeofday - Returns the time of day in a timespec
  * @ts:		pointer to the timespec to be set
Index: linux-compile-i386.git/include/linux/clocksource.h
===================================================================
--- linux-compile-i386.git.orig/include/linux/clocksource.h	2008-01-09 14:27:51.000000000 -0500
+++ linux-compile-i386.git/include/linux/clocksource.h	2008-01-09 14:29:44.000000000 -0500
@@ -273,6 +273,9 @@ extern int clocksource_register(struct c
 extern struct clocksource* clocksource_get_next(void);
 extern void clocksource_change_rating(struct clocksource *cs, int rating);
 extern void clocksource_resume(void);
+extern cycle_t get_monotonic_cycles(void);
+extern unsigned long cycles_to_usecs(cycle_t cycles);
+extern cycle_t usecs_to_cycles(unsigned long usecs);
 
 /* used to initialize clock */
 extern struct clocksource clocksource_jiffies;

-- 
--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Wed Jan 9, 7:29 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Tue Jan 15, 5:46 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Tue Jan 15, 6:01 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Tue Jan 15, 6:03 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Tue Jan 15, 6:08 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Tue Jan 15, 9:38 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Tue Jan 15, 11:17 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Wed Jan 16, 9:17 am)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Wed Jan 16, 10:56 am)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Wed Jan 16, 11:06 am)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Wed Jan 16, 11:28 am)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Wed Jan 16, 11:58 am)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Wed Jan 16, 1:00 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Wed Jan 16, 3:43 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Wed Jan 16, 4:17 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Thu Jan 17, 4:08 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Frank Ch. Eigler, (Thu Jan 17, 4:37 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Thu Jan 17, 5:03 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Fri Jan 18, 6:26 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Fri Jan 18, 6:49 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Frank Ch. Eigler, (Fri Jan 18, 11:32 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Fri Jan 18, 7:19 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Frank Ch. Eigler, (Fri Jan 18, 11:36 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Fri Jan 18, 11:55 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Frank Ch. Eigler, (Sat Jan 19, 12:23 am)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Sat Jan 19, 11:29 am)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Wed Jan 16, 4:49 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Wed Jan 16, 1:49 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, john stultz, (Wed Jan 16, 6:36 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, john stultz, (Wed Jan 16, 6:51 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Wed Jan 16, 7:33 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, john stultz, (Wed Jan 16, 10:28 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Wed Jan 16, 10:40 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Wed Jan 16, 10:51 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Wed Jan 16, 10:50 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Wed Jan 16, 11:02 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Paul Mackerras, (Wed Jan 16, 11:21 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Wed Jan 16, 11:39 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Thu Jan 17, 12:25 am)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Thu Jan 17, 12:22 am)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Thu Jan 17, 12:14 am)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Linus Torvalds, (Thu Jan 17, 1:46 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Thu Jan 17, 11:22 am)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Wed Jan 16, 7:39 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Wed Jan 16, 7:50 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Steven Rostedt, (Wed Jan 16, 8:36 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Linus Torvalds, (Wed Jan 16, 9:03 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Wed Jan 16, 9:35 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, john stultz, (Wed Jan 16, 10:20 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Wed Jan 16, 10:35 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, john stultz, (Wed Jan 16, 8:33 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Mathieu Desnoyers, (Wed Jan 16, 10:20 pm)
Re: [RFC PATCH 16/22 -v2] add get_monotonic_cycles, Daniel Walker, (Wed Jan 9, 11:28 pm)