On Mon, Apr 26, 2021 at 1:24 AM Pure White <wu.purewh...@gmail.com> wrote:
>
> 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?

The right way is to use cgo.  But probably the cost of making the cgo
call will overwhelm the speed advantage of using the coarse clocks.

Using go:linkname to call systemstack is completely unsupported, and
is even less maintainable than your first method.

I have to admit that I don't see a way to do this at all.  What is the
application?  How much difference will it make to use the coarse
timestamps?

Ian

-- 
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/CAOyqgcWZUduYZDRfBZTcN3%2BrK%3DYCyt8fKAY2w2P%3DHHh--W1Tjg%40mail.gmail.com.

Reply via email to