Skip to content

Commit abb3a4e

Browse files
time: Introduce get_monotonic_boottime and ktime_get_boottime
This adds new functions that return the monotonic time since boot (in other words, CLOCK_MONOTONIC + suspend time). CC: Jamie Lokier <jamie@shareable.org> CC: Thomas Gleixner <tglx@linutronix.de> CC: Alexander Shishkin <virtuoso@slind.org> CC: Arve Hjønnevåg <arve@android.com> Signed-off-by: John Stultz <john.stultz@linaro.org>
1 parent e06383d commit abb3a4e

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎include/linux/hrtimer.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ static inline int hrtimer_is_hres_active(struct hrtimer *timer)
312312

313313
extern ktime_t ktime_get(void);
314314
extern ktime_t ktime_get_real(void);
315+
extern ktime_t ktime_get_boottime(void);
315316

316317

317318
DECLARE_PER_CPU(struct tick_device, tick_cpu_device);

‎include/linux/time.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ extern void getnstime_raw_and_real(struct timespec *ts_raw,
161161
struct timespec *ts_real);
162162
extern void getboottime(struct timespec *ts);
163163
extern void monotonic_to_bootbased(struct timespec *ts);
164+
extern void get_monotonic_boottime(struct timespec *ts);
164165

165166
extern struct timespec timespec_trunc(struct timespec t, unsigned gran);
166167
extern int timekeeping_valid_for_hres(void);

‎kernel/time/timekeeping.c‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ static void update_wall_time(void)
907907
* getboottime - Return the real time of system boot.
908908
* @ts: pointer to the timespec to be set
909909
*
910-
* Returns the time of day in a timespec.
910+
* Returns the wall-time of boot in a timespec.
911911
*
912912
* This is based on the wall_to_monotonic offset and the total suspend
913913
* time. Calls to settimeofday will affect the value returned (which
@@ -925,6 +925,55 @@ void getboottime(struct timespec *ts)
925925
}
926926
EXPORT_SYMBOL_GPL(getboottime);
927927

928+
929+
/**
930+
* get_monotonic_boottime - Returns monotonic time since boot
931+
* @ts: pointer to the timespec to be set
932+
*
933+
* Returns the monotonic time since boot in a timespec.
934+
*
935+
* This is similar to CLOCK_MONTONIC/ktime_get_ts, but also
936+
* includes the time spent in suspend.
937+
*/
938+
void get_monotonic_boottime(struct timespec *ts)
939+
{
940+
struct timespec tomono, sleep;
941+
unsigned int seq;
942+
s64 nsecs;
943+
944+
WARN_ON(timekeeping_suspended);
945+
946+
do {
947+
seq = read_seqbegin(&xtime_lock);
948+
*ts = xtime;
949+
tomono = wall_to_monotonic;
950+
sleep = total_sleep_time;
951+
nsecs = timekeeping_get_ns();
952+
953+
} while (read_seqretry(&xtime_lock, seq));
954+
955+
set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec + sleep.tv_sec,
956+
ts->tv_nsec + tomono.tv_nsec + sleep.tv_nsec + nsecs);
957+
}
958+
EXPORT_SYMBOL_GPL(get_monotonic_boottime);
959+
960+
/**
961+
* ktime_get_boottime - Returns monotonic time since boot in a ktime
962+
*
963+
* Returns the monotonic time since boot in a ktime
964+
*
965+
* This is similar to CLOCK_MONTONIC/ktime_get, but also
966+
* includes the time spent in suspend.
967+
*/
968+
ktime_t ktime_get_boottime(void)
969+
{
970+
struct timespec ts;
971+
972+
get_monotonic_boottime(&ts);
973+
return timespec_to_ktime(ts);
974+
}
975+
EXPORT_SYMBOL_GPL(ktime_get_boottime);
976+
928977
/**
929978
* monotonic_to_bootbased - Convert the monotonic time to boot based.
930979
* @ts: pointer to the timespec to be converted

0 commit comments

Comments
 (0)