root/trunk/libffado/src/libieee1394/CycleTimerHelper.cpp

Revision 782, 9.9 kB (checked in by ppalmers, 13 years ago)

fix mutex macro's; don't run thread in old_style mode

Line 
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(m_usecs_per_update);
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 false;
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 #else
186
187 bool
188 CycleTimerHelper::Execute()
189 {
190     debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Execute %p...\n", this);
191     uint32_t cycle_timer;
192     uint64_t local_time;
193     if(!m_Parent.readCycleTimerReg(&cycle_timer, &local_time)) {
194         debugError("Could not read cycle timer register\n");
195         return false;
196     }
197     debugOutput( DEBUG_LEVEL_VERY_VERBOSE, " read : CTR: %11lu, local: %17llu\n",
198                     cycle_timer, local_time);
199
200     double usecs_late;
201     if (m_first_run) {
202         usecs_late = 0.0;
203         m_dll_e2 = m_ticks_per_update;
204         m_current_time_usecs = local_time;
205         m_next_time_usecs = m_current_time_usecs + m_usecs_per_update;
206         m_current_time_ticks = CYCLE_TIMER_TO_TICKS( cycle_timer );
207         m_next_time_ticks = addTicks( (uint64_t)m_current_time_ticks, (uint64_t)m_dll_e2);
208         debugOutput( DEBUG_LEVEL_VERBOSE, " First run\n");
209         debugOutput( DEBUG_LEVEL_VERBOSE, "  usecs/update: %lu, ticks/update: %lu, m_dll_e2: %f\n",
210                                           m_usecs_per_update, m_ticks_per_update, m_dll_e2);
211         debugOutput( DEBUG_LEVEL_VERBOSE, "  usecs current: %f, next: %f\n", m_current_time_usecs, m_next_time_usecs);
212         debugOutput( DEBUG_LEVEL_VERBOSE, "  ticks current: %f, next: %f\n", m_current_time_ticks, m_next_time_ticks);
213         m_first_run = false;
214     } else {
215
216         double diff = m_next_time_usecs - m_current_time_usecs;
217         debugOutput( DEBUG_LEVEL_VERY_VERBOSE, " usecs: local: %11llu current: %f next: %f, diff: %f\n",
218                     local_time, m_current_time_usecs, m_next_time_usecs, diff);
219
220         uint64_t cycle_timer_ticks = CYCLE_TIMER_TO_TICKS(cycle_timer);
221         usecs_late = ((double)local_time) - (m_next_time_usecs);
222
223         // we update the x-axis values
224         m_current_time_usecs = m_next_time_usecs;
225         m_next_time_usecs = (local_time - usecs_late) + m_usecs_per_update;
226         debugOutput( DEBUG_LEVEL_VERY_VERBOSE, " usecs: current: %f next: %f usecs_late=%f\n",
227                     m_current_time_usecs, m_next_time_usecs, usecs_late);
228
229         // and the y-axis values
230         double diff_ticks = diffTicks(cycle_timer_ticks, (int64_t)m_next_time_ticks);
231         m_current_time_ticks = m_next_time_ticks;
232         m_next_time_ticks = addTicks((uint64_t)m_current_time_ticks,
233                                      (uint64_t)((DLL_COEFF_B * diff_ticks) + m_dll_e2));
234         m_dll_e2 += DLL_COEFF_C * diff_ticks;
235         debugOutput( DEBUG_LEVEL_VERY_VERBOSE, " ticks: current: %f next: %f diff=%f\n",
236                     m_current_time_ticks, m_next_time_ticks, diff_ticks);
237
238         debugOutput( DEBUG_LEVEL_VERY_VERBOSE, " state: local: %11llu, dll_e2: %f, rate: %f\n",
239                     local_time, m_dll_e2, getRate());
240     }
241
242     // track the average wakeup delay
243     m_avg_wakeup_delay += 0.01 * usecs_late;
244
245     // FIXME: priority inversion!
246     ENTER_CRITICAL_SECTION;
247     m_current_vars.ticks = m_current_time_ticks;
248     m_current_vars.usecs = m_current_time_usecs;
249     m_current_vars.rate = getRate();
250     EXIT_CRITICAL_SECTION;
251
252     // wait for the next update period
253     int64_t time_to_sleep = (int64_t)m_next_time_usecs - m_Parent.getCurrentTimeAsUsecs();
254     time_to_sleep -= (int64_t)m_avg_wakeup_delay;
255     //int64_t time_to_sleep = m_usecs_per_update;
256     if (time_to_sleep > 0) {
257         debugOutput( DEBUG_LEVEL_VERY_VERBOSE, " sleeping %lld usecs (avg delay: %f)\n", time_to_sleep, m_avg_wakeup_delay);
258         usleep(time_to_sleep);
259     }
260     return true;
261 }
262
263 uint32_t
264 CycleTimerHelper::getCycleTimerTicks()
265 {
266     uint64_t now = m_Parent.getCurrentTimeAsUsecs();
267     return getCycleTimerTicks(now);
268 }
269
270 uint32_t
271 CycleTimerHelper::getCycleTimerTicks(uint64_t now)
272 {
273     uint32_t retval;
274     struct compute_vars my_vars;
275
276     // reduce lock contention
277     ENTER_CRITICAL_SECTION;
278     my_vars = m_current_vars;
279     EXIT_CRITICAL_SECTION;
280
281     double time_diff = now - my_vars.usecs;
282     double y_step_in_ticks = time_diff * my_vars.rate;
283     int64_t y_step_in_ticks_int = (int64_t)y_step_in_ticks;
284     uint64_t offset_in_ticks_int = (uint64_t)my_vars.ticks;
285
286     if (y_step_in_ticks_int > 0) {
287         retval = addTicks(offset_in_ticks_int, y_step_in_ticks_int);
288         debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "y_step_in_ticks_int > 0: %lld, time_diff: %f, rate: %f, retval: %lu\n",
289                      y_step_in_ticks_int, time_diff, my_vars.rate, retval);
290     } else {
291         retval = substractTicks(offset_in_ticks_int, -y_step_in_ticks_int);
292
293         // this can happen if the update thread was woken up earlier than it should have been
294         debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "y_step_in_ticks_int <= 0: %lld, time_diff: %f, rate: %f, retval: %lu\n",
295                      y_step_in_ticks_int, time_diff, my_vars.rate, retval);
296     }
297
298     return retval;
299 }
300 #endif
301
302 uint32_t
303 CycleTimerHelper::getCycleTimer()
304 {
305     return TICKS_TO_CYCLE_TIMER(getCycleTimerTicks());
306 }
307
308 uint32_t
309 CycleTimerHelper::getCycleTimer(uint64_t now)
310 {
311     return TICKS_TO_CYCLE_TIMER(getCycleTimerTicks(now));
312 }
313
314 void
315 CycleTimerHelper::setVerboseLevel(int l)
316 {
317     setDebugLevel(l);
318 }
Note: See TracBrowser for help on using the browser.