Skip to contentSkip to conversation

posix_cpu_timers: Some code consolidation v2

PATCH In review Frederic Weisbecker 2 participants 9 messages Mar 15, 2013 – Mar 20, 2013

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

cover posix_cpu_timers: Some code consolidation v2

Hi,

In this version, just a few warnings fixed due to missing type updates. And also a selftest for quick basic breakage checks.

The branch is pullable from:

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/linux-dynticks.git
timers/posix-cpu-timers

Thanks.

Frederic Weisbecker (4):
posix_cpu_timer: Consolidate expiry time type
posix_cpu_timers: Consolidate timer list cleanups
posix_cpu_timers: Consolidate expired timers check
selftests: Add basic posix timers selftests

 include/linux/posix-timers.h                  |   16 +-
 kernel/posix-cpu-timers.c                     |  390 +++++++++----------------
 tools/testing/selftests/Makefile              |    1 +
 tools/testing/selftests/timers/Makefile       |    8 +
 tools/testing/selftests/timers/posix_timers.c |  221 ++++++++++++++
 5 files changed, 378 insertions(+), 258 deletions(-)
 create mode 100644 tools/testing/selftests/timers/Makefile
 create mode 100644 tools/testing/selftests/timers/posix_timers.c
Signature · 1 line
1.7.5.4
#1 lore raw

PATCH 1/4 posix_cpu_timer: Consolidate expiry time type

The posix cpu timer expiry time is stored in a union of two types: a 64 bits field if we rely on scheduler precise accounting, or a cputime_t if we rely on jiffies.

This result in quite some duplicate code and special cases to handle the two types.

Just unify this into a single 64 bits field. cputime_t can always fit into it.

Cc Stanislaw Gruszka <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Oleg Nesterov <[email protected]>
---
 include/linux/posix-timers.h |   16 ++-
 kernel/posix-cpu-timers.c    |  266 +++++++++++++++++-------------------------
 2 files changed, 117 insertions(+), 165 deletions(-)
include/linux/posix-timers.h +11 -5
index 042058f..92a51df 100644
@@ -7,14 +7,20 @@
 #include <linux/timex.h>
 #include <linux/alarmtimer.h>
 
-union cpu_time_count {
-	cputime_t cpu;
-	unsigned long long sched;
-};
+
+static inline unsigned long long cputime_to_expires(cputime_t expires)
+{
+	return (__force unsigned long long)expires;
+}
+
+static inline cputime_t expires_to_cputime(unsigned long long expires)
+{
+	return (__force cputime_t)expires;
+}
 
 struct cpu_timer_list {
 	struct list_head entry;
-	union cpu_time_count expires, incr;
+	unsigned long long expires, incr;
 	struct task_struct *task;
 	int firing;
 };
kernel/posix-cpu-timers.c +106 -160

598 lines not on this page · raw message

Signature · 1 line
1.7.5.4
#2 lore raw

Re: [PATCH 1/4] posix_cpu_timer: Consolidate expiry time type

Quoted 10 lines · On Fri, 15 Mar 2013 17:53:59 +0100 Frederic Weisbecker <[email protected]>…

The posix cpu timer expiry time is stored in a union of two types: a 64 bits field if we rely on scheduler precise accounting, or a cputime_t if we rely on jiffies.

This result in quite some duplicate code and special cases to handle the two types.

Just unify this into a single 64 bits field. cputime_t can always fit into it.

x86_64 allnoconfig:

kernel/posix-cpu-timers.c: In function 'posix_cpu_timer_schedule':
kernel/posix-cpu-timers.c:1127: warning: 'now' may be used uninitialized in this function

It looks like it's always been buggy, but your switch from union cou_time_count' to unsigned long long' made gcc notice it.

#3 lore raw

Re: [PATCH 1/4] posix_cpu_timer: Consolidate expiry time type

2013/3/19 Andrew Morton <[email protected]>:

Quoted 13 lines · On Fri, 15 Mar 2013 17:53:59 +0100 Frederic Weisbecker <[email protected]>…
Quoted 11 lines

The posix cpu timer expiry time is stored in a union of two types: a 64 bits field if we rely on scheduler precise accounting, or a cputime_t if we rely on jiffies.

This result in quite some duplicate code and special cases to handle the two types.

Just unify this into a single 64 bits field. cputime_t can always fit into it.

