-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: convert kernel monotonic times to wall time (#268)
# Description Adjusts timestamps from eBPF that use the kernel monotonic timer to UTC during Flow ingestion. This is done on a best-effort basis, because it is impossible to sample from the monotonic timer and the wall-clock at the same instant. The difference in the time it takes to execute these instructions should be small enough for our purposes in practice. ## Related Issue Fixes #204 ## Checklist - [x] I have read the [contributing documentation](https://retina.sh/docs/contributing). - [x] I signed and signed-off the commits (`git commit -S -s ...`). See [this documentation](https://docs.github.com/en/authentication/managing-commit-signature-verification/about-commit-signature-verification) on signing commits. - [x] I have correctly attributed the author(s) of the code. - [ ] I have tested the changes locally. - [x] I have followed the project's style guidelines. - [x] I have updated the documentation, if necessary. - [x] I have added tests, if applicable. ## Screenshots (if applicable) or Testing Completed Please add any relevant screenshots or GIFs to showcase the changes made. ## Additional Notes This is effectively only implemented for Unix builds as I don't know how Windows ktime behaves (and we're only compiling these plugins for Linux at the moment). It can be implemented on !unix in the future as necessary. --- Please refer to the [CONTRIBUTING.md](../CONTRIBUTING.md) file for more information on how to contribute to this project. Signed-off-by: Evan Baker <rbtr@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
57 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ktime | ||
|
||
// MonotonicOffset is the calculated skew between the kernel's | ||
// monotonic timer and UTC. | ||
// | ||
// If clock readings were instantaneous, this would mean that | ||
// MonotonicTimer - MonotonicOFfset = the UTC Boot Time, but | ||
// that is idealized and there will be some small errror. | ||
var MonotonicOffset = calculateMonotonicOffset() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//go:build !unix | ||
|
||
package ktime | ||
|
||
import "time" | ||
|
||
func calculateMonotonicOffset() time.Duration { | ||
return 0 * time.Nanosecond | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//go:build unix | ||
|
||
package ktime | ||
|
||
import ( | ||
"time" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// calculateMonotonicOffset tries to determine the offset of the kernel's | ||
// monotonic clock from UTC so that measurements from eBPF using the | ||
// monotonic clock timestamp may be adjusted to wall-time. | ||
// | ||
// These instructions do not execute instantaneously so it will always be | ||
// impossible to sample both clocks at exactly the same time. | ||
// This means that for any single process there will be constant error in | ||
// the accuracy of this measurement despite the nanosecond-level precision | ||
// of the individual clocks. | ||
func calculateMonotonicOffset() time.Duration { | ||
mono := &unix.Timespec{} | ||
now := time.Now() | ||
_ = unix.ClockGettime(unix.CLOCK_BOOTTIME, mono) | ||
return time.Duration(now.UnixNano() - unix.TimespecToNsec(*mono)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters