Skip to content

Commit

Permalink
add walltime for linux amd64/arm64
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Jul 2, 2024
1 parent 183004f commit 0d41c20
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions walltime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build (linux && amd64) || (linux && arm64)

package log

func walltime() (sec int64, nsec int32)
26 changes: 26 additions & 0 deletions walltime.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "textflag.h"

#ifdef GOOS_linux
#ifdef GOARCH_amd64
// func walltime() (sec int64, nsec int32)
TEXT ·walltime(SB), NOSPLIT, $24-12
CALL runtime·nanotime1(SB)
MOVQ AX, ret+0(FP) // sec
MOVL DX, ret+8(FP) // nsec
RET
#endif
#endif

#ifdef GOOS_linux
#ifdef GOARCH_arm64
// func walltime() (sec int64, nsec int32)
TEXT ·walltime(SB), NOSPLIT, $24-12
// Call runtime.walltime
CALL runtime·walltime(SB)
// Get the return values
MOVD R3, ret+0(FP) // sec
MOVW R5, ret+8(FP) // nsec
RET
#endif
#endif

16 changes: 16 additions & 0 deletions walltime_safe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build !((linux && amd64) || (linux && arm64))

package log

import (
_ "unsafe"
)

//go:noescape
//go:linkname time_now time.now
func time_now() (sec int64, nsec int32, mono int64)

func walltime() (sec int64, nsec int32) {
sec, nsec, _ = time_now()
return
}
11 changes: 11 additions & 0 deletions walltime_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package log

import (
"testing"
"time"
)

func TestWalltime(t *testing.T) {
sec, nsec := walltime()
t.Log(time.Unix(sec, int64(nsec)))
}

0 comments on commit 0d41c20

Please sign in to comment.