File: test-timestampedbuffer.cpp

package info (click to toggle)
libffado 2.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 9,320 kB
  • ctags: 11,892
  • sloc: cpp: 75,412; python: 8,911; ansic: 2,951; sh: 1,261; xml: 822; makefile: 54
file content (596 lines) | stat: -rw-r--r-- 20,081 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
 * Copyright (C) 2005-2008 by Pieter Palmers
 *
 * This file is part of FFADO
 * FFADO = Free Firewire (pro-)audio drivers for linux
 *
 * FFADO is based upon FreeBoB.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <argp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <endian.h>

#include <signal.h>
#include "src/debugmodule/debugmodule.h"

#include "libutil/ByteSwap.h"

#include "src/libieee1394/cycletimer.h"

#include "src/libutil/TimestampedBuffer.h"
#include "libutil/Time.h"

#include <pthread.h>

using namespace Util;

class TimestampedBufferTestClient
    : public TimestampedBufferClient {
public:
    bool processReadBlock(char *data, unsigned int nevents, unsigned int offset) {return true;};
    bool processWriteBlock(char *data, unsigned int nevents, unsigned int offset) {return true;};

    void setVerboseLevel(int l) {setDebugLevel(l);};
private:
    DECLARE_DEBUG_MODULE;
};

IMPL_DEBUG_MODULE( TimestampedBufferTestClient, TimestampedBufferTestClient, DEBUG_LEVEL_VERBOSE );

DECLARE_GLOBAL_DEBUG_MODULE;

int run;
// Program documentation.
static char doc[] = "FFADO -- Timestamped buffer test\n\n";

// A description of the arguments we accept.
static char args_doc[] = "";


struct arguments
{
    short verbose;
    uint64_t wrap_at;
    uint64_t frames_per_packet;
    uint64_t events_per_frame;
    float rate;
    uint64_t total_cycles;
    uint64_t buffersize;
    uint64_t start_at_cycle;
};

// The options we understand.
static struct argp_option options[] = {
    {"verbose",     'v',    "n",    0,  "Verbose level" },
    {"wrap",        'w',    "n",    0,  "Wrap at (ticks) (3072000)" },
    {"fpp",        'f',    "n",    0,  "Frames per packet (8)" },
    {"epf",        'e',    "n",    0,  "Events per frame (10)" },
    {"rate",        'r',    "n",    0,  "Rate (ticks/frame) (512.0)" },
    {"cycles",        'c',    "n",    0,  "Total cycles to run (2000)" },
    {"buffersize",        'b',    "n",    0,  "Buffer size (in frames) (1024)" },
    {"startcycle",        's',    "n",    0,  "Start at cycle (0)" },
    { 0 }
};

//-------------------------------------------------------------

