Skip to content

Commit 4d96eb2

Browse files
committed
ALSA: pcm_lib - add possibility to log last 10 DMA ring buffer positions
In some debug cases, it might be usefull to see previous ring buffer positions to determine position problems from the lowlevel drivers. Signed-off-by: Jaroslav Kysela <perex@perex.cz>
1 parent 741b20c commit 4d96eb2

3 files changed

Lines changed: 127 additions & 23 deletions

File tree

‎include/sound/pcm.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ struct snd_pcm_hw_constraint_list {
262262
unsigned int mask;
263263
};
264264

265+
struct snd_pcm_hwptr_log;
266+
265267
struct snd_pcm_runtime {
266268
/* -- Status -- */
267269
struct snd_pcm_substream *trigger_master;
@@ -340,6 +342,10 @@ struct snd_pcm_runtime {
340342
/* -- OSS things -- */
341343
struct snd_pcm_oss_runtime oss;
342344
#endif
345+
346+
#ifdef CONFIG_SND_PCM_XRUN_DEBUG
347+
struct snd_pcm_hwptr_log *hwptr_log;
348+
#endif
343349
};
344350

345351
struct snd_pcm_group { /* keep linked substreams */

‎sound/core/pcm.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,10 @@ void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
921921
snd_free_pages((void*)runtime->control,
922922
PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
923923
kfree(runtime->hw_constraints.rules);
924+
#ifdef CONFIG_SND_PCM_XRUN_DEBUG
925+
if (runtime->hwptr_log)
926+
kfree(runtime->hwptr_log);
927+
#endif
924928
kfree(runtime);
925929
substream->runtime = NULL;
926930
put_pid(substream->pid);

‎sound/core/pcm_lib.c‎

Lines changed: 117 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -126,34 +126,34 @@ void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_ufram
126126
}
127127
}
128128

129+
static void pcm_debug_name(struct snd_pcm_substream *substream,
130+
char *name, size_t len)
131+
{
132+
snprintf(name, len, "pcmC%dD%d%c:%d",
133+
substream->pcm->card->number,
134+
substream->pcm->device,
135+
substream->stream ? 'c' : 'p',
136+
substream->number);
137+
}
138+
129139
#define XRUN_DEBUG_BASIC (1<<0)
130140
#define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
131141
#define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
132142
#define XRUN_DEBUG_PERIODUPDATE (1<<3) /* full period update info */
133143
#define XRUN_DEBUG_HWPTRUPDATE (1<<4) /* full hwptr update info */
144+
#define XRUN_DEBUG_LOG (1<<5) /* show last 10 positions on err */
145+
#define XRUN_DEBUG_LOGONCE (1<<6) /* do above only once */
134146

135147
#ifdef CONFIG_SND_PCM_XRUN_DEBUG
148+
136149
#define xrun_debug(substream, mask) \
137150
((substream)->pstr->xrun_debug & (mask))
138-
#else
139-
#define xrun_debug(substream, mask) 0
140-
#endif
141151

142152
#define dump_stack_on_xrun(substream) do { \
143153
if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
144154
dump_stack(); \
145155
} while (0)
146156

147-
static void pcm_debug_name(struct snd_pcm_substream *substream,
148-
char *name, size_t len)
149-
{
150-
snprintf(name, len, "pcmC%dD%d%c:%d",
151-
substream->pcm->card->number,
152-
substream->pcm->device,
153-
substream->stream ? 'c' : 'p',
154-
substream->number);
155-
}
156-
157157
static void xrun(struct snd_pcm_substream *substream)
158158
{
159159
struct snd_pcm_runtime *runtime = substream->runtime;
@@ -169,6 +169,102 @@ static void xrun(struct snd_pcm_substream *substream)
169169
}
170170
}
171171

