Skip to contentSkip to conversation

Add CLOCK_BOOTTIME functionality

PATCH In review John Stultz 2 participants 8 messages Feb 1, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Add CLOCK_BOOTTIME functionality

This patchset extends the hrtimers code to handle additional clockids and then introduces CLOCK_BOOTTIME which is identical to CLOCK_MONOTONIC, except it also includes any time spent in suspend (currently as measured by read_persistent_clock()).

CC Jamie Lokier <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Alexander Shishkin <[email protected]>
CC Arve Hjønnevåg <[email protected]>

John Stultz (2):
[RFC] hrtimers: extend hrtimer base code to handle more then 2
clockids
[RFC] hrtimers: Add CLOCK_BOOTTIME clockid, hrtimerbase and posix
interface

 include/linux/hrtimer.h   |    8 ++++-
 include/linux/time.h      |    4 ++
 kernel/hrtimer.c          |   71 ++++++++++++++++++++++++++++++++--------
 kernel/posix-timers.c     |   16 ++++++++-
 kernel/time/timekeeping.c |   79 ++++++++++++++++++++++++++++++++++++++++++++-
 5 files changed, 160 insertions(+), 18 deletions(-)
Signature · 1 line
1.7.3.2.146.gca209
#1 lore raw

PATCH 1/2 hrtimers: extend hrtimer base code to handle more then 2 clockids

The hrtimer code is written mainly with CLOCK_REALTIME and CLOCK_MONOTONIC in mind. These are clockids 0 and 1 resepctively. However, if we are to introduce any new hrtimer bases, using new clockids, we have to skip the cputimers (clockids 2,3) as well as other clockids that may not impelement timers.

This patch adds a little bit of indirection between the clockid and the base, so that we can extend the base by one when we add a new clockid at number 7 or so.

CC Jamie Lokier <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Alexander Shishkin <[email protected]>
CC Arve Hjønnevåg <[email protected]>
---
 include/linux/hrtimer.h |    6 ++++-
 kernel/hrtimer.c        |   55 +++++++++++++++++++++++++++++++++++-----------
 2 files changed, 47 insertions(+), 14 deletions(-)
include/linux/hrtimer.h +5 -1
index f376ddc..20b8e66 100644
@@ -148,7 +148,11 @@ struct hrtimer_clock_base {
 #endif
 };
 
-#define HRTIMER_MAX_CLOCK_BASES 2
+enum  hrtimer_base_type {
+	HRTIMER_BASE_REALTIME,
+	HRTIMER_BASE_MONOTONIC,
+	HRTIMER_MAX_CLOCK_BASES,
+};
 
 /*
  * struct hrtimer_cpu_base - the per cpu clock bases
kernel/hrtimer.c +42 -13

120 lines not on this page · raw message

Signature · 1 line
1.7.3.2.146.gca209
#2 lore raw

Re: [PATCH 1/2] hrtimers: extend hrtimer base code to handle more then 2 clockids

kernel/hrtimer.c line 56

Quoted 10 lines · John Stultz
kernel/hrtimer.c +1 -5
@@ -53,11 +53,10 @@
 /*
  * The timer bases:
  *
- * Note: If we want to add new timer bases, we have to skip the two
- * clock ids captured by the cpu-timers. We do this by holding empty
- * entries rather than doing math adjustment of the clock ids.
- * This ensures that we capture erroneous accesses to these clock ids
- * rather than moving them into the range of valid clock id's.
+ * There are more clockids then hrtimer bases. Thus, we index

s/then/than/

Quoted 12 lines
kernel/hrtimer.c +5 -0
+ * into the timer bases by the hrtimer_base_type enum. When trying
+ * to reach a base using a clockid, hrtimer_clockid_to_base()
+ * is used to convert from clockid to the proper hrtimer_base_type.
  */
 DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
 {
@@ -77,6 +76,24 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
 	}
 };
 
+static int hrtimer_clock_to_base_table[MAX_CLOCKS];
+

kernel/hrtimer.c DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = · line 90

Quoted 10 lines
kernel/hrtimer.c +10 -0
+static inline int hrtimer_clockid_to_base(clockid_t clock_id)
+{
+	int ret = hrtimer_clock_to_base_table[clock_id];
+
+	if(ret == -1) {
+		WARN_ON(1);
+		/* We just hit an invalid clock base,
+		 * but returning -1 isn't safe, so
+		 * return the _REALTIME base
+		 */

How can this happen ?

kernel/hrtimer.c DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = · line 94

Quoted 4 lines
kernel/hrtimer.c +4 -0
+		return HRTIMER_BASE_REALTIME; 
+	}
+	return ret;
+}

Thanks,

tglx

#3 lore raw

