2 * simple multi-thread stress test for PCM
4 * The main thread simply feeds or reads the sample data with the
5 * random size continuously. Meanwhile, the worker threads call some
6 * update function depending on the given mode, and show the thread
7 * number of the read value.
9 * The function for the worker thread is specified via -m option.
10 * When the random mode ('r') is set, the update function is chosen
11 * randomly in the loop.
13 * When the -v option is passed, this tries to show some obtained value
14 * from the function. Without -v, as default, it shows the thread number
15 * (0-9). In addition, it puts the mode suffix ('a' for avail, 'd' for
16 * delay, etc) for the random mode, as well as the suffix '!' indicating
17 * the error from the called function.
23 #include "../include/asoundlib.h"
25 #define MAX_THREADS 10
36 static char mode_suffix[] = {
37 'a', 's', 'h', 't', 'd', 'r'
40 static const char *devname = "default";
41 static int stream = SND_PCM_STREAM_PLAYBACK;
42 static int num_threads = 1;
43 static int periodsize = 16 * 1024;
44 static int bufsize = 16 * 1024 * 4;
45 static int channels = 2;
46 static int rate = 48000;
47 static snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
49 static int running_mode = MODE_AVAIL_UPDATE;
50 static int show_value = 0;
53 static pthread_t peeper_threads[MAX_THREADS];
54 static int running = 1;
55 static snd_pcm_t *pcm;
57 static void *peeper(void *data)
59 int thread_no = (long)data;
60 snd_pcm_sframes_t val;
61 snd_pcm_status_t *stat;
62 snd_htimestamp_t tstamp;
63 int mode = running_mode, err;
65 snd_pcm_status_alloca(&stat);
68 if (running_mode == MODE_RANDOM)
69 mode = rand() % MODE_RANDOM;
71 case MODE_AVAIL_UPDATE:
72 val = snd_pcm_avail_update(pcm);
76 err = snd_pcm_status(pcm, stat);
77 val = snd_pcm_status_get_avail(stat);
80 err = snd_pcm_hwsync(pcm);
83 err = snd_pcm_htimestamp(pcm, (snd_pcm_uframes_t *)&val,
87 err = snd_pcm_delay(pcm, &val);
93 if (running_mode == MODE_RANDOM) {
94 fprintf(stderr, "%d%c%s", thread_no, mode_suffix[mode],
97 if (show_value && mode != MODE_HWSYNC)
98 fprintf(stderr, "\r%d ", (int)val);
100 fprintf(stderr, "%d%s", thread_no,
107 static void usage(void)
109 fprintf(stderr, "usage: multi-thread [-options]\n");
110 fprintf(stderr, " -D str Set device name\n");
111 fprintf(stderr, " -r val Set sample rate\n");
112 fprintf(stderr, " -p val Set period size (in frame)\n");
113 fprintf(stderr, " -b val Set buffer size (in frame)\n");
114 fprintf(stderr, " -c val Set number of channels\n");
115 fprintf(stderr, " -f str Set PCM format\n");
116 fprintf(stderr, " -s str Set stream direction (playback or capture)\n");
117 fprintf(stderr, " -t val Set number of threads\n");
118 fprintf(stderr, " -m str Running mode (avail, status, hwsync, timestamp, delay, random)\n");
119 fprintf(stderr, " -v Show value\n");
120 fprintf(stderr, " -q Quiet mode\n");
123 static int parse_options(int argc, char **argv)
127 while ((c = getopt(argc, argv, "D:r:f:p:b:s:t:m:vq")) >= 0) {
136 periodsize = atoi(optarg);
139 bufsize = atoi(optarg);
142 channels = atoi(optarg);
145 format = snd_pcm_format_value(optarg);
148 if (*optarg == 'p' || *optarg == 'P')
149 stream = SND_PCM_STREAM_PLAYBACK;
150 else if (*optarg == 'c' || *optarg == 'C')
151 stream = SND_PCM_STREAM_CAPTURE;
153 fprintf(stderr, "invalid stream direction\n");
158 num_threads = atoi(optarg);
159 if (num_threads < 1 || num_threads > MAX_THREADS) {
160 fprintf(stderr, "invalid number of threads\n");
165 for (i = 0; i <= MODE_RANDOM; i++)
166 if (mode_suffix[i] == *optarg)
168 if (i > MODE_RANDOM) {
169 fprintf(stderr, "invalid mode type\n");
188 static int setup_params(void)
190 snd_pcm_hw_params_t *hw;
192 /* FIXME: more finer error checks */
193 snd_pcm_hw_params_alloca(&hw);
194 snd_pcm_hw_params_any(pcm, hw);
195 snd_pcm_hw_params_set_access(pcm, hw, SND_PCM_ACCESS_RW_INTERLEAVED);
196 snd_pcm_hw_params_set_format(pcm, hw, format);
197 snd_pcm_hw_params_set_channels(pcm, hw, channels);
198 snd_pcm_hw_params_set_rate(pcm, hw, rate, 0);
199 snd_pcm_hw_params_set_period_size(pcm, hw, periodsize, 0);
200 snd_pcm_hw_params_set_buffer_size(pcm, hw, bufsize);
201 if (snd_pcm_hw_params(pcm, hw) < 0) {
202 fprintf(stderr, "snd_pcm_hw_params error\n");
208 int main(int argc, char **argv)
213 if (parse_options(argc, argv))
216 err = snd_pcm_open(&pcm, devname, stream, 0);
218 fprintf(stderr, "cannot open pcm %s\n", devname);
225 buf = calloc(1, snd_pcm_format_size(format, bufsize) * channels);
227 fprintf(stderr, "cannot alloc buffer\n");
231 for (i = 0; i < num_threads; i++) {
232 if (pthread_create(&peeper_threads[i], NULL, peeper, (void *)(long)i)) {
233 fprintf(stderr, "pthread_create error\n");
238 if (stream == SND_PCM_STREAM_CAPTURE)
241 int size = rand() % (bufsize / 2);
242 if (stream == SND_PCM_STREAM_PLAYBACK)
243 err = snd_pcm_writei(pcm, buf, size);
245 err = snd_pcm_readi(pcm, buf, size);
247 fprintf(stderr, "read/write error %d\n", err);
248 err = snd_pcm_recover(pcm, err, 0);
251 if (stream == SND_PCM_STREAM_CAPTURE)
257 for (i = 0; i < num_threads; i++)
258 pthread_cancel(peeper_threads[i]);
259 for (i = 0; i < num_threads; i++)
260 pthread_join(peeper_threads[i], NULL);