From: Marcelo Tosatti <mtosatti@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: Tai Yunfang <yunfangtai@tencent.com>,
Xiao Guangrong <guangrong.xiao@gmail.com>,
qemu-devel <qemu-devel@nongnu.org>,
Vadim Rozenfeld <vrozenfe@redhat.com>
Subject: Re: [PATCH] mc146818rtc: fix timer interrupt reinjection
Date: Thu, 10 Oct 2019 09:28:07 -0300 [thread overview]
Message-ID: <20191010122806.GA19189@amt.cnet> (raw)
In-Reply-To: <8319ce4d-2775-a68d-c08b-f4312d9c30a2@redhat.com>
On Wed, Oct 09, 2019 at 11:13:16PM +0200, Paolo Bonzini wrote:
> On 09/10/19 20:40, Marcelo Tosatti wrote:
> > s->period = period;
> > lost_clock += old_irq_coalesced * old_period;
> > - s->irq_coalesced = lost_clock / s->period;
> > + if (old_period) {
> > + s->irq_coalesced = lost_clock / s->period;
> > + }
> > +
> > lost_clock %= s->period;
> > if (old_irq_coalesced != s->irq_coalesced ||
> > old_period != s->period) {
>
> I think none of the code in the "if (s->lost_tick_policy ==
> LOST_TICK_POLICY_SLEW) {" matters if old_period == 0 (and lost_clock
> will always be 0). So perhaps we should place all that big "if" under
> the existing "if (old_period)"?
>
> Or even something like:
This skips reprogramming the timer if the period is equal.
Sending v2 based on your suggestions and patch below.
>
> diff --git a/hw/timer/mc146818rtc.c b/hw/timer/mc146818rtc.c
> index 6cb378751b..3337a8da98 100644
> --- a/hw/timer/mc146818rtc.c
> +++ b/hw/timer/mc146818rtc.c
> @@ -202,25 +202,33 @@ periodic_timer_update(RTCState *s, int64_t current_time, uint32_t old_period)
> int64_t cur_clock, next_irq_clock, lost_clock = 0;
>
> period = rtc_periodic_clock_ticks(s);
> + if (old_period && old_period == period) {
> + return;
> + }
>
> - if (period) {
> - /* compute 32 khz clock */
> - cur_clock =
> - muldiv64(current_time, RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
> + if (!period) {
> + s->irq_coalesced = 0;
> + timer_del(s->periodic_timer);
> + return;
>
> - /*
> - * if the periodic timer's update is due to period re-configuration,
> - * we should count the clock since last interrupt.
> - */
> - if (old_period) {
> - int64_t last_periodic_clock, next_periodic_clock;
> -
> - next_periodic_clock = muldiv64(s->next_periodic_time,
> - RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
> - last_periodic_clock = next_periodic_clock - old_period;
> - lost_clock = cur_clock - last_periodic_clock;
> - assert(lost_clock >= 0);
> - }
> + }
> +
> + /* compute 32 khz clock */
> + cur_clock =
> + muldiv64(current_time, RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
> +
> + /*
> + * if the periodic timer's update is due to period re-configuration,
> + * we should count the clock since last interrupt.
> + */
> + if (old_period) {
> + int64_t last_periodic_clock, next_periodic_clock;
> +
> + next_periodic_clock = muldiv64(s->next_periodic_time,
> + RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
> + last_periodic_clock = next_periodic_clock - old_period;
> + lost_clock = cur_clock - last_periodic_clock;
> + assert(lost_clock >= 0);
>
> /*
> * s->irq_coalesced can change for two reasons:
> @@ -243,13 +251,10 @@ periodic_timer_update(RTCState *s, int64_t current_time, uint32_t old_period)
> lost_clock += old_irq_coalesced * old_period;
> s->irq_coalesced = lost_clock / s->period;
> lost_clock %= s->period;
> - if (old_irq_coalesced != s->irq_coalesced ||
> - old_period != s->period) {
> - DPRINTF_C("cmos: coalesced irqs scaled from %d to %d, "
> - "period scaled from %d to %d\n", old_irq_coalesced,
> - s->irq_coalesced, old_period, s->period);
> - rtc_coalesced_timer_update(s);
> - }
> + DPRINTF_C("cmos: coalesced irqs scaled from %d to %d, "
> + "period scaled from %d to %d\n", old_irq_coalesced,
> + s->irq_coalesced, old_period, s->period);
> + rtc_coalesced_timer_update(s);
> } else {
> /*
> * no way to compensate the interrupt if LOST_TICK_POLICY_SLEW
> @@ -257,16 +262,12 @@ periodic_timer_update(RTCState *s, int64_t current_time, uint32_t old_period)
> */
> lost_clock = MIN(lost_clock, period);
> }
> -
> assert(lost_clock >= 0 && lost_clock <= period);
> -
> - next_irq_clock = cur_clock + period - lost_clock;
> - s->next_periodic_time = periodic_clock_to_ns(next_irq_clock) + 1;
> - timer_mod(s->periodic_timer, s->next_periodic_time);
> - } else {
> - s->irq_coalesced = 0;
> - timer_del(s->periodic_timer);
> }
> +
> + next_irq_clock = cur_clock + period - lost_clock;
> + s->next_periodic_time = periodic_clock_to_ns(next_irq_clock) + 1;
> + timer_mod(s->periodic_timer, s->next_periodic_time);
> }
>
> static void rtc_periodic_timer(void *opaque)
>
>
> Best read with "diff -b".
>
> Paolo
prev parent reply other threads:[~2019-10-10 12:56 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-10-09 18:40 [PATCH] mc146818rtc: fix timer interrupt reinjection Marcelo Tosatti
2019-10-09 21:13 ` Paolo Bonzini
2019-10-10 12:28 ` Marcelo Tosatti [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191010122806.GA19189@amt.cnet \
--to=mtosatti@redhat.com \
--cc=guangrong.xiao@gmail.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=vrozenfe@redhat.com \
--cc=yunfangtai@tencent.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).