1 |
/* |
---|
2 |
* Copyright (C) 2005-2007 by Pieter Palmers |
---|
3 |
* |
---|
4 |
* This file is part of FFADO |
---|
5 |
* FFADO = Free Firewire (pro-)audio drivers for linux |
---|
6 |
* |
---|
7 |
* FFADO is based upon FreeBoB |
---|
8 |
* |
---|
9 |
* This program is free software: you can redistribute it and/or modify |
---|
10 |
* it under the terms of the GNU General Public License as published by |
---|
11 |
* the Free Software Foundation, either version 3 of the License, or |
---|
12 |
* (at your option) any later version. |
---|
13 |
* |
---|
14 |
* This program is distributed in the hope that it will be useful, |
---|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 |
* GNU General Public License for more details. |
---|
18 |
* |
---|
19 |
* You should have received a copy of the GNU General Public License |
---|
20 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
21 |
* |
---|
22 |
*/ |
---|
23 |
|
---|
24 |
#include "CycleTimerHelper.h" |
---|
25 |
#include "ieee1394service.h" |
---|
26 |
#include "libutil/PosixThread.h" |
---|
27 |
|
---|
28 |
#define DLL_BANDWIDTH (0.01) |
---|
29 |
#define DLL_PI (3.141592653589793238) |
---|
30 |
#define DLL_SQRT2 (1.414213562373095049) |
---|
31 |
#define DLL_OMEGA (2.0*DLL_PI*DLL_BANDWIDTH) |
---|
32 |
#define DLL_COEFF_B (DLL_SQRT2 * DLL_OMEGA) |
---|
33 |
#define DLL_COEFF_C (DLL_OMEGA * DLL_OMEGA) |
---|
34 |
|
---|
35 |
/* |
---|
36 |
#define ENTER_CRITICAL_SECTION { \ |
---|
37 |
if (pthread_mutex_trylock(&m_compute_vars_lock) == EBUSY) { \ |
---|
38 |
debugWarning(" (%p) lock clash\n", this); \ |
---|
39 |
ENTER_CRITICAL_SECTION; \ |
---|
40 |
} \ |
---|
41 |
} |
---|
42 |
*/ |
---|
43 |
#define ENTER_CRITICAL_SECTION { \ |
---|
44 |
pthread_mutex_lock(&m_compute_vars_lock); \ |
---|
45 |
} |
---|
46 |
#define EXIT_CRITICAL_SECTION { \ |
---|
47 |
pthread_mutex_unlock(&m_compute_vars_lock); \ |
---|
48 |
} |
---|
49 |
|
---|
50 |
#define OLD_STYLE |
---|
51 |
|
---|
52 |
IMPL_DEBUG_MODULE( CycleTimerHelper, CycleTimerHelper, DEBUG_LEVEL_NORMAL ); |
---|
53 |
|
---|
54 |
CycleTimerHelper::CycleTimerHelper(Ieee1394Service &parent, unsigned int update_period_us) |
---|
55 |
: m_Parent ( parent ) |
---|
56 |
, m_ticks_per_update ( ((uint64_t)TICKS_PER_SECOND) * ((uint64_t)update_period_us) / 1000000ULL ) |
---|
57 |
, m_usecs_per_update ( update_period_us ) |
---|
58 |
, m_avg_wakeup_delay ( 0.0 ) |
---|
59 |
, m_dll_e2 ( 0.0 ) |
---|
60 |
, m_current_time_usecs ( 0 ) |
---|
61 |
, m_next_time_usecs ( 0 ) |
---|
62 |
, m_current_time_ticks ( 0 ) |
---|
63 |
, m_next_time_ticks ( 0 ) |
---|
64 |
, m_first_run ( true ) |
---|
65 |
, m_Thread ( NULL ) |
---|
66 |
, m_realtime ( false ) |
---|
67 |
, m_priority ( 0 ) |
---|
68 |
{ |
---|
69 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Create %p...\n", this); |
---|
70 |
} |
---|
71 |
|
---|
72 |
CycleTimerHelper::CycleTimerHelper(Ieee1394Service &parent, unsigned int update_period_us, bool rt, int prio) |
---|
73 |
: m_Parent ( parent ) |
---|
74 |
, m_ticks_per_update ( ((uint64_t)TICKS_PER_SECOND) * ((uint64_t)update_period_us) / 1000000ULL ) |
---|
75 |
, m_usecs_per_update ( update_period_us ) |
---|
76 |
, m_avg_wakeup_delay ( 0.0 ) |
---|
77 |
, m_dll_e2 ( 0.0 ) |
---|
78 |
, m_current_time_usecs ( 0 ) |
---|
79 |
, m_next_time_usecs ( 0 ) |
---|
80 |
, m_current_time_ticks ( 0 ) |
---|
81 |
, m_next_time_ticks ( 0 ) |
---|
82 |
, m_first_run ( true ) |
---|
83 |
, m_Thread ( NULL ) |
---|
84 |
, m_realtime ( rt ) |
---|
85 |
, m_priority ( prio ) |
---|
86 |
{ |
---|
87 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Create %p...\n", this); |
---|
88 |
} |
---|
89 |
|
---|
90 |
CycleTimerHelper::~CycleTimerHelper() |
---|
91 |
{ |
---|
92 |
if (m_Thread) { |
---|
93 |
m_Thread->Stop(); |
---|
94 |
delete m_Thread; |
---|
95 |
} |
---|
96 |
} |
---|
97 |
|
---|
98 |
bool |
---|
99 |
CycleTimerHelper::Start() |
---|
100 |
{ |
---|
101 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Start %p...\n", this); |
---|
102 |
#ifndef OLD_STYLE |
---|
103 |
m_Thread = new Util::PosixThread(this, m_realtime, m_priority, |
---|
104 |
PTHREAD_CANCEL_DEFERRED); |
---|
105 |
if(!m_Thread) { |
---|
106 |
debugFatal("No thread\n"); |
---|
107 |
return false; |
---|
108 |
} |
---|
109 |
if (m_Thread->Start() != 0) { |
---|
110 |
debugFatal("Could not start update thread\n"); |
---|
111 |
return false; |
---|
112 |
} |
---|
113 |
#endif |
---|
114 |
return true; |
---|
115 |
} |
---|
116 |
|
---|
117 |
bool |
---|
118 |
CycleTimerHelper::Init() |
---|
119 |
{ |
---|
120 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Initialize %p...\n", this); |
---|
121 |
pthread_mutex_init(&m_compute_vars_lock, NULL); |
---|
122 |
return true; |
---|
123 |
} |
---|
124 |
|
---|
125 |
bool |
---|
126 |
CycleTimerHelper::setThreadParameters(bool rt, int priority) { |
---|
127 |
debugOutput( DEBUG_LEVEL_VERBOSE, "(%p) switch to: (rt=%d, prio=%d)...\n", this, rt, priority); |
---|
128 |
if (priority > 98) priority = 98; // cap the priority |
---|
129 |
m_realtime = rt; |
---|
130 |
m_priority = priority; |
---|
131 |
|
---|
132 |
#ifndef OLD_STYLE |
---|
133 |
if (m_Thread) { |
---|
134 |
if (m_realtime) { |
---|
135 |
m_Thread->AcquireRealTime(m_priority); |
---|
136 |
} else { |
---|
137 |
m_Thread->DropRealTime(); |
---|
138 |
} |
---|
139 |
} |
---|
140 |
#endif |
---|
141 |
return true; |
---|
142 |
} |
---|
143 |
|
---|
144 |
float |
---|
145 |
CycleTimerHelper::getRate() |
---|
146 |
{ |
---|
147 |
float rate = (float)(diffTicks((uint64_t)m_next_time_ticks, (uint64_t)m_current_time_ticks)); |
---|
148 |
rate /= (float)(m_next_time_usecs - m_current_time_usecs); |
---|
149 |
return rate; |
---|
150 |
} |
---|
151 |
|
---|
152 |
float |
---|
153 |
CycleTimerHelper::getNominalRate() |
---|
154 |
{ |
---|
155 |
float rate = ((double)TICKS_PER_SECOND) / 1000000.0; |
---|
156 |
return rate; |
---|
157 |
} |
---|
158 |
|
---|
159 |
#ifdef OLD_STYLE |
---|
160 |
|
---|
161 |
bool |
---|
162 |
CycleTimerHelper::Execute() |
---|
163 |
{ |
---|
164 |
usleep(1000*1000); |
---|
165 |
return true; |
---|
166 |
} |
---|
167 |
uint32_t |
---|
168 |
CycleTimerHelper::getCycleTimerTicks() |
---|
169 |
{ |
---|
170 |
uint32_t cycle_timer; |
---|
171 |
uint64_t local_time; |
---|
172 |
if(!m_Parent.readCycleTimerReg(&cycle_timer, &local_time)) { |
---|
173 |
debugError("Could not read cycle timer register\n"); |
---|
174 |
return 0; |
---|
175 |
} |
---|
176 |
return CYCLE_TIMER_TO_TICKS(cycle_timer); |
---|
177 |
} |
---|
178 |
|
---|
179 |
uint32_t |
---|
180 |
CycleTimerHelper::getCycleTimerTicks(uint64_t now) |
---|
181 |
{ |
---|
182 |
return getCycleTimerTicks(); |
---|
183 |
} |
---|
184 |
|
---|
185 |
uint32_t |
---|
186 |
CycleTimerHelper::getCycleTimer() |
---|
187 |
{ |
---|
188 |
uint32_t cycle_timer; |
---|
189 |
uint64_t local_time; |
---|
190 |
if(!m_Parent.readCycleTimerReg(&cycle_timer, &local_time)) { |
---|
191 |
debugError("Could not read cycle timer register\n"); |
---|
192 |
return 0; |
---|
193 |
} |
---|
194 |
return cycle_timer; |
---|
195 |
} |
---|
196 |
|
---|
197 |
uint32_t |
---|
198 |
CycleTimerHelper::getCycleTimer(uint64_t now) |
---|
199 |
{ |
---|
200 |
return getCycleTimer(); |
---|
201 |
} |
---|
202 |
#else |
---|
203 |
|
---|
204 |
bool |
---|
205 |
CycleTimerHelper::Execute() |
---|
206 |
{ |
---|
207 |
debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Execute %p...\n", this); |
---|
208 |
uint32_t cycle_timer; |
---|
209 |
uint64_t local_time; |
---|
210 |
if(!m_Parent.readCycleTimerReg(&cycle_timer, &local_time)) { |
---|
211 |
debugError("Could not read cycle timer register\n"); |
---|
212 |
return false; |
---|
213 |
} |
---|
214 |
debugOutput( DEBUG_LEVEL_VERY_VERBOSE, " read : CTR: %11lu, local: %17llu\n", |
---|
215 |
cycle_timer, local_time); |
---|
216 |
|
---|
217 |
double usecs_late; |
---|
218 |
if (m_first_run) { |
---|
219 |
usecs_late = 0.0; |
---|
220 |
m_dll_e2 = m_ticks_per_update; |
---|
221 |
m_current_time_usecs = local_time; |
---|
222 |
m_next_time_usecs = m_current_time_usecs + m_usecs_per_update; |
---|
223 |
m_current_time_ticks = CYCLE_TIMER_TO_TICKS( cycle_timer ); |
---|
224 |
m_next_time_ticks = addTicks( (uint64_t)m_current_time_ticks, (uint64_t)m_dll_e2); |
---|
225 |
debugOutput( DEBUG_LEVEL_VERBOSE, " First run\n"); |
---|
226 |
debugOutput( DEBUG_LEVEL_VERBOSE, " usecs/update: %lu, ticks/update: %lu, m_dll_e2: %f\n", |
---|
227 |
m_usecs_per_update, m_ticks_per_update, m_dll_e2); |
---|
228 |
debugOutput( DEBUG_LEVEL_VERBOSE, " usecs current: %f, next: %f\n", m_current_time_usecs, m_next_time_usecs); |
---|
229 |
debugOutput( DEBUG_LEVEL_VERBOSE, " ticks current: %f, next: %f\n", m_current_time_ticks, m_next_time_ticks); |
---|
230 |
m_first_run = false; |
---|
231 |
} else { |
---|
232 |
|
---|
233 |
double diff = m_next_time_usecs - m_current_time_usecs; |
---|
234 |
debugOutput( DEBUG_LEVEL_VERY_VERBOSE, " usecs: local: %11llu current: %f next: %f, diff: %f\n", |
---|
235 |
local_time, m_current_time_usecs, m_next_time_usecs, diff); |
---|
236 |
|
---|
237 |
uint64_t cycle_timer_ticks = CYCLE_TIMER_TO_TICKS(cycle_timer); |
---|
238 |
usecs_late = ((double)local_time) - (m_next_time_usecs); |
---|
239 |
|
---|
240 |
// we update the x-axis values |
---|
241 |
m_current_time_usecs = m_next_time_usecs; |
---|
242 |
m_next_time_usecs = (local_time - usecs_late) + m_usecs_per_update; |
---|
243 |
debugOutput( DEBUG_LEVEL_VERY_VERBOSE, " usecs: current: %f next: %f usecs_late=%f\n", |
---|
244 |
m_current_time_usecs, m_next_time_usecs, usecs_late); |
---|
245 |
|
---|
246 |
// and the y-axis values |
---|
247 |
double diff_ticks = diffTicks(cycle_timer_ticks, (int64_t)m_next_time_ticks); |
---|
248 |
m_current_time_ticks = m_next_time_ticks; |
---|
249 |
m_next_time_ticks = addTicks((uint64_t)m_current_time_ticks, |
---|
250 |
(uint64_t)((DLL_COEFF_B * diff_ticks) + m_dll_e2)); |
---|
251 |
m_dll_e2 += DLL_COEFF_C * diff_ticks; |
---|
252 |
debugOutput( DEBUG_LEVEL_VERY_VERBOSE, " ticks: current: %f next: %f diff=%f\n", |
---|
253 |
m_current_time_ticks, m_next_time_ticks, diff_ticks); |
---|
254 |
|
---|
255 |
debugOutput( DEBUG_LEVEL_VERY_VERBOSE, " state: local: %11llu, dll_e2: %f, rate: %f\n", |
---|
256 |
local_time, m_dll_e2, getRate()); |
---|
257 |
} |
---|
258 |
|
---|
259 |
// track the average wakeup delay |
---|
260 |
m_avg_wakeup_delay += 0.01 * usecs_late; |
---|
261 |
|
---|
262 |
// FIXME: priority inversion! |
---|
263 |
ENTER_CRITICAL_SECTION; |
---|
264 |
m_current_vars.ticks = m_current_time_ticks; |
---|
265 |
m_current_vars.usecs = m_current_time_usecs; |
---|
266 |
m_current_vars.rate = getRate(); |
---|
267 |
EXIT_CRITICAL_SECTION; |
---|
268 |
|
---|
269 |
// wait for the next update period |
---|
270 |
int64_t time_to_sleep = (int64_t)m_next_time_usecs - m_Parent.getCurrentTimeAsUsecs(); |
---|
271 |
time_to_sleep -= (int64_t)m_avg_wakeup_delay; |
---|
272 |
//int64_t time_to_sleep = m_usecs_per_update; |
---|
273 |
if (time_to_sleep > 0) { |
---|
274 |
debugOutput( DEBUG_LEVEL_VERY_VERBOSE, " sleeping %lld usecs (avg delay: %f)\n", time_to_sleep, m_avg_wakeup_delay); |
---|
275 |
usleep(time_to_sleep); |
---|
276 |
} |
---|
277 |
return true; |
---|
278 |
} |
---|
279 |
|
---|
280 |
uint32_t |
---|
281 |
CycleTimerHelper::getCycleTimerTicks() |
---|
282 |
{ |
---|
283 |
uint64_t now = m_Parent.getCurrentTimeAsUsecs(); |
---|
284 |
return getCycleTimerTicks(now); |
---|
285 |
} |
---|
286 |
|
---|
287 |
uint32_t |
---|
288 |
CycleTimerHelper::getCycleTimerTicks(uint64_t now) |
---|
289 |
{ |
---|
290 |
uint32_t retval; |
---|
291 |
struct compute_vars my_vars; |
---|
292 |
|
---|
293 |
// reduce lock contention |
---|
294 |
ENTER_CRITICAL_SECTION; |
---|
295 |
my_vars = m_current_vars; |
---|
296 |
EXIT_CRITICAL_SECTION; |
---|
297 |
|
---|
298 |
double time_diff = now - my_vars.usecs; |
---|
299 |
double y_step_in_ticks = time_diff * my_vars.rate; |
---|
300 |
int64_t y_step_in_ticks_int = (int64_t)y_step_in_ticks; |
---|
301 |
uint64_t offset_in_ticks_int = (uint64_t)my_vars.ticks; |
---|
302 |
|
---|
303 |
if (y_step_in_ticks_int > 0) { |
---|
304 |
retval = addTicks(offset_in_ticks_int, y_step_in_ticks_int); |
---|
305 |
debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "y_step_in_ticks_int > 0: %lld, time_diff: %f, rate: %f, retval: %lu\n", |
---|
306 |
y_step_in_ticks_int, time_diff, my_vars.rate, retval); |
---|
307 |
} else { |
---|
308 |
retval = substractTicks(offset_in_ticks_int, -y_step_in_ticks_int); |
---|
309 |
|
---|
310 |
// this can happen if the update thread was woken up earlier than it should have been |
---|
311 |
debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "y_step_in_ticks_int <= 0: %lld, time_diff: %f, rate: %f, retval: %lu\n", |
---|
312 |
y_step_in_ticks_int, time_diff, my_vars.rate, retval); |
---|
313 |
} |
---|
314 |
|
---|
315 |
return retval; |
---|
316 |
} |
---|
317 |
|
---|
318 |
uint32_t |
---|
319 |
CycleTimerHelper::getCycleTimer() |
---|
320 |
{ |
---|
321 |
return TICKS_TO_CYCLE_TIMER(getCycleTimerTicks()); |
---|
322 |
} |
---|
323 |
|
---|
324 |
uint32_t |
---|
325 |
CycleTimerHelper::getCycleTimer(uint64_t now) |
---|
326 |
{ |
---|
327 |
return TICKS_TO_CYCLE_TIMER(getCycleTimerTicks(now)); |
---|
328 |
} |
---|
329 |
#endif |
---|
330 |
|
---|
331 |
void |
---|
332 |
CycleTimerHelper::setVerboseLevel(int l) |
---|
333 |
{ |
---|
334 |
setDebugLevel(l); |
---|
335 |
} |
---|