summaryrefslogtreecommitdiff
path: root/test/pcm-multi-thread.c
blob: de178899d044f79838d7586db485be3147f548a2 (plain)
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
/*
 * simple multi-thread stress test for PCM
 *
 * The main thread simply feeds or reads the sample data with the
 * random size continuously.  Meanwhile, the worker threads call some
 * update function depending on the given mode, and show the thread
 * number of the read value.
 *
 * The function for the worker thread is specified via -m option.
 * When the random mode ('r') is set, the update function is chosen
 * randomly in the loop.
 *
 * When the -v option is passed, this tries to show some obtained value
 * from the function.  Without -v, as default, it shows the thread number
 * (0-9).  In addition, it puts the mode suffix ('a' for avail, 'd' for
 * delay, etc) for the random mode, as well as the suffix '!' indicating
 * the error from the called function.
 */

#include <stdio.h>
#include <pthread.h>
#include <getopt.h>
#include "../include/asoundlib.h"

#define MAX_THREADS	10

enum {
	MODE_AVAIL_UPDATE,
	MODE_STATUS,
	MODE_HWSYNC,
	MODE_TIMESTAMP,
	MODE_DELAY,
	MODE_RANDOM
};

static char mode_suffix[] = {
	'a', 's', 'h', 't', 'd', 'r'
};

static const char *pcmdev = "default";
static int stream = SND_PCM_STREAM_PLAYBACK;
static int num_threads = 1;
static int periodsize = 16 * 1024;
static int bufsize = 16 * 1024 * 4;
static int channels = 2;
static int rate = 48000;
static snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;

static int running_mode = MODE_AVAIL_UPDATE;
static int show_value = 0;
static int quiet = 0;

static pthread_t peeper_threads[MAX_THREADS];
static int running = 1;
static snd_pcm_t *pcm;

static void *peeper(void *data)
{
	int thread_no = (long)data;
	snd_pcm_sframes_t val;
	snd_pcm_status_t *stat;
	snd_htimestamp_t tstamp;
	int mode = running_mode, err;

	snd_pcm_status_alloca(&stat);

	while (running) {
		if (running_mode == MODE_RANDOM)
			mode = rand() % MODE_RANDOM;
		switch (mode) {
		case MODE_AVAIL_UPDATE:
			val = snd_pcm_avail_update(pcm);
			err = 0;
			break;
		case MODE_STATUS:
			err = snd_pcm_status(pcm, stat);
			val = snd_pcm_status_get_avail(stat);
			break;
		case MODE_HWSYNC:
			err = snd_pcm_hwsync(pcm);
			break;
		case MODE_TIMESTAMP:
			err = snd_pcm_htimestamp(pcm, (snd_pcm_uframes_t *)&val,
						 &tstamp);
			break;
		default:
			err = snd_pcm_delay(pcm, &val);
			break;
		}

		if (quiet)
			continue;
		if (running_mode == MODE_RANDOM) {
			fprintf(stderr, "%d%c%s", thread_no, mode_suffix[mode],
				err ? "!" : "");
		} else {
			if (show_value && mode != MODE_HWSYNC)
				fprintf(stderr, "\r%d     ", (int)val);
			else
				fprintf(stderr, "%d%s", thread_no,
					err ? "!" : "");
		}
	}
	return NULL;
}

static void usage(void)
{
	fprintf(stderr, "usage: multi-thread [-options]\n");
	fprintf(stderr, "  -D str  Set device name\n");
	fprintf(stderr, "  -r val  Set sample rate\n");
	fprintf(stderr, "  -p val  Set period size (in frame)\n");
	fprintf(stderr, "  -b val  Set buffer size (in frame)\n");
	fprintf(stderr, "  -c val  Set number of channels\n");
	fprintf(stderr, "  -f str  Set PCM format\n");
	fprintf(stderr, "  -s str  Set stream direction (playback or capture)\n");
	fprintf(stderr, "  -t val  Set number of threads\n");
	fprintf(stderr, "  -m str  Running mode (avail, status, hwsync, timestamp, delay, random)\n");
	fprintf(stderr, "  -v      Show value\n");
	fprintf(stderr, "  -q      Quiet mode\n");
}

