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

Revision 980, 4.6 kB (checked in by ppalmers, 13 years ago)

- add runaway checks to more RT threads

Line 
1 /*
2  * Copyright (C) 2005-2008 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 2 of the License, or
12  * (at your option) version 3 of the License.
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 "libutil/Functors.h"
57
58 #include "debugmodule/debugmodule.h"
59
60 class Ieee1394Service;
61
62 class CycleTimerHelper : public Util::RunnableInterface
63 {
64 public:
65     CycleTimerHelper(Ieee1394Service &, unsigned int);
66     CycleTimerHelper(Ieee1394Service &, unsigned int, bool rt, int prio);
67     ~CycleTimerHelper();
68
69     virtual bool Init();
70     virtual bool Execute();
71
72     bool setThreadParameters(bool rt, int priority);
73     bool Start();
74
75     /**
76      * @brief get the current cycle timer value (in ticks)
77      * @note thread safe
78      */
79     uint32_t getCycleTimerTicks();
80
81     /**
82      * @brief get the cycle timer value for a specific time instant (in ticks)
83      * @note thread safe
84      */
85     uint32_t getCycleTimerTicks(uint64_t now);
86
87     /**
88      * @brief get the current cycle timer value (in CTR format)
89      * @note thread safe
90      */
91     uint32_t getCycleTimer();
92
93     /**
94      * @brief get the cycle timer value for a specific time instant (in CTR format)
95      * @note thread safe
96      */
97     uint32_t getCycleTimer(uint64_t now);
98
99     float getRate();
100     float getNominalRate();
101
102     /**
103      * @brief handle a bus reset
104      */
105     void busresetHandler();
106
107     void setVerboseLevel(int l);
108
109 private:
110     bool readCycleTimerWithRetry(uint32_t *cycle_timer, uint64_t *local_time, int ntries);
111     bool initValues();
112
113 #if IEEE1394SERVICE_USE_CYCLETIMER_DLL
114     bool initDLL();
115 #endif
116
117     Ieee1394Service &m_Parent;
118     Util::SystemTimeSource m_TimeSource;
119     // parameters
120     uint32_t m_ticks_per_update;
121     uint32_t m_usecs_per_update;
122
123     float    m_avg_wakeup_delay;
124
125     // state variables
126     double m_dll_e2;
127
128     double m_current_time_usecs;
129     double m_next_time_usecs;
130     double m_current_time_ticks;
131     double m_next_time_ticks;
132     bool m_first_run;
133     ffado_microsecs_t m_sleep_until;
134
135     uint32_t m_cycle_timer_prev;
136     uint64_t m_cycle_timer_ticks_prev;
137     int m_high_bw_updates;
138
139     // cached vars used for computation
140     struct compute_vars {
141         uint64_t usecs;
142         uint64_t ticks;
143         double rate;
144     };
145
146     #define CTRHELPER_NB_SHADOW_VARS 8
147     struct compute_vars m_shadow_vars[CTRHELPER_NB_SHADOW_VARS];
148     volatile unsigned int m_current_shadow_idx;
149
150     // Threading
151     Util::Thread *  m_Thread;
152     bool            m_realtime;
153     unsigned int    m_priority;
154     pthread_mutex_t mb_update_lock;
155
156     // busreset handling
157     Util::Functor* m_busreset_functor;
158     bool            m_unhandled_busreset;
159
160 #ifdef DEBUG
161     uint64_t m_last_loop_entry;
162     int m_successive_short_loops;
163 #endif
164
165     // debug stuff
166     DECLARE_DEBUG_MODULE;
167 };
168 #endif
Note: See TracBrowser for help on using the browser.