From: Takashi Iwai Subject: Re: Timer instability Date: Tue, 03 Mar 2009 18:41:22 +0100 Message-ID: References: <20090219024611.GA7930@tango.0pointer.de> <200902251736.24306.clive@vacuumtube.org.uk> <200902251834.31492.clive@vacuumtube.org.uk> Mime-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: multipart/mixed; boundary="Multipart_Tue_Mar__3_18:41:22_2009-1" Content-Transfer-Encoding: 7bit Return-path: Received: from mx1.suse.de (ns.suse.de [195.135.220.2]) by alsa0.perex.cz (Postfix) with ESMTP id 8A34C103889 for ; Tue, 3 Mar 2009 18:41:22 +0100 (CET) In-Reply-To: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: alsa-devel-bounces@alsa-project.org Errors-To: alsa-devel-bounces@alsa-project.org To: Clive Messer Cc: alsa-devel@alsa-project.org, Lennart Poettering List-Id: alsa-devel@alsa-project.org --Multipart_Tue_Mar__3_18:41:22_2009-1 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable At Tue, 03 Mar 2009 17:05:31 +0100, I wrote: >=20 > At Wed, 25 Feb 2009 18:34:31 +0000, > Clive Messer wrote: > >=20 > > On Wednesday 25 Feb 2009 18:13:47 Takashi Iwai wrote: > >=20 > > > > > diff --git a/src/pcm/pcm_hw.c b/src/pcm/pcm_hw.c > > > > > index e9ce092..5211d1c 100644 > > > > > --- a/src/pcm/pcm_hw.c > > > > > +++ b/src/pcm/pcm_hw.c > > > > > @@ -131,6 +131,10 @@ struct timespec snd_pcm_hw_fast_tstamp(snd= _pcm_t > > > > > *pcm) static int sync_ptr1(snd_pcm_hw_t *hw, unsigned int flags= ) > > > > > =A0{ > > > > > =A0=A0=A0=A0=A0=A0=A0=A0int err; > > > > > +=A0=A0=A0=A0=A0=A0=A0long old_hwptr, new_hwptr; > > > > > +=A0=A0=A0=A0=A0=A0=A0long old_applptr, new_applptr; > > > > > +=A0=A0=A0=A0=A0=A0=A0old_hwptr =3D hw->sync_ptr->s.status.hw_p= tr; > > > > > +=A0=A0=A0=A0=A0=A0=A0old_applptr =3D hw->sync_ptr->c.control.a= ppl_ptr; > > > > > =A0=A0=A0=A0=A0=A0=A0=A0hw->sync_ptr->flags =3D flags; > > > > > =A0=A0=A0=A0=A0=A0=A0=A0err =3D ioctl((hw)->fd, SNDRV_PCM_IOCTL= _SYNC_PTR, > > > > > (hw)->sync_ptr); if (err < 0) { > > > > > @@ -138,6 +142,11 @@ static int sync_ptr1(snd_pcm_hw_t *hw, uns= igned > > > > > int flags) SYSMSG("SNDRV_PCM_IOCTL_SYNC_PTR failed"); > > > > > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0return err; > > > > > =A0=A0=A0=A0=A0=A0=A0=A0} > > > > > +=A0=A0=A0=A0=A0=A0=A0new_hwptr =3D hw->sync_ptr->s.status.hw_p= tr; > > > > > +=A0=A0=A0=A0=A0=A0=A0new_applptr =3D hw->sync_ptr->c.control.a= ppl_ptr; > > > > > +=A0=A0=A0=A0=A0=A0=A0printf("sync_ptr1: %ld(%ld), %ld(%ld)\n", > > > > > +=A0=A0=A0=A0=A0=A0=A0 =A0 =A0 =A0 new_hwptr, new_hwptr - old_h= wptr, > > > > > +=A0=A0=A0=A0=A0=A0=A0 =A0 =A0 =A0 new_applptr, new_applptr - o= ld_applptr); > > > > > =A0=A0=A0=A0=A0=A0=A0=A0return 0; > > > > > =A0} > > > > > =A0 > >=20 > > With sync_ptr I cannot reproduce the very large numbers. Here's a typ= ical log.=20 > > (Attached). >=20 > Hrm, but still an unexpected jump is found. >=20 > If you build the driver with CONFIG_SND_PCM_XRUN_DEBUG=3Dy, you must > have /proc/asound/card0/pcm0p/xrun_debug. Echo 1 to this (as root) > echo 1 > /proc/asound/card0/pcm0p/xrun_debug > and try the program, see whether any debug message appears. > If any message appears, it means basically an unstable hardware. >=20 > Also, the below is a patch I tried to clean up and improve the > handling. Give it a try (and do echo 1 above for testing). BTW, the original test program is very hard to see what's wrong because it spews out way too many lines. The below is a filtered-out version. It prints only unexpected jumps of avail values (the threshold is set 100 blindly). The output is like below: % ./alsa-time-test hw 21720872 (4987) delta 216 avail 216 delay 4200 21943118 (65) delta 208 avail 208 delay 4208 23752310 (3) delta 232 avail 232 delay 4184 23761847 (5972) delta 264 avail 264 delay 4152 The first column is the usec from the program start, the next value in parentheses is the time-step from the last time of status changes, the delta is the increase of avail, and the rest are raw values. If a too large delta appears in a short time-step, something is wrong. If it appears in a large time-step, it's simply a wrong responsiveness (aka system latency). Takashi --Multipart_Tue_Mar__3_18:41:22_2009-1 Content-Type: application/octet-stream Content-Disposition: attachment; filename="alsa-time-test.c" Content-Transfer-Encoding: 7bit #include #include #include #include #define JUMP_DETECT 100 /* 100 frames */ static uint64_t timespec_us(const struct timespec *ts) { return ts->tv_sec * 1000000LLU + ts->tv_nsec / 1000LLU; } int main(int argc, char *argv[]) { const char *dev; int r; snd_pcm_hw_params_t *hwparams; snd_pcm_sw_params_t *swparams; snd_pcm_status_t *status; snd_pcm_t *pcm; unsigned rate = 44100; unsigned periods = 2; snd_pcm_uframes_t boundary, buffer_size = 44100/10; /* 100s */ snd_pcm_uframes_t period_size; int dir = 1; struct timespec start, last_timestamp = { 0, 0 }; uint64_t start_us; snd_pcm_sframes_t last_avail, last_delay; struct pollfd *pollfds; int n_pollfd; int64_t sample_count = 0; snd_pcm_hw_params_alloca(&hwparams); snd_pcm_sw_params_alloca(&swparams); snd_pcm_status_alloca(&status); r = clock_gettime(CLOCK_MONOTONIC, &start); assert(r == 0); start_us = timespec_us(&start); dev = argc > 1 ? argv[1] : "front:AudioPCI"; r = snd_pcm_open(&pcm, dev, SND_PCM_STREAM_PLAYBACK, 0); assert(r == 0); r = snd_pcm_hw_params_any(pcm, hwparams); assert(r == 0); r = snd_pcm_hw_params_set_rate_resample(pcm, hwparams, 0); assert(r == 0); r = snd_pcm_hw_params_set_access(pcm, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED); assert(r == 0); r = snd_pcm_hw_params_set_format(pcm, hwparams, SND_PCM_FORMAT_S16_LE); assert(r == 0); r = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rate, NULL); assert(r == 0); r = snd_pcm_hw_params_set_channels(pcm, hwparams, 2); assert(r == 0); r = snd_pcm_hw_params_set_periods_integer(pcm, hwparams); assert(r == 0); r = snd_pcm_hw_params_set_periods_near(pcm, hwparams, &periods, &dir); assert(r == 0); r = snd_pcm_hw_params_set_buffer_size_near(pcm, hwparams, &buffer_size); assert(r == 0); r = snd_pcm_hw_params(pcm, hwparams); assert(r == 0); r = snd_pcm_hw_params_current(pcm, hwparams); assert(r == 0); r = snd_pcm_sw_params_current(pcm, swparams); assert(r == 0); r = snd_pcm_sw_params_set_avail_min(pcm, swparams, 1); assert(r == 0); r = snd_pcm_sw_params_set_period_event(pcm, swparams, 0); assert(r == 0); r = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size); assert(r == 0); r = snd_pcm_sw_params_set_start_threshold(pcm, swparams, buffer_size); assert(r == 0); period_size = buffer_size / periods; r = snd_pcm_sw_params_get_boundary(swparams, &boundary); assert(r == 0); r = snd_pcm_sw_params_set_stop_threshold(pcm, swparams, boundary); assert(r == 0); r = snd_pcm_sw_params_set_tstamp_mode(pcm, swparams, SND_PCM_TSTAMP_ENABLE); assert(r == 0); r = snd_pcm_sw_params(pcm, swparams); assert(r == 0); r = snd_pcm_prepare(pcm); assert(r == 0); r = snd_pcm_sw_params_current(pcm, swparams); assert(r == 0); /* assert(snd_pcm_hw_params_is_monotonic(hwparams) > 0); */ n_pollfd = snd_pcm_poll_descriptors_count(pcm); assert(n_pollfd > 0); pollfds = malloc(sizeof(struct pollfd) * n_pollfd); assert(pollfds); r = snd_pcm_poll_descriptors(pcm, pollfds, n_pollfd); assert(r == n_pollfd); for (;;) { snd_pcm_sframes_t avail, delay, delta; struct timespec now, timestamp; unsigned short revents; int written = 0; uint64_t now_us, timestamp_us; snd_pcm_state_t state; r = poll(pollfds, n_pollfd, 0); assert(r >= 0); r = snd_pcm_poll_descriptors_revents(pcm, pollfds, n_pollfd, &revents); assert(r == 0); assert((revents & ~POLLOUT) == 0); avail = snd_pcm_avail(pcm); assert(avail >= 0); r = snd_pcm_status(pcm, status); assert(r == 0); /* This assertion fails from time to time. ALSA seems to be broken */ /* assert(avail == (snd_pcm_sframes_t) snd_pcm_status_get_avail(status)); */ /* printf("%lu %lu\n", (unsigned long) avail, (unsigned long) snd_pcm_status_get_avail(status)); */ snd_pcm_status_get_htstamp(status, ×tamp); delay = snd_pcm_status_get_delay(status); state = snd_pcm_status_get_state(status); r = clock_gettime(CLOCK_MONOTONIC, &now); assert(r == 0); assert(!revents || avail > 0); if (avail) { snd_pcm_sframes_t sframes; static const uint16_t samples[2] = { 0, 0 }; sframes = snd_pcm_writei(pcm, samples, 1); assert(sframes == 1); written = 1; sample_count++; } if (avail == last_avail && delay == last_delay) { /* This is boring */ last_timestamp = timestamp; continue; } now_us = timespec_us(&now); timestamp_us = timespec_us(×tamp); delta = avail - last_avail; if (state == SND_PCM_STATE_RUNNING && (long)delta >= JUMP_DETECT) { /* jump */ printf("%llu\t(%llu)\tdelta %li\tavail %li\tdelay %li\n", (unsigned long long) (timestamp_us ? timestamp_us - start_us : 0), (unsigned long long ) (timestamp_us - timespec_us(&last_timestamp)), (signed long) delta, (signed long) avail, (signed long) delay); } /** When this assert is hit, most likely something bad * happened, i.e. the avail jumped suddenly. */ if ((unsigned) avail > buffer_size) { printf("XXX avail (%ld) > buffer_size (%ld)\n", (long)avail, (long)buffer_size); printf("XXX last step = %ld\n", (long)(timestamp_us - timespec_us(&last_timestamp))); return 1; } last_avail = avail; last_delay = delay; last_timestamp = timestamp; } return 0; } --Multipart_Tue_Mar__3_18:41:22_2009-1 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Alsa-devel mailing list Alsa-devel@alsa-project.org http://mailman.alsa-project.org/mailman/listinfo/alsa-devel --Multipart_Tue_Mar__3_18:41:22_2009-1--