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

Revision 752, 3.9 kB (checked in by ppalmers, 13 years ago)

- Implement a DLL based mechanism to read the cycle timer. This can potentially be more lightweight for the reader threads since it avoids a the CTR read kernel call. It also has the
side effect that FFADO now works on older kernels that don't implement the cycle timer read call.

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