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

Revision 863, 4.0 kB (checked in by ppalmers, 13 years ago)

fix cycle timer DLL code

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 #ifndef __CYCLETIMERHELPER_H__
24 #define __CYCLETIMERHELPER_H__
25
26 /**
27  * Implements a DLL based mechanism to track the cycle timer
28  * register of the Ieee1394Service pointed to by the 'parent'.
29  *
30  * A DLL mechanism is performance-wise better suited, since it
31  * does not require an OS call. Hence we run a thread to update
32  * the DLL at regular time intervals, and then use the DLL to
33  * generate a cycle timer estimate for the parent to pass on
34  * to it's clients.
35  *
36  * The idea is to make reading the cycle timer real-time safe,
37  * which isn't (necessarily)the case for the direct raw1394 call,
38  * since it's a kernel call that could block (although the current
39  * implementation is RT safe).
40  *
41  * This also allows us to run on systems not having the
42  * raw1394_read_cycle_timer kernel call. We can always do a normal
43  * read of our own cycle timer register, but that's not very accurate.
44  * The accuracy is improved by this DLL mechanism. Still not as good
45  * as when using the raw1394_read_cycle_timer call, but anyway.
46  *
47  * On the long run this code will also allow us to map system time
48  * on to 1394 time for the current host controller, hence enabling
49  * different clock domains to operate together.
50  */
51
52 #include "libutil/Thread.h"
53 #include "libutil/SystemTimeSource.h"
54 #include "cycletimer.h"
55
56 #include "debugmodule/debugmodule.h"
57
58 class Ieee1394Service;
59
60 class CycleTimerHelper : public Util::RunnableInterface
61 {
62 public:
63     CycleTimerHelper(Ieee1394Service &, unsigned int);
64     CycleTimerHelper(Ieee1394Service &, unsigned int, bool rt, int prio);
65     ~CycleTimerHelper();
66
67     virtual bool Init();
68     virtual bool Execute();
69
70     bool setThreadParameters(bool rt, int priority);
71     bool Start();
72
73     /**
74      * @brief get the current cycle timer value (in ticks)
75      * @note thread safe
76      */
77     uint32_t getCycleTimerTicks();
78
79     /**
80      * @brief get the cycle timer value for a specific time instant (in ticks)
81      * @note thread safe
82      */
83     uint32_t getCycleTimerTicks(uint64_t now);
84
85     /**
86      * @brief get the current cycle timer value (in CTR format)
87      * @note thread safe
88      */
89     uint32_t getCycleTimer();
90
91     /**
92      * @brief get the cycle timer value for a specific time instant (in CTR format)
93      * @note thread safe
94      */
95     uint32_t getCycleTimer(uint64_t now);
96
97     float getRate();
98     float getNominalRate();
99
100     void setVerboseLevel(int l);
101
102 private:
103     Ieee1394Service &m_Parent;
104     Util::SystemTimeSource m_TimeSource;
105     // parameters
106     uint32_t m_ticks_per_update;
107     uint32_t m_usecs_per_update;
108
109     float               m_avg_wakeup_delay;
110
111     // state variables
112     double m_dll_e2;
113
114     double m_current_time_usecs;
115     double m_next_time_usecs;
116     double m_current_time_ticks;
117     double m_next_time_ticks;
118     bool m_first_run;
119     ffado_microsecs_t m_sleep_until;
120
121     // cached vars used for computation
122     struct compute_vars {
123         uint64_t usecs;
124         uint64_t ticks;
125         double rate;
126     };
127
128     struct compute_vars m_current_vars;
129     pthread_mutex_t m_compute_vars_lock;
130
131     // Threading
132     Util::Thread *  m_Thread;
133     bool            m_realtime;
134     unsigned int    m_priority;
135
136     // debug stuff
137     DECLARE_DEBUG_MODULE;
138 };
139 #endif
Note: See TracBrowser for help on using the browser.