root/branches/streaming-rework/tests/test-cycletimer.cpp

Revision 393, 10.0 kB (checked in by pieterpalmers, 16 years ago)

- fixed some bugs in the timestampedbuffer
- cleaned up the amdtpstreamprocessor
- updated test-sytmonitor and test-cycletimer

to the new threading structure

- implemented test for timestampedbuffer

Line 
1 /***************************************************************************
2   Copyright (C) 2005 by Pieter Palmers   *
3                                                                         *
4   This program is free software; you can redistribute it and/or modify  *
5   it under the terms of the GNU General Public License as published by  *
6   the Free Software Foundation; either version 2 of the License, or     *
7   (at your option) any later version.                                   *
8                                                                         *
9   This program is distributed in the hope that it will be useful,       *
10   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12   GNU General Public License for more details.                          *
13                                                                         *
14   You should have received a copy of the GNU General Public License     *
15   along with this program; if not, write to the                         *
16   Free Software Foundation, Inc.,                                       *
17   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <endian.h>
28
29 #include <signal.h>
30 #include "src/debugmodule/debugmodule.h"
31
32 #include <netinet/in.h>
33
34 #include "src/libstreaming/cycletimer.h"
35
36 #include "src/libstreaming/IsoHandler.h"
37 #include "src/libstreaming/IsoStream.h"
38 #include "src/libstreaming/IsoHandlerManager.h"
39 #include "src/libutil/PosixThread.h"
40
41 #define TEST_PORT_0
42 // #define TEST_PORT_1
43 // #define TEST_PORT_2
44
45 using namespace FreebobStreaming;
46 using namespace FreebobUtil;
47
48 DECLARE_GLOBAL_DEBUG_MODULE;
49
50 int run;
51
52 struct CYCLE_TIMER_REGISTER {
53         uint16_t seconds;
54         uint16_t cycles;
55         uint16_t offset;
56 };
57
58 uint64_t ctr_to_quadlet(struct CYCLE_TIMER_REGISTER x) {
59     uint64_t retval=0;
60    
61     x.seconds &= 0x7F;
62    
63     x.cycles &= 0x1FFF;
64     x.cycles %= 8000;
65    
66     x.offset &= 0xFFF;
67     x.offset %= 3072;
68    
69     retval = (x.seconds << 25) + (x.cycles << 12) + (x.offset);
70     return retval & 0xFFFFFFFF;
71 }
72
73 static void sighandler (int sig)
74 {
75         run = 0;
76 }
77
78 int do_cycletimer_test() {
79    
80     struct CYCLE_TIMER_REGISTER cycle_timer;
81     uint32_t *cycle_timer_as_uint=(uint32_t *)&cycle_timer;
82    
83     uint32_t i=0;
84     uint32_t targetval=0;
85    
86     int failures=0;
87    
88    
89     // test 1
90     //
91    
92     *cycle_timer_as_uint=0;
93     for (i=0;i<3072;i++) {
94         cycle_timer.offset=i;
95         targetval=CYCLE_TIMER_GET_OFFSET(ctr_to_quadlet(cycle_timer));
96        
97         if(targetval != i) {
98             debugOutput(DEBUG_LEVEL_NORMAL, "  test1 failed on i=%d (%08X), returns %d (%08X)\n",i,i,targetval,targetval);
99             failures++;
100         }
101     }
102    
103     for (i=0;i<8000;i++) {
104         cycle_timer.cycles=i;
105         targetval=CYCLE_TIMER_GET_CYCLES(ctr_to_quadlet(cycle_timer));
106        
107         if(targetval != i) {
108             debugOutput(DEBUG_LEVEL_NORMAL, "  test2 failed on i=%d (%08X), returns %d (%08X)\n",i,i,targetval,targetval);
109             failures++;
110         }
111     }
112    
113     for (i=0;i<128;i++) {
114         cycle_timer.seconds=i;
115         targetval=CYCLE_TIMER_GET_SECS(ctr_to_quadlet(cycle_timer));
116        
117         if(targetval != i) {
118             debugOutput(DEBUG_LEVEL_NORMAL, "  test3 failed on i=%d (%08X), returns %d (%08X)\n",i,i,targetval,targetval);
119             failures++;
120         }
121     }
122    
123    
124     // a value in ticks
125     // should be: 10sec, 1380cy, 640ticks
126     targetval=250000000L;
127     cycle_timer.seconds = TICKS_TO_SECS(targetval);
128     cycle_timer.cycles  = TICKS_TO_CYCLES(targetval);
129     cycle_timer.offset  = TICKS_TO_OFFSET(targetval);
130    
131     if((cycle_timer.seconds != 10) |
132         (cycle_timer.cycles != 1380) |
133         (cycle_timer.offset != 640))
134         {
135         debugOutput(DEBUG_LEVEL_NORMAL, "  test4 failed: (%u,10)sec (%u,1380)cy (%u,640)ticks\n",
136             cycle_timer.seconds,cycle_timer.cycles,cycle_timer.offset);
137         failures++;
138     } else {
139          debugOutput(DEBUG_LEVEL_NORMAL, "  test4 ok\n");
140     }
141    
142     i=TICKS_TO_CYCLE_TIMER(targetval);
143     if (i != 0x14564280) {
144          debugOutput(DEBUG_LEVEL_NORMAL, "  test5 failed: (0x%08X,0x14564280)\n",
145             i);
146         failures++;   
147     } else {
148          debugOutput(DEBUG_LEVEL_NORMAL, "  test5 ok\n");
149     }
150    
151     targetval=CYCLE_TIMER_TO_TICKS(i);
152     if (targetval!=250000000L) {
153          debugOutput(DEBUG_LEVEL_NORMAL, "  test6 failed: (%u,250000000)\n",
154             targetval);
155         failures++;   
156     } else {
157          debugOutput(DEBUG_LEVEL_NORMAL, "  test6 ok\n");
158     }
159    
160     int32_t subs;
161     subs=substractTicks(10, 8);
162     if (subs != 2) {
163          debugOutput(DEBUG_LEVEL_NORMAL, "  substractTicks(10, 8) != 2 : %ld\n",
164             subs);
165         failures++;   
166     }
167    
168     subs=substractTicks(10, 12);
169     if (subs != -2) {
170          debugOutput(DEBUG_LEVEL_NORMAL, "  substractTicks(10, 12) != -2 : %ld\n",
171             subs);
172         failures++;   
173     }
174    
175     subs=substractTicks(TICKS_PER_SECOND*128L + 10, 8);
176     if (subs != 2) {
177          debugOutput(DEBUG_LEVEL_NORMAL, "  substractTicks(TICKS_PER_SECOND*128L + 10, 8) != 2 : %ld\n",
178             subs);
179         failures++;   
180     }
181    
182     subs=substractTicks(TICKS_PER_SECOND*128L + 10, 12);
183     if (subs != -2) {
184          debugOutput(DEBUG_LEVEL_NORMAL, "  substractTicks(TICKS_PER_SECOND*128L + 10, 12) != -2 : %ld\n",
185             subs);
186         failures++;   
187     }
188    
189     subs=substractTicks(10, TICKS_PER_SECOND*128L + 8);
190     if (subs != 2) {
191          debugOutput(DEBUG_LEVEL_NORMAL, "  substractTicks(10, TICKS_PER_SECOND*128L + 8) != 2 : %ld\n",
192             subs);
193         failures++;   
194     }
195    
196     subs=substractTicks(10, TICKS_PER_SECOND*128L + 12);
197     if (subs != -2) {
198          debugOutput(DEBUG_LEVEL_NORMAL, "  substractTicks(10, TICKS_PER_SECOND*128L + 12) != -2 : %ld\n",
199             subs);
200         failures++;   
201     }   
202    
203     if (failures) {
204         debugOutput(DEBUG_LEVEL_NORMAL, " %d failures\n",failures);
205         return -1;
206     } else {
207         debugOutput(DEBUG_LEVEL_NORMAL, " no failures\n");
208     }
209     return 0;
210 }
211
212 int main(int argc, char *argv[])
213 {
214
215         run=1;
216        
217         IsoHandlerManager *m_isoManager=NULL;
218    
219 #ifdef TEST_PORT_0     
220     IsoStream *s=NULL;
221 #endif
222 #ifdef TEST_PORT_1     
223     IsoStream *s2=NULL;
224 #endif
225 #ifdef TEST_PORT_2
226     IsoStream *s3=NULL;
227 #endif
228
229         signal (SIGINT, sighandler);
230         signal (SIGPIPE, sighandler);
231
232         debugOutput(DEBUG_LEVEL_NORMAL, "Freebob Cycle timer test application\n");
233        
234         debugOutput(DEBUG_LEVEL_NORMAL, "Testing cycle timer helper functions & macro's... \n");
235         if(do_cycletimer_test()) {
236            debugOutput(DEBUG_LEVEL_NORMAL, " !!! FAILED !!!\n");
237            exit(1);
238         } else {
239            debugOutput(DEBUG_LEVEL_NORMAL, " !!! PASSED !!!\n");
240         }
241        
242 //      exit(1);
243        
244         m_isoManager=new IsoHandlerManager();
245        
246         if(!m_isoManager) {
247                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoHandlerManager\n");
248                 goto finish;
249         }
250        
251         m_isoManager->setVerboseLevel(DEBUG_LEVEL_VERBOSE);
252        
253         if(!m_isoManager->init()) {
254                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not init() IsoHandlerManager\n");
255                 goto finish;
256         }
257                
258        
259 #ifdef TEST_PORT_0     
260         // add a stream to the manager so that it has something to do
261         s=new IsoStream(IsoStream::EST_Receive, 0);
262        
263         if (!s) {
264                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoStream\n");
265                 goto finish;
266         }       
267        
268         s->setVerboseLevel(DEBUG_LEVEL_VERBOSE);
269        
270         if (!s->init()) {
271                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not init IsoStream\n");
272                 goto finish;
273         }
274        
275         s->setChannel(0);
276        
277         if(!m_isoManager->registerStream(s)) {
278                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not register IsoStream\n");
279                 goto finish;
280         }
281 #endif
282
283 #ifdef TEST_PORT_1     
284         // add a stream to the manager so that it has something to do
285         s2=new IsoStream(IsoStream::EST_Receive, 1);
286        
287         if (!s2) {
288                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoStream\n");
289                 goto finish;
290         }       
291        
292         s2->setVerboseLevel(DEBUG_LEVEL_VERBOSE);
293        
294         if (!s2->init()) {
295                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not init IsoStream\n");
296                 goto finish;
297         }
298        
299         s2->setChannel(0);
300        
301         if(!m_isoManager->registerStream(s2)) {
302                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not register IsoStream\n");
303                 goto finish;
304         }
305 #endif
306
307 #ifdef TEST_PORT_2             
308         // add a stream to the manager so that it has something to do
309         s3=new IsoStream(IsoStream::EST_Receive,2);
310        
311         if (!s3) {
312                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoStream\n");
313                 goto finish;
314         }       
315        
316         s3->setVerboseLevel(DEBUG_LEVEL_VERBOSE);
317        
318         if (!s3->init()) {
319                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not init IsoStream\n");
320                 goto finish;
321         }
322        
323         s3->setChannel(0);
324        
325         if(!m_isoManager->registerStream(s3)) {
326                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not register IsoStream\n");
327                 goto finish;
328         }
329 #endif
330
331         debugOutput(DEBUG_LEVEL_NORMAL,   "Preparing IsoHandlerManager...\n");
332         if (!m_isoManager->prepare()) {
333                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not prepare isoManager\n");
334                 goto finish;
335         }
336
337         debugOutput(DEBUG_LEVEL_NORMAL,   "Starting IsoHandler...\n");
338         if (!m_isoManager->startHandlers(0)) {
339                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not start handlers...\n");
340                 goto finish;
341         }
342        
343         while(run) {
344         sleep(1);
345         m_isoManager->dumpInfo();
346         }
347
348         debugOutput(DEBUG_LEVEL_NORMAL,   "Stopping handlers...\n");
349         if(!m_isoManager->stopHandlers()) {
350            debugOutput(DEBUG_LEVEL_NORMAL, "Could not stop ISO handlers\n");
351            goto finish;
352         }
353        
354 #ifdef TEST_PORT_0     
355         if(!m_isoManager->unregisterStream(s)) {
356                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not unregister IsoStream\n");
357                 goto finish;
358         }
359         delete s;
360 #endif
361
362 #ifdef TEST_PORT_1     
363         if(!m_isoManager->unregisterStream(s1)) {
364                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not unregister IsoStream\n");
365                 goto finish;
366         }
367         delete s1;
368 #endif
369
370 #ifdef TEST_PORT_2     
371         if(!m_isoManager->unregisterStream(s2)) {
372                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not unregister IsoStream\n");
373                 goto finish;
374         }
375         delete s2;
376 #endif
377        
378     delete m_isoManager;
379
380 finish:
381         debugOutput(DEBUG_LEVEL_NORMAL, "Bye...\n");
382
383   return EXIT_SUCCESS;
384 }
Note: See TracBrowser for help on using the browser.