From mboxrd@z Thu Jan 1 00:00:00 1970 From: Zachary Amsden Subject: Re: [RFC 1/8] Implement getnsboottime kernel API Date: Wed, 01 Sep 2010 13:56:17 -1000 Message-ID: <4C7EE821.8030804@redhat.com> References: <1283184391-7785-1-git-send-email-glommer@redhat.com> <1283184391-7785-2-git-send-email-glommer@redhat.com> <1283184391-7785-3-git-send-email-glommer@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: kvm@vger.kernel.org, avi@redhat.com, mtosatti@redhat.com, riel@redhat.com, peterz@infradead.org, mingo@elte.hu, jeremy@goop.org To: Glauber Costa Return-path: Received: from mx1.redhat.com ([209.132.183.28]:23213 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752323Ab0IAX4e (ORCPT ); Wed, 1 Sep 2010 19:56:34 -0400 In-Reply-To: <1283184391-7785-3-git-send-email-glommer@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: On 08/30/2010 06:06 AM, Glauber Costa wrote: > From: Zachary Amsden > > Add a kernel call to get the number of nanoseconds since boot. This > is generally useful enough to make it a generic call. > > Signed-off-by: Zachary Amsden > --- > include/linux/time.h | 1 + > kernel/time/timekeeping.c | 27 +++++++++++++++++++++++++++ > 2 files changed, 28 insertions(+), 0 deletions(-) > > diff --git a/include/linux/time.h b/include/linux/time.h > index ea3559f..5d04108 100644 > --- a/include/linux/time.h > +++ b/include/linux/time.h > @@ -145,6 +145,7 @@ extern void getnstimeofday(struct timespec *tv); > extern void getrawmonotonic(struct timespec *ts); > extern void getboottime(struct timespec *ts); > extern void monotonic_to_bootbased(struct timespec *ts); > +extern s64 getnsboottime(void); > > extern struct timespec timespec_trunc(struct timespec t, unsigned gran); > extern int timekeeping_valid_for_hres(void); > diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c > index caf8d4d..d250f0a 100644 > --- a/kernel/time/timekeeping.c > +++ b/kernel/time/timekeeping.c > @@ -285,6 +285,33 @@ void ktime_get_ts(struct timespec *ts) > } > EXPORT_SYMBOL_GPL(ktime_get_ts); > > + > +/** > + * getnsboottime - get the bootbased clock in nsec format > + * > + * The function calculates the bootbased clock from the realtime > + * clock and the wall_to_monotonic offset and stores the result > + * in normalized timespec format in the variable pointed to by @ts. > + */ > +s64 getnsboottime(void) > +{ > + unsigned int seq; > + s64 secs, nsecs; > + > + WARN_ON(timekeeping_suspended); > + > + do { > + seq = read_seqbegin(&xtime_lock); > + secs = xtime.tv_sec + wall_to_monotonic.tv_sec; > + secs += total_sleep_time.tv_sec; > + nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec; > + nsecs += total_sleep_time.tv_nsec + timekeeping_get_ns(); > + > + } while (read_seqretry(&xtime_lock, seq)); > + return nsecs + (secs * NSEC_PER_SEC); > +} > +EXPORT_SYMBOL_GPL(getnsboottime); > + > /** > * do_gettimeofday - Returns the time of day in a timeval > * @tv: pointer to the timeval to be set > FWIW, I sent a patch to drop this yesterday, in favor of this approach: +static inline u64 get_kernel_ns(void) +{ + struct timespec ts; + + WARN_ON(preemptible()); + ktime_get_ts(&ts); + monotonic_to_bootbased(&ts); + return timespec_to_ns(&ts); +} +