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, see <http://www.gnu.org/licenses/>. 18***/ 19 20#ifdef HAVE_CONFIG_H 21#include <config.h> 22#endif 23 24#include <stdlib.h> 25#include <unistd.h> 26#include <time.h> 27#include <inttypes.h> 28 29#ifdef HAVE_PTHREAD 30#include <pthread.h> 31#ifdef HAVE_PTHREAD_SETAFFINITY_NP 32#ifdef __FreeBSD__ 33#include <pthread_np.h> 34#include <sys/param.h> 35#include <sys/cpuset.h> 36#endif 37#endif 38#endif 39 40#include <pulse/util.h> 41#include <pulse/timeval.h> 42#include <pulse/gccmacro.h> 43 44#include <pulsecore/log.h> 45#include <pulsecore/macro.h> 46#include <pulsecore/thread.h> 47#include <pulsecore/core-util.h> 48#include <pulsecore/core-rtclock.h> 49 50static int msec_lower, msec_upper; 51 52static void work(void *p) PA_GCC_NORETURN; 53 54static void work(void *p) { 55 56 pa_log_notice("CPU%i: Created thread.", PA_PTR_TO_UINT(p)); 57 58 pa_thread_make_realtime(12); 59 60#ifdef HAVE_PTHREAD_SETAFFINITY_NP 61{ 62#ifdef __FreeBSD__ 63 cpuset_t mask; 64#else 65 cpu_set_t mask; 66#endif 67 68 CPU_ZERO(&mask); 69 CPU_SET((size_t) PA_PTR_TO_UINT(p), &mask); 70 pa_assert_se(pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) == 0); 71} 72#endif 73 74 for (;;) { 75 struct timeval now, end; 76 uint64_t usec; 77 78 pa_log_notice("CPU%i: Sleeping for 1s", PA_PTR_TO_UINT(p)); 79 pa_msleep(1000); 80 81 usec = 82 (uint64_t) ((((double) rand())*(double)(msec_upper-msec_lower)*PA_USEC_PER_MSEC)/RAND_MAX) + 83 (uint64_t) ((uint64_t) msec_lower*PA_USEC_PER_MSEC); 84 85 pa_log_notice("CPU%i: Freezing for %ims", PA_PTR_TO_UINT(p), (int) (usec/PA_USEC_PER_MSEC)); 86 87 pa_rtclock_get(&end); 88 pa_timeval_add(&end, usec); 89 90 do { 91 pa_rtclock_get(&now); 92 } while (pa_timeval_cmp(&now, &end) < 0); 93 } 94} 95 96int main(int argc, char*argv[]) { 97 unsigned n; 98 99 pa_log_set_level(PA_LOG_INFO); 100 101 srand((unsigned) time(NULL)); 102 103 if (argc >= 3) { 104 msec_lower = atoi(argv[1]); 105 msec_upper = atoi(argv[2]); 106 } else if (argc >= 2) { 107 msec_lower = 0; 108 msec_upper = atoi(argv[1]); 109 } else { 110 msec_lower = 0; 111 msec_upper = 1000; 112 } 113 114 pa_assert(msec_upper > 0); 115 pa_assert(msec_upper >= msec_lower); 116 117 pa_log_notice("Creating random latencies in the range of %ims to %ims.", msec_lower, msec_upper); 118 119 for (n = 1; n < pa_ncpus(); n++) { 120 pa_assert_se(pa_thread_new("rtstutter", work, PA_UINT_TO_PTR(n))); 121 } 122 123 work(PA_INT_TO_PTR(0)); 124 125 return 0; 126} 127