]> code.delx.au - pulseaudio/blob - src/tests/rtstutter.c
Merge branch 'master' of git://0pointer.de/pulseaudio into dbus-work
[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 <sched.h>
30 #include <inttypes.h>
31 #include <string.h>
32 #include <pthread.h>
33
34 #include <pulse/timeval.h>
35 #include <pulse/gccmacro.h>
36
37 #include <pulsecore/log.h>
38 #include <pulsecore/macro.h>
39 #include <pulsecore/core-util.h>
40
41 static int msec_lower, msec_upper;
42
43 static void* work(void *p) PA_GCC_NORETURN;
44
45 static void* work(void *p) {
46 #ifdef HAVE_PTHREAD_SETAFFINITY_NP
47 cpu_set_t mask;
48 #endif
49 struct sched_param param;
50
51 pa_log_notice("CPU%i: Created thread.", PA_PTR_TO_UINT(p));
52
53 memset(&param, 0, sizeof(param));
54 param.sched_priority = 12;
55 pa_assert_se(pthread_setschedparam(pthread_self(), SCHED_FIFO, &param) == 0);
56
57 #ifdef HAVE_PTHREAD_SETAFFINITY_NP
58 CPU_ZERO(&mask);
59 CPU_SET((size_t) PA_PTR_TO_UINT(p), &mask);
60 pa_assert_se(pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) == 0);
61 #endif
62
63 for (;;) {
64 struct timespec now, end;
65 uint64_t nsec;
66
67 pa_log_notice("CPU%i: Sleeping for 1s", PA_PTR_TO_UINT(p));
68 sleep(1);
69
70 #ifdef CLOCK_REALTIME
71 pa_assert_se(clock_gettime(CLOCK_REALTIME, &end) == 0);
72 #endif
73
74 nsec =
75 (uint64_t) ((((double) rand())*(double)(msec_upper-msec_lower)*PA_NSEC_PER_MSEC)/RAND_MAX) +
76 (uint64_t) ((uint64_t) msec_lower*PA_NSEC_PER_MSEC);
77
78 pa_log_notice("CPU%i: Freezing for %ims", PA_PTR_TO_UINT(p), (int) (nsec/PA_NSEC_PER_MSEC));
79
80 end.tv_sec += (time_t) (nsec / PA_NSEC_PER_SEC);
81 end.tv_nsec += (long int) (nsec % PA_NSEC_PER_SEC);
82
83 while ((pa_usec_t) end.tv_nsec > PA_NSEC_PER_SEC) {
84 end.tv_sec++;
85 end.tv_nsec -= (long int) PA_NSEC_PER_SEC;
86 }
87
88 do {
89 #ifdef CLOCK_REALTIME
90 pa_assert_se(clock_gettime(CLOCK_REALTIME, &now) == 0);
91 #endif
92 } while (now.tv_sec < end.tv_sec ||
93 (now.tv_sec == end.tv_sec && now.tv_nsec < end.tv_nsec));
94 }
95 }
96
97 int main(int argc, char*argv[]) {
98 unsigned n;
99
100 pa_log_set_level(PA_LOG_DEBUG);
101
102 srand((unsigned) time(NULL));
103
104 if (argc >= 3) {
105 msec_lower = atoi(argv[1]);
106 msec_upper = atoi(argv[2]);
107 } else if (argc >= 2) {
108 msec_lower = 0;
109 msec_upper = atoi(argv[1]);
110 } else {
111 msec_lower = 0;
112 msec_upper = 1000;
113 }
114
115 pa_assert(msec_upper > 0);
116 pa_assert(msec_upper >= msec_lower);
117
118 pa_log_notice("Creating random latencies in the range of %ims to %ims.", msec_lower, msec_upper);
119
120 for (n = 1; n < pa_ncpus(); n++) {
121 pthread_t t;
122 pa_assert_se(pthread_create(&t, NULL, work, PA_UINT_TO_PTR(n)) == 0);
123 }
124
125 work(PA_INT_TO_PTR(0));
126
127 return 0;
128 }