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

Revision 753, 8.8 kB (checked in by ppalmers, 13 years ago)

have separate threads for every handler

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) (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 bool
139 CycleTimerHelper::Execute()
140 {
141     debugOutput( DEBUG_LEVEL_ULTRA_VERBOSE, "Execute %p...\n", this);
142     uint32_t cycle_timer;
143     uint64_t local_time;
144     if(!m_Parent.readCycleTimerReg(&cycle_timer, &local_time)) {
145         debugError("Could not read cycle timer register\n");
146         return false;
147     }
148     debugOutput( DEBUG_LEVEL_ULTRA_VERBOSE, " read : CTR: %11lu, local: %17llu\n",
149                     cycle_timer, local_time);
150
151     double usecs_late;
152     if (m_first_run) {
153         usecs_late = 0.0;
154         m_dll_e2 = m_ticks_per_update;
155         m_current_time_usecs = local_time;
156         m_next_time_usecs = m_current_time_usecs + m_usecs_per_update;
157         m_current_time_ticks = CYCLE_TIMER_TO_TICKS( cycle_timer );
158         m_next_time_ticks = addTicks( (uint64_t)m_current_time_ticks, (uint64_t)m_dll_e2);
159         debugOutput( DEBUG_LEVEL_VERBOSE, " First run\n");
160         debugOutput( DEBUG_LEVEL_VERBOSE, "  usecs/update: %lu, ticks/update: %lu, m_dll_e2: %f\n",
161                                           m_usecs_per_update, m_ticks_per_update, m_dll_e2);
162         debugOutput( DEBUG_LEVEL_VERBOSE, "  usecs current: %f, next: %f\n", m_current_time_usecs, m_next_time_usecs);
163         debugOutput( DEBUG_LEVEL_VERBOSE, "  ticks current: %f, next: %f\n", m_current_time_ticks, m_next_time_ticks);
164         m_first_run = false;
165     } else {
166
167         double diff = m_next_time_usecs - m_current_time_usecs;
168         debugOutput( DEBUG_LEVEL_ULTRA_VERBOSE, " usecs: local: %11llu current: %f next: %f, diff: %f\n",
169                     local_time, m_current_time_usecs, m_next_time_usecs, diff);
170
171         uint64_t cycle_timer_ticks = CYCLE_TIMER_TO_TICKS(cycle_timer);
172         usecs_late = ((double)local_time) - (m_next_time_usecs);
173
174         // we update the x-axis values
175         m_current_time_usecs = m_next_time_usecs;
176         m_next_time_usecs = (local_time - usecs_late) + m_usecs_per_update;
177         debugOutput( DEBUG_LEVEL_ULTRA_VERBOSE, " usecs: current: %f next: %f usecs_late=%f\n",
178                     m_current_time_usecs, m_next_time_usecs, usecs_late);
179
180         // and the y-axis values
181         double diff_ticks = diffTicks(cycle_timer_ticks, (int64_t)m_next_time_ticks);
182         m_current_time_ticks = m_next_time_ticks;
183         m_next_time_ticks = addTicks((uint64_t)m_current_time_ticks,
184                                      (uint64_t)((DLL_COEFF_B * diff_ticks) + m_dll_e2));
185         m_dll_e2 += DLL_COEFF_C * diff_ticks;
186         debugOutput( DEBUG_LEVEL_ULTRA_VERBOSE, " ticks: current: %f next: %f diff=%f\n",
187                     m_current_time_ticks, m_next_time_ticks, diff_ticks);
188
189         debugOutput( DEBUG_LEVEL_ULTRA_VERBOSE, " state: local: %11llu, dll_e2: %f, rate: %f\n",
190                     local_time, m_dll_e2, getRate());
191     }
192
193     // track the average wakeup delay
194     m_avg_wakeup_delay += 0.01 * usecs_late;
195
196     // FIXME: priority inversion!
197     pthread_mutex_lock(&m_compute_vars_lock);
198     m_current_vars.ticks = m_current_time_ticks;
199     m_current_vars.usecs = m_current_time_usecs;
200     m_current_vars.rate = getRate();
201     pthread_mutex_unlock(&m_compute_vars_lock);
202
203     // wait for the next update period
204     int64_t time_to_sleep = (int64_t)m_next_time_usecs - m_Parent.getCurrentTimeAsUsecs();
205     time_to_sleep -= (int64_t)m_avg_wakeup_delay;
206     //int64_t time_to_sleep = m_usecs_per_update;
207     if (time_to_sleep > 0) {
208         debugOutput( DEBUG_LEVEL_ULTRA_VERBOSE, " sleeping %lld usecs (avg delay: %f)\n", time_to_sleep, m_avg_wakeup_delay);
209         usleep(time_to_sleep);
210     }
211     return true;
212 }
213
214 uint32_t
215 CycleTimerHelper::getCycleTimerTicks()
216 {
217     uint64_t now = m_Parent.getCurrentTimeAsUsecs();
218     return getCycleTimerTicks(now);
219 }
220
221 uint32_t
222 CycleTimerHelper::getCycleTimerTicks(uint64_t now)
223 {
224     uint32_t retval;
225     struct compute_vars my_vars;
226
227     // reduce lock contention
228     pthread_mutex_lock(&m_compute_vars_lock);
229     my_vars = m_current_vars;
230     pthread_mutex_unlock(&m_compute_vars_lock);
231
232     double time_diff = now - my_vars.usecs;
233     double y_step_in_ticks = time_diff * my_vars.rate;
234     int64_t y_step_in_ticks_int = (int64_t)y_step_in_ticks;
235     uint64_t offset_in_ticks_int = (uint64_t)my_vars.ticks;
236
237     if (y_step_in_ticks_int > 0) {
238         retval = addTicks(offset_in_ticks_int, y_step_in_ticks_int);
239     } else {
240         // this can happen if the update thread was woken up earlier than it should have been
241         debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "y_step_in_ticks_int <= 0: %lld, time_diff: %f, rate: %f\n",
242                      y_step_in_ticks_int, time_diff, my_vars.rate);
243         retval = substractTicks(offset_in_ticks_int, -y_step_in_ticks_int);
244     }
245
246     return retval;
247 }
248
249 uint32_t
250 CycleTimerHelper::getCycleTimer()
251 {
252     return TICKS_TO_CYCLE_TIMER(getCycleTimerTicks());
253 }
254
255 uint32_t
256 CycleTimerHelper::getCycleTimer(uint64_t now)
257 {
258     return TICKS_TO_CYCLE_TIMER(getCycleTimerTicks(now));
259 }
260
261 void
262 CycleTimerHelper::setVerboseLevel(int l)
263 {
264     setDebugLevel(l);
265 }
Note: See TracBrowser for help on using the browser.