]> code.delx.au - pulseaudio/blob - src/tests/rtstutter.c
Merge dead branch 'prepare-0.9.10'
[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 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
40 static int msec_lower, msec_upper;
41
42 static void* work(void *p) PA_GCC_NORETURN;
43
44 static void* work(void *p) {
45 cpu_set_t mask;
46 struct sched_param param;
47
48 pa_log_notice("CPU%i: Created thread.", PA_PTR_TO_INT(p));
49
50 memset(&param, 0, sizeof(param));
51 param.sched_priority = 12;
52 pa_assert_se(pthread_setschedparam(pthread_self(), SCHED_FIFO, &param) == 0);
53
54 CPU_ZERO(&mask);
55 CPU_SET(PA_PTR_TO_INT(p), &mask);
56 pa_assert_se(pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) == 0);
57
58 for (;;) {
59 struct timespec now, end;
60 uint64_t nsec;
61
62 pa_log_notice("CPU%i: Sleeping for 1s", PA_PTR_TO_INT(p));
63 sleep(1);
64
65 pa_assert_se(clock_gettime(CLOCK_REALTIME, &end) == 0);
66
67 nsec =
68 (uint64_t) ((((double) rand())*(msec_upper-msec_lower)*PA_NSEC_PER_MSEC)/RAND_MAX) +
69 (uint64_t) (msec_lower*PA_NSEC_PER_MSEC);
70
71 pa_log_notice("CPU%i: Freezing for %ims", PA_PTR_TO_INT(p), (int) (nsec/PA_NSEC_PER_MSEC));
72
73 end.tv_sec += nsec / PA_NSEC_PER_SEC;
74 end.tv_nsec += nsec % PA_NSEC_PER_SEC;
75
76 while ((pa_usec_t) end.tv_nsec > PA_NSEC_PER_SEC) {
77 end.tv_sec++;
78 end.tv_nsec -= PA_NSEC_PER_SEC;
79 }
80
81 do {
82 pa_assert_se(clock_gettime(CLOCK_REALTIME, &now) == 0);
83 } while (now.tv_sec < end.tv_sec ||
84 (now.tv_sec == end.tv_sec && now.tv_nsec < end.tv_nsec));
85 }
86 }
87
88 int main(int argc, char*argv[]) {
89 int n;
90
91 srand(time(NULL));
92
93 if (argc >= 3) {
94 msec_lower = atoi(argv[1]);
95 msec_upper = atoi(argv[2]);
96 } else if (argc >= 2) {
97 msec_lower = 0;
98 msec_upper = atoi(argv[1]);
99 } else {
100 msec_lower = 0;
101 msec_upper = 1000;
102 }
103
104 pa_assert(msec_upper > 0);
105 pa_assert(msec_upper >= msec_lower);
106
107 pa_log_notice("Creating random latencies in the range of %ims to %ims.", msec_lower, msec_upper);
108
109 for (n = 1; n < sysconf(_SC_NPROCESSORS_CONF); n++) {
110 pthread_t t;
111 pa_assert_se(pthread_create(&t, NULL, work, PA_INT_TO_PTR(n)) == 0);
112 }
113
114 work(PA_INT_TO_PTR(0));
115
116 return 0;
117 }