Add Maintainer and Mailing list contact information to README
[ust.git] / include / ust / clock.h
CommitLineData
518d7abb
PMF
1/* Copyright (C) 2010 Pierre-Marc Fournier
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
9c6bb081
JD
18#ifndef _UST_CLOCK_H
19#define _UST_CLOCK_H
518d7abb 20
ca4525b5 21#include <time.h>
518d7abb
PMF
22#include <sys/time.h>
23#include <ust/kcompat/kcompat.h>
24
25/* TRACE CLOCK */
26
27/* There are two types of clocks that can be used.
28 - TSC based clock
29 - gettimeofday() clock
30
31 Microbenchmarks on Linux 2.6.30 on Core2 Duo 3GHz (functions are inlined):
9c6bb081
JD
32 Calls (100000000) to tsc(): 4004035641 cycles or 40 cycles/call
33 Calls (100000000) to gettimeofday(): 9723158352 cycles or 97 cycles/call
518d7abb
PMF
34
35 For merging traces with the kernel, a time source compatible with that of
36 the kernel is necessary.
37
ca4525b5
PMF
38 Instead of gettimeofday(), we are now using clock_gettime for better
39 precision and monotonicity.
518d7abb
PMF
40*/
41
9c6bb081
JD
42/* Only available for x86 arch */
43#define CLOCK_TRACE_FREQ 14
44#define CLOCK_TRACE 15
45union lttng_timespec {
ca4525b5 46 struct timespec ts;
9c6bb081
JD
47 u64 lttng_ts;
48};
518d7abb 49
08b070db 50extern int ust_clock_source;
518d7abb 51
9c6bb081 52/* Choosing correct trace clock */
518d7abb 53
9c6bb081
JD
54static __inline__ u64 trace_clock_read64(void)
55{
56 struct timespec ts;
57 u64 retval;
58 union lttng_timespec *lts = (union lttng_timespec *) &ts;
59
60 clock_gettime(ust_clock_source, &ts);
61 /*
62 * Clock source can change when loading the binary (tracectl.c)
63 * so we must check if the clock source has changed before
64 * returning the correct value
65 */
66 if (likely(ust_clock_source == CLOCK_TRACE)) {
67 retval = lts->lttng_ts;
68 } else { /* CLOCK_MONOTONIC */
69 retval = ts.tv_sec;
70 retval *= 1000000000;
71 retval += ts.tv_nsec;
72 }
73
74 return retval;
75}
76
772d939d 77#if __i386__ || __x86_64__
518d7abb
PMF
78static __inline__ u64 trace_clock_frequency(void)
79{
9c6bb081
JD
80 struct timespec ts;
81 union lttng_timespec *lts = (union lttng_timespec *) &ts;
82
83 if (likely(ust_clock_source == CLOCK_TRACE)) {
84 clock_gettime(CLOCK_TRACE_FREQ, &ts);
85 return lts->lttng_ts;
86 }
ca4525b5 87 return 1000000000LL;
518d7abb 88}
772d939d
MD
89#else /* #if __i386__ || __x86_64__ */
90static __inline__ u64 trace_clock_frequency(void)
91{
92 return 1000000000LL;
93}
94#endif /* #else #if __i386__ || __x86_64__ */
518d7abb
PMF
95
96static __inline__ u32 trace_clock_freq_scale(void)
97{
98 return 1;
99}
100
9c6bb081 101#endif /* _UST_CLOCK_H */
This page took 0.03381 seconds and 4 git commands to generate.