]> code.delx.au - pulseaudio/blob - src/tests/rtstutter.c
Make tests compile on FreeBSD
[pulseaudio] / src / tests / rtstutter.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2008 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <time.h>
29 #include <inttypes.h>
30
31 #ifdef HAVE_PTHREAD
32 #include <pthread.h>
33 #ifdef HAVE_PTHREAD_SETAFFINITY_NP
34 #if defined(__FreeBSD__)
35 #include <pthread_np.h>
36 #include <sys/param.h>
37 #include <sys/cpuset.h>
38 #endif
39 #endif
40 #endif
41
42 #include <pulse/util.h>
43 #include <pulse/timeval.h>
44 #include <pulse/gccmacro.h>
45
46 #include <pulsecore/log.h>
47 #include <pulsecore/macro.h>
48 #include <pulsecore/thread.h>
49 #include <pulsecore/core-util.h>
50 #include <pulsecore/core-rtclock.h>
51
52 static int msec_lower, msec_upper;
53
54 static void work(void *p) PA_GCC_NORETURN;
55
56 static void work(void *p) {
57
58 pa_log_notice("CPU%i: Created thread.", PA_PTR_TO_UINT(p));
59
60 pa_make_realtime(12);
61
62 #ifdef HAVE_PTHREAD_SETAFFINITY_NP
63 {
64 #ifdef __FreeBSD__
65 cpuset_t mask;
66 #else
67 cpu_set_t mask;
68 #endif
69
70 CPU_ZERO(&mask);
71 CPU_SET((size_t) PA_PTR_TO_UINT(p), &mask);
72 pa_assert_se(pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) == 0);
73 }
74 #endif
75
76 for (;;) {
77 struct timeval now, end;
78 uint64_t usec;
79
80 pa_log_notice("CPU%i: Sleeping for 1s", PA_PTR_TO_UINT(p));
81 pa_msleep(1000);
82
83 usec =
84 (uint64_t) ((((double) rand())*(double)(msec_upper-msec_lower)*PA_USEC_PER_MSEC)/RAND_MAX) +
85 (uint64_t) ((uint64_t) msec_lower*PA_USEC_PER_MSEC);
86
87 pa_log_notice("CPU%i: Freezing for %ims", PA_PTR_TO_UINT(p), (int) (usec/PA_USEC_PER_MSEC));
88
89 pa_rtclock_get(&end);
90 pa_timeval_add(&end, usec);
91
92 do {
93 pa_rtclock_get(&now);
94 } while (pa_timeval_cmp(&now, &end) < 0);
95 }
96 }
97
98 int main(int argc, char*argv[]) {
99 unsigned n;
100
101 pa_log_set_level(PA_LOG_INFO);
102
103 srand((unsigned) time(NULL));
104
105 if (argc >= 3) {
106 msec_lower = atoi(argv[1]);
107 msec_upper = atoi(argv[2]);
108 } else if (argc >= 2) {
109 msec_lower = 0;
110 msec_upper = atoi(argv[1]);
111 } else {
112 msec_lower = 0;
113 msec_upper = 1000;
114 }
115
116 pa_assert(msec_upper > 0);
117 pa_assert(msec_upper >= msec_lower);
118
119 pa_log_notice("Creating random latencies in the range of %ims to %ims.", msec_lower, msec_upper);
120
121 for (n = 1; n < pa_ncpus(); n++) {
122 pa_assert_se(pa_thread_new("rtstutter", work, PA_UINT_TO_PTR(n)));
123 }
124
125 work(PA_INT_TO_PTR(0));
126
127 return 0;
128 }