A few ALSA PCM core update patches
Subsystems (2)
cover A few ALSA PCM core update patches
Hi,
here is a small series of patches for cleaning up / enhancing the PCM core stuff.
[PATCH 1/5] ALSA: pcm: Simplify snd_pcm_action_lock_irq() [PATCH 2/5] ALSA: pcm: Refactoring snd_pcm_action() [PATCH 3/5] ALSA: pcm: Correct PCM BUG error message [PATCH 4/5] ALSA: pcm: Replace PCM hwptr tracking with tracepoints [PATCH 5/5] ALSA: pcm: Add xrun_injection proc entry
Takashi
PATCH 1/5 ALSA: pcm: Simplify snd_pcm_action_lock_irq()
The function snd_pcm_action_lock_irq() can be much simplified by simply wrapping snd_pcm_action() with the stream lock. This was rather the original idea, but later it was open coded for optimization. However, looking at the optimization part closely, one notices that the probability of the optimized path is quite low; in normal situations, the linked stream action happens only for the triggered substream, thus the operation becomes identical. So the code simplification has a clear win, especially because we have now doubly codes for both atomic and non-atomic locks.
--- sound/core/pcm_native.c | 41 +++-------------------------------------- 1 file changed, 3 insertions(+), 38 deletions(-)
sound/core/pcm_native.c +3 -38
@@ -947,28 +947,6 @@ static int snd_pcm_action(struct action_ops *ops,return res; } -static int snd_pcm_action_lock_mutex (struct action_ops *ops, -struct snd_pcm_substream *substream, -int state) -{ -int res; - -down_read (&snd_pcm_link_rwsem); -if (snd_pcm_stream_linked (substream)) { -mutex_lock (&substream->group->mutex); -mutex_lock (&substream->self_group.mutex); - res =snd_pcm_action_group (ops, substream, state,1 ); -mutex_unlock (&substream->self_group.mutex); -mutex_unlock (&substream->group->mutex); - }else { -mutex_lock (&substream->self_group.mutex); - res =snd_pcm_action_single (ops, substream, state); -mutex_unlock (&substream->self_group.mutex); - } -up_read (&snd_pcm_link_rwsem); -return res; -} -/* * Note: don't use any locks before */
@@ -978,22 +956,9 @@ static int snd_pcm_action_lock_irq(struct action_ops *ops, {int res; -if (substream->pcm->nonatomic) -return snd_pcm_action_lock_mutex (ops, substream, state); - -read_lock_irq (&snd_pcm_link_rwlock); -if (snd_pcm_stream_linked (substream)) { -spin_lock (&substream->group->lock); -spin_lock (&substream->self_group.lock); - res =snd_pcm_action_group (ops, substream, state,1 ); -spin_unlock (&substream->self_group.lock); -spin_unlock (&substream->group->lock); - }else { -spin_lock (&substream->self_group.lock); - res =snd_pcm_action_single (ops, substream, state); -spin_unlock (&substream->self_group.lock); - } -read_unlock_irq (&snd_pcm_link_rwlock); +snd_pcm_stream_lock_irq (substream); + res =snd_pcm_action (ops, substream, state); +snd_pcm_stream_unlock_irq (substream);return res; }
Signature · 1 line
2.1.3
PATCH 2/5 ALSA: pcm: Refactoring snd_pcm_action()
In reply to Takashi IwaiJust a small code refactoring to reduce more lines.
--- sound/core/pcm_native.c | 35 ++++++++++------------------------- 1 file changed, 10 insertions(+), 25 deletions(-)
sound/core/pcm_native.c +10 -25
@@ -900,14 +900,19 @@ static int snd_pcm_action_single(struct action_ops *ops,return res; } -/* call in mutex-protected context */ -static int snd_pcm_action_mutex (struct action_ops *ops, -struct snd_pcm_substream *substream, -int state) +/* +* Note: call with stream lock +*/ +static int snd_pcm_action (struct action_ops *ops, +struct snd_pcm_substream *substream, +int state) {int res; -if (snd_pcm_stream_linked (substream)) { +if (!snd_pcm_stream_linked (substream)) +return snd_pcm_action_single (ops, substream, state); + +if (substream->pcm->nonatomic) {if (!mutex_trylock (&substream->group->mutex)) {mutex_unlock (&substream->self_group.mutex);mutex_lock (&substream->group->mutex);
@@ -916,24 +921,6 @@ static int snd_pcm_action_mutex(struct action_ops *ops, res =snd_pcm_action_group (ops, substream, state,1 );mutex_unlock (&substream->group->mutex); }else { - res =snd_pcm_action_single (ops, substream, state); - } -return res; -} - -/* -* Note: call with stream lock -*/ -static int snd_pcm_action (struct action_ops *ops, -struct snd_pcm_substream *substream, -int state) -{ -int res; - -if (substream->pcm->nonatomic) -return snd_pcm_action_mutex (ops, substream, state); - -if (snd_pcm_stream_linked (substream)) {if (!spin_trylock (&substream->group->lock)) {spin_unlock (&substream->self_group.lock);spin_lock (&substream->group->lock);
@@ -941,8 +928,6 @@ static int snd_pcm_action(struct action_ops *ops, } res =snd_pcm_action_group (ops, substream, state,1 );spin_unlock (&substream->group->lock); - }else { - res =snd_pcm_action_single (ops, substream, state); }return res; }
Signature · 1 line
2.1.3
PATCH 3/5 ALSA: pcm: Correct PCM BUG error message
In reply to Takashi IwaiWhile converting to dev_*(), the message showing the invalid PCM position was wrongly tagged as if an XRUN although it's actually a BUG. This patch corrects the message again.
--- sound/core/pcm_lib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
sound/core/pcm_lib.c +1 -1
@@ -345,7 +345,7 @@ static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,snd_pcm_debug_name (substream, name,sizeof (name));xrun_log_show (substream);pcm_err (substream->pcm, -"XRUN: %s, pos = %ld, buffer size = %ld, period size = %ld\n" , +"BUG: %s, pos = %ld, buffer size = %ld, period size = %ld\n" , name, pos, runtime->buffer_size, runtime->period_size); }
Signature · 1 line
2.1.3
PATCH 4/5 ALSA: pcm: Replace PCM hwptr tracking with tracepoints
In reply to Takashi IwaiALSA PCM core has a mechanism tracking the PCM hwptr updates for analyzing XRUNs. But its log is limited (up to 10) and its log output is a kernel message, which is hard to handle.
In this patch, the hwptr logging is moved to the tracing infrastructure instead of its own. Not only the hwptr updates but also XRUN and hwptr errors are recorded on the trace log, so that user can see such events at the exact timing.
The new "snd_pcm" entry will appear in the tracing events:
# ls -F /sys/kernel/debug/tracing/events/snd_pcm enable filter hw_ptr_error/ hwptr/ xrun/
The hwptr is for the regular hwptr update events. An event trace looks like:
aplay-26187 [004] d..3 4012.834761: hwptr: pcmC0D0p/sub0: POS: pos=488, old=0, base=0, period=1024, buf=16384
"POS" shows the hwptr update by the explicit position update call and "IRQ" means the hwptr update by the interrupt,
i.e. snd_pcm_period_elapsed() call. The "pos" is the passed ring-buffer offset by the caller, "old" is the previous hwptr, "base" is the hwptr base position, "period" and "buf" are period- and buffer-size of the target PCM substream.
(Note that the hwptr position displayed here isn't the ring-buffer
offset. It increments up to the PCM position boundary.)
The XRUN event appears similarly, but without "pos" field. The hwptr error events appear with the PCM identifier and its reason string, such as "Lost interrupt?".
The XRUN and hwptr error reports on kernel message are still left, can be turned on/off via xrun_debug proc like before. But the bit 3, 4, 5 and 6 bits of xrun_debug proc are dropped by this patch. Also, along with the change, the message strings have been reformatted to be a bit more consistent.
Last but not least, the hwptr reporting is enabled only when CONFIG_SND_PCM_XRUN_DEBUG is set.
--- Documentation/sound/alsa/Procfile.txt | 13 --- sound/core/Makefile | 3 + sound/core/pcm_lib.c | 146 ++++++---------------------------- sound/core/pcm_trace.h | 110 +++++++++++++++++++++++++ 4 files changed, 137 insertions(+), 135 deletions(-) create mode 100644 sound/core/pcm_trace.h
Documentation/sound/alsa/Procfile.txt +0 -13
@@ -101,10 +101,6 @@ card*/pcm*/xrun_debug bit 0 = Enable XRUN/jiffies debug messages bit 1 = Show stack trace at XRUN / jiffies check bit 2 = Enable additional jiffies check - bit 3 = Log hwptr update at each period interrupt - bit 4 = Log hwptr update at each snd_pcm_update_hw_ptr() - bit 5 = Show last 10 positions on error - bit 6 = Do above only once When the bit 0 is set, the driver will show the messages to kernel log when an xrun is detected. The debug message is
@@ -121,15 +117,6 @@ card*/pcm*/xrun_debug buggy) hardware that doesn't give smooth pointer updates. This feature is enabled via the bit 2. - Bits 3 and 4 are for logging the hwptr records. Note that - these will give flood of kernel messages. - - When bit 5 is set, the driver logs the last 10 xrun errors and - the proc file shows each jiffies, position, period_size, - buffer_size, old_hw_ptr, and hw_ptr_base values. - - When bit 6 is set, the full xrun log is shown only once. - card*/pcm*/sub*/info The general information of this PCM sub-stream.
sound/core/Makefile +3 -0
@@ -14,6 +14,9 @@ snd-pcm-y := pcm.o pcm_native.o pcm_lib.o pcm_timer.o pcm_misc.o \ pcm_memory.o memalloc.o snd-pcm-$(CONFIG_SND_DMA_SGBUF) += sgbuf.o +# for trace-points +CFLAGS_pcm_lib.o := -I$(src) + snd-pcm-dmaengine-objs := pcm_dmaengine.o snd-rawmidi-objs := rawmidi.o
sound/core/pcm_lib.c +24 -122
215 lines not on this page · raw message
sound/core/pcm_trace.h new file +110 -0
110 lines not on this page · raw message
Signature · 1 line
2.1.3
PATCH 5/5 ALSA: pcm: Add xrun_injection proc entry
In reply to Takashi IwaiThis patch adds a new proc entry for PCM substreams to inject an XRUN. When a PCM substream is running and any value is written to its xrun_injection proc file, the driver triggers XRUN. This is a useful feature for debugging XRUN and error handling code paths.
Note that this entry is enabled only when CONFIG_SND_PCM_XRUN_DEBUG is set.
--- Documentation/sound/alsa/Procfile.txt | 4 ++++ include/sound/pcm.h | 3 +++ sound/core/pcm.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+)
Documentation/sound/alsa/Procfile.txt +4 -0
@@ -133,6 +133,10 @@ card*/pcm*/sub*/sw_params card*/pcm*/sub*/prealloc The buffer pre-allocation information. +card*/pcm*/sub*/xrun_injection + Triggers an XRUN to the running stream when any value is + written to this proc file. Used for fault injection. + This entry is write-only. AC97 Codec Information ----------------------
include/sound/pcm.h +3 -0
@@ -416,7 +416,10 @@ struct snd_pcm_substream {struct snd_info_entry *proc_status_entry;struct snd_info_entry *proc_prealloc_entry;struct snd_info_entry *proc_prealloc_max_entry; +#ifdef CONFIG_SND_PCM_XRUN_DEBUG +struct snd_info_entry *proc_xrun_injection_entry;#endif +#endif /* CONFIG_SND_VERBOSE_PROCFS */ /* misc flags */ unsigned int hw_opened: 1; };
sound/core/pcm.c +33 -0
@@ -483,6 +483,19 @@ static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry, }#ifdef CONFIG_SND_PCM_XRUN_DEBUG +static void snd_pcm_xrun_injection_write (struct snd_info_entry *entry, +struct snd_info_buffer *buffer) +{ +struct snd_pcm_substream *substream = entry->private_data; +struct snd_pcm_runtime *runtime; + +snd_pcm_stream_lock_irq (substream); + runtime = substream->runtime; +if (runtime && runtime->status->state ==SNDRV_PCM_STATE_RUNNING ) +snd_pcm_stop (substream,SNDRV_PCM_STATE_XRUN ); +snd_pcm_stream_unlock_irq (substream); +} +static void snd_pcm_xrun_debug_read (struct snd_info_entry *entry,struct snd_info_buffer *buffer) {
@@ -614,6 +627,22 @@ static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) } substream->proc_status_entry = entry; +#ifdef CONFIG_SND_PCM_XRUN_DEBUG + entry =snd_info_create_card_entry (card,"xrun_injection" , + substream->proc_root); +if (entry) { + entry->private_data = substream; + entry->c.text.read =NULL ; + entry->c.text.write = snd_pcm_xrun_injection_write; + entry->mode =S_IFREG |S_IWUSR ; +if (snd_info_register (entry) <0 ) { +snd_info_free_entry (entry); + entry =NULL ; + } + } + substream->proc_xrun_injection_entry = entry; +#endif /* CONFIG_SND_PCM_XRUN_DEBUG */ +return 0 ; }
@@ -627,6 +656,10 @@ static int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream) substream->proc_sw_params_entry =NULL ;snd_info_free_entry (substream->proc_status_entry); substream->proc_status_entry =NULL ; +#ifdef CONFIG_SND_PCM_XRUN_DEBUG +snd_info_free_entry (substream->proc_xrun_injection_entry); + substream->proc_xrun_injection_entry =NULL ; +#endif snd_info_free_entry (substream->proc_root); substream->proc_root =NULL ;return 0 ;
Signature · 1 line
2.1.3