Skip to content

Commit b751eef

Browse files
committed
[ALSA] Use posix clock monotonic for PCM and timer timestamps
We need an accurate and continuous (monotonic) time sources to do accurate synchronization among more timing sources. This patch allows to enable monotonic timestamps for ALSA PCM devices and enables monotonic timestamps for ALSA timer devices. Signed-off-by: Jaroslav Kysela <perex@perex.cz>
1 parent 25543fa commit b751eef

5 files changed

Lines changed: 52 additions & 11 deletions

File tree

‎include/sound/asound.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ enum {
138138
* *
139139
*****************************************************************************/
140140

141-
#define SNDRV_PCM_VERSION SNDRV_PROTOCOL_VERSION(2, 0, 8)
141+
#define SNDRV_PCM_VERSION SNDRV_PROTOCOL_VERSION(2, 0, 9)
142142

143143
typedef unsigned long snd_pcm_uframes_t;
144144
typedef signed long snd_pcm_sframes_t;
@@ -434,10 +434,16 @@ struct snd_xfern {
434434
snd_pcm_uframes_t frames;
435435
};
436436

437+
enum {
438+
SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY = 0, /* gettimeofday equivalent */
439+
SNDRV_PCM_TSTAMP_TYPE_MONOTONIC, /* posix_clock_monotonic equivalent */
440+
SNDRV_PCM_TSTAMP_TYPE_LAST = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC,
441+
};
442+
437443
enum {
438444
SNDRV_PCM_IOCTL_PVERSION = _IOR('A', 0x00, int),
439445
SNDRV_PCM_IOCTL_INFO = _IOR('A', 0x01, struct snd_pcm_info),
440-
SNDRV_PCM_IOCTL_TSTAMP = _IOW('A', 0x02, int),
446+
SNDRV_PCM_IOCTL_TTSTAMP = _IOW('A', 0x03, int),
441447
SNDRV_PCM_IOCTL_HW_REFINE = _IOWR('A', 0x10, struct snd_pcm_hw_params),
442448
SNDRV_PCM_IOCTL_HW_PARAMS = _IOWR('A', 0x11, struct snd_pcm_hw_params),
443449
SNDRV_PCM_IOCTL_HW_FREE = _IO('A', 0x12),

‎include/sound/pcm.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ struct snd_pcm_runtime {
323323

324324
/* -- timer -- */
325325
unsigned int timer_resolution; /* timer resolution */
326+
int tstamp_type; /* timestamp type */
326327

327328
/* -- DMA -- */
328329
unsigned char *dma_area; /* DMA area */
@@ -952,6 +953,15 @@ void snd_pcm_timer_resolution_change(struct snd_pcm_substream *substream);
952953
void snd_pcm_timer_init(struct snd_pcm_substream *substream);
953954
void snd_pcm_timer_done(struct snd_pcm_substream *substream);
954955

956+
static inline void snd_pcm_gettime(struct snd_pcm_runtime *runtime,
957+
struct timespec *tv)
958+
{
959+
if (runtime->tstamp_type == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
960+
do_posix_clock_monotonic_gettime(tv);
961+
else
962+
getnstimeofday(tv);
963+
}
964+
955965
/*
956966
* Memory
957967
*/

‎sound/core/pcm_lib.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ static inline int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *subs
188188
snd_pcm_sframes_t delta;
189189

190190
if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_MMAP)
191-
getnstimeofday((struct timespec *)&runtime->status->tstamp);
191+
snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
192192
pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
193193
if (pos == SNDRV_PCM_POS_XRUN) {
194194
xrun(substream);

‎sound/core/pcm_native.c‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -598,9 +598,9 @@ int snd_pcm_status(struct snd_pcm_substream *substream,
598598
if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_MMAP)
599599
status->tstamp = runtime->status->tstamp;
600600
else
601-
getnstimeofday(&status->tstamp);
601+
snd_pcm_gettime(runtime, &status->tstamp);
602602
} else
603-
getnstimeofday(&status->tstamp);
603+
snd_pcm_gettime(runtime, &status->tstamp);
604604
status->appl_ptr = runtime->control->appl_ptr;
605605
status->hw_ptr = runtime->status->hw_ptr;
606606
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
@@ -688,7 +688,7 @@ static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
688688
if (runtime->trigger_master == NULL)
689689
return;
690690
if (runtime->trigger_master == substream) {
691-
getnstimeofday(&runtime->trigger_tstamp);
691+
snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
692692
} else {
693693
snd_pcm_trigger_tstamp(runtime->trigger_master);
694694
runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
@@ -2519,6 +2519,21 @@ static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
25192519
return -EFAULT;
25202520
return 0;
25212521
}
2522+
2523+
static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2524+
{
2525+
struct snd_pcm_runtime *runtime = substream->runtime;
2526+
int arg;
2527+
2528+
if (get_user(arg, _arg))
2529+
return -EFAULT;
2530+
if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2531+
return -EINVAL;
2532+
runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2533+
if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2534+
runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2535+
return 0;
2536+
}
25222537

25232538
static int snd_pcm_common_ioctl1(struct file *file,
25242539
struct snd_pcm_substream *substream,
@@ -2531,8 +2546,8 @@ static int snd_pcm_common_ioctl1(struct file *file,
25312546
return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
25322547
case SNDRV_PCM_IOCTL_INFO:
25332548
return snd_pcm_info_user(substream, arg);
2534-
case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2535-
return 0;
2549+
case SNDRV_PCM_IOCTL_TTSTAMP:
2550+
return snd_pcm_tstamp(substream, arg);
25362551
case SNDRV_PCM_IOCTL_HW_REFINE:
25372552
return snd_pcm_hw_refine_user(substream, arg);
25382553
case SNDRV_PCM_IOCTL_HW_PARAMS:

‎sound/core/timer.c‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@
4444
#endif
4545

4646
static int timer_limit = DEFAULT_TIMER_LIMIT;
47+
static int timer_tstamp_monotonic = 1;
4748
MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
4849
MODULE_DESCRIPTION("ALSA timer interface");
4950
MODULE_LICENSE("GPL");
5051
module_param(timer_limit, int, 0444);
5152
MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
53+
module_param(timer_tstamp_monotonic, int, 0444);
54+
MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
5255

5356
struct snd_timer_user {
5457
struct snd_timer_instance *timeri;
@@ -381,7 +384,10 @@ static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
381384
struct snd_timer_instance *ts;
382385
struct timespec tstamp;
383386

384-
getnstimeofday(&tstamp);
387+
if (timer_tstamp_monotonic)
388+
do_posix_clock_monotonic_gettime(&tstamp);
389+
else
390+
getnstimeofday(&tstamp);
385391
snd_assert(event >= SNDRV_TIMER_EVENT_START &&
386392
event <= SNDRV_TIMER_EVENT_PAUSE, return);
387393
if (event == SNDRV_TIMER_EVENT_START ||
@@ -1182,8 +1188,12 @@ static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
11821188
spin_unlock(&tu->qlock);
11831189
return;
11841190
}
1185-
if (tu->last_resolution != resolution || ticks > 0)
1186-
getnstimeofday(&tstamp);
1191+
if (tu->last_resolution != resolution || ticks > 0) {
1192+
if (timer_tstamp_monotonic)
1193+
do_posix_clock_monotonic_gettime(&tstamp);
1194+
else
1195+
getnstimeofday(&tstamp);
1196+
}
11871197
if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
11881198
tu->last_resolution != resolution) {
11891199
r1.event = SNDRV_TIMER_EVENT_RESOLUTION;

0 commit comments

Comments
 (0)