This feels like a "XY Problem" <https://xyproblem.info/> but probably is not an example of that class of problems. Nonetheless, as Ian points out, the Go community can probably provide more useful answers if given more context regarding why you feel the need for this optimization. In my experience applications that call the equivalent of `gettimeofday()` so frequently that function call is a bottleneck tend to have more serious problems. See also questions such as https://stackoverflow.com/questions/58189790/do-clock-monotonic-and-clock-monotonic-coarse-have-the-same-base .
On Mon, Apr 26, 2021 at 1:24 AM Pure White <wu.purewh...@gmail.com> wrote: > Hi all, > > I'm trying to get time using `CLOCK_REALTIME_COARSE` and > `CLOCK_MONOTONIC_COARSE` for performance reasons, and need to use vdso call > by hand-written assembly code. That is, I want to reimplement `time.Now` > using `CLOCK_REALTIME_COARSE` and `CLOCK_MONOTONIC_COARSE`. > > I referenced the code in runtime and found that there's an issue #20427 > indicates that I need to switch to g0 for vdso calls, so I tried two > methods but neither is good. > > ## The first method > > The first method I tried is just copy the code in runtime and simply > change the clockid, but this requires copying all the runtime type > definations as well to make the compiler generate "go_asm.h" for me. > > The code runs well, but this is really ugly and unmaintainable as the type > definations may change across different go versions. > > ## The second method > > The second method I tried is to link the `runtime.systemstack` and use it > to do vdso calls: > ```go > //go:linkname systemstack runtime.systemstack > //go:noescape > func systemstack(fn func()) > ``` > > My code is something like this: > > ```go > // now calls vdso and is implemented in asm > func now() (sec int64, nsec int32, mono int64) > > func Now() { > var sec, mono int64 > var nsec int32 > systemstack(func() { > sec, nsec, mono = now() > }) > ... // logic copied from time.Now() > } > ``` > > The code runs well without `-race`(test isn't enough), but I encountered > fatal error under `-race` mode. > For detailed information, I've filed an issue: > https://github.com/golang/go/issues/45768. > > ## The right way? > > So I really want to know what is the right way to do vdso call outside > runtime? > > Thanks very much! > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/49e183dc-4f13-4627-b0ae-cab2c15ae931n%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/49e183dc-4f13-4627-b0ae-cab2c15ae931n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- Kurtis Rader Caretaker of the exceptional canines Junior and Hank -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD-o%3DbP6DB-zxoS8NTi8Yd7Cim3M02cYzG%2B2SVHZTWd%2BBA%40mail.gmail.com.