static int parse_options(int argc, char **argv)
{
	int c, i;

	while ((c = getopt(argc, argv, "D:r:f:p:b:s:t:m:vq")) >= 0) {
		switch (c) {
		case 'D':
			pcmdev = optarg;
			break;
		case 'r':
			rate = atoi(optarg);
			break;
		case 'p':
			periodsize = atoi(optarg);
			break;
		case 'b':
			bufsize = atoi(optarg);
			break;
		case 'c':
			channels = atoi(optarg);
			break;
		case 'f':
			format = snd_pcm_format_value(optarg);
			break;
		case 's':
			if (*optarg == 'p' || *optarg == 'P')
				stream = SND_PCM_STREAM_PLAYBACK;
			else if (*optarg == 'c' || *optarg == 'C')
				stream = SND_PCM_STREAM_CAPTURE;
			else {
				fprintf(stderr, "invalid stream direction\n");
				return 1;
			}
			break;
		case 't':
			num_threads = atoi(optarg);
			if (num_threads < 1 || num_threads > MAX_THREADS) {
				fprintf(stderr, "invalid number of threads\n");
				return 1;
			}
			break;
		case 'm':
			for (i = 0; i <= MODE_RANDOM; i++)
				if (mode_suffix[i] == *optarg)
					break;
			if (i > MODE_RANDOM) {
				fprintf(stderr, "invalid mode type\n");
				return 1;
			}
			running_mode = i;
			break;
		case 'v':
			show_value = 1;
			break;
		case 'q':
			quiet = 1;
			break;
		default:
			usage();
			return 1;
		}
	}
	return 0;
}

static int setup_params(void)
{
	snd_pcm_hw_params_t *hw;

	/* FIXME: more finer error checks */
	snd_pcm_hw_params_alloca(&hw);
	snd_pcm_hw_params_any(pcm, hw);
	snd_pcm_hw_params_set_access(pcm, hw, SND_PCM_ACCESS_RW_INTERLEAVED);
	snd_pcm_hw_params_set_format(pcm, hw, format);
	snd_pcm_hw_params_set_channels(pcm, hw, channels);
	snd_pcm_hw_params_set_rate(pcm, hw, rate, 0);
	snd_pcm_hw_params_set_period_size(pcm, hw, periodsize, 0);
	snd_pcm_hw_params_set_buffer_size(pcm, hw, bufsize);
	if (snd_pcm_hw_params(pcm, hw) < 0) {
		fprintf(stderr, "snd_pcm_hw_params error\n");
		return 1;
	}
	return 0;
}

int main(int argc, char **argv)
{
	char *buf;
	int i, err;

	if (parse_options(argc, argv))
		return 1;

	err = snd_pcm_open(&pcm, pcmdev, stream, 0);
	if (err < 0) {
		fprintf(stderr, "cannot open pcm %s\n", pcmdev);
		return 1;
	}

	if (setup_params())
		return 1;

	buf = calloc(1, snd_pcm_format_size(format, bufsize) * channels);
	if (!buf) {
		fprintf(stderr, "cannot alloc buffer\n");
		return 1;
	}

	for (i = 0; i < num_threads; i++) {
		if (pthread_create(&peeper_threads[i], NULL, peeper, (void *)(long)i)) {
			fprintf(stderr, "pthread_create error\n");
			return 1;
		}
	}

	if (stream == SND_PCM_STREAM_CAPTURE)
		snd_pcm_start(pcm);
	for (;;) {
		int size = rand() % (bufsize / 2);
		if (stream == SND_PCM_STREAM_PLAYBACK)
			err = snd_pcm_writei(pcm, buf, size);
		else
			err = snd_pcm_readi(pcm, buf, size);
		if (err < 0) {
			fprintf(stderr, "read/write error %d\n", err);
			err = snd_pcm_recover(pcm, err, 0);
			if (err < 0)
				break;
			if (stream == SND_PCM_STREAM_CAPTURE)
				snd_pcm_start(pcm);
		}
	}

	running = 0;
	for (i = 0; i < num_threads; i++)
		pthread_cancel(peeper_threads[i]);
	for (i = 0; i < num_threads; i++)
		pthread_join(peeper_threads[i], NULL);

	return 1;
}