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

Revision 767, 9.8 kB (checked in by ppalmers, 13 years ago)

code refactoring

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