Quoted 7 lines

x86_64 allnoconfig:

kernel/posix-cpu-timers.c: In function 'posix_cpu_timer_schedule':
kernel/posix-cpu-timers.c:1127: warning: 'now' may be used uninitialized in this function

It looks like it's always been buggy, but your switch from union cou_time_count' to unsigned long long' made gcc notice it.

Ok, from a quick view it seems what's missing is a call to cpu_timer_sample_group() to compute the residual time for further call of timer_gettime(). I'll look at it deeper and send you a fix.

Thanks.

#4 lore raw

PATCH 2/4 posix_cpu_timers: Consolidate timer list cleanups

In reply to Frederic Weisbecker

Cleaning up the posix cpu timers on task exit shares some common code among timer list types, most notably the list traversal and expiry time update.

Unify this in a common helper.

Cc Stanislaw Gruszka <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Oleg Nesterov <[email protected]>
---
 kernel/posix-cpu-timers.c |   48 +++++++++++++++++---------------------------
 1 files changed, 19 insertions(+), 29 deletions(-)
kernel/posix-cpu-timers.c +19 -29
index e5286b5..b30775f 100644
@@ -382,6 +382,21 @@ static int posix_cpu_timer_del(struct k_itimer *timer)
 	return ret;
 }
 
+static void cleanup_timers_list(struct list_head *head,
+				unsigned long long curr)
+{
+	struct cpu_timer_list *timer, *next;
+
+	list_for_each_entry_safe(timer, next, head, entry) {
+		list_del_init(&timer->entry);
+		if (timer->expires < curr) {
+			timer->expires = 0;
+		} else {
+			timer->expires -= curr;
+		}
+	}
+}
+
 /*
  * Clean out CPU timers still ticking when a thread exited.  The task
  * pointer is cleared, and the expiry time is replaced with the residual
@@ -392,37 +407,12 @@ static void cleanup_timers(struct list_head *head,
 			   cputime_t utime, cputime_t stime,
 			   unsigned long long sum_exec_runtime)
 {
-	struct cpu_timer_list *timer, *next;
-	cputime_t ptime = utime + stime;
 
-	list_for_each_entry_safe(timer, next, head, entry) {
-		list_del_init(&timer->entry);
-		if (timer->expires < cputime_to_expires(ptime)) {
-			timer->expires = 0;
-		} else {
-			timer->expires -= cputime_to_expires(ptime);
-		}
-	}
-
-	++head;
-	list_for_each_entry_safe(timer, next, head, entry) {
-		list_del_init(&timer->entry);
-		if (timer->expires < cputime_to_expires(utime)) {
-			timer->expires = 0;
-		} else {
-			timer->expires -= cputime_to_expires(utime);
-		}
-	}
+	cputime_t ptime = utime + stime;
 
-	++head;
-	list_for_each_entry_safe(timer, next, head, entry) {
-		list_del_init(&timer->entry);
-		if (timer->expires < sum_exec_runtime) {
-			timer->expires = 0;
-		} else {
-			timer->expires -= sum_exec_runtime;
-		}
-	}
+	cleanup_timers_list(head, cputime_to_expires(ptime));
+	cleanup_timers_list(++head, cputime_to_expires(utime));
+	cleanup_timers_list(++head, sum_exec_runtime);
 }
 
 /*
Signature · 1 line
1.7.5.4
#5 lore raw

PATCH 3/4 posix_cpu_timers: Consolidate expired timers check

In reply to Frederic Weisbecker

Consolidate the common code amongst per thread and per process timers list on tick time.

List traversal, expiry check and subsequent updates can be shared in a common helper.

Cc Stanislaw Gruszka <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Oleg Nesterov <[email protected]>
---
 kernel/posix-cpu-timers.c |  118 +++++++++++++--------------------------------
 1 files changed, 33 insertions(+), 85 deletions(-)
kernel/posix-cpu-timers.c +33 -85
index b30775f..edf94b6 100644
@@ -812,6 +812,28 @@ static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
 	}
 }
 
+static unsigned long long
+check_timers_list(struct list_head *timers,
+		  struct list_head *firing,
+		  unsigned long long curr)
+{
+	int maxfire = 20;
+
+	while (!list_empty(timers)) {
+		struct cpu_timer_list *t;
+
+		t = list_first_entry(timers, struct cpu_timer_list, entry);
+
+		if (!--maxfire || curr < t->expires)
+			return t->expires;
+
+		t->firing = 1;
+		list_move_tail(&t->entry, firing);
+	}
+
+	return 0;
+}
+
 /*
  * Check for any per-thread CPU timers that have fired and move them off
  * the tsk->cpu_timers[N] list onto the firing list.  Here we update the
@@ -820,54 +842,20 @@ static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
 static void check_thread_timers(struct task_struct *tsk,
 				struct list_head *firing)
 {
-	int maxfire;
 	struct list_head *timers = tsk->cpu_timers;
 	struct signal_struct *const sig = tsk->signal;
+	struct task_cputime *tsk_expires = &tsk->cputime_expires;
+	unsigned long long expires;
 	unsigned long soft;
 
-	maxfire = 20;
-	tsk->cputime_expires.prof_exp = 0;
-	while (!list_empty(timers)) {
-		struct cpu_timer_list *t = list_first_entry(timers,
-						      struct cpu_timer_list,
-						      entry);
-		if (!--maxfire || prof_ticks(tsk) < t->expires) {
-			tsk->cputime_expires.prof_exp = expires_to_cputime(t->expires);
-			break;
-		}
-		t->firing = 1;
-		list_move_tail(&t->entry, firing);
-	}
+	expires = check_timers_list(timers, firing, prof_ticks(tsk));
+	tsk_expires->prof_exp = expires_to_cputime(expires);
 
-	++timers;
-	maxfire = 20;
-	tsk->cputime_expires.virt_exp = 0;
-	while (!list_empty(timers)) {
-		struct cpu_timer_list *t = list_first_entry(timers,
-						      struct cpu_timer_list,
-						      entry);
-		if (!--maxfire || virt_ticks(tsk) < t->expires) {
-			tsk->cputime_expires.virt_exp = expires_to_cputime(t->expires);
-			break;
-		}
-		t->firing = 1;
-		list_move_tail(&t->entry, firing);
-	}
+	expires = check_timers_list(++timers, firing, virt_ticks(tsk));
+	tsk_expires->virt_exp = expires_to_cputime(expires);
 
-	++timers;
-	maxfire = 20;
-	tsk->cputime_expires.sched_exp = 0;
-	while (!list_empty(timers)) {
-		struct cpu_timer_list *t = list_first_entry(timers,
-						      struct cpu_timer_list,
-						      entry);
-		if (!--maxfire || tsk->se.sum_exec_runtime < t->expires) {
-			tsk->cputime_expires.sched_exp = t->expires;
-			break;
-		}
-		t->firing = 1;
-		list_move_tail(&t->entry, firing);
-	}
+	tsk_expires->sched_exp = check_timers_list(++timers, firing,
+						   tsk->se.sum_exec_runtime);
 
 	/*
 	 * Check for the special case thread timers.
@@ -967,7 +955,6 @@ static inline int task_cputime_zero(const struct task_cputime *cputime)
 static void check_process_timers(struct task_struct *tsk,
 				 struct list_head *firing)
 {
-	int maxfire;
 	struct signal_struct *const sig = tsk->signal;
 	unsigned long long utime, ptime, virt_expires, prof_expires;
 	unsigned long long sum_sched_runtime, sched_expires;
@@ -982,49 +969,10 @@ static void check_process_timers(struct task_struct *tsk,
 	utime = cputime_to_expires(cputime.utime);
 	ptime = utime + cputime_to_expires(cputime.stime);
 	sum_sched_runtime = cputime.sum_exec_runtime;
-	maxfire = 20;
-	prof_expires = 0;
-	while (!list_empty(timers)) {
-		struct cpu_timer_list *tl = list_first_entry(timers,
-						      struct cpu_timer_list,
-						      entry);
-		if (!--maxfire || ptime < tl->expires) {
-			prof_expires = tl->expires;
-			break;
-		}
-		tl->firing = 1;
-		list_move_tail(&tl->entry, firing);
-	}
 
-	++timers;
-	maxfire = 20;
-	virt_expires = 0;
-	while (!list_empty(timers)) {
-		struct cpu_timer_list *tl = list_first_entry(timers,
-						      struct cpu_timer_list,
-						      entry);
-		if (!--maxfire || utime < tl->expires) {
-			virt_expires = tl->expires;
-			break;
-		}
-		tl->firing = 1;
-		list_move_tail(&tl->entry, firing);
-	}
-
-	++timers;
-	maxfire = 20;
-	sched_expires = 0;
-	while (!list_empty(timers)) {
-		struct cpu_timer_list *tl = list_first_entry(timers,
-						      struct cpu_timer_list,
-						      entry);
-		if (!--maxfire || sum_sched_runtime < tl->expires) {
-			sched_expires = tl->expires;
-			break;
-		}
-		tl->firing = 1;
-		list_move_tail(&tl->entry, firing);
-	}
+	prof_expires = check_timers_list(timers, firing, ptime);
+	virt_expires = check_timers_list(++timers, firing, utime);
+	sched_expires = check_timers_list(++timers, firing, sum_sched_runtime);
 
 	/*
 	 * Check for the special case process timers.
Signature · 1 line
1.7.5.4
#6 lore raw

PATCH 4/4 selftests: Add basic posix timers selftests

In reply to Frederic Weisbecker

Add some initial basic tests on a few posix timers interface such as setitimer() and timer_settime().

These simply check that expiration happens in a reasonable timeframe after expected elapsed clock time (user time, user + system time, real time, ...).

This is helpful for finding basic breakages while hacking on this subsystem.

Cc Stanislaw Gruszka <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Steven Rostedt <[email protected]>
---
 tools/testing/selftests/Makefile              |    1 +
 tools/testing/selftests/timers/Makefile       |    8 +
 tools/testing/selftests/timers/posix_timers.c |  221 +++++++++++++++++++++++++
 3 files changed, 230 insertions(+), 0 deletions(-)
 create mode 100644 tools/testing/selftests/timers/Makefile
 create mode 100644 tools/testing/selftests/timers/posix_timers.c
tools/testing/selftests/Makefile +1 -0
index 3cc0ad7..4bdaede 100644
@@ -5,6 +5,7 @@ TARGETS += vm
 TARGETS += cpu-hotplug
 TARGETS += memory-hotplug
 TARGETS += efivarfs
+TARGETS += timers
 
 all:
 	for TARGET in $(TARGETS); do \
tools/testing/selftests/timers/Makefile new file +8 -0
new file mode 100644
index 0000000..eb2859f
@@ -0,0 +1,8 @@
+all:
+	gcc posix_timers.c -o posix_timers -lrt
+
+run_tests: all
+	./posix_timers
+
+clean:
+	rm -f ./posix_timers
tools/testing/selftests/timers/posix_timers.c new file +221 -0

221 lines not on this page · raw message

Signature · 1 line
1.7.5.4
#7 lore raw

Re: [PATCH 4/4] selftests: Add basic posix timers selftests

Quoted 7 lines · On Fri, 15 Mar 2013 17:54:02 +0100 Frederic Weisbecker <[email protected]>…

Add some initial basic tests on a few posix timers interface such as setitimer() and timer_settime().

These simply check that expiration happens in a reasonable timeframe after expected elapsed clock time (user time, user + system time, real time, ...).

Quoted 6 lines

This is helpful for finding basic breakages while hacking on this subsystem.

...

 tools/testing/selftests/timers/Makefile       |    8 +

I wonder if this should be tools/testing/posix-timers/, to leave room in the namespace for other varieties of timer.

#8 lore raw

Re: [PATCH 4/4] selftests: Add basic posix timers selftests

2013/3/18 Andrew Morton <[email protected]>:

Quoted 12 lines · On Fri, 15 Mar 2013 17:54:02 +0100 Frederic Weisbecker <[email protected]>…
Quoted 11 lines

Add some initial basic tests on a few posix timers interface such as setitimer() and timer_settime().

These simply check that expiration happens in a reasonable timeframe after expected elapsed clock time (user time, user + system time, real time, ...).

This is helpful for finding basic breakages while hacking on this subsystem.

Quoted 6 lines
Quoted 3 lines

...

 tools/testing/selftests/timers/Makefile       |    8 +

I wonder if this should be tools/testing/posix-timers/, to leave room in the namespace for other varieties of timer.

In fact I called it timers/ on purpose such that some more timers related selftests can be added and gathered in the future. But I don't mind if we keep very single purpose directories like tools/testing/posix-timers/