172+
#define hw_ptr_error(substream, fmt, args...) \
173+
do { \
174+
if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
175+
if (printk_ratelimit()) { \
176+
snd_printd("PCM: " fmt, ##args); \
177+
} \
178+
dump_stack_on_xrun(substream); \
179+
} \
180+
} while (0)
181+
182+
#define XRUN_LOG_CNT 10
183+
184+
struct hwptr_log_entry {
185+
unsigned long jiffies;
186+
snd_pcm_uframes_t pos;
187+
snd_pcm_uframes_t period_size;
188+
snd_pcm_uframes_t buffer_size;
189+
snd_pcm_uframes_t old_hw_ptr;
190+
snd_pcm_uframes_t hw_ptr_base;
191+
snd_pcm_uframes_t hw_ptr_interrupt;
192+
};
193+
194+
struct snd_pcm_hwptr_log {
195+
unsigned int idx;
196+
unsigned int hit: 1;
197+
struct hwptr_log_entry entries[XRUN_LOG_CNT];
198+
};
199+
200+
static void xrun_log(struct snd_pcm_substream *substream,
201+
snd_pcm_uframes_t pos)
202+
{
203+
struct snd_pcm_runtime *runtime = substream->runtime;
204+
struct snd_pcm_hwptr_log *log = runtime->hwptr_log;
205+
struct hwptr_log_entry *entry;
206+
207+
if (log == NULL) {
208+
log = kzalloc(sizeof(*log), GFP_ATOMIC);
209+
if (log == NULL)
210+
return;
211+
runtime->hwptr_log = log;
212+
} else {
213+
if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
214+
return;
215+
}
216+
entry = &log->entries[log->idx];
217+
entry->jiffies = jiffies;
218+
entry->pos = pos;
219+
entry->period_size = runtime->period_size;
220+
entry->buffer_size = runtime->buffer_size;;
221+
entry->old_hw_ptr = runtime->status->hw_ptr;
222+
entry->hw_ptr_base = runtime->hw_ptr_base;
223+
entry->hw_ptr_interrupt = runtime->hw_ptr_interrupt;;
224+
log->idx = (log->idx + 1) % XRUN_LOG_CNT;
225+
}
226+
227+
static void xrun_log_show(struct snd_pcm_substream *substream)
228+
{
229+
struct snd_pcm_hwptr_log *log = substream->runtime->hwptr_log;
230+
struct hwptr_log_entry *entry;
231+
char name[16];
232+
unsigned int idx;
233+
int cnt;
234+
235+
if (log == NULL)
236+
return;
237+
if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
238+
return;
239+
pcm_debug_name(substream, name, sizeof(name));
240+
for (cnt = 0, idx = log->idx; cnt < XRUN_LOG_CNT; cnt++) {
241+
entry = &log->entries[idx];
242+
if (entry->period_size == 0)
243+
break;
244+
snd_printd("hwptr log: %s: j=%lu, pos=0x%lx/0x%lx/0x%lx, "
245+
"hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n",
246+
name, entry->jiffies, (unsigned long)entry->pos,
247+
(unsigned long)entry->period_size,
248+
(unsigned long)entry->buffer_size,
249+
(unsigned long)entry->old_hw_ptr,
250+
(unsigned long)entry->hw_ptr_base,
251+
(unsigned long)entry->hw_ptr_interrupt);
252+
idx++;
253+
idx %= XRUN_LOG_CNT;
254+
}
255+
log->hit = 1;
256+
}
257+
258+
#else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
259+
260+
#define xrun_debug(substream, mask) 0
261+
#define xrun(substream) do { } while (0)
262+
#define hw_ptr_error(substream, fmt, args...) do { } while (0)
263+
#define xrun_log(substream, pos) do { } while (0)
264+
#define xrun_log_show(substream) do { } while (0)
265+
266+
#endif
267+
172268
static snd_pcm_uframes_t
173269
snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
174270
struct snd_pcm_runtime *runtime)
@@ -182,6 +278,7 @@ snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
182278
if (printk_ratelimit()) {
183279
char name[16];
184280
pcm_debug_name(substream, name, sizeof(name));
281+
xrun_log_show(substream);
185282
snd_printd(KERN_ERR "BUG: %s, pos = 0x%lx, "
186283
"buffer size = 0x%lx, period size = 0x%lx\n",
187284
name, pos, runtime->buffer_size,
@@ -190,6 +287,8 @@ snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
190287
pos = 0;
191288
}
192289
pos -= pos % runtime->min_align;
290+
if (xrun_debug(substream, XRUN_DEBUG_LOG))
291+
xrun_log(substream, pos);
193292
return pos;
194293
}
195294

@@ -220,16 +319,6 @@ static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
220319
return 0;
221320
}
222321

223-
#define hw_ptr_error(substream, fmt, args...) \
224-
do { \
225-
if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
226-
if (printk_ratelimit()) { \
227-
snd_printd("PCM: " fmt, ##args); \
228-
} \
229-
dump_stack_on_xrun(substream); \
230-
} \
231-
} while (0)
232-
233322
static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
234323
{
235324
struct snd_pcm_runtime *runtime = substream->runtime;
@@ -270,6 +359,7 @@ static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
270359
if (runtime->periods == 1 || new_hw_ptr < old_hw_ptr)
271360
delta += runtime->buffer_size;
272361
if (delta < 0) {
362+
xrun_log_show(substream);
273363
hw_ptr_error(substream,
274364
"Unexpected hw_pointer value "
275365
"(stream=%i, pos=%ld, intr_ptr=%ld)\n",
@@ -315,6 +405,7 @@ static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
315405
delta = jdelta /
316406
(((runtime->period_size * HZ) / runtime->rate)
317407
+ HZ/100);
408+
xrun_log_show(substream);
318409
hw_ptr_error(substream,
319410
"hw_ptr skipping! [Q] "
320411
"(pos=%ld, delta=%ld, period=%ld, "
@@ -334,6 +425,7 @@ static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
334425
}
335426
no_jiffies_check:
336427
if (delta > runtime->period_size + runtime->period_size / 2) {
428+
xrun_log_show(substream);
337429
hw_ptr_error(substream,
338430
"Lost interrupts? "
339431
"(stream=%i, delta=%ld, intr_ptr=%ld)\n",
@@ -397,6 +489,7 @@ int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
397489
if (delta < 0) {
398490
delta += runtime->buffer_size;
399491
if (delta < 0) {
492+
xrun_log_show(substream);
400493
hw_ptr_error(substream,
401494
"Unexpected hw_pointer value [2] "
402495
"(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
@@ -416,6 +509,7 @@ int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
416509
goto no_jiffies_check;
417510
delta -= runtime->delay;
418511
if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
512+
xrun_log_show(substream);
419513
hw_ptr_error(substream,
420514
"hw_ptr skipping! "
421515
"(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",

0 commit comments

Comments
 (0)