merge glitch-free branch back into trunk
[profile/ivi/pulseaudio.git] / src / tests / rtstutter.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2008 Lennart Poettering
7
8   PulseAudio is free software; you can redistribute it and/or modify
9   it under the terms of the GNU Lesser General Public License as
10   published by the Free Software Foundation; either version 2 of the
11   License, or (at your option) any later version.
12
13   PulseAudio is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public
19   License along with PulseAudio; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21   USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <time.h>
31 #include <sched.h>
32 #include <inttypes.h>
33 #include <string.h>
34 #include <pthread.h>
35
36 #include <pulse/timeval.h>
37 #include <pulse/gccmacro.h>
38
39 #include <pulsecore/log.h>
40 #include <pulsecore/macro.h>
41
42 static int msec_lower, msec_upper;
43
44 static void* work(void *p) PA_GCC_NORETURN;
45
46 static void* work(void *p) {
47     cpu_set_t mask;
48     struct sched_param param;
49
50     pa_log_notice("CPU%i: Created thread.", PA_PTR_TO_INT(p));
51
52     memset(&param, 0, sizeof(param));
53     param.sched_priority = 12;
54     pa_assert_se(pthread_setschedparam(pthread_self(), SCHED_FIFO, &param) == 0);
55
56     CPU_ZERO(&mask);
57     CPU_SET(PA_PTR_TO_INT(p), &mask);
58     pa_assert_se(pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) == 0);
59
60     for (;;) {
61         struct timespec now, end;
62         uint64_t nsec;
63
64         pa_log_notice("CPU%i: Sleeping for 1s", PA_PTR_TO_INT(p));
65         sleep(1);
66
67         pa_assert_se(clock_gettime(CLOCK_REALTIME, &end) == 0);
68
69         nsec =
70             (uint64_t) ((((double) rand())*(msec_upper-msec_lower)*PA_NSEC_PER_MSEC)/RAND_MAX) +
71             (uint64_t) (msec_lower*PA_NSEC_PER_MSEC);
72
73         pa_log_notice("CPU%i: Freezing for %ims", PA_PTR_TO_INT(p), (int) (nsec/PA_NSEC_PER_MSEC));
74
75         end.tv_sec += nsec / PA_NSEC_PER_SEC;
76         end.tv_nsec += nsec % PA_NSEC_PER_SEC;
77
78         while (end.tv_nsec > PA_NSEC_PER_SEC) {
79             end.tv_sec++;
80             end.tv_nsec -= PA_NSEC_PER_SEC;
81         }
82
83         do {
84             pa_assert_se(clock_gettime(CLOCK_REALTIME, &now) == 0);
85         } while (now.tv_sec < end.tv_sec ||
86                  (now.tv_sec == end.tv_sec && now.tv_nsec < end.tv_nsec));
87     }
88 }
89
90 int main(int argc, char*argv[]) {
91     int n;
92
93     srand(time(NULL));
94
95     if (argc >= 3) {
96         msec_lower = atoi(argv[1]);
97         msec_upper = atoi(argv[2]);
98     } else if (argc >= 2) {
99         msec_lower = 0;
100         msec_upper = atoi(argv[1]);
101     } else {
102         msec_lower = 0;
103         msec_upper = 1000;
104     }
105
106     pa_assert(msec_upper > 0);
107     pa_assert(msec_upper >= msec_lower);
108
109     pa_log_notice("Creating random latencies in the range of %ims to  %ims.", msec_lower, msec_upper);
110
111     for (n = 1; n < sysconf(_SC_NPROCESSORS_CONF); n++) {
112         pthread_t t;
113         pa_assert_se(pthread_create(&t, NULL, work, PA_INT_TO_PTR(n)) == 0);
114     }
115
116     work(PA_INT_TO_PTR(0));
117
118     return 0;
119 }