diff options
author | Richard Cochran <richardcochran@gmail.com> | 2014-12-21 13:47:06 -0500 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2014-12-30 18:29:27 -0500 |
commit | 2eebdde6528a722fbf8e2cffcf7aa52cbb4c2de0 (patch) | |
tree | c4b176d1e40eecb2caa00952eae95ecb2b2a046b /include/linux/timecounter.h | |
parent | f25a30be359d0535fb1c7c1619cabb0ad17cfbf1 (diff) |
timecounter: keep track of accumulated fractional nanoseconds
The current timecounter implementation will drop a variable amount
of resolution, depending on the magnitude of the time delta. In
other words, reading the clock too often or too close to a time
stamp conversion will introduce errors into the time values. This
patch fixes the issue by introducing a fractional nanosecond field
that accumulates the low order bits.
Reported-by: Janusz Użycki <j.uzycki@elproma.com.pl>
Signed-off-by: Richard Cochran <richardcochran@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'include/linux/timecounter.h')
-rw-r--r-- | include/linux/timecounter.h | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/include/linux/timecounter.h b/include/linux/timecounter.h index af3dfa4e90f0..74f45496e6d1 100644 --- a/include/linux/timecounter.h +++ b/include/linux/timecounter.h | |||
@@ -55,27 +55,32 @@ struct cyclecounter { | |||
55 | * @cycle_last: most recent cycle counter value seen by | 55 | * @cycle_last: most recent cycle counter value seen by |
56 | * timecounter_read() | 56 | * timecounter_read() |
57 | * @nsec: continuously increasing count | 57 | * @nsec: continuously increasing count |
58 | * @mask: bit mask for maintaining the 'frac' field | ||
59 | * @frac: accumulated fractional nanoseconds | ||
58 | */ | 60 | */ |
59 | struct timecounter { | 61 | struct timecounter { |
60 | const struct cyclecounter *cc; | 62 | const struct cyclecounter *cc; |
61 | cycle_t cycle_last; | 63 | cycle_t cycle_last; |
62 | u64 nsec; | 64 | u64 nsec; |
65 | u64 mask; | ||
66 | u64 frac; | ||
63 | }; | 67 | }; |
64 | 68 | ||
65 | /** | 69 | /** |
66 | * cyclecounter_cyc2ns - converts cycle counter cycles to nanoseconds | 70 | * cyclecounter_cyc2ns - converts cycle counter cycles to nanoseconds |
67 | * @cc: Pointer to cycle counter. | 71 | * @cc: Pointer to cycle counter. |
68 | * @cycles: Cycles | 72 | * @cycles: Cycles |
69 | * | 73 | * @mask: bit mask for maintaining the 'frac' field |
70 | * XXX - This could use some mult_lxl_ll() asm optimization. Same code | 74 | * @frac: pointer to storage for the fractional nanoseconds. |
71 | * as in cyc2ns, but with unsigned result. | ||
72 | */ | 75 | */ |
73 | static inline u64 cyclecounter_cyc2ns(const struct cyclecounter *cc, | 76 | static inline u64 cyclecounter_cyc2ns(const struct cyclecounter *cc, |
74 | cycle_t cycles) | 77 | cycle_t cycles, u64 mask, u64 *frac) |
75 | { | 78 | { |
76 | u64 ret = (u64)cycles; | 79 | u64 ns = (u64) cycles; |
77 | ret = (ret * cc->mult) >> cc->shift; | 80 | |
78 | return ret; | 81 | ns = (ns * cc->mult) + *frac; |
82 | *frac = ns & mask; | ||
83 | return ns >> cc->shift; | ||
79 | } | 84 | } |
80 | 85 | ||
81 | /** | 86 | /** |