]> code.delx.au - pulseaudio/blob - src/pulsecore/core-rtclock.c
Merge remote branch 'mkbosmans/rate-adjustment'
[pulseaudio] / src / pulsecore / core-rtclock.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of the
10 License, or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stddef.h>
28 #include <time.h>
29 #include <sys/time.h>
30 #include <errno.h>
31
32 #ifdef HAVE_SYS_PRCTL_H
33 #include <sys/prctl.h>
34 #endif
35
36 #ifdef OS_IS_DARWIN
37 #include <CoreServices/CoreServices.h>
38 #include <mach/mach.h>
39 #include <mach/mach_time.h>
40 #include <unistd.h>
41 #endif
42
43 #include <pulse/timeval.h>
44 #include <pulsecore/macro.h>
45 #include <pulsecore/core-error.h>
46
47 #include "core-rtclock.h"
48
49 pa_usec_t pa_rtclock_age(const struct timeval *tv) {
50 struct timeval now;
51 pa_assert(tv);
52
53 return pa_timeval_diff(pa_rtclock_get(&now), tv);
54 }
55
56 struct timeval *pa_rtclock_get(struct timeval *tv) {
57
58 #if defined(OS_IS_DARWIN)
59 uint64_t val, abs_time = mach_absolute_time();
60 Nanoseconds nanos;
61
62 nanos = AbsoluteToNanoseconds(*(AbsoluteTime *) &abs_time);
63 val = *(uint64_t *) &nanos;
64
65 tv->tv_sec = val / PA_NSEC_PER_SEC;
66 tv->tv_usec = (val % PA_NSEC_PER_SEC) / PA_NSEC_PER_USEC;
67
68 return tv;
69
70 #elif defined(HAVE_CLOCK_GETTIME)
71 struct timespec ts;
72
73 #ifdef CLOCK_MONOTONIC
74 /* No locking or atomic ops for no_monotonic here */
75 static pa_bool_t no_monotonic = FALSE;
76
77 if (!no_monotonic)
78 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
79 no_monotonic = TRUE;
80
81 if (no_monotonic)
82 #endif /* CLOCK_MONOTONIC */
83 pa_assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
84
85 pa_assert(tv);
86
87 tv->tv_sec = ts.tv_sec;
88 tv->tv_usec = ts.tv_nsec / PA_NSEC_PER_USEC;
89
90 return tv;
91 #endif /* HAVE_CLOCK_GETTIME */
92
93 return pa_gettimeofday(tv);
94 }
95
96 pa_bool_t pa_rtclock_hrtimer(void) {
97
98 #if defined (OS_IS_DARWIN)
99 mach_timebase_info_data_t tbi;
100 uint64_t time_nsec;
101
102 mach_timebase_info(&tbi);
103
104 /* nsec = nticks * (N/D) - we want 1 tick == resolution !? */
105 time_nsec = tbi.numer / tbi.denom;
106 return time_nsec <= (long) (PA_HRTIMER_THRESHOLD_USEC*PA_NSEC_PER_USEC);
107
108 #elif defined(HAVE_CLOCK_GETTIME)
109 struct timespec ts;
110
111 #ifdef CLOCK_MONOTONIC
112
113 if (clock_getres(CLOCK_MONOTONIC, &ts) >= 0)
114 return ts.tv_sec == 0 && ts.tv_nsec <= (long) (PA_HRTIMER_THRESHOLD_USEC*PA_NSEC_PER_USEC);
115
116 #endif /* CLOCK_MONOTONIC */
117
118 pa_assert_se(clock_getres(CLOCK_REALTIME, &ts) == 0);
119 return ts.tv_sec == 0 && ts.tv_nsec <= (long) (PA_HRTIMER_THRESHOLD_USEC*PA_NSEC_PER_USEC);
120
121 #endif /* HAVE_CLOCK_GETTIME */
122
123 return FALSE;
124 }
125
126 #define TIMER_SLACK_NS (int) ((500 * PA_NSEC_PER_USEC))
127
128 void pa_rtclock_hrtimer_enable(void) {
129
130 #ifdef PR_SET_TIMERSLACK
131 int slack_ns;
132
133 if ((slack_ns = prctl(PR_GET_TIMERSLACK, 0, 0, 0, 0)) < 0) {
134 pa_log_info("PR_GET_TIMERSLACK/PR_SET_TIMERSLACK not supported.");
135 return;
136 }
137
138 pa_log_debug("Timer slack is set to %i us.", (int) (slack_ns/PA_NSEC_PER_USEC));
139
140 if (slack_ns > TIMER_SLACK_NS) {
141 slack_ns = TIMER_SLACK_NS;
142
143 pa_log_debug("Setting timer slack to %i us.", (int) (slack_ns/PA_NSEC_PER_USEC));
144
145 if (prctl(PR_SET_TIMERSLACK, slack_ns, 0, 0, 0) < 0) {
146 pa_log_warn("PR_SET_TIMERSLACK failed: %s", pa_cstrerror(errno));
147 return;
148 }
149 }
150
151 #endif
152 }
153
154 struct timeval* pa_rtclock_from_wallclock(struct timeval *tv) {
155
156 #ifdef HAVE_CLOCK_GETTIME
157 struct timeval wc_now, rt_now;
158
159 pa_gettimeofday(&wc_now);
160 pa_rtclock_get(&rt_now);
161
162 pa_assert(tv);
163
164 /* pa_timeval_sub() saturates on underflow! */
165
166 if (pa_timeval_cmp(&wc_now, tv) < 0)
167 pa_timeval_add(&rt_now, pa_timeval_diff(tv, &wc_now));
168 else
169 pa_timeval_sub(&rt_now, pa_timeval_diff(&wc_now, tv));
170
171 *tv = rt_now;
172 #endif
173
174 return tv;
175 }
176
177 pa_usec_t pa_timespec_load(const struct timespec *ts) {
178
179 if (PA_UNLIKELY(!ts))
180 return PA_USEC_INVALID;
181
182 return
183 (pa_usec_t) ts->tv_sec * PA_USEC_PER_SEC +
184 (pa_usec_t) ts->tv_nsec / PA_NSEC_PER_USEC;
185 }
186
187 struct timespec* pa_timespec_store(struct timespec *ts, pa_usec_t v) {
188 pa_assert(ts);
189
190 if (PA_UNLIKELY(v == PA_USEC_INVALID)) {
191 ts->tv_sec = PA_INT_TYPE_MAX(time_t);
192 ts->tv_nsec = (long) (PA_NSEC_PER_SEC-1);
193 return NULL;
194 }
195
196 ts->tv_sec = (time_t) (v / PA_USEC_PER_SEC);
197 ts->tv_nsec = (long) ((v % PA_USEC_PER_SEC) * PA_NSEC_PER_USEC);
198
199 return ts;
200 }
201
202 static struct timeval* wallclock_from_rtclock(struct timeval *tv) {
203
204 #ifdef HAVE_CLOCK_GETTIME
205 struct timeval wc_now, rt_now;
206
207 pa_gettimeofday(&wc_now);
208 pa_rtclock_get(&rt_now);
209
210 pa_assert(tv);
211
212 /* pa_timeval_sub() saturates on underflow! */
213
214 if (pa_timeval_cmp(&rt_now, tv) < 0)
215 pa_timeval_add(&wc_now, pa_timeval_diff(tv, &rt_now));
216 else
217 pa_timeval_sub(&wc_now, pa_timeval_diff(&rt_now, tv));
218
219 *tv = wc_now;
220 #endif
221
222 return tv;
223 }
224
225 struct timeval* pa_timeval_rtstore(struct timeval *tv, pa_usec_t v, pa_bool_t rtclock) {
226 pa_assert(tv);
227
228 if (v == PA_USEC_INVALID)
229 return NULL;
230
231 pa_timeval_store(tv, v);
232
233 if (rtclock)
234 tv->tv_usec |= PA_TIMEVAL_RTCLOCK;
235 else
236 wallclock_from_rtclock(tv);
237
238 return tv;
239 }