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

Revision 754, 9.3 kB (checked in by ppalmers, 16 years ago)

- simplify IsoHandler?
- fix some small issues

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 IMPL_DEBUG_MODULE( CycleTimerHelper, CycleTimerHelper, DEBUG_LEVEL_NORMAL );
36
37 CycleTimerHelper::CycleTimerHelper(Ieee1394Service &parent, unsigned int update_period_us)
38     : m_Parent ( parent )
39     , m_ticks_per_update ( ((uint64_t)TICKS_PER_SECOND) * ((uint64_t)update_period_us) / 1000000ULL )
40     , m_usecs_per_update ( update_period_us )
41     , m_avg_wakeup_delay ( 0.0 )
42     , m_dll_e2 ( 0.0 )
43     , m_current_time_usecs ( 0 )
44     , m_next_time_usecs ( 0 )
45     , m_current_time_ticks ( 0 )
46     , m_next_time_ticks ( 0 )
47     , m_first_run ( true )
48     , m_Thread ( NULL )
49     , m_realtime ( false )
50     , m_priority ( 0 )
51 {
52     debugOutput( DEBUG_LEVEL_VERBOSE, "Create %p...\n", this);
53 }
54
55 CycleTimerHelper::CycleTimerHelper(Ieee1394Service &parent, unsigned int update_period_us, bool rt, int prio)
56     : m_Parent ( parent )
57     , m_ticks_per_update ( ((uint64_t)TICKS_PER_SECOND) * ((uint64_t)update_period_us) / 1000000ULL )
58     , m_usecs_per_update ( update_period_us )
59     , m_avg_wakeup_delay ( 0.0 )
60     , m_dll_e2 ( 0.0 )
61     , m_current_time_usecs ( 0 )
62     , m_next_time_usecs ( 0 )
63     , m_current_time_ticks ( 0 )
64     , m_next_time_ticks ( 0 )
65     , m_first_run ( true )
66     , m_Thread ( NULL )
67     , m_realtime ( rt )
68     , m_priority ( prio )
69 {
70     debugOutput( DEBUG_LEVEL_VERBOSE, "Create %p...\n", this);
71 }
72
73 CycleTimerHelper::~CycleTimerHelper()
74 {
75     if (m_Thread) {
76         m_Thread->Stop();
77         delete m_Thread;
78     }
79 }
80
81 bool
82 CycleTimerHelper::Start()
83 {
84     debugOutput( DEBUG_LEVEL_VERBOSE, "Start %p...\n", this);
85     m_Thread = new Util::PosixThread(this, m_realtime, m_priority,
86                                      PTHREAD_CANCEL_DEFERRED);
87     if(!m_Thread) {
88         debugFatal("No thread\n");
89         return false;
90     }
91     if (m_Thread->Start() != 0) {
92         debugFatal("Could not start update thread\n");
93         return false;
94     }
95     return true;
96 }
97
98 bool
99 CycleTimerHelper::Init()
100 {
101     debugOutput( DEBUG_LEVEL_VERBOSE, "Initialize %p...\n", this);
102     pthread_mutex_init(&m_compute_vars_lock, NULL);
103     return true;
104 }
105
106 bool
107 CycleTimerHelper::setThreadParameters(bool rt, int priority) {
108     debugOutput( DEBUG_LEVEL_VERBOSE, "(%p) switch to: (rt=%d, prio=%d)...\n", this, rt, priority);
109     if (priority > 98) priority = 98; // cap the priority
110     m_realtime = rt;
111     m_priority = priority;
112
113     if (m_Thread) {
114         if (m_realtime) {
115             m_Thread->AcquireRealTime(m_priority);
116         } else {
117             m_Thread->DropRealTime();
118         }
119     }
120     return true;
121 }
122
123 float
124 CycleTimerHelper::getRate()
125 {
126     float rate = (float)(diffTicks((uint64_t)m_next_time_ticks, (uint64_t)m_current_time_ticks));
127     rate /= (float)(m_next_time_usecs - m_current_time_usecs);
128     return rate;
129 }
130
131 float
132 CycleTimerHelper::getNominalRate()
133 {
134     float rate = ((double)TICKS_PER_SECOND) / 1000000.0;
135     return rate;
136 }
137
138 //#define OLD_STYLE
139 #ifdef OLD_STYLE
140
141 bool
142 CycleTimerHelper::Execute()
143 {
144     usleep(m_usecs_per_update);
145     return true;
146 }
147 uint32_t
148 CycleTimerHelper::getCycleTimerTicks()
149 {
150     uint32_t cycle_timer;
151     uint64_t local_time;
152     if(!m_Parent.readCycleTimerReg(&cycle_timer, &local_time)) {
153         debugError("Could not read cycle timer register\n");
154         return false;
155     }
156     return CYCLE_TIMER_TO_TICKS(cycle_timer);
157 }
158
159 uint32_t
160 CycleTimerHelper::getCycleTimerTicks(uint64_t now)
161 {
162     return getCycleTimerTicks();
163 }
164
165 #else
166
167 bool
168 CycleTimerHelper::Execute()
169 {
170     debugOutput( DEBUG_LEVEL_ULTRA_VERBOSE, "Execute %p...\n", this);
171     uint32_t cycle_timer;
172     uint64_t local_time;
173     if(!m_Parent.readCycleTimerReg(&cycle_timer, &local_time)) {
174         debugError("Could not read cycle timer register\n");
175         return false;
176     }
177     debugOutput( DEBUG_LEVEL_ULTRA_VERBOSE, " read : CTR: %11lu, local: %17llu\n",
178                     cycle_timer, local_time);
179
180     double usecs_late;
181     if (m_first_run) {
182         usecs_late = 0.0;
183         m_dll_e2 = m_ticks_per_update;
184         m_current_time_usecs = local_time;
185         m_next_time_usecs = m_current_time_usecs + m_usecs_per_update;
186         m_current_time_ticks = CYCLE_TIMER_TO_TICKS( cycle_timer );
187         m_next_time_ticks = addTicks( (uint64_t)m_current_time_ticks, (uint64_t)m_dll_e2);
188         debugOutput( DEBUG_LEVEL_VERBOSE, " First run\n");
189         debugOutput( DEBUG_LEVEL_VERBOSE, "  usecs/update: %lu, ticks/update: %lu, m_dll_e2: %f\n",
190                                           m_usecs_per_update, m_ticks_per_update, m_dll_e2);
191         debugOutput( DEBUG_LEVEL_VERBOSE, "  usecs current: %f, next: %f\n", m_current_time_usecs, m_next_time_usecs);
192         debugOutput( DEBUG_LEVEL_VERBOSE, "  ticks current: %f, next: %f\n", m_current_time_ticks, m_next_time_ticks);
193         m_first_run = false;
194     } else {
195
196         double diff = m_next_time_usecs - m_current_time_usecs;
197         debugOutput( DEBUG_LEVEL_ULTRA_VERBOSE, " usecs: local: %11llu current: %f next: %f, diff: %f\n",
198                     local_time, m_current_time_usecs, m_next_time_usecs, diff);
199
200         uint64_t cycle_timer_ticks = CYCLE_TIMER_TO_TICKS(cycle_timer);
201         usecs_late = ((double)local_time) - (m_next_time_usecs);
202
203         // we update the x-axis values
204         m_current_time_usecs = m_next_time_usecs;
205         m_next_time_usecs = (local_time - usecs_late) + m_usecs_per_update;
206         debugOutput( DEBUG_LEVEL_ULTRA_VERBOSE, " usecs: current: %f next: %f usecs_late=%f\n",
207                     m_current_time_usecs, m_next_time_usecs, usecs_late);
208
209         // and the y-axis values
210         double diff_ticks = diffTicks(cycle_timer_ticks, (int64_t)m_next_time_ticks);
211         m_current_time_ticks = m_next_time_ticks;
212         m_next_time_ticks = addTicks((uint64_t)m_current_time_ticks,
213                                      (uint64_t)((DLL_COEFF_B * diff_ticks) + m_dll_e2));
214         m_dll_e2 += DLL_COEFF_C * diff_ticks;
215         debugOutput( DEBUG_LEVEL_ULTRA_VERBOSE, " ticks: current: %f next: %f diff=%f\n",
216                     m_current_time_ticks, m_next_time_ticks, diff_ticks);
217
218         debugOutput( DEBUG_LEVEL_ULTRA_VERBOSE, " state: local: %11llu, dll_e2: %f, rate: %f\n",
219                     local_time, m_dll_e2, getRate());
220     }
221
222     // track the average wakeup delay
223     m_avg_wakeup_delay += 0.01 * usecs_late;
224
225     // FIXME: priority inversion!
226     pthread_mutex_lock(&m_compute_vars_lock);
227     m_current_vars.ticks = m_current_time_ticks;
228     m_current_vars.usecs = m_current_time_usecs;
229     m_current_vars.rate = getRate();
230     pthread_mutex_unlock(&m_compute_vars_lock);
231
232     // wait for the next update period
233     int64_t time_to_sleep = (int64_t)m_next_time_usecs - m_Parent.getCurrentTimeAsUsecs();
234     time_to_sleep -= (int64_t)m_avg_wakeup_delay;
235     //int64_t time_to_sleep = m_usecs_per_update;
236     if (time_to_sleep > 0) {
237         debugOutput( DEBUG_LEVEL_ULTRA_VERBOSE, " sleeping %lld usecs (avg delay: %f)\n", time_to_sleep, m_avg_wakeup_delay);
238         usleep(time_to_sleep);
239     }
240     return true;
241 }
242
243 uint32_t
244 CycleTimerHelper::getCycleTimerTicks()
245 {
246     uint64_t now = m_Parent.getCurrentTimeAsUsecs();
247     return getCycleTimerTicks(now);
248 }
249
250 uint32_t
251 CycleTimerHelper::getCycleTimerTicks(uint64_t now)
252 {
253     uint32_t retval;
254     struct compute_vars my_vars;
255
256     // reduce lock contention
257     pthread_mutex_lock(&m_compute_vars_lock);
258     my_vars = m_current_vars;
259     pthread_mutex_unlock(&m_compute_vars_lock);
260
261     double time_diff = now - my_vars.usecs;
262     double y_step_in_ticks = time_diff * my_vars.rate;
263     int64_t y_step_in_ticks_int = (int64_t)y_step_in_ticks;
264     uint64_t offset_in_ticks_int = (uint64_t)my_vars.ticks;
265
266     if (y_step_in_ticks_int > 0) {
267         retval = addTicks(offset_in_ticks_int, y_step_in_ticks_int);
268     } else {
269         // this can happen if the update thread was woken up earlier than it should have been
270         debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "y_step_in_ticks_int <= 0: %lld, time_diff: %f, rate: %f\n",
271                      y_step_in_ticks_int, time_diff, my_vars.rate);
272         retval = substractTicks(offset_in_ticks_int, -y_step_in_ticks_int);
273     }
274
275     return retval;
276 }
277 #endif
278
279 uint32_t
280 CycleTimerHelper::getCycleTimer()
281 {
282     return TICKS_TO_CYCLE_TIMER(getCycleTimerTicks());
283 }
284
285 uint32_t
286 CycleTimerHelper::getCycleTimer(uint64_t now)
287 {
288     return TICKS_TO_CYCLE_TIMER(getCycleTimerTicks(now));
289 }
290
291 void
292 CycleTimerHelper::setVerboseLevel(int l)
293 {
294     setDebugLevel(l);
295 }
Note: See TracBrowser for help on using the browser.