root/trunk/libffado/tests/test-ieee1394service.cpp

Revision 881, 12.1 kB (checked in by ppalmers, 13 years ago)

remove jitter graph output debug prints

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
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <endian.h>
32
33 #include <signal.h>
34 #include "src/debugmodule/debugmodule.h"
35
36 #include <netinet/in.h>
37
38 #include "src/libieee1394/cycletimer.h"
39 #include "src/libieee1394/configrom.h"
40 #include "src/libieee1394/ieee1394service.h"
41 #include "src/libieee1394/ARMHandler.h"
42
43 #include "src/libutil/Thread.h"
44 #include "src/libutil/PosixThread.h"
45 #include <libraw1394/raw1394.h>
46 #include "libutil/Time.h"
47
48
49     #define NB_THREADS 1
50     #define THREAD_RT  true
51     #define THREAD_PRIO 51
52     #define THREAD_SLEEP_US 2000
53    
54 using namespace Util;
55
56 DECLARE_GLOBAL_DEBUG_MODULE;
57
58 #define DIFF_CONSIDERED_LARGE (3027/2)
59 int PORT_TO_USE = 1;
60
61 int max_diff=-99999;
62 int min_diff= 99999;
63
64 int run=1;
65 static void sighandler (int sig)
66 {
67     run = 0;
68 }
69
70 class MyFunctor : public Functor
71 {
72 public:
73     MyFunctor() {}
74     virtual ~MyFunctor() {}
75
76     void operator() () {
77         printf("hello from the functor (%p)\n", this);
78     };
79 };
80
81 class CtrThread : public Util::RunnableInterface
82 {
83     public:
84         CtrThread(Ieee1394Service *s)
85         : m_service(s)
86         {};
87         virtual ~CtrThread() {};
88         virtual bool Init()
89         {
90             debugOutput(DEBUG_LEVEL_NORMAL, "(%p) Execute\n", this);
91             ctr=0;
92             ctr_dll=0;
93        
94             ctr_prev=0;
95             ctr_dll_prev=0;
96             m_handle = raw1394_new_handle_on_port( PORT_TO_USE );
97             if ( !m_handle ) {
98                 if ( !errno ) {
99                     debugFatal("libraw1394 not compatible\n");
100                 } else {
101                     debugFatal("Ieee1394Service::initialize: Could not get 1394 handle: %s\n",
102                         strerror(errno) );
103                     debugFatal("Is ieee1394 and raw1394 driver loaded?\n");
104                 }
105                 return false;
106             }
107             return true;
108         }
109         virtual bool Execute();
110
111         Ieee1394Service *m_service;
112         raw1394handle_t m_handle;
113         uint64_t ctr;
114         uint64_t ctr_dll;
115
116         uint64_t ctr_prev;
117         uint64_t ctr_dll_prev;
118        
119         uint64_t nb_checks;
120         int64_t summed_diff;
121         double avg_diff;
122 };
123
124 bool CtrThread::Execute() {
125     debugOutput(DEBUG_LEVEL_VERBOSE, "(%p) Execute\n", this);
126    
127     SleepRelativeUsec(THREAD_SLEEP_US);
128
129     uint32_t cycle_timer;
130     uint64_t local_time;
131     uint32_t cycle_timer2;
132     uint64_t local_time2;
133     uint64_t ticks1, ticks2;
134     int err;
135
136     do {
137         // read the CTR 'raw' from a handle
138         // and read it from the 1394 service, which uses a DLL
139         err = raw1394_read_cycle_timer(m_handle, &cycle_timer2, &local_time2);
140         err = raw1394_read_cycle_timer(m_handle, &cycle_timer, &local_time);
141        
142         ticks1 = CYCLE_TIMER_TO_TICKS(cycle_timer);
143         ticks2 = CYCLE_TIMER_TO_TICKS(cycle_timer2);
144     } while (diffTicks(ticks1, ticks2) < 0);
145    
146     ctr_prev = ctr;
147     ctr_dll_prev = ctr_dll;
148    
149     ctr = CYCLE_TIMER_TO_TICKS( cycle_timer );
150     ctr_dll = m_service->getCycleTimerTicks(local_time);
151
152     if(err) {
153         debugError("(%p) CTR read error\n", this);
154     }
155     debugOutput ( DEBUG_LEVEL_VERBOSE,
156                 "(%p) Cycle timer: %011llu (%03us %04ucy %04uticks)\n",
157                 this, ctr,
158                 (unsigned int)TICKS_TO_SECS( ctr ),
159                 (unsigned int)TICKS_TO_CYCLES( ctr ),
160                 (unsigned int)TICKS_TO_OFFSET( ctr ) );
161     debugOutput ( DEBUG_LEVEL_VERBOSE,
162                 "(%p)    from DLL: %011llu (%03us %04ucy %04uticks)\n",
163                 this, ctr_dll,
164                 (unsigned int)TICKS_TO_SECS( ctr_dll ),
165                 (unsigned int)TICKS_TO_CYCLES( ctr_dll ),
166                 (unsigned int)TICKS_TO_OFFSET( ctr_dll ) );
167     int64_t diff = diffTicks(ctr, ctr_dll);
168     uint64_t abs_diff;
169     // for jitter plots
170     //     debugOutput(DEBUG_LEVEL_NORMAL, "9876543210: %lld\n", diff);
171
172     // not 100% thread safe, but will do
173     if (diff > max_diff) max_diff = diff;
174     if (diff < min_diff) min_diff = diff;
175     summed_diff += diff;
176     nb_checks++;
177     avg_diff = ((double)summed_diff)/((double)nb_checks);
178    
179     if (diff < 0) {
180         abs_diff = -diff;
181     } else {
182         abs_diff = diff;
183     }
184     debugOutput ( DEBUG_LEVEL_VERBOSE,
185                 "(%p)       diff: %s%011llu (%03us %04ucy %04uticks)\n", this,
186                 ((int64_t)abs_diff==diff?" ":"-"), abs_diff, (unsigned int)TICKS_TO_SECS( abs_diff ),
187                 (unsigned int)TICKS_TO_CYCLES( abs_diff ), (unsigned int)TICKS_TO_OFFSET( abs_diff ) );
188     if (abs_diff > DIFF_CONSIDERED_LARGE) {
189         debugWarning("(%p) Alert, large diff: %lld\n", this, diff);
190         debugOutput ( DEBUG_LEVEL_NORMAL,
191                     "(%p)  Cycle timer: %011llu (%03us %04ucy %04uticks)\n",
192                     this, ctr,
193                     (unsigned int)TICKS_TO_SECS( ctr ),
194                     (unsigned int)TICKS_TO_CYCLES( ctr ),
195                     (unsigned int)TICKS_TO_OFFSET( ctr ) );
196         debugOutput ( DEBUG_LEVEL_NORMAL,
197                     "(%p)   from DLL: %011llu (%03us %04ucy %04uticks)\n",
198                     this, ctr_dll,
199                     (unsigned int)TICKS_TO_SECS( ctr_dll ),
200                     (unsigned int)TICKS_TO_CYCLES( ctr_dll ),
201                     (unsigned int)TICKS_TO_OFFSET( ctr_dll ) );
202     }
203    
204     diff = diffTicks(ctr, ctr_prev);
205     if (diff < 0) {
206         debugWarning("(%p) Alert, non-monotonic ctr (direct): %llu - %llu = %lld\n",
207                      this, ctr, ctr_prev, diff);
208         debugOutput ( DEBUG_LEVEL_NORMAL,
209                     "(%p)  Cycle timer now : %011llu (%03us %04ucy %04uticks)\n",
210                     this, ctr,
211                     (unsigned int)TICKS_TO_SECS( ctr ),
212                     (unsigned int)TICKS_TO_CYCLES( ctr ),
213                     (unsigned int)TICKS_TO_OFFSET( ctr ) );
214         debugOutput ( DEBUG_LEVEL_NORMAL,
215                     "(%p)  Cycle timer prev: %011llu (%03us %04ucy %04uticks)\n",
216                     this, ctr_prev,
217                     (unsigned int)TICKS_TO_SECS( ctr_prev ),
218                     (unsigned int)TICKS_TO_CYCLES( ctr_prev ),
219                     (unsigned int)TICKS_TO_OFFSET( ctr_prev ) );
220     }
221     diff = diffTicks(ctr_dll, ctr_dll_prev);
222     if (diff < 0) {
223         debugWarning("(%p) Alert, non-monotonic ctr (dll): %llu - %llu = %lld\n",
224                      this, ctr_dll, ctr_dll_prev, diff);
225         debugOutput ( DEBUG_LEVEL_NORMAL,
226                     "(%p)  Cycle timer now : %011llu (%03us %04ucy %04uticks)\n",
227                     this, ctr_dll,
228                     (unsigned int)TICKS_TO_SECS( ctr_dll ),
229                     (unsigned int)TICKS_TO_CYCLES( ctr_dll ),
230                     (unsigned int)TICKS_TO_OFFSET( ctr_dll ) );
231         debugOutput ( DEBUG_LEVEL_NORMAL,
232                     "(%p)  Cycle timer prev: %011llu (%03us %04ucy %04uticks)\n",
233                     this, ctr_dll_prev,
234                     (unsigned int)TICKS_TO_SECS( ctr_dll_prev ),
235                     (unsigned int)TICKS_TO_CYCLES( ctr_dll_prev ),
236                     (unsigned int)TICKS_TO_OFFSET( ctr_dll_prev ) );
237     }
238    
239     // check some calculations
240     uint32_t tmp_orig = m_service->getCycleTimer();
241     uint32_t tmp_ticks = CYCLE_TIMER_TO_TICKS(tmp_orig);
242     uint32_t tmp_ctr = TICKS_TO_CYCLE_TIMER(tmp_ticks);
243    
244     if (tmp_orig != tmp_ctr) {
245         debugError("CTR => TICKS => CTR failed\n");
246         debugOutput ( DEBUG_LEVEL_VERBOSE,
247                     "(%p) orig CTR : %08X (%03us %04ucy %04uticks)\n",
248                     this, (uint32_t)tmp_orig,
249                     (unsigned int)CYCLE_TIMER_GET_SECS( tmp_orig ),
250                     (unsigned int)CYCLE_TIMER_GET_CYCLES( tmp_orig ),
251                     (unsigned int)CYCLE_TIMER_GET_OFFSET( tmp_orig ) );
252         debugOutput ( DEBUG_LEVEL_VERBOSE,
253                     "(%p) TICKS: %011llu (%03us %04ucy %04uticks)\n",
254                     this, tmp_ticks,
255                     (unsigned int)TICKS_TO_SECS( tmp_ticks ),
256                     (unsigned int)TICKS_TO_CYCLES( tmp_ticks ),
257                     (unsigned int)TICKS_TO_OFFSET( tmp_ticks ) );
258         debugOutput ( DEBUG_LEVEL_VERBOSE,
259                     "(%p) new CTR : %08X (%03us %04ucy %04uticks)\n",
260                     this, (uint32_t)tmp_ctr,
261                     (unsigned int)CYCLE_TIMER_GET_SECS( tmp_ctr ),
262                     (unsigned int)CYCLE_TIMER_GET_CYCLES( tmp_ctr ),
263                     (unsigned int)CYCLE_TIMER_GET_OFFSET( tmp_ctr ) );
264     }
265    
266     debugOutput ( DEBUG_LEVEL_VERBOSE,
267                 "(%p)  wait...\n", this);
268     return true;
269 }
270
271 int main(int argc, char *argv[])
272 {
273     int i=0;
274     setDebugLevel(DEBUG_LEVEL_NORMAL);
275     signal (SIGINT, sighandler);
276     signal (SIGPIPE, sighandler);
277
278
279     printf("FFADO Ieee1394Service test application\n");
280
281     Ieee1394Service *m_service=NULL;
282
283     m_service = new Ieee1394Service();
284     m_service->setVerboseLevel(DEBUG_LEVEL_VERBOSE);
285     m_service->initialize(PORT_TO_USE);
286     m_service->setThreadParameters(true, 60);
287
288     MyFunctor *test_busreset=new MyFunctor();
289
290     printf(" adding (%p) as busreset handler\n", test_busreset);
291
292     m_service->addBusResetHandler(test_busreset);
293
294     nodeaddr_t addr =  m_service->findFreeARMBlock(0x0000FFFFE0000000ULL, 4, 4 );
295
296     ARMHandler *test_arm=new ARMHandler(addr,
297                          4,
298                          RAW1394_ARM_READ | RAW1394_ARM_WRITE | RAW1394_ARM_LOCK,
299                          RAW1394_ARM_READ | RAW1394_ARM_WRITE | RAW1394_ARM_LOCK,
300                          0);
301
302     printf(" adding (%p) as arm handler\n", test_arm);
303
304     if (!m_service->registerARMHandler(test_arm)) {
305         printf("  failed\n");
306     }
307
308     addr =  m_service->findFreeARMBlock(0x0000FFFFE0000000ULL, 4, 4 );
309
310     ARMHandler *test_arm2=new ARMHandler(addr,
311                          4,
312                          RAW1394_ARM_READ | RAW1394_ARM_WRITE | RAW1394_ARM_LOCK,
313                          RAW1394_ARM_READ | RAW1394_ARM_WRITE | RAW1394_ARM_LOCK,
314                          0);
315
316     printf(" adding (%p) as arm handler\n", test_arm2);
317
318     if (!m_service->registerARMHandler(test_arm2)) {
319         printf("  failed\n");
320     }
321
322     CtrThread *thread_runners[NB_THREADS];
323     Thread* threads[NB_THREADS];
324     for (i=0; i < NB_THREADS; i++) {
325         thread_runners[i] = new CtrThread(m_service);
326         if (thread_runners[i] == NULL) {
327             debugError("could not create thread runner %d\n", i);
328             exit(-1);
329         }
330         threads[i] = new PosixThread(thread_runners[i], THREAD_RT, THREAD_PRIO, PTHREAD_CANCEL_DEFERRED);
331         if (threads[i] == NULL) {
332             debugError("could not create thread %d\n", i);
333             exit(-1);
334         }
335     }
336    
337     for (i=0; i < NB_THREADS; i++) {
338         threads[i]->Start();
339     }
340
341     int cnt=0;
342     while(run) {
343         cnt++;
344         debugOutput(DEBUG_LEVEL_NORMAL, "%08d: (max: %6d, min: %6d)\n", cnt, max_diff, min_diff);
345         m_service->show();
346        
347         for (i=0; i < NB_THREADS; i++) {
348             debugOutput(DEBUG_LEVEL_NORMAL, "%2d: avg: %6f\n", i,  thread_runners[i]->avg_diff);
349         }
350        
351        
352         sleep(5);
353     }
354
355     for (i=0; i < NB_THREADS; i++) {
356         threads[i]->Stop();
357     }
358
359     for (i=0; i < NB_THREADS; i++) {
360         delete threads[i];
361         delete thread_runners[i];
362     }
363
364     delete m_service;
365     delete test_busreset;
366     delete test_arm;
367     delete test_arm2;
368
369     printf("Bye...\n");
370
371     return EXIT_SUCCESS;
372 }
Note: See TracBrowser for help on using the browser.