History

[PATCH] Time: Introduce arch generic time accessors

Introduces clocksource switching code and the arch generic time accessor functions that use the clocksource infrastructure.

john stultz committed 20 years ago

Commit cf3c7691 parent 5eb6d203 files · +187 −0Browse files

Unified · Split

3 files
  1. include/linux/time.h +15 −0
  2. kernel/time.c +2 −0
  3. kernel/timer.c +170 −0
include/linux/time.h+15−0
@@ -102,6 +102,7 @@ extern int do_getitimer(int which, struct itimerval *value); · 101 unmodified lines
102102 extern void getnstimeofday(struct timespec *tv);
103103
104104 extern struct timespec timespec_trunc(struct timespec t, unsigned gran);
105+extern int timekeeping_is_continuous(void);
105106
106107 /**
107108 * timespec_to_ns - Convert timespec to nanoseconds
@@ -144,6 +145,20 @@ extern struct timespec ns_to_timespec(const s64 nsec); · 36 unmodified lines
144145 */
145146 extern struct timeval ns_to_timeval(const s64 nsec);
146147
148+/**
149+ * timespec_add_ns - Adds nanoseconds to a timespec
150+ * @a: pointer to timespec to be incremented
151+ * @ns: unsigned nanoseconds value to be added
152+ */
153+static inline void timespec_add_ns(struct timespec *a, u64 ns)
154+{
155+ ns += a->tv_nsec;
156+ while(unlikely(ns >= NSEC_PER_SEC)) {
157+ ns -= NSEC_PER_SEC;
158+ a->tv_sec++;
159+ }
160+ a->tv_nsec = ns;
161+}
147162 #endif /* __KERNEL__ */
148163
149164 #define NFDBITS __NFDBITS
kernel/time.c+2−0
@@ -523,6 +523,7 @@ EXPORT_SYMBOL(do_gettimeofday); · 522 unmodified lines
523523
524524
525525 #else
526+#ifndef CONFIG_GENERIC_TIME
526527 /*
527528 * Simulate gettimeofday using do_gettimeofday which only allows a timeval
528529 * and therefore only yields usec accuracy
@@ -537,6 +538,7 @@ void getnstimeofday(struct timespec *tv) · 8 unmodified lines
537538 }
538539 EXPORT_SYMBOL_GPL(getnstimeofday);
539540 #endif
541+#endif
540542
541543 /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
542544 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
kernel/timer.c+170−0
@@ -795,6 +795,169 @@ u64 current_tick_length(long shift) · 794 unmodified lines
795795 #include <linux/clocksource.h>
796796 static struct clocksource *clock; /* pointer to current clocksource */
797797 static cycle_t last_clock_cycle; /* cycle value at last update_wall_time */
798+
799+#ifdef CONFIG_GENERIC_TIME
800+/**
801+ * __get_nsec_offset - Returns nanoseconds since last call to periodic_hook
802+ *
803+ * private function, must hold xtime_lock lock when being
804+ * called. Returns the number of nanoseconds since the
805+ * last call to update_wall_time() (adjusted by NTP scaling)
806+ */
807+static inline s64 __get_nsec_offset(void)
808+{
809+ cycle_t cycle_now, cycle_delta;
810+ s64 ns_offset;
811+
812+ /* read clocksource: */
813+ cycle_now = read_clocksource(clock);
814+
815+ /* calculate the delta since the last update_wall_time: */
816+ cycle_delta = (cycle_now - last_clock_cycle) & clock->mask;
817+
818+ /* convert to nanoseconds: */
819+ ns_offset = cyc2ns(clock, cycle_delta);
820+
821+ return ns_offset;
822+}
823+
824+/**
825+ * __get_realtime_clock_ts - Returns the time of day in a timespec
826+ * @ts: pointer to the timespec to be set
827+ *
828+ * Returns the time of day in a timespec. Used by
829+ * do_gettimeofday() and get_realtime_clock_ts().
830+ */
831+static inline void __get_realtime_clock_ts(struct timespec *ts)
832+{
833+ unsigned long seq;
834+ s64 nsecs;
835+
836+ do {
837+ seq = read_seqbegin(&xtime_lock);
838+
839+ *ts = xtime;
840+ nsecs = __get_nsec_offset();
841+
842+ } while (read_seqretry(&xtime_lock, seq));
843+
844+ timespec_add_ns(ts, nsecs);
845+}
846+
847+/**
848+ * get_realtime_clock_ts - Returns the time of day in a timespec
849+ * @ts: pointer to the timespec to be set
850+ *
851+ * Returns the time of day in a timespec.
852+ */
853+void getnstimeofday(struct timespec *ts)
854+{
855+ __get_realtime_clock_ts(ts);
856+}
857+
858+EXPORT_SYMBOL(getnstimeofday);
859+
860+/**
861+ * do_gettimeofday - Returns the time of day in a timeval
862+ * @tv: pointer to the timeval to be set
863+ *
864+ * NOTE: Users should be converted to using get_realtime_clock_ts()
865+ */
866+void do_gettimeofday(struct timeval *tv)
867+{
868+ struct timespec now;
869+
870+ __get_realtime_clock_ts(&now);
871+ tv->tv_sec = now.tv_sec;
872+ tv->tv_usec = now.tv_nsec/1000;
873+}
874+
875+EXPORT_SYMBOL(do_gettimeofday);
876+/**
877+ * do_settimeofday - Sets the time of day
878+ * @tv: pointer to the timespec variable containing the new time
879+ *
880+ * Sets the time of day to the new time and update NTP and notify hrtimers
881+ */
882+int do_settimeofday(struct timespec *tv)
883+{
884+ unsigned long flags;
885+ time_t wtm_sec, sec = tv->tv_sec;
886+ long wtm_nsec, nsec = tv->tv_nsec;
887+
888+ if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
889+ return -EINVAL;
890+
891+ write_seqlock_irqsave(&xtime_lock, flags);
892+
893+ nsec -= __get_nsec_offset();
894+
895+ wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
896+ wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
897+
898+ set_normalized_timespec(&xtime, sec, nsec);
899+ set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
900+
901+ ntp_clear();
902+
903+ write_sequnlock_irqrestore(&xtime_lock, flags);
904+
905+ /* signal hrtimers about time change */
906+ clock_was_set();
907+
908+ return 0;
909+}
910+
911+EXPORT_SYMBOL(do_settimeofday);
912+
913+/**
914+ * change_clocksource - Swaps clocksources if a new one is available
915+ *
916+ * Accumulates current time interval and initializes new clocksource
917+ */
918+static int change_clocksource(void)
919+{
920+ struct clocksource *new;
921+ cycle_t now;
922+ u64 nsec;
923+ new = get_next_clocksource();
924+ if (clock != new) {
925+ now = read_clocksource(new);
926+ nsec = __get_nsec_offset();
927+ timespec_add_ns(&xtime, nsec);
928+
929+ clock = new;
930+ last_clock_cycle = now;
931+ printk(KERN_INFO "Time: %s clocksource has been installed.\n",
932+ clock->name);
933+ return 1;
934+ } else if (clock->update_callback) {
935+ return clock->update_callback();
936+ }
937+ return 0;
938+}
939+#else
940+#define change_clocksource() (0)
941+#endif
942+
943+/**
944+ * timeofday_is_continuous - check to see if timekeeping is free running
945+ */
946+int timekeeping_is_continuous(void)
947+{
948+ unsigned long seq;
949+ int ret;
950+
951+ do {
952+ seq = read_seqbegin(&xtime_lock);
953+
954+ ret = clock->is_continuous;
955+
956+ } while (read_seqretry(&xtime_lock, seq));
957+
958+ return ret;
959+}
960+
798961 /*
799962 * timekeeping_init - Initializes the clocksource and common timekeeping values
800963 */
@@ -901,6 +1064,13 @@ static void update_wall_time(void) · 100 unmodified lines
9011064 /* store full nanoseconds into xtime */
9021065 xtime.tv_nsec = remainder_snsecs >> clock->shift;
9031066 remainder_snsecs -= (s64)xtime.tv_nsec << clock->shift;
1067+
1068+ /* check to see if there is a new clocksource to use */
1069+ if (change_clocksource()) {
1070+ error = 0;
1071+ remainder_snsecs = 0;
1072+ calculate_clocksource_interval(clock, tick_nsec);
1073+ }
9041074 }
9051075
9061076 /*

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t