The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH resend3 2/2] itimers: fix periodic tics precision
@ 2009-05-18 11:50 Stanislaw Gruszka
  2009-05-19 14:53 ` Thomas Gleixner
  0 siblings, 1 reply; 2+ messages in thread
From: Stanislaw Gruszka @ 2009-05-18 11:50 UTC (permalink / raw)
  To: Thomas Gleixner
  Cc: linux-kernel@vger.kernel.org, Oleg Nesterov, Peter Zijlstra,
	Ingo Molnar, Andrew Morton

Measure interval of tics generated by ITIMER_VIRT and ITIMER_PROF using ktime
instead of cputime. Calculate error between requested interval and current one,
take it into account when scheduling next tick.

This patch introduce possibility where time between two consecutive tics is
smaller then requested interval, it preserve however dependency that n tick
is generated not earlier than n*interval time - counting from the beginning
of periodic signal generation.

Signed-off-by: Stanislaw Gruszka <sgruszka@redhat.com>
---
Compared to previous patch bug (using value->it_interval instead of
ovalue->it_interavl) is fixed.

 include/linux/sched.h     |    3 ++-
 kernel/fork.c             |    6 ++++--
 kernel/itimer.c           |   23 +++++++++++++----------
 kernel/posix-cpu-timers.c |   24 +++++++++++++++++++++---
 4 files changed, 40 insertions(+), 16 deletions(-)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index 0d8367b..f01ae49 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -456,7 +456,8 @@ struct pacct_struct {
 
 struct cpu_itimer {
 	cputime_t expires;
-	cputime_t incr;
+	ktime_t	incr;
+	u32 err_ns;
 };
 
 /**
diff --git a/kernel/fork.c b/kernel/fork.c
index fbde8e0..325ebd9 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -791,9 +791,11 @@ static void posix_cpu_timers_init_group(struct signal_struct *sig)
 
 	/* Expiration times and increments. */
 	sig->it[0].expires = cputime_zero;
-	sig->it[0].incr = cputime_zero;
+	sig->it[0].incr = ktime_set(0, 0);
+	sig->it[0].err_ns = 0;
 	sig->it[1].expires = cputime_zero;
-	sig->it[1].incr = cputime_zero;
+	sig->it[1].incr = ktime_set(0, 0);
+	sig->it[1].err_ns = 0;
 
 	/* Cached expiration times. */
 	sig->cputime_expires.prof_exp = cputime_zero;
diff --git a/kernel/itimer.c b/kernel/itimer.c
index bb8bde6..5930d94 100644
--- a/kernel/itimer.c
+++ b/kernel/itimer.c
@@ -42,15 +42,16 @@ static struct timeval itimer_get_remtime(struct hrtimer *timer)
 }
 
 static void get_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
-			   struct itimerval *value)
+			   struct itimerval *const value)
 {
-	cputime_t cval, cinterval;
+	cputime_t cval;
+	ktime_t kt_cinterval;
 	struct cpu_itimer *it = &tsk->signal->it[clock_id];
 
 	spin_lock_irq(&tsk->sighand->siglock);
 
 	cval = it->expires;
-	cinterval = it->incr;
+	kt_cinterval = it->incr;
 	if (!cputime_eq(cval, cputime_zero)) {
 		struct task_cputime cputime;
 		cputime_t t;
@@ -77,7 +78,7 @@ static void get_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
 	spin_unlock_irq(&tsk->sighand->siglock);
 
 	cputime_to_timeval(cval, &value->it_value);
-	cputime_to_timeval(cinterval, &value->it_interval);
+	value->it_interval = ktime_to_timeval(kt_cinterval);
 }
 
 int do_getitimer(int which, struct itimerval *value)
@@ -133,18 +134,20 @@ enum hrtimer_restart it_real_fn(struct hrtimer *timer)
 }
 
 static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
-			   struct itimerval *value, struct itimerval *ovalue)
+			   const struct itimerval *const value,
+			   struct itimerval *const ovalue)
 {
-	cputime_t cval, cinterval, nval, ninterval;
+	cputime_t cval, nval;
+	ktime_t kt_cinterval, kt_ninterval;
 	struct cpu_itimer *it = &tsk->signal->it[clock_id];
 
 	nval = timeval_to_cputime(&value->it_value);
-	ninterval = timeval_to_cputime(&value->it_interval);
+	kt_ninterval = timeval_to_ktime(value->it_interval);
 
 	spin_lock_irq(&tsk->sighand->siglock);
 
 	cval = it->expires;
-	cinterval = it->incr;
+	kt_cinterval = it->incr;
 	if (!cputime_eq(cval, cputime_zero) ||
 	    !cputime_eq(nval, cputime_zero)) {
 		if (cputime_gt(nval, cputime_zero))
@@ -152,13 +155,13 @@ static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
 		set_process_cpu_timer(tsk, clock_id, &nval, &cval);
 	}
 	it->expires = nval;
-	it->incr = ninterval;
+	it->incr = kt_ninterval;
 
 	spin_unlock_irq(&tsk->sighand->siglock);
 
 	if (ovalue) {
 		cputime_to_timeval(cval, &ovalue->it_value);
-		cputime_to_timeval(cinterval, &ovalue->it_interval);
+		ovalue->it_interval = ktime_to_timeval(kt_cinterval);
 	}
 }
 
diff --git a/kernel/posix-cpu-timers.c b/kernel/posix-cpu-timers.c
index b6accca..905734b 100644
--- a/kernel/posix-cpu-timers.c
+++ b/kernel/posix-cpu-timers.c
@@ -1080,9 +1080,27 @@ static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
 		return;
 
 	if (cputime_ge(cur_time, it->expires)) {
-		it->expires = it->incr;
-		if (!cputime_eq(it->expires, cputime_zero)) {
-			it->expires = cputime_add(it->expires, cur_time);
+		if (it->incr.tv64 != 0) {
+			ktime_t incr, real_incr, diff;
+			cputime_t cpu_incr;
+			struct timespec ts_incr, ts_real_incr;
+
+			incr = ktime_sub_ns(it->incr, it->err_ns);
+			if (unlikely(incr.tv64 <= 0))
+				incr = ktime_set(0, 1);
+
+			ts_incr = ktime_to_timespec(incr);
+			cpu_incr = timespec_to_cputime(&ts_incr);
+
+			cputime_to_timespec(cpu_incr, &ts_real_incr);
+			real_incr = timespec_to_ktime(ts_real_incr);
+
+			diff = ktime_sub(real_incr, incr);
+			it->err_ns = ktime_to_ns(diff);
+			it->expires = cputime_add(it->expires, cpu_incr);
+		} else {
+			it->expires = cputime_zero;
+			it->err_ns = 0;
 		}
 
 		__group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
-- 
1.6.0.6


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH resend3 2/2] itimers: fix periodic tics precision
  2009-05-18 11:50 [PATCH resend3 2/2] itimers: fix periodic tics precision Stanislaw Gruszka
@ 2009-05-19 14:53 ` Thomas Gleixner
  0 siblings, 0 replies; 2+ messages in thread
