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

Revision 1018, 13.6 kB (checked in by ppalmers, 16 years ago)

update tests

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