[PATCH 4/5] generic gettimeofday functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This provides generic functions for gettimeofday/settimeofday for archs,
which are ready to completely switch to clocksources.

 kernel/time.c  |    2 +
 kernel/timer.c |   91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+)

Signed-off-by: Roman Zippel <[email protected]>

---

Index: linux-2.6-mm/kernel/time.c
===================================================================
--- linux-2.6-mm.orig/kernel/time.c	2006-04-02 17:23:13.000000000 +0200
+++ linux-2.6-mm/kernel/time.c	2006-04-02 17:23:52.000000000 +0200
@@ -523,6 +523,7 @@ EXPORT_SYMBOL(do_gettimeofday);
 
 
 #else
+#ifndef CONFIG_GENERIC_TIME
 /*
  * Simulate gettimeofday using do_gettimeofday which only allows a timeval
  * and therefore only yields usec accuracy
@@ -537,6 +538,7 @@ void getnstimeofday(struct timespec *tv)
 }
 EXPORT_SYMBOL_GPL(getnstimeofday);
 #endif
+#endif
 
 /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
  * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
Index: linux-2.6-mm/kernel/timer.c
===================================================================
--- linux-2.6-mm.orig/kernel/timer.c	2006-04-02 17:23:36.000000000 +0200
+++ linux-2.6-mm/kernel/timer.c	2006-04-02 17:23:52.000000000 +0200
@@ -1030,6 +1030,97 @@ void clocksource_update(struct pt_regs *
 	softlockup_tick();
 }
 
+#ifdef CONFIG_GENERIC_TIME
+
+/**
+ * get_realtime_clock_ts - Returns the time of day in a timespec
+ * @ts:		pointer to the timespec to be set
+ *
+ * Returns the time of day in a timespec.
+ */
+void getnstimeofday(struct timespec *ts)
+{
+	unsigned long seq, nsec;
+
+	do {
+		seq = read_seqbegin(&xtime_lock);
+		ts->tv_sec = xtime.tv_sec;
+		nsec = clocksource_get_nsec_offset(curr_clocksource);
+	} while (read_seqretry(&xtime_lock, seq));
+
+	while (nsec >= NSEC_PER_SEC) {
+		nsec -= NSEC_PER_SEC;
+		ts->tv_sec++;
+	}
+	ts->tv_nsec = nsec;
+}
+
+EXPORT_SYMBOL(getnstimeofday);
+/**
+ * do_gettimeofday - Returns the time of day in a timeval
+ * @tv:		pointer to the timeval to be set
+ *
+ * NOTE: Users should be converted to using get_realtime_clock_ts()
+ */
+void do_gettimeofday(struct timeval *tv)
+{
+	unsigned long seq, nsec;
+
+	do {
+		seq = read_seqbegin(&xtime_lock);
+		tv->tv_sec = xtime.tv_sec;
+		nsec = clocksource_get_nsec_offset(curr_clocksource);
+	} while (read_seqretry(&xtime_lock, seq));
+
+	while (nsec >= NSEC_PER_SEC) {
+		nsec -= NSEC_PER_SEC;
+		tv->tv_sec++;
+	}
+	tv->tv_usec = nsec / NSEC_PER_USEC;
+}
+
+EXPORT_SYMBOL(do_gettimeofday);
+
+/**
+ * do_settimeofday - Sets the time of day
+ * @tv:		pointer to the timespec variable containing the new time
+ *
+ * Sets the time of day to the new time and update NTP and notify hrtimers
+ */
+int do_settimeofday(struct timespec *ts)
+{
+	time_t wtm_sec;
+	long wtm_nsec;
+
+	if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
+		return -EINVAL;
+
+	write_seqlock_irq(&xtime_lock);
+
+	wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - ts->tv_sec);
+	wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - ts->tv_nsec);
+	set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
+
+	xtime = *ts;
+
+	curr_clocksource->ntp_error = 0;
+	curr_clocksource->cycles_last = curr_clocksource->read();
+	curr_clocksource->xtime_nsec = (u64)xtime.tv_nsec << curr_clocksource->shift;
+
+	ntp_clear();
+
+	write_sequnlock_irq(&xtime_lock);
+
+	/* signal hrtimers about time change */
+	clock_was_set();
+
+	return 0;
+}
+
+EXPORT_SYMBOL(do_settimeofday);
+
+#endif
+
 #ifdef __ARCH_WANT_SYS_ALARM
 
 /*
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[Index of Archives]     [Kernel Newbies]     [Netfilter]     [Bugtraq]     [Photo]     [Stuff]     [Gimp]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Video 4 Linux]     [Linux for the blind]     [Linux Resources]
  Powered by Linux