-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathtestbench.c
More file actions
548 lines (457 loc) · 14 KB
/
testbench.c
File metadata and controls
548 lines (457 loc) · 14 KB
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
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2018-2024 Intel Corporation. All rights reserved.
//
// Author: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
// Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
#include <sof/audio/module_adapter/module/generic.h>
#include <platform/lib/heap_usage.h>
#include <sof/ipc/driver.h>
#include <sof/ipc/topology.h>
#include <sof/list.h>
#include <tplg_parser/topology.h>
#include "testbench/trace.h"
#include "testbench/file.h"
#include "testbench/utils.h"
#include <ctype.h>
#include <getopt.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#define TESTBENCH_NCH 2
extern struct platform_library_heap_usage sof_platform_library_heap_usage;
/*
* Parse output filenames from user input
* This function takes in the output filenames as an input in the format:
* "output_file1,output_file2,..."
* The max supported output filename number is 4, min is 1.
*/
static int parse_output_files(char *outputs, struct testbench_prm *tp)
{
char *output_token = NULL;
char *token = strtok_r(outputs, ",", &output_token);
int index;
for (index = 0; index < TB_MAX_OUTPUT_FILE_NUM && token; index++) {
/* get output file name with current index */
tp->output_file[index] = strdup(token);
/* next output */
token = strtok_r(NULL, ",", &output_token);
}
if (index == TB_MAX_OUTPUT_FILE_NUM && token) {
fprintf(stderr, "error: max output file number is %d\n",
TB_MAX_OUTPUT_FILE_NUM);
for (index = 0; index < TB_MAX_OUTPUT_FILE_NUM; index++)
free(tp->output_file[index]);
return -EINVAL;
}
/* set total output file number */
tp->output_file_num = index;
return 0;
}
/*
* Parse inputfilenames from user input
*/
static int parse_input_files(char *inputs, struct testbench_prm *tp)
{
char *input_token = NULL;
char *token = strtok_r(inputs, ",", &input_token);
int index;
for (index = 0; index < TB_MAX_INPUT_FILE_NUM && token; index++) {
/* get input file name with current index */
tp->input_file[index] = strdup(token);
/* next input */
token = strtok_r(NULL, ",", &input_token);
}
if (index == TB_MAX_INPUT_FILE_NUM && token) {
fprintf(stderr, "error: max input file number is %d\n",
TB_MAX_INPUT_FILE_NUM);
for (index = 0; index < TB_MAX_INPUT_FILE_NUM; index++)
free(tp->input_file[index]);
return -EINVAL;
}
/* set total input file number */
tp->input_file_num = index;
return 0;
}
static int parse_pipelines(char *pipelines, struct testbench_prm *tp)
{
char *output_token = NULL;
char *token = strtok_r(pipelines, ",", &output_token);
int index;
for (index = 0; index < TB_MAX_OUTPUT_FILE_NUM && token; index++) {
/* get output file name with current index */
tp->pipelines[index] = atoi(token);
/* next output */
token = strtok_r(NULL, ",", &output_token);
}
if (index == TB_MAX_OUTPUT_FILE_NUM && token) {
fprintf(stderr, "error: max output file number is %d\n",
TB_MAX_OUTPUT_FILE_NUM);
return -EINVAL;
}
/* set total output file number */
tp->pipeline_num = index;
return 0;
}
/* print usage for testbench */
static void print_usage(char *executable)
{
printf("Usage: %s <options> -i <input_file> ", executable);
printf("-o <output_file1,output_file2,...>\n\n");
printf("Options for processing:\n");
printf(" -t <topology file>\n\n");
printf("Options to control test:\n");
printf(" -d <level> Sets the traces print level:\n");
printf(" 0 all traces are suppressed\n");
printf(" 1 shows error traces\n");
printf(" 2 shows warning traces and previous\n");
printf(" 3 shows info traces and previous\n");
printf(" 4 shows debug traces and previous, plus other testbench debug messages\n");
printf(" -p <pipeline1,pipeline2,...>\n");
printf(" -C <number of copy() iterations>\n");
printf(" -P <number of dynamic pipeline iterations>\n");
printf(" -s <script file to set controls, with amixer and sleep commands>\n\n");
printf("Options for input and output format override:\n");
printf(" -b <input_format>, S16_LE, S24_LE, or S32_LE\n");
printf(" -c <input channels>\n");
printf(" -n <output channels>\n");
printf(" -r <input rate>\n");
printf(" -R <output rate>\n\n");
printf("Help:\n");
printf(" -h\n\n");
printf("Example Usage:\n");
printf("%s -r 48000 -c 2 -b S16_LE -i in.raw -o out.raw -t <test.tplg>\n\n", executable);
}
static int parse_input_args(int argc, char **argv, struct testbench_prm *tp)
{
int option = 0;
int ret = 0;
while ((option = getopt(argc, argv, "hd:i:o:t:b:r:R:c:n:C:P:p:s:")) != -1) {
switch (option) {
/* input sample file */
case 'i':
ret = parse_input_files(optarg, tp);
break;
/* output sample files */
case 'o':
ret = parse_output_files(optarg, tp);
break;
/* topology file */
case 't':
tp->tplg_file = strdup(optarg);
break;
/* input samples bit format */
case 'b':
tp->bits_in = strdup(optarg);
tp->frame_fmt = tplg_find_format(tp->bits_in);
break;
/* input sample rate */
case 'r':
tp->fs_in = atoi(optarg);
break;
/* output sample rate */
case 'R':
tp->fs_out = atoi(optarg);
break;
/* input/output channels */
case 'c':
tp->channels_in = atoi(optarg);
break;
/* output channels */
case 'n':
tp->channels_out = atoi(optarg);
break;
/* set debug log level */
case 'd':
if (isdigit((int)*optarg)) {
tp->trace_level = atoi(optarg);
} else {
fprintf(stderr, "Error: Debug level must be a digit.\n");
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
break;
/* number of pipeline copy() iterations */
case 'C':
tp->copy_iterations = atoi(optarg);
tp->copy_check = true;
break;
/* number of dynamic pipeline iterations */
case 'P':
tp->dynamic_pipeline_iterations = atoi(optarg);
break;
/* output sample files */
case 'p':
ret = parse_pipelines(optarg, tp);
break;
/* control script file name */
case 's':
tp->control_file = strdup(optarg);
break;
/* print usage */
case 'h':
print_usage(argv[0]);
exit(EXIT_SUCCESS);
default:
fprintf(stderr, "unknown option %c\n", option);
print_usage(argv[0]);
ret = -EINVAL;
}
if (ret < 0)
return ret;
}
return ret;
}
static void test_pipeline_stats(struct testbench_prm *tp, long long delta_t)
{
long long file_cycles, pipeline_cycles;
float pipeline_mcps;
int n_in, n_out, frames_out;
int i;
int count = 1;
n_out = 0;
n_in = 0;
file_cycles = 0;
for (i = 0; i < tp->input_file_num; i++) {
if (tp->fr[i].id < 0)
continue;
n_in += tp->fr[i].state->n;
file_cycles += tp->fr[i].state->cycles_count;
}
for (i = 0; i < tp->output_file_num; i++) {
if (tp->fw[i].id < 0)
continue;
n_out += tp->fw[i].state->n;
file_cycles += tp->fw[i].state->cycles_count;
}
/* print test summary */
printf("==========================================================\n");
printf(" Test Summary %d\n", count);
printf("==========================================================\n");
for (i = 0; i < tp->pipeline_num; i++) {
printf("pipeline %d\n", tp->pipelines[i]);
tb_show_file_stats(tp, tp->pipelines[i]);
}
printf("Input bit format: %s\n", tp->bits_in);
printf("Input sample rate: %d\n", tp->fs_in);
printf("Output sample rate: %d\n", tp->fs_out);
frames_out = n_out / tp->channels_out;
printf("Input sample (frame) count: %d (%d)\n", n_in, n_in / tp->channels_in);
printf("Output sample (frame) count: %d (%d)\n", n_out, frames_out);
if (tp->total_cycles) {
pipeline_cycles = tp->total_cycles - file_cycles;
pipeline_mcps = (float)pipeline_cycles * tp->fs_out / frames_out / 1e6;
printf("Total execution cycles: %lld\n", tp->total_cycles);
printf("File component cycles: %lld\n", file_cycles);
printf("Pipeline cycles: %lld\n", pipeline_cycles);
printf("Pipeline MCPS: %6.2f\n", pipeline_mcps);
if (tb_check_trace(LOG_LEVEL_DEBUG))
printf("Warning: Use -d 3 or smaller value to avoid traces to increase MCPS.\n");
}
if (delta_t)
printf("Total execution time: %lld us, %.2f x realtime\n",
delta_t, (float)frames_out / tp->fs_out * 1000000 / delta_t);
if (sof_platform_library_heap_usage.enable) {
printf("Size of rmalloc() allocations: %zu B\n",
sof_platform_library_heap_usage.rmalloc_size);
printf("Size of rzalloc() allocations: %zu B\n",
sof_platform_library_heap_usage.rzalloc_size);
printf("Size of rballoc_align() allocations: %zu B\n",
sof_platform_library_heap_usage.rballoc_align_size);
printf("Size of rbrealloc_align() allocations: %zu B\n",
sof_platform_library_heap_usage.rbrealloc_align_size);
printf("Total size of allocation from heap: %zu B\n",
sof_platform_library_heap_usage.rmalloc_size +
sof_platform_library_heap_usage.rzalloc_size +
sof_platform_library_heap_usage.rballoc_align_size +
sof_platform_library_heap_usage.rbrealloc_align_size);
}
printf("\n");
}
/*
* Tester thread, one for each virtual core. This is NOT the thread that will
* execute the virtual core.
*/
static int pipline_test(struct testbench_prm *tp)
{
float samples_to_ns;
int dp_count = 0;
struct timespec td0, td1;
struct file_state *out_stat;
long long delta_t;
int64_t next_control_ns;
int64_t time_ns;
int err;
/* build, run and teardown pipelines */
while (dp_count < tp->dynamic_pipeline_iterations) {
fprintf(stdout, "pipeline run %d/%d\n", dp_count,
tp->dynamic_pipeline_iterations);
/* print test summary */
printf("==========================================================\n");
printf(" Test Start %d\n", dp_count);
printf("==========================================================\n");
err = tb_load_topology(tp);
if (err < 0) {
fprintf(stderr, "error: topology load %d failed %d\n", dp_count, err);
break;
}
/* Start follow heap usage */
sof_platform_library_heap_usage.enable = true;
err = tb_set_up_all_pipelines(tp);
if (err < 0) {
fprintf(stderr, "error: pipelines set up %d failed %d\n", dp_count, err);
break;
}
err = tb_set_running_state(tp);
if (err < 0) {
fprintf(stderr, "error: pipelines state set %d failed %d\n", dp_count, err);
break;
}
err = tb_find_file_components(tp); /* Track file comp status during copying */
if (err < 0) {
fprintf(stderr, "error: file component find failed %d\n", err);
break;
}
/* Use first file writer to create simulation time. Calculate coefficient
* to calculate current time from file write samples count
*/
out_stat = tp->fw[0].state;
samples_to_ns = 1.0e9 / ((float)out_stat->channels * out_stat->rate);
/* Apply initial controls time to call again controls handler */
err = tb_read_controls(tp, &next_control_ns);
if (err) {
fprintf(stderr, "error: failed to read control commands.\n");
goto out;
}
tb_gettime(&td0);
while (true) {
if (tp->copy_check) {
if (tp->copy_iterations-- <= 0)
break;
}
if (tb_schedule_pipeline_check_state(tp))
break;
if (next_control_ns) {
time_ns = (int64_t)(samples_to_ns * out_stat->n);
if (time_ns >= next_control_ns) {
err = tb_read_controls(tp, &next_control_ns);
if (err) {
fprintf(stderr,
"error: failed to read control commands.\n");
goto out;
}
if (next_control_ns)
next_control_ns += time_ns;
}
}
}
tb_schedule_pipeline_check_state(tp); /* Once more to flush out remaining data */
tb_gettime(&td1);
out:
err = tb_set_reset_state(tp);
if (err < 0) {
fprintf(stderr, "error: pipeline reset %d failed %d\n",
dp_count, err);
break;
}
/* TODO: This should be printed after reset and free to get cleaner output
* but the file internal status would be lost there.
*/
delta_t = (td1.tv_sec - td0.tv_sec) * 1000000;
delta_t += (td1.tv_nsec - td0.tv_nsec) / 1000;
test_pipeline_stats(tp, delta_t);
err = tb_free_all_pipelines(tp);
if (err < 0) {
fprintf(stderr, "error: free pipelines %d failed %d\n", dp_count, err);
break;
}
tb_free_topology(tp);
dp_count++;
}
return 0;
}
int main(int argc, char **argv)
{
struct testbench_prm *tp;
int i, ret;
tp = calloc(1, sizeof(struct testbench_prm));
if (!tp)
return EXIT_FAILURE;
/* initialize input and output sample rates, files, etc. */
tp->channels_in = TESTBENCH_NCH;
tp->copy_check = false;
tp->dynamic_pipeline_iterations = 1;
tp->pipeline_string = calloc(1, TB_DEBUG_MSG_LEN);
tp->pipelines[0] = 1;
tp->pipeline_num = 1;
tp->copy_iterations = 1;
tp->trace_level = LOG_LEVEL_INFO;
/* command line arguments*/
ret = parse_input_args(argc, argv, tp);
if (ret < 0)
goto out;
if (!tp->channels_out)
tp->channels_out = tp->channels_in;
if (!tp->fs_out)
tp->fs_out = tp->fs_in;
/* check mandatory args */
if (!tp->tplg_file) {
fprintf(stderr, "topology file not specified, use -t file.tplg\n");
print_usage(argv[0]);
ret = EXIT_FAILURE;
goto out;
}
if (!tp->input_file_num) {
fprintf(stderr, "input files not specified, use -i file1,file2\n");
print_usage(argv[0]);
ret = EXIT_FAILURE;
goto out;
}
if (!tp->output_file_num) {
fprintf(stderr, "output files not specified, use -o file1,file2\n");
print_usage(argv[0]);
ret = EXIT_FAILURE;
goto out;
}
if (!tp->bits_in) {
fprintf(stderr, "input format not specified, use -b format\n");
print_usage(argv[0]);
ret = EXIT_FAILURE;
goto out;
}
/* initialize ipc and scheduler */
if (tb_setup(sof_get(), tp) < 0) {
fprintf(stderr, "error: pipeline init\n");
ret = EXIT_FAILURE;
goto out;
}
if (tp->control_file) {
tp->control_fh = fopen(tp->control_file, "r");
if (!tp->control_fh) {
fprintf(stderr, "error: opening script %s (%s).\n",
tp->control_file, strerror(errno));
ret = -errno;
goto out;
}
}
/* build, run and teardown pipelines */
pipline_test(tp);
/* free other core FW services */
tb_free(sof_get());
ret = EXIT_SUCCESS;
out:
/* free all other data */
free(tp->bits_in);
free(tp->tplg_file);
free(tp->control_file);
if (tp->control_fh)
fclose(tp->control_fh);
for (i = 0; i < tp->output_file_num; i++)
free(tp->output_file[i]);
for (i = 0; i < tp->input_file_num; i++)
free(tp->input_file[i]);
free(tp->pipeline_string);
free(tp);
return ret;
}