1*49cdfc7eSAndroid Build Coastguard Worker /******************************************************************************
2*49cdfc7eSAndroid Build Coastguard Worker *
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright © International Business Machines Corp., 2006-2008
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13*49cdfc7eSAndroid Build Coastguard Worker * the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker *
15*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker * along with this program; if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker *
19*49cdfc7eSAndroid Build Coastguard Worker * NAME
20*49cdfc7eSAndroid Build Coastguard Worker * librttest.h
21*49cdfc7eSAndroid Build Coastguard Worker *
22*49cdfc7eSAndroid Build Coastguard Worker * DESCRIPTION
23*49cdfc7eSAndroid Build Coastguard Worker * A set of commonly used convenience functions for writing
24*49cdfc7eSAndroid Build Coastguard Worker * threaded realtime test cases.
25*49cdfc7eSAndroid Build Coastguard Worker *
26*49cdfc7eSAndroid Build Coastguard Worker * USAGE:
27*49cdfc7eSAndroid Build Coastguard Worker * To be included in testcases.
28*49cdfc7eSAndroid Build Coastguard Worker *
29*49cdfc7eSAndroid Build Coastguard Worker * AUTHOR
30*49cdfc7eSAndroid Build Coastguard Worker * Darren Hart <[email protected]>
31*49cdfc7eSAndroid Build Coastguard Worker *
32*49cdfc7eSAndroid Build Coastguard Worker * HISTORY
33*49cdfc7eSAndroid Build Coastguard Worker * 2006-Apr-26: Initial version by Darren Hart
34*49cdfc7eSAndroid Build Coastguard Worker * 2006-May-08: Added atomic_{inc,set,get}, thread struct, debug function,
35*49cdfc7eSAndroid Build Coastguard Worker * rt_init, buffered printing -- Vernon Mauery
36*49cdfc7eSAndroid Build Coastguard Worker * 2006-May-09: improved command line argument handling
37*49cdfc7eSAndroid Build Coastguard Worker * 2007-Jul-12: Added latency tracing functions -- Josh Triplett
38*49cdfc7eSAndroid Build Coastguard Worker * 2007-Jul-26: Renamed to librttest.h -- Josh Triplett
39*49cdfc7eSAndroid Build Coastguard Worker * 2009-Nov-4: TSC macros within another header -- Giuseppe Cavallaro
40*49cdfc7eSAndroid Build Coastguard Worker *
41*49cdfc7eSAndroid Build Coastguard Worker *****************************************************************************/
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker #ifndef LIBRTTEST_H
44*49cdfc7eSAndroid Build Coastguard Worker #define LIBRTTEST_H
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker #include <sys/syscall.h>
47*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
48*49cdfc7eSAndroid Build Coastguard Worker #include <getopt.h>
49*49cdfc7eSAndroid Build Coastguard Worker #include <math.h>
50*49cdfc7eSAndroid Build Coastguard Worker #include <pthread.h>
51*49cdfc7eSAndroid Build Coastguard Worker #include <sched.h>
52*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
53*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
54*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
55*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
56*49cdfc7eSAndroid Build Coastguard Worker #include <time.h>
57*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
58*49cdfc7eSAndroid Build Coastguard Worker #include "list.h"
59*49cdfc7eSAndroid Build Coastguard Worker #include "realtime_config.h"
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker extern void setup(void);
62*49cdfc7eSAndroid Build Coastguard Worker extern void cleanup(int i);
63*49cdfc7eSAndroid Build Coastguard Worker
64*49cdfc7eSAndroid Build Coastguard Worker extern int optind, opterr, optopt;
65*49cdfc7eSAndroid Build Coastguard Worker extern char *optarg;
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker #define _MAXTHREADS 256
68*49cdfc7eSAndroid Build Coastguard Worker #define CALIBRATE_LOOPS 100000
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker #define NS_PER_MS 1000000
71*49cdfc7eSAndroid Build Coastguard Worker #define NS_PER_US 1000
72*49cdfc7eSAndroid Build Coastguard Worker #define NS_PER_SEC 1000000000
73*49cdfc7eSAndroid Build Coastguard Worker #define US_PER_MS 1000
74*49cdfc7eSAndroid Build Coastguard Worker #define US_PER_SEC 1000000
75*49cdfc7eSAndroid Build Coastguard Worker #define MS_PER_SEC 1000
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker typedef unsigned long long nsec_t;
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker struct thread {
80*49cdfc7eSAndroid Build Coastguard Worker struct list_head _threads;
81*49cdfc7eSAndroid Build Coastguard Worker pthread_t pthread;
82*49cdfc7eSAndroid Build Coastguard Worker pthread_attr_t attr;
83*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_t mutex;
84*49cdfc7eSAndroid Build Coastguard Worker pthread_cond_t cond;
85*49cdfc7eSAndroid Build Coastguard Worker void *arg;
86*49cdfc7eSAndroid Build Coastguard Worker void *(*func)(void*);
87*49cdfc7eSAndroid Build Coastguard Worker int priority;
88*49cdfc7eSAndroid Build Coastguard Worker int policy;
89*49cdfc7eSAndroid Build Coastguard Worker int flags;
90*49cdfc7eSAndroid Build Coastguard Worker int id;
91*49cdfc7eSAndroid Build Coastguard Worker };
92*49cdfc7eSAndroid Build Coastguard Worker typedef struct { volatile int counter; } atomic_t;
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker // flags for threads
95*49cdfc7eSAndroid Build Coastguard Worker #define THREAD_START 1
96*49cdfc7eSAndroid Build Coastguard Worker #define THREAD_QUIT 2
97*49cdfc7eSAndroid Build Coastguard Worker #define thread_quit(T) (((T)->flags) & THREAD_QUIT)
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Worker #define PRINT_BUFFER_SIZE (1024*1024*4)
100*49cdfc7eSAndroid Build Coastguard Worker #define ULL_MAX 18446744073709551615ULL // (1 << 64) - 1
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker extern pthread_mutex_t _buffer_mutex;
103*49cdfc7eSAndroid Build Coastguard Worker extern char * _print_buffer;
104*49cdfc7eSAndroid Build Coastguard Worker extern int _print_buffer_offset;
105*49cdfc7eSAndroid Build Coastguard Worker extern int _dbg_lvl;
106*49cdfc7eSAndroid Build Coastguard Worker extern double pass_criteria;
107*49cdfc7eSAndroid Build Coastguard Worker
108*49cdfc7eSAndroid Build Coastguard Worker /* function prototypes */
109*49cdfc7eSAndroid Build Coastguard Worker
110*49cdfc7eSAndroid Build Coastguard Worker /* atomic_add - add integer to atomic variable and returns a value.
111*49cdfc7eSAndroid Build Coastguard Worker * i: integer value to add
112*49cdfc7eSAndroid Build Coastguard Worker * v: pointer of type atomic_t
113*49cdfc7eSAndroid Build Coastguard Worker */
atomic_add(int i,atomic_t * v)114*49cdfc7eSAndroid Build Coastguard Worker static inline int atomic_add(int i, atomic_t *v)
115*49cdfc7eSAndroid Build Coastguard Worker {
116*49cdfc7eSAndroid Build Coastguard Worker /* XXX (garrcoop): only available in later versions of gcc */
117*49cdfc7eSAndroid Build Coastguard Worker #if HAVE___SYNC_ADD_AND_FETCH
118*49cdfc7eSAndroid Build Coastguard Worker return __sync_add_and_fetch(&v->counter, i);
119*49cdfc7eSAndroid Build Coastguard Worker #else
120*49cdfc7eSAndroid Build Coastguard Worker printf("%s: %s\n", __func__, strerror(ENOSYS));
121*49cdfc7eSAndroid Build Coastguard Worker exit(1);
122*49cdfc7eSAndroid Build Coastguard Worker return -1;
123*49cdfc7eSAndroid Build Coastguard Worker #endif
124*49cdfc7eSAndroid Build Coastguard Worker }
125*49cdfc7eSAndroid Build Coastguard Worker /* atomic_inc: atomically increment the integer passed by reference
126*49cdfc7eSAndroid Build Coastguard Worker */
atomic_inc(atomic_t * v)127*49cdfc7eSAndroid Build Coastguard Worker static inline int atomic_inc(atomic_t *v)
128*49cdfc7eSAndroid Build Coastguard Worker {
129*49cdfc7eSAndroid Build Coastguard Worker return atomic_add(1, v);
130*49cdfc7eSAndroid Build Coastguard Worker }
131*49cdfc7eSAndroid Build Coastguard Worker
132*49cdfc7eSAndroid Build Coastguard Worker /* atomic_get: atomically get the integer passed by reference
133*49cdfc7eSAndroid Build Coastguard Worker */
134*49cdfc7eSAndroid Build Coastguard Worker //#define atomic_get(v) ((v)->counter)
atomic_get(atomic_t * v)135*49cdfc7eSAndroid Build Coastguard Worker static inline int atomic_get(atomic_t *v)
136*49cdfc7eSAndroid Build Coastguard Worker {
137*49cdfc7eSAndroid Build Coastguard Worker return v->counter;
138*49cdfc7eSAndroid Build Coastguard Worker }
139*49cdfc7eSAndroid Build Coastguard Worker
140*49cdfc7eSAndroid Build Coastguard Worker /* atomic_set: atomically get the integer passed by reference
141*49cdfc7eSAndroid Build Coastguard Worker */
142*49cdfc7eSAndroid Build Coastguard Worker //#define atomic_set(i,v) ((v)->counter = (i))
atomic_set(int i,atomic_t * v)143*49cdfc7eSAndroid Build Coastguard Worker static inline void atomic_set(int i, atomic_t *v)
144*49cdfc7eSAndroid Build Coastguard Worker {
145*49cdfc7eSAndroid Build Coastguard Worker v->counter = i;
146*49cdfc7eSAndroid Build Coastguard Worker }
147*49cdfc7eSAndroid Build Coastguard Worker
148*49cdfc7eSAndroid Build Coastguard Worker /* buffer_init: initialize the buffered printing system
149*49cdfc7eSAndroid Build Coastguard Worker */
150*49cdfc7eSAndroid Build Coastguard Worker void buffer_init(void);
151*49cdfc7eSAndroid Build Coastguard Worker
152*49cdfc7eSAndroid Build Coastguard Worker /* buffer_print: prints the contents of the buffer
153*49cdfc7eSAndroid Build Coastguard Worker */
154*49cdfc7eSAndroid Build Coastguard Worker void buffer_print(void);
155*49cdfc7eSAndroid Build Coastguard Worker
156*49cdfc7eSAndroid Build Coastguard Worker /* buffer_fini: destroy the buffer
157*49cdfc7eSAndroid Build Coastguard Worker */
158*49cdfc7eSAndroid Build Coastguard Worker void buffer_fini(void);
159*49cdfc7eSAndroid Build Coastguard Worker
160*49cdfc7eSAndroid Build Coastguard Worker /* debug: do debug prints at level L (see DBG_* below). If buffer_init
161*49cdfc7eSAndroid Build Coastguard Worker * has been called previously, this will print to the internal memory
162*49cdfc7eSAndroid Build Coastguard Worker * buffer rather than to stderr.
163*49cdfc7eSAndroid Build Coastguard Worker * L: debug level (see below) This will print if L is lower than _dbg_lvl
164*49cdfc7eSAndroid Build Coastguard Worker * A: format string (printf style)
165*49cdfc7eSAndroid Build Coastguard Worker * B: args to format string (printf style)
166*49cdfc7eSAndroid Build Coastguard Worker */
167*49cdfc7eSAndroid Build Coastguard Worker static volatile int _debug_count = 0;
168*49cdfc7eSAndroid Build Coastguard Worker #define debug(L,A,B...) do {\
169*49cdfc7eSAndroid Build Coastguard Worker if ((L) <= _dbg_lvl) {\
170*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_lock(&_buffer_mutex);\
171*49cdfc7eSAndroid Build Coastguard Worker if (_print_buffer) {\
172*49cdfc7eSAndroid Build Coastguard Worker if (PRINT_BUFFER_SIZE - _print_buffer_offset < 1000)\
173*49cdfc7eSAndroid Build Coastguard Worker buffer_print();\
174*49cdfc7eSAndroid Build Coastguard Worker _print_buffer_offset += snprintf(&_print_buffer[_print_buffer_offset],\
175*49cdfc7eSAndroid Build Coastguard Worker PRINT_BUFFER_SIZE - _print_buffer_offset, "%06d: "A, _debug_count++, ##B);\
176*49cdfc7eSAndroid Build Coastguard Worker } else {\
177*49cdfc7eSAndroid Build Coastguard Worker fprintf(stderr, "%06d: "A, _debug_count++, ##B);\
178*49cdfc7eSAndroid Build Coastguard Worker }\
179*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_unlock(&_buffer_mutex);\
180*49cdfc7eSAndroid Build Coastguard Worker }\
181*49cdfc7eSAndroid Build Coastguard Worker } while (0)
182*49cdfc7eSAndroid Build Coastguard Worker #define DBG_ERR 1
183*49cdfc7eSAndroid Build Coastguard Worker #define DBG_WARN 2
184*49cdfc7eSAndroid Build Coastguard Worker #define DBG_INFO 3
185*49cdfc7eSAndroid Build Coastguard Worker #define DBG_DEBUG 4
186*49cdfc7eSAndroid Build Coastguard Worker
187*49cdfc7eSAndroid Build Coastguard Worker /* rt_help: print help for standard args */
188*49cdfc7eSAndroid Build Coastguard Worker void rt_help(void);
189*49cdfc7eSAndroid Build Coastguard Worker
190*49cdfc7eSAndroid Build Coastguard Worker /* rt_init_long: initialize librttest
191*49cdfc7eSAndroid Build Coastguard Worker * options: pass in an getopt style string of options -- e.g. "ab:cd::e:"
192*49cdfc7eSAndroid Build Coastguard Worker * if this or parse_arg is null, no option parsing will be done
193*49cdfc7eSAndroid Build Coastguard Worker * on behalf of the calling program (only internal args will be parsed)
194*49cdfc7eSAndroid Build Coastguard Worker * longopts: a pointer to the first element of an array of struct option, the
195*49cdfc7eSAndroid Build Coastguard Worker * last entry must be set to all zeros.
196*49cdfc7eSAndroid Build Coastguard Worker * parse_arg: a function that will get called when one of the above
197*49cdfc7eSAndroid Build Coastguard Worker * options is encountered on the command line. It will be passed
198*49cdfc7eSAndroid Build Coastguard Worker * the option -- e.g. 'b' -- and the value. Something like:
199*49cdfc7eSAndroid Build Coastguard Worker * // assume we passed "af:z::" to rt_init
200*49cdfc7eSAndroid Build Coastguard Worker * int parse_my_options(int option, char *value) {
201*49cdfc7eSAndroid Build Coastguard Worker * int handled = 1;
202*49cdfc7eSAndroid Build Coastguard Worker * switch (option) {
203*49cdfc7eSAndroid Build Coastguard Worker * case 'a':
204*49cdfc7eSAndroid Build Coastguard Worker * alphanum = 1;
205*49cdfc7eSAndroid Build Coastguard Worker * break;
206*49cdfc7eSAndroid Build Coastguard Worker * case 'f':
207*49cdfc7eSAndroid Build Coastguard Worker * // we passed f: which means f has an argument
208*49cdfc7eSAndroid Build Coastguard Worker * freedom = strcpy(value);
209*49cdfc7eSAndroid Build Coastguard Worker * break;
210*49cdfc7eSAndroid Build Coastguard Worker * case 'z':
211*49cdfc7eSAndroid Build Coastguard Worker * // we passed z:: which means z has an optional argument
212*49cdfc7eSAndroid Build Coastguard Worker * if (value)
213*49cdfc7eSAndroid Build Coastguard Worker * zero_size = atoi(value);
214*49cdfc7eSAndroid Build Coastguard Worker * else
215*49cdfc7eSAndroid Build Coastguard Worker * zero_size++;
216*49cdfc7eSAndroid Build Coastguard Worker * default:
217*49cdfc7eSAndroid Build Coastguard Worker * handled = 0;
218*49cdfc7eSAndroid Build Coastguard Worker * }
219*49cdfc7eSAndroid Build Coastguard Worker * return handled;
220*49cdfc7eSAndroid Build Coastguard Worker * }
221*49cdfc7eSAndroid Build Coastguard Worker * argc: passed from main
222*49cdfc7eSAndroid Build Coastguard Worker * argv: passed from main
223*49cdfc7eSAndroid Build Coastguard Worker */
224*49cdfc7eSAndroid Build Coastguard Worker int rt_init_long(const char *options, const struct option *longopts,
225*49cdfc7eSAndroid Build Coastguard Worker int (*parse_arg)(int option, char *value),
226*49cdfc7eSAndroid Build Coastguard Worker int argc, char *argv[]);
227*49cdfc7eSAndroid Build Coastguard Worker
228*49cdfc7eSAndroid Build Coastguard Worker /* rt_init: same as rt_init_long with no long options */
229*49cdfc7eSAndroid Build Coastguard Worker int rt_init(const char *options, int (*parse_arg)(int option, char *value),
230*49cdfc7eSAndroid Build Coastguard Worker int argc, char *argv[]);
231*49cdfc7eSAndroid Build Coastguard Worker
232*49cdfc7eSAndroid Build Coastguard Worker int create_thread(void*(*func)(void*), void *arg, int prio, int policy);
233*49cdfc7eSAndroid Build Coastguard Worker
234*49cdfc7eSAndroid Build Coastguard Worker /* create_fifo_thread: spawn a SCHED_FIFO thread with priority prio running
235*49cdfc7eSAndroid Build Coastguard Worker * func as the thread function with arg as it's parameter.
236*49cdfc7eSAndroid Build Coastguard Worker * func:
237*49cdfc7eSAndroid Build Coastguard Worker * arg: argument to func
238*49cdfc7eSAndroid Build Coastguard Worker * prio: 1-100, 100 being highest priority
239*49cdfc7eSAndroid Build Coastguard Worker */
240*49cdfc7eSAndroid Build Coastguard Worker int create_fifo_thread(void*(*func)(void*), void *arg, int prio);
241*49cdfc7eSAndroid Build Coastguard Worker
242*49cdfc7eSAndroid Build Coastguard Worker /* create_rr_thread: spawn a SCHED_RR thread with priority prio running
243*49cdfc7eSAndroid Build Coastguard Worker * func as the thread function with arg as it's parameter.
244*49cdfc7eSAndroid Build Coastguard Worker * func:
245*49cdfc7eSAndroid Build Coastguard Worker * arg: argument to func
246*49cdfc7eSAndroid Build Coastguard Worker * prio: 1-100, 100 being highest priority
247*49cdfc7eSAndroid Build Coastguard Worker */
248*49cdfc7eSAndroid Build Coastguard Worker int create_rr_thread(void*(*func)(void*), void *arg, int prio);
249*49cdfc7eSAndroid Build Coastguard Worker
250*49cdfc7eSAndroid Build Coastguard Worker /* create_other_thread: spawn a SCHED_OTHER thread
251*49cdfc7eSAndroid Build Coastguard Worker * func as the thread function with arg as it's parameter.
252*49cdfc7eSAndroid Build Coastguard Worker * func:
253*49cdfc7eSAndroid Build Coastguard Worker * arg: argument to func
254*49cdfc7eSAndroid Build Coastguard Worker */
255*49cdfc7eSAndroid Build Coastguard Worker int create_other_thread(void*(*func)(void*), void *arg);
256*49cdfc7eSAndroid Build Coastguard Worker
257*49cdfc7eSAndroid Build Coastguard Worker /* Change the priority of a running thread */
258*49cdfc7eSAndroid Build Coastguard Worker int set_thread_priority(pthread_t pthread, int prio);
259*49cdfc7eSAndroid Build Coastguard Worker
260*49cdfc7eSAndroid Build Coastguard Worker /* Change the priority of the current context (usually called from main())
261*49cdfc7eSAndroid Build Coastguard Worker * and its policy to SCHED_FIFO
262*49cdfc7eSAndroid Build Coastguard Worker * prio: 1-99
263*49cdfc7eSAndroid Build Coastguard Worker */
264*49cdfc7eSAndroid Build Coastguard Worker int set_priority(int prio);
265*49cdfc7eSAndroid Build Coastguard Worker
266*49cdfc7eSAndroid Build Coastguard Worker /* all_threads_quit: signal all threads to quit */
267*49cdfc7eSAndroid Build Coastguard Worker void all_threads_quit(void);
268*49cdfc7eSAndroid Build Coastguard Worker
269*49cdfc7eSAndroid Build Coastguard Worker /* join_threads: wait for all threads to finish
270*49cdfc7eSAndroid Build Coastguard Worker * (calls all_threads_quit interally)
271*49cdfc7eSAndroid Build Coastguard Worker */
272*49cdfc7eSAndroid Build Coastguard Worker void join_threads(void);
273*49cdfc7eSAndroid Build Coastguard Worker
274*49cdfc7eSAndroid Build Coastguard Worker /* get_thread: return a struct thread pointer from the list */
275*49cdfc7eSAndroid Build Coastguard Worker struct thread * get_thread(int i);
276*49cdfc7eSAndroid Build Coastguard Worker
277*49cdfc7eSAndroid Build Coastguard Worker /* signal thread i to quit and then call join */
278*49cdfc7eSAndroid Build Coastguard Worker void join_thread(int i);
279*49cdfc7eSAndroid Build Coastguard Worker
280*49cdfc7eSAndroid Build Coastguard Worker /* return the delta in ts_delta
281*49cdfc7eSAndroid Build Coastguard Worker * ts_end > ts_start
282*49cdfc7eSAndroid Build Coastguard Worker * if ts_delta is not null, the difference will be returned in it
283*49cdfc7eSAndroid Build Coastguard Worker */
284*49cdfc7eSAndroid Build Coastguard Worker void ts_minus(struct timespec *ts_end, struct timespec *ts_start, struct timespec *ts_delta);
285*49cdfc7eSAndroid Build Coastguard Worker
286*49cdfc7eSAndroid Build Coastguard Worker /* return the sum in ts_sum
287*49cdfc7eSAndroid Build Coastguard Worker * all arguments are not null
288*49cdfc7eSAndroid Build Coastguard Worker */
289*49cdfc7eSAndroid Build Coastguard Worker void ts_plus(struct timespec *ts_a, struct timespec *ts_b, struct timespec *ts_sum);
290*49cdfc7eSAndroid Build Coastguard Worker
291*49cdfc7eSAndroid Build Coastguard Worker /* put a ts into proper form (nsec < NS_PER_SEC)
292*49cdfc7eSAndroid Build Coastguard Worker * ts must not be null
293*49cdfc7eSAndroid Build Coastguard Worker */
294*49cdfc7eSAndroid Build Coastguard Worker void ts_normalize(struct timespec *ts);
295*49cdfc7eSAndroid Build Coastguard Worker
296*49cdfc7eSAndroid Build Coastguard Worker /* convert nanoseconds to a timespec
297*49cdfc7eSAndroid Build Coastguard Worker * ts must not be null
298*49cdfc7eSAndroid Build Coastguard Worker */
299*49cdfc7eSAndroid Build Coastguard Worker void nsec_to_ts(nsec_t ns, struct timespec *ts);
300*49cdfc7eSAndroid Build Coastguard Worker
301*49cdfc7eSAndroid Build Coastguard Worker /* convert a timespec to nanoseconds
302*49cdfc7eSAndroid Build Coastguard Worker * ts must not be null
303*49cdfc7eSAndroid Build Coastguard Worker */
304*49cdfc7eSAndroid Build Coastguard Worker int ts_to_nsec(struct timespec *ts, nsec_t *ns);
305*49cdfc7eSAndroid Build Coastguard Worker
306*49cdfc7eSAndroid Build Coastguard Worker /* return difference in microseconds */
307*49cdfc7eSAndroid Build Coastguard Worker unsigned long long tsc_minus(unsigned long long tsc_start, unsigned long long tsc_end);
308*49cdfc7eSAndroid Build Coastguard Worker
309*49cdfc7eSAndroid Build Coastguard Worker /* rt_nanosleep: sleep for ns nanoseconds using clock_nanosleep
310*49cdfc7eSAndroid Build Coastguard Worker */
311*49cdfc7eSAndroid Build Coastguard Worker void rt_nanosleep(nsec_t ns);
312*49cdfc7eSAndroid Build Coastguard Worker
313*49cdfc7eSAndroid Build Coastguard Worker /* rt_nanosleep: sleep until absolute time ns given in
314*49cdfc7eSAndroid Build Coastguard Worker * nanoseconds using clock_nanosleep
315*49cdfc7eSAndroid Build Coastguard Worker */
316*49cdfc7eSAndroid Build Coastguard Worker void rt_nanosleep_until(nsec_t ns);
317*49cdfc7eSAndroid Build Coastguard Worker
318*49cdfc7eSAndroid Build Coastguard Worker /* rt_gettime: get CLOCK_MONOTONIC time in nanoseconds
319*49cdfc7eSAndroid Build Coastguard Worker */
320*49cdfc7eSAndroid Build Coastguard Worker nsec_t rt_gettime(void);
321*49cdfc7eSAndroid Build Coastguard Worker
322*49cdfc7eSAndroid Build Coastguard Worker /* busy_work_ms: do busy work for ms milliseconds
323*49cdfc7eSAndroid Build Coastguard Worker */
324*49cdfc7eSAndroid Build Coastguard Worker void *busy_work_ms(int ms);
325*49cdfc7eSAndroid Build Coastguard Worker
326*49cdfc7eSAndroid Build Coastguard Worker /* busy_work_us: do busy work for us microseconds
327*49cdfc7eSAndroid Build Coastguard Worker */
328*49cdfc7eSAndroid Build Coastguard Worker void *busy_work_us(int us);
329*49cdfc7eSAndroid Build Coastguard Worker
330*49cdfc7eSAndroid Build Coastguard Worker /* init_pi_mutex: initialize a pthread mutex to have PI support
331*49cdfc7eSAndroid Build Coastguard Worker */
332*49cdfc7eSAndroid Build Coastguard Worker void init_pi_mutex(pthread_mutex_t *m);
333*49cdfc7eSAndroid Build Coastguard Worker
334*49cdfc7eSAndroid Build Coastguard Worker /* latency_trace_enable: Enable latency tracing via sysctls.
335*49cdfc7eSAndroid Build Coastguard Worker */
336*49cdfc7eSAndroid Build Coastguard Worker void latency_trace_enable(void);
337*49cdfc7eSAndroid Build Coastguard Worker
338*49cdfc7eSAndroid Build Coastguard Worker /* latency_trace_start: Start tracing latency; call before running test.
339*49cdfc7eSAndroid Build Coastguard Worker */
340*49cdfc7eSAndroid Build Coastguard Worker void latency_trace_start(void);
341*49cdfc7eSAndroid Build Coastguard Worker
342*49cdfc7eSAndroid Build Coastguard Worker /* latency_trace_stop: Stop tracing latency; call immediately after observing
343*49cdfc7eSAndroid Build Coastguard Worker * excessive latency and stopping test.
344*49cdfc7eSAndroid Build Coastguard Worker */
345*49cdfc7eSAndroid Build Coastguard Worker void latency_trace_stop(void);
346*49cdfc7eSAndroid Build Coastguard Worker
347*49cdfc7eSAndroid Build Coastguard Worker /* latency_trace_print: Print latency trace information from
348*49cdfc7eSAndroid Build Coastguard Worker * /proc/latency_trace.
349*49cdfc7eSAndroid Build Coastguard Worker */
350*49cdfc7eSAndroid Build Coastguard Worker void latency_trace_print(void);
351*49cdfc7eSAndroid Build Coastguard Worker
352*49cdfc7eSAndroid Build Coastguard Worker #endif /* LIBRTTEST_H */
353