Re: [PATCH 1/2] hrtimers: extend hrtimer base code to handle more then 2 clockids

Quoted 13 lines · Thomas Gleixner

kernel/hrtimer.c line 56

Quoted 10 lines · John Stultz
kernel/hrtimer.c +1 -5
@@ -53,11 +53,10 @@
 /*
  * The timer bases:
  *
- * Note: If we want to add new timer bases, we have to skip the two
- * clock ids captured by the cpu-timers. We do this by holding empty
- * entries rather than doing math adjustment of the clock ids.
- * This ensures that we capture erroneous accesses to these clock ids
- * rather than moving them into the range of valid clock id's.
+ * There are more clockids then hrtimer bases. Thus, we index

s/then/than/

Thenks! ;)

Quoted 10 lines
Quoted 10 lines
kernel/hrtimer.c +3 -0
+ * into the timer bases by the hrtimer_base_type enum. When trying
+ * to reach a base using a clockid, hrtimer_clockid_to_base()
+ * is used to convert from clockid to the proper hrtimer_base_type.
  */
 DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
 {
@@ -77,6 +76,24 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
 	}
 };
 
Quoted 14 lines

kernel/hrtimer.c DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = · line 90

Quoted 12 lines
kernel/hrtimer.c +12 -0
+static int hrtimer_clock_to_base_table[MAX_CLOCKS];
+
+static inline int hrtimer_clockid_to_base(clockid_t clock_id)
+{
+	int ret = hrtimer_clock_to_base_table[clock_id];
+
+	if(ret == -1) {
+		WARN_ON(1);
+		/* We just hit an invalid clock base,
+		 * but returning -1 isn't safe, so
+		 * return the _REALTIME base
+		 */

How can this happen ?

It can't right now. I'm just worried someone will try to put a clockid that doesn't have an hrtimer base into that function sometime in the future. We can wrap it in a debug config maybe?

thanks
-john

#4 lore raw

Re: [PATCH 1/2] hrtimers: extend hrtimer base code to handle more then 2 clockids

Quoted 15 lines · John Stultz
Quoted 13 lines · Thomas Gleixner

kernel/hrtimer.c DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = · line 90

Quoted 10 lines · John Stultz
kernel/hrtimer.c +10 -0
+static inline int hrtimer_clockid_to_base(clockid_t clock_id)
+{
+	int ret = hrtimer_clock_to_base_table[clock_id];
+
+	if(ret == -1) {
+		WARN_ON(1);
+		/* We just hit an invalid clock base,
+		 * but returning -1 isn't safe, so
+		 * return the _REALTIME base
+		 */

How can this happen ?

Quoted 3 lines

It can't right now. I'm just worried someone will try to put a clockid that doesn't have an hrtimer base into that function sometime in the future. We can wrap it in a debug config maybe?

Hmm. That would have exploded the current code as well. So I won't worry too much about.

Thanks,

tglx

#5 lore raw

PATCH 2/2 hrtimers: Add CLOCK_BOOTTIME clockid, hrtimerbase and posix interface

In reply to John Stultz

CLOCK_MONOTONIC stops while the system is in suspend. This is because to applications system suspend is invisible. However, there is a growing set of applications that are wanting to be suspend-aware, but do not want to deal with the complicatoins of CLOCK_REALTIME (which might jump around if settimeofday is called).

For these applications, I propose a new clockid: CLOCK_BOOTTIME. CLOCK_BOOTTIME is idential to CLOCK_MONOTONIC, except it also includes any time spent in suspend.

This patch adds the new CLOCK_BOOTTIME clockid, as well as the infrastructure needed to support hrtimers against it, and the wiring to expose it out via the posix interface.

CC Jamie Lokier <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Alexander Shishkin <[email protected]>
CC Arve Hjønnevåg <[email protected]>
---
 include/linux/hrtimer.h   |    2 +
 include/linux/time.h      |    4 ++
 kernel/hrtimer.c          |   16 ++++++++-
 kernel/posix-timers.c     |   16 ++++++++-
 kernel/time/timekeeping.c |   79 ++++++++++++++++++++++++++++++++++++++++++++-
 5 files changed, 113 insertions(+), 4 deletions(-)
include/linux/hrtimer.h +2 -0
index 20b8e66..6bc1804 100644
@@ -151,6 +151,7 @@ struct hrtimer_clock_base {
 enum  hrtimer_base_type {
 	HRTIMER_BASE_REALTIME,
 	HRTIMER_BASE_MONOTONIC,
+	HRTIMER_BASE_BOOTTIME,
 	HRTIMER_MAX_CLOCK_BASES,
 };
 
@@ -312,6 +313,7 @@ static inline int hrtimer_is_hres_active(struct hrtimer *timer)
 
 extern ktime_t ktime_get(void);
 extern ktime_t ktime_get_real(void);
+extern ktime_t ktime_get_boottime(void);
 
 
 DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
include/linux/time.h +4 -0
index 1e6d3b5..9c520f4 100644
@@ -126,6 +126,8 @@ unsigned long get_seconds(void);
 struct timespec current_kernel_time(void);
 struct timespec __current_kernel_time(void); /* does not take xtime_lock */
 struct timespec __get_wall_to_monotonic(void); /* does not take xtime_lock */
+extern struct timespec __get_sleep_time(void); /* does not take xtime_lock */
+
 struct timespec get_monotonic_coarse(void);
 
 #define CURRENT_TIME		(current_kernel_time())
@@ -162,6 +164,7 @@ extern void getnstime_raw_and_real(struct timespec *ts_raw,
 		struct timespec *ts_real);
 extern void getboottime(struct timespec *ts);
 extern void monotonic_to_bootbased(struct timespec *ts);
+extern void get_monotonic_boottime(struct timespec *ts);
 
 extern struct timespec timespec_trunc(struct timespec t, unsigned gran);
 extern int timekeeping_valid_for_hres(void);
@@ -292,6 +295,7 @@ struct itimerval {
 #define CLOCK_MONOTONIC_RAW		4
 #define CLOCK_REALTIME_COARSE		5
 #define CLOCK_MONOTONIC_COARSE		6
+#define CLOCK_BOOTTIME			7
 
 /*
  * The IDs of various hardware clocks:
kernel/hrtimer.c +14 -2
index ce6dd95..628c734 100644
@@ -73,6 +73,11 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
 			.get_time = &ktime_get,
 			.resolution = KTIME_LOW_RES,
 		},
+		{
+			.index = CLOCK_BOOTTIME,
+			.get_time = &ktime_get_boottime,
+			.resolution = KTIME_LOW_RES,
+		},
 	}
 };
 
@@ -100,21 +105,26 @@ static inline int hrtimer_clockid_to_base(clockid_t clock_id)
  */
 static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
 {
-	ktime_t xtim, tomono;
-	struct timespec xts, tom;
+	ktime_t xtim, tomono, sleep;
+	struct timespec xts, tom, slp;
 	unsigned long seq;
 
 	do {
 		seq = read_seqbegin(&xtime_lock);
 		xts = __current_kernel_time();
 		tom = __get_wall_to_monotonic();
+		slp = __get_sleep_time();
 	} while (read_seqretry(&xtime_lock, seq));
 
 	xtim = timespec_to_ktime(xts);
 	tomono = timespec_to_ktime(tom);
+	sleep = timespec_to_ktime(slp);
 	base->clock_base[HRTIMER_BASE_REALTIME].softirq_time = xtim;
 	base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time =
 		ktime_add(xtim, tomono);
+	base->clock_base[HRTIMER_BASE_BOOTTIME].softirq_time =
+		ktime_add(base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time,
+				sleep);
 }
 
 /*
@@ -745,6 +755,7 @@ static int hrtimer_switch_to_hres(void)
 	base->hres_active = 1;
 	base->clock_base[HRTIMER_BASE_REALTIME].resolution = KTIME_HIGH_RES;
 	base->clock_base[HRTIMER_BASE_MONOTONIC].resolution = KTIME_HIGH_RES;
+	base->clock_base[HRTIMER_BASE_BOOTTIME].resolution = KTIME_HIGH_RES;
 
 	tick_setup_sched_timer();
 
@@ -1742,6 +1753,7 @@ void __init hrtimers_init(void)
 		hrtimer_clock_to_base_table[i] = -1;
 	hrtimer_clock_to_base_table[CLOCK_REALTIME] = HRTIMER_BASE_REALTIME;
 	hrtimer_clock_to_base_table[CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC;
+	hrtimer_clock_to_base_table[CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME;
 
 	hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
 			  (void *)(long)smp_processor_id());
kernel/posix-timers.c +15 -1
index 93bd2eb..389d5d5 100644
@@ -240,7 +240,7 @@ static int posix_ktime_get_ts(clockid_t which_clock, struct timespec *tp)
 }
 
 /*
- * Get monotonic time for posix timers
+ * Get monotonic-raw time for posix timers
  */
 static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec *tp)
 {
@@ -267,6 +267,14 @@ static int posix_get_coarse_res(const clockid_t which_clock, struct timespec *tp
 	*tp = ktime_to_timespec(KTIME_LOW_RES);
 	return 0;
 }
+
+static int posix_get_boottime(const clockid_t which_clock, struct timespec *tp)
+{
+	get_monotonic_boottime(tp);
+	return 0;
+}
+
+
 /*
  * Initialize everything, well, just everything in Posix clocks/timers ;)
  */
@@ -301,12 +309,18 @@ static __init int init_posix_timers(void)
 		.timer_create = no_timer_create,
 		.nsleep = no_nsleep,
 	};
+	struct k_clock clock_boottime = {
+		.clock_getres = hrtimer_get_res,
+		.clock_get = posix_get_boottime,
+		.clock_set = do_posix_clock_nosettime,
+	};
 
 	register_posix_clock(CLOCK_REALTIME, &clock_realtime);
 	register_posix_clock(CLOCK_MONOTONIC, &clock_monotonic);
 	register_posix_clock(CLOCK_MONOTONIC_RAW, &clock_monotonic_raw);
 	register_posix_clock(CLOCK_REALTIME_COARSE, &clock_realtime_coarse);
 	register_posix_clock(CLOCK_MONOTONIC_COARSE, &clock_monotonic_coarse);
+	register_posix_clock(CLOCK_BOOTTIME, &clock_boottime);
 
 	posix_timers_cache = kmem_cache_create("posix_timers_cache",
 					sizeof (struct k_itimer), 0, SLAB_PANIC,
kernel/time/timekeeping.c +78 -1

91 lines not on this page · raw message

Signature · 1 line
1.7.3.2.146.gca209
#6 lore raw

Re: [PATCH 2/2] hrtimers: Add CLOCK_BOOTTIME clockid, hrtimerbase and posix interface

Quoted 6 lines · John Stultz

CLOCK_MONOTONIC stops while the system is in suspend. This is because to applications system suspend is invisible. However, there is a growing set of applications that are wanting to be suspend-aware, but do not want to deal with the complicatoins of CLOCK_REALTIME (which might jump around if settimeofday is called).

Quoted 7 lines

For these applications, I propose a new clockid: CLOCK_BOOTTIME. CLOCK_BOOTTIME is idential to CLOCK_MONOTONIC, except it also includes any time spent in suspend.

This patch adds the new CLOCK_BOOTTIME clockid, as well as the infrastructure needed to support hrtimers against it, and the wiring to expose it out via the posix interface.

Can you please split that into parts:

- Add the new functions
- Wire up hrtimers, posix-timers

Quoted 4 lines

static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base) {

kernel/hrtimer.c +0 -2
-	ktime_t xtim, tomono;
-	struct timespec xts, tom;

kernel/hrtimer.c static inline int hrtimer_clockid_to_base(clockid_t clock_id) · line 117

Quoted 10 lines
kernel/hrtimer.c +3 -0
+	ktime_t xtim, tomono, sleep;
+	struct timespec xts, tom, slp;
 	unsigned long seq;
 
 	do {
 		seq = read_seqbegin(&xtime_lock);
 		xts = __current_kernel_time();
 		tom = __get_wall_to_monotonic();
+		slp = __get_sleep_time();
 	} while (read_seqretry(&xtime_lock, seq));

Hmm, that conflicts with the xtime_lock cleanup in tip/timers/core.

kernel/posix-timers.c static __init int init_posix_timers(void) · line 315

Quoted 11 lines

/*
* Initialize everything, well, just everything in Posix clocks/timers ;) */

kernel/posix-timers.c +4 -0
@@ -301,12 +309,18 @@ static __init int init_posix_timers(void)
 		.timer_create = no_timer_create,
 		.nsleep = no_nsleep,
 	};
+	struct k_clock clock_boottime = {
+		.clock_getres = hrtimer_get_res,
+		.clock_get = posix_get_boottime,
+		.clock_set = do_posix_clock_nosettime,

That's going to conflict with the posix timer cleanup, but it's easy enough to fix.

Thanks,

tglx

#7 lore raw

Re: [PATCH 2/2] hrtimers: Add CLOCK_BOOTTIME clockid, hrtimerbase and posix interface

Quoted 12 lines · Thomas Gleixner
Quoted 10 lines · John Stultz

CLOCK_MONOTONIC stops while the system is in suspend. This is because to applications system suspend is invisible. However, there is a growing set of applications that are wanting to be suspend-aware, but do not want to deal with the complicatoins of CLOCK_REALTIME (which might jump around if settimeofday is called).

For these applications, I propose a new clockid: CLOCK_BOOTTIME. CLOCK_BOOTTIME is idential to CLOCK_MONOTONIC, except it also includes any time spent in suspend.

Quoted 8 lines
Quoted 3 lines

This patch adds the new CLOCK_BOOTTIME clockid, as well as the infrastructure needed to support hrtimers against it, and the wiring to expose it out via the posix interface.

Can you please split that into parts:

- Add the new functions
- Wire up hrtimers, posix-timers

Will do. And will rebase to tip/timers/core.

thanks
-john