Skip to content

Commit afde80f

Browse files
committed
experiment with getclock syscall
1 parent 03571df commit afde80f

File tree

7 files changed

+56
-13
lines changed

7 files changed

+56
-13
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module github.com/jschaf/observe
22

33
go 1.24.2
4+

internal/epoch/nanos.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ func NewNanos(t time.Time) Nanos {
1919
return Nanos(t.UnixNano())
2020
}
2121

22-
// NanosNow returns the current time in nanoseconds since the epoch.
23-
func NanosNow() Nanos { return Nanos(time.Now().UnixNano()) }
24-
2522
// ToTime converts the Nanos to a time.Time. If the value is zero, it
2623
// returns the zero-value of time.Time.
2724
//

internal/epoch/now.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//go:build !unix
2+
3+
package epoch
4+
5+
// Now returns the current time in nanoseconds since the epoch.
6+
func Now() Nanos { return Nanos(time.Now().UnixNano()) }

internal/epoch/now_unix.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//go:build unix
2+
3+
package epoch
4+
5+
import (
6+
"syscall"
7+
"time"
8+
)
9+
10+
func Now() Nanos {
11+
var tv syscall.Timeval
12+
err := syscall.Gettimeofday(&tv)
13+
if err != nil {
14+
return Nanos(time.Now().UnixNano())
15+
}
16+
return Nanos(tv.Nano())
17+
}

internal/epoch/now_unix_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package epoch
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
// Benchmarks as of 2025-04-28:
9+
// BenchmarkNow/unix_epoch.Now 72574975 14.64 ns/op
10+
// BenchmarkNow/time_Now 38530129 31.12 ns/op
11+
func BenchmarkNow_unix(b *testing.B) {
12+
b.Run("unix epoch.Now", func(b *testing.B) {
13+
for b.Loop() {
14+
Now()
15+
}
16+
})
17+
b.Run("time Now", func(b *testing.B) {
18+
for b.Loop() {
19+
time.Now().UnixNano()
20+
}
21+
})
22+
}

trace/span.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ func (s *Span) EndTime() time.Time {
4545
}
4646

4747
type spanEndConfig struct {
48-
endTime epoch.Nanos
48+
end epoch.Nanos
4949
}
5050

5151
type SpanEndOption func(*spanEndConfig)
5252

5353
// WithEndTime sets the end time of the span.
5454
func WithEndTime(t time.Time) SpanEndOption {
55-
return func(cfg *spanEndConfig) { cfg.endTime = epoch.NewNanos(t) }
55+
return func(cfg *spanEndConfig) { cfg.end = epoch.NewNanos(t) }
5656
}
5757

5858
// End signals the span has ended.
@@ -65,12 +65,12 @@ func (s *Span) End(opts ...SpanEndOption) {
6565
for _, opt := range opts {
6666
opt(&cfg)
6767
}
68-
if cfg.endTime == 0 {
69-
cfg.endTime = epoch.NanosNow()
68+
if cfg.end == 0 {
69+
cfg.end = epoch.Now()
7070
}
7171

7272
// The first call sets the end time. Ignore all later calls.
73-
if !(&s.end).SwapIfZero(cfg.endTime) {
73+
if !(&s.end).SwapIfZero(cfg.end) {
7474
return
7575
}
7676
}

trace/tracer.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import (
1010
type Tracer struct{}
1111

1212
type startConfig struct {
13-
startTime epoch.Nanos
13+
start epoch.Nanos
1414
}
1515

1616
type SpanStartOption func(*startConfig)
1717

1818
func WithStartTime(t time.Time) SpanStartOption {
19-
return func(cfg *startConfig) { cfg.startTime = epoch.NewNanos(t) }
19+
return func(cfg *startConfig) { cfg.start = epoch.NewNanos(t) }
2020
}
2121

2222
// Start starts a Span and returns a new context containing the Span.
@@ -25,14 +25,14 @@ func (t *Tracer) Start(ctx context.Context, name string, opts ...SpanStartOption
2525
for _, opt := range opts {
2626
opt(&cfg)
2727
}
28-
if cfg.startTime == 0 {
29-
cfg.startTime = epoch.NanosNow()
28+
if cfg.start == 0 {
29+
cfg.start = epoch.Now()
3030
}
3131

3232
span := &Span{
3333
name: name,
3434
tracer: t,
35-
start: cfg.startTime,
35+
start: cfg.start,
3636
}
3737

3838
return ctx, span

0 commit comments

Comments
 (0)