// Parse a single option.
static error_t
parse_opt( int key, char* arg, struct argp_state* state )
{
    // Get the input argument from `argp_parse', which we
    // know is a pointer to our arguments structure.
    struct arguments* arguments = ( struct arguments* ) state->input;
    char* tail;

    errno = 0;
    switch (key) {
        case 'v':
            if (arg) {
                arguments->verbose = strtoll( arg, &tail, 0 );
                if ( errno ) {
                    fprintf( stderr, "Could not parse 'verbose' argument\n" );
                    return ARGP_ERR_UNKNOWN;
                }
            } else {
                if ( errno ) {
                    fprintf( stderr, "Could not parse 'verbose' argument\n" );
                    return ARGP_ERR_UNKNOWN;
                }
            }
            break;
        case 'w':
            if (arg) {
                arguments->wrap_at = strtoll( arg, &tail, 0 );
                if ( errno ) {
                    fprintf( stderr, "Could not parse 'wrap' argument\n" );
                    return ARGP_ERR_UNKNOWN;
                }
            } else {
                if ( errno ) {
                    fprintf( stderr, "Could not parse 'wrap' argument\n" );
                    return ARGP_ERR_UNKNOWN;
                }
            }
            break;
        case 'f':
            if (arg) {
                arguments->frames_per_packet = strtoll( arg, &tail, 0 );
                if ( errno ) {
                    fprintf( stderr, "Could not parse 'fpp' argument\n" );
                    return ARGP_ERR_UNKNOWN;
                }
            } else {
                if ( errno ) {
                    fprintf( stderr, "Could not parse 'fpp' argument\n" );
                    return ARGP_ERR_UNKNOWN;
                }
            }
            break;
        case 'e':
            if (arg) {
                arguments->events_per_frame = strtoll( arg, &tail, 0 );
                if ( errno ) {
                    fprintf( stderr, "Could not parse 'epf' argument\n" );
                    return ARGP_ERR_UNKNOWN;
                }
            } else {
                if ( errno ) {
                    fprintf( stderr, "Could not parse 'epf' argument\n" );
                    return ARGP_ERR_UNKNOWN;
                }
            }
            break;
        case 'c':
            if (arg) {
                arguments->total_cycles = strtoll( arg, &tail, 0 );
                if ( errno ) {
                    fprintf( stderr, "Could not parse 'cycles' argument\n" );
                    return ARGP_ERR_UNKNOWN;
                }
            } else {
                if ( errno ) {
                    fprintf( stderr, "Could not parse 'cycles' argument\n" );
                    return ARGP_ERR_UNKNOWN;
                }
            }
            break;
        case 's':
            if (arg) {
                arguments->start_at_cycle = strtoll( arg, &tail, 0 );
                if ( errno ) {
                    fprintf( stderr, "Could not parse 'startcycle' argument\n" );
                    return ARGP_ERR_UNKNOWN;
                }
            } else {
                if ( errno ) {
                    fprintf( stderr, "Could not parse 'startcycle' argument\n" );
                    return ARGP_ERR_UNKNOWN;
                }
            }
            break;
        case 'b':
            if (arg) {
                arguments->buffersize = strtoll( arg, &tail, 0 );
                if ( errno ) {
                    fprintf( stderr, "Could not parse 'buffersize' argument\n" );
                    return ARGP_ERR_UNKNOWN;
                }
            } else {
                if ( errno ) {
                    fprintf( stderr, "Could not parse 'buffersize' argument\n" );
                    return ARGP_ERR_UNKNOWN;
                }
            }
            break;
        case 'r':
            if (arg) {
                arguments->rate = strtof( arg, &tail );
                if ( errno ) {
                    fprintf( stderr, "Could not parse 'rate' argument\n" );
                    return ARGP_ERR_UNKNOWN;
                }
            } else {
                if ( errno ) {
                    fprintf( stderr, "Could not parse 'rate' argument\n" );
                    return ARGP_ERR_UNKNOWN;
                }
            }
            break;
       default:
            return ARGP_ERR_UNKNOWN;
    }
    return 0;
}

// Our argp parser.
static struct argp argp = { options, parse_opt, args_doc, doc };


static void sighandler (int sig)
{
        run = 0;
}

int main(int argc, char *argv[])
{

    TimestampedBuffer *t=NULL;
    TimestampedBufferTestClient *c=NULL;

    struct arguments arguments;

    // Default values.
    arguments.verbose        = 0;
    arguments.wrap_at = 3072000LLU; // 1000 cycles
    arguments.frames_per_packet = 8;
    arguments.events_per_frame = 10;
    arguments.rate = 512.0;
    arguments.total_cycles = 2000;
    arguments.buffersize = 1024;
    arguments.start_at_cycle = 0;

    // Parse our arguments; every option seen by `parse_opt' will
    // be reflected in `arguments'.
    if ( argp_parse ( &argp, argc, argv, 0, 0, &arguments ) ) {
        fprintf( stderr, "Could not parse command line\n" );
        exit(1);
    }

    setDebugLevel(arguments.verbose);

    run=1;

    signal (SIGINT, sighandler);
    signal (SIGPIPE, sighandler);

    c=new TimestampedBufferTestClient();

    if(!c) {
        debugOutput(DEBUG_LEVEL_NORMAL, "Could not create TimestampedBufferTestClient\n");
        exit(1);
    }
    c->setVerboseLevel(arguments.verbose);

    t=new TimestampedBuffer(c);

    if(!t) {
        debugOutput(DEBUG_LEVEL_NORMAL, "Could not create TimestampedBuffer\n");
        delete c;
        exit(1);
    }
    t->setVerboseLevel(arguments.verbose);

    // Setup the buffer
    t->setBufferSize(arguments.buffersize);
    t->setEventSize(sizeof(int));
    t->setEventsPerFrame(arguments.events_per_frame);

    t->setUpdatePeriod(arguments.frames_per_packet);
    t->setNominalRate(arguments.rate);

    t->setWrapValue(arguments.wrap_at);

    t->prepare();

    SleepRelativeUsec(1000);

    debugOutput(DEBUG_LEVEL_NORMAL, "Start setBufferHeadTimestamp test...\n");
    {
        bool pass=true;
        uint64_t time=arguments.start_at_cycle*3072;
        int dummyframe_in[arguments.events_per_frame*arguments.frames_per_packet];

        // initialize the timestamp
        uint64_t timestamp=time;
        if (timestamp >= arguments.wrap_at) {
            // here we need a modulo because start_at_cycle can be large
            timestamp %= arguments.wrap_at;
        }

        // account for the fact that there is offset,
        // and that setBufferHeadTimestamp doesn't take offset
        // into account
        uint64_t timestamp2=timestamp;
        if (timestamp2>=arguments.wrap_at) {
            timestamp2-=arguments.wrap_at;
        }

        t->setBufferHeadTimestamp(timestamp2);

        timestamp += (uint64_t)(arguments.rate * arguments.frames_per_packet);
        if (timestamp >= arguments.wrap_at) {
            timestamp -= arguments.wrap_at;
        }

        // write some packets
        for (unsigned int i=0;i<20;i++) {
            t->writeFrames(arguments.frames_per_packet, (char *)&dummyframe_in, timestamp);
            timestamp += (uint64_t)(arguments.rate * arguments.frames_per_packet);
            if (timestamp >= arguments.wrap_at) {
                timestamp -= arguments.wrap_at;
            }
        }

        for(unsigned int cycle=arguments.start_at_cycle;
            cycle < arguments.start_at_cycle+arguments.total_cycles;
            cycle++) {
                ffado_timestamp_t ts_head_tmp;
                uint64_t ts_head;
                signed int fc_head;

                t->setBufferHeadTimestamp(timestamp);
                t->getBufferHeadTimestamp(&ts_head_tmp, &fc_head);
                ts_head=(uint64_t)ts_head_tmp;

                if (timestamp != ts_head) {
                    debugError(" cycle %4u error: %011"PRIu64" != %011"PRIu64"\n",
                               cycle, timestamp, ts_head);
                    pass=false;
                }

                timestamp += (uint64_t)(arguments.rate * arguments.frames_per_packet);
                if (timestamp >= arguments.wrap_at) {
                    timestamp -= arguments.wrap_at;
                }

            // simulate the cycle timer clock in ticks
            time += 3072;
            if (time >= arguments.wrap_at) {
                time -= arguments.wrap_at;
            }

            // allow for the messagebuffer thread to catch up
            SleepRelativeUsec(200);

            if(!run) break;
        }

        if(!pass) {
            debugError("Test failed, exiting...\n");

            delete t;
            delete c;

            return -1;

        }
    }



    debugOutput(DEBUG_LEVEL_NORMAL, "Start read/write test...\n");
    {
        int dummyframe_in[arguments.events_per_frame*arguments.frames_per_packet];
        int dummyframe_out[arguments.events_per_frame*arguments.frames_per_packet];

        for (unsigned int i=0;i<arguments.events_per_frame*arguments.frames_per_packet;i++) {
            dummyframe_in[i]=i;
        }

        uint64_t time=arguments.start_at_cycle*3072;

        // initialize the timestamp
        uint64_t timestamp=time;
        if (timestamp >= arguments.wrap_at) {
            // here we need a modulo because start_at_cycle can be large
            timestamp %= arguments.wrap_at;
        }
        t->setBufferTailTimestamp(timestamp);

        timestamp += (uint64_t)(arguments.rate * arguments.frames_per_packet);
        if (timestamp >= arguments.wrap_at) {
            timestamp -= arguments.wrap_at;
        }

        for(unsigned int cycle=arguments.start_at_cycle;
            cycle < arguments.start_at_cycle+arguments.total_cycles;
            cycle++) {

            // simulate the rate adaptation
            int64_t diff=(time%arguments.wrap_at)-timestamp;

            if (diff>(int64_t)arguments.wrap_at/2) {
                diff -= arguments.wrap_at;
            } else if (diff<(-(int64_t)arguments.wrap_at)/2){
                diff += arguments.wrap_at;
            }

            debugOutput(DEBUG_LEVEL_NORMAL,
                        "Simulating cycle %d @ time=%011"PRIu64", diff=%"PRId64"\n",
                        cycle, time, diff);

            if(diff > 0) {
                ffado_timestamp_t ts_head_tmp, ts_tail_tmp;
                uint64_t ts_head, ts_tail;
                signed int fc_head, fc_tail;

                // write one packet
                t->writeFrames(arguments.frames_per_packet, (char *)&dummyframe_in, timestamp);

                // read the buffer head timestamp
                t->getBufferHeadTimestamp(&ts_head_tmp, &fc_head);
                t->getBufferTailTimestamp(&ts_tail_tmp, &fc_tail);
                ts_head=(uint64_t)ts_head_tmp;
                ts_tail=(uint64_t)ts_tail_tmp;
                debugOutput(DEBUG_LEVEL_NORMAL,
                        " TS after write: HEAD: %011"PRIu64", FC=%04u\n",
                        ts_head,fc_head);
                debugOutput(DEBUG_LEVEL_NORMAL,
                        "                 TAIL: %011"PRIu64", FC=%04u\n",
                        ts_tail,fc_tail);

                // read one packet
                t->readFrames(arguments.frames_per_packet, (char *)&dummyframe_out);

                // read the buffer head timestamp
                t->getBufferHeadTimestamp(&ts_head_tmp, &fc_head);
                t->getBufferTailTimestamp(&ts_tail_tmp, &fc_tail);
                ts_head=(uint64_t)ts_head_tmp;
                ts_tail=(uint64_t)ts_tail_tmp;
                debugOutput(DEBUG_LEVEL_NORMAL,
                        " TS after write: HEAD: %011"PRIu64", FC=%04u\n",
                        ts_head,fc_head);
                debugOutput(DEBUG_LEVEL_NORMAL,
                        "                 TAIL: %011"PRIu64", FC=%04u\n",
                        ts_tail,fc_tail);

                // check
                bool pass=true;
                for (unsigned int i=0;i<arguments.events_per_frame*arguments.frames_per_packet;i++) {
                    pass = pass && (dummyframe_in[i]==dummyframe_out[i]);
                }
                if (!pass) {
                    debugOutput(DEBUG_LEVEL_NORMAL, "write/read check for cycle %d failed\n",cycle);
                }

                // update the timestamp
                timestamp += (uint64_t)(arguments.rate * arguments.frames_per_packet);
                if (timestamp >= arguments.wrap_at) {
                    timestamp -= arguments.wrap_at;
                }
            }

            // simulate the cycle timer clock in ticks
            time += 3072;
            if (time >= arguments.wrap_at) {
                time -= arguments.wrap_at;
            }

            // allow for the messagebuffer thread to catch up
            SleepRelativeUsec(200);

            if(!run) break;
        }
    }

    // second run, now do block processing
    debugOutput(DEBUG_LEVEL_NORMAL, "Start block read test...\n");
    {
        unsigned int blocksize=32;
        int dummyframe_out_block[arguments.events_per_frame*arguments.frames_per_packet*blocksize];
        int dummyframe_in[arguments.events_per_frame*arguments.frames_per_packet];

        for (unsigned int i=0;i<arguments.events_per_frame*arguments.frames_per_packet;i++) {
            dummyframe_in[i]=i;
        }

        uint64_t time=arguments.start_at_cycle*3072;

        // initialize the timestamp
        uint64_t timestamp=time;
        if (timestamp >= arguments.wrap_at) {
            // here we need a modulo because start_at_cycle can be large
            timestamp %= arguments.wrap_at;
        }
        t->setBufferTailTimestamp(timestamp);

        timestamp += (uint64_t)(arguments.rate * arguments.frames_per_packet);
        if (timestamp >= arguments.wrap_at) {
            timestamp -= arguments.wrap_at;
        }

        for(unsigned int cycle=arguments.start_at_cycle;
            cycle < arguments.start_at_cycle+arguments.total_cycles;
            cycle++) {

            // simulate the rate adaptation
            int64_t diff=(time%arguments.wrap_at)-timestamp;

            if (diff>(int64_t)arguments.wrap_at/2) {
                diff -= arguments.wrap_at;
            } else if (diff<(-(int64_t)arguments.wrap_at)/2){
                diff += arguments.wrap_at;
            }

            debugOutput(DEBUG_LEVEL_NORMAL,
                        "Simulating cycle %d @ time=%011"PRIu64", diff=%"PRId64"\n",
                        cycle, time, diff);

            if(diff>0) {
                ffado_timestamp_t ts_head_tmp, ts_tail_tmp;
                uint64_t ts_head, ts_tail;
                signed int fc_head, fc_tail;

                // write one packet
                t->writeFrames(arguments.frames_per_packet, (char *)&dummyframe_in, timestamp);

                // read the buffer head timestamp
                t->getBufferHeadTimestamp(&ts_head_tmp, &fc_head);
                t->getBufferTailTimestamp(&ts_tail_tmp, &fc_tail);
                ts_head=(uint64_t)ts_head_tmp;
                ts_tail=(uint64_t)ts_tail_tmp;
                debugOutput(DEBUG_LEVEL_NORMAL,
                        " TS after write: HEAD: %011"PRIu64", FC=%04u\n",
                        ts_head,fc_head);
                debugOutput(DEBUG_LEVEL_NORMAL,
                        "                 TAIL: %011"PRIu64", FC=%04u\n",
                        ts_tail,fc_tail);

                if (fc_head > (signed int)blocksize) {
                    debugOutput(DEBUG_LEVEL_NORMAL,"Reading one block (%u frames)\n",blocksize);

                    // read one block
                    t->readFrames(blocksize, (char *)&dummyframe_out_block);

                    // read the buffer head timestamp
                    t->getBufferHeadTimestamp(&ts_head_tmp, &fc_head);
                    t->getBufferTailTimestamp(&ts_tail_tmp, &fc_tail);
                    ts_head=(uint64_t)ts_head_tmp;
                    ts_tail=(uint64_t)ts_tail_tmp;
                    debugOutput(DEBUG_LEVEL_NORMAL,
                            " TS after read: HEAD: %011"PRIu64", FC=%04u\n",
                            ts_head,fc_head);
                    debugOutput(DEBUG_LEVEL_NORMAL,
                            "                TAIL: %011"PRIu64", FC=%04u\n",
                            ts_tail,fc_tail);
                }

                // update the timestamp
                timestamp += (uint64_t)(arguments.rate * arguments.frames_per_packet);
                if (timestamp >= arguments.wrap_at) {
                    timestamp -= arguments.wrap_at;
                }
            }

            // simulate the cycle timer clock in ticks
            time += 3072;
            if (time >= arguments.wrap_at) {
                time -= arguments.wrap_at;
            }

            // allow for the messagebuffer thread to catch up
            SleepRelativeUsec(200);

            if(!run) break;
        }
    }

    delete t;
    delete c;

    return EXIT_SUCCESS;
}