From: Thomas Gleixner @ 2009-05-19 14:53 UTC (permalink / raw)
  To: Stanislaw Gruszka
  Cc: linux-kernel@vger.kernel.org, Oleg Nesterov, Peter Zijlstra,
	Ingo Molnar, Andrew Morton

On Mon, 18 May 2009, Stanislaw Gruszka wrote:
>  int do_getitimer(int which, struct itimerval *value)
> @@ -133,18 +134,20 @@ enum hrtimer_restart it_real_fn(struct hrtimer *timer)
>  }
>  
>  static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
> -			   struct itimerval *value, struct itimerval *ovalue)
> +			   const struct itimerval *const value,
> +			   struct itimerval *const ovalue)
>  {
> -	cputime_t cval, cinterval, nval, ninterval;
> +	cputime_t cval, nval;
> +	ktime_t kt_cinterval, kt_ninterval;

  Just nitpicking. That kt_ prefix is not really helpful, but that's
  my personal taste :)

> diff --git a/kernel/posix-cpu-timers.c b/kernel/posix-cpu-timers.c
> index b6accca..905734b 100644
> --- a/kernel/posix-cpu-timers.c
> +++ b/kernel/posix-cpu-timers.c
> @@ -1080,9 +1080,27 @@ static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
>  		return;
>  
>  	if (cputime_ge(cur_time, it->expires)) {
> -		it->expires = it->incr;
> -		if (!cputime_eq(it->expires, cputime_zero)) {
> -			it->expires = cputime_add(it->expires, cur_time);
> +		if (it->incr.tv64 != 0) {
> +			ktime_t incr, real_incr, diff;
> +			cputime_t cpu_incr;
> +			struct timespec ts_incr, ts_real_incr;
> +
> +			incr = ktime_sub_ns(it->incr, it->err_ns);
> +			if (unlikely(incr.tv64 <= 0))
> +				incr = ktime_set(0, 1);
> +
> +			ts_incr = ktime_to_timespec(incr);
> +			cpu_incr = timespec_to_cputime(&ts_incr);
> +
> +			cputime_to_timespec(cpu_incr, &ts_real_incr);
> +			real_incr = timespec_to_ktime(ts_real_incr);

  Yuck, we convert back and forth here. That's lots of really
  expensive math operations.

  ktime -> timespec -> cputime -> timespec -> ktime

  Isn't there a more intelligent way to get the delta ?

  We can precompute the real (cputime) increment of the given
  increment value, which should be always >= the precise increment
  value. We also can precompute the ktime_t value of one cputime
  increment.

  So now we can do:

  it->expires = cputime_add(it->expires, it->cpu_incr);
  it->error = ktime_add(it->error, it->incr_error);
  if (it->error.tv64 >= onecputimetick.tv64) {
  	it->expires--;
	it->error = ktime_sub(it->error, onecputimetick);
  }

  And the whole function boils down to simple add/sub/compare math.

Thanks,

	tglx

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-05-19 14:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-05-18 11:50 [PATCH resend3 2/2] itimers: fix periodic tics precision Stanislaw Gruszka
2009-05-19 14:53 ` Thomas Gleixner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox