Hi Cheng, I'm not sure I understand your question, but here's what little I 
know about the preempt function.

runtime.asyncPreempt is used to implement asynchronous preemption. 
Basically, the go runtime scheduler occasionally suspends the execution of 
a goroutine to check if it has been running too long. If it has, it 
"preempts" the routine and passes control to another goroutine. This 
prevents long running goroutines from stealing all the resources from other 
routines.

The following information is probably considered "dangerous" because it 
depends on low level knowledge that is probably going to change in the 
future..

There is a function in the runtime package called GOMAXPROCS which gives 
you some control over the number of system threads used by the go 
scheduler. You might be able to get a tiny bit of control over how often 
the preempt function is called by changing this value.

There's also a LockOSThread function which you can call to lock the routine 
to a thread and prevent the current goroutine from being preempted (the OS 
may still preempt the thread). It's counterpart, UnlockOSThread unlocks the 
routine from the thread so that it can again be used by the scheduler to 
run different goroutines.
On Tuesday, March 14, 2023 at 8:18:34 PM UTC-6 cheng dong wrote:

> here is the go env
>
> `
> GO111MODULE="on"
> GOARCH="amd64"
> GOBIN=""
> GOCACHE="/home/dongcheng/.cache/go-build"
> GOENV="/home/dongcheng/.config/go/env"
> GOEXE=""
> GOEXPERIMENT=""
> GOFLAGS=""
> GOHOSTARCH="amd64"
> GOHOSTOS="linux"
> GOINSECURE=""
> GONOPROXY=""
> GONOSUMDB=""
> GOOS="linux"
> GOPRIVATE=""
> GOPROXY="https://goproxy.cn,direct";
> GOROOT="/usr/local/go1.18"
> GOSUMDB="sum.golang.org"
> GOTMPDIR=""
> GOTOOLDIR="/usr/local/go1.18/pkg/tool/linux_amd64"
> GOVCS=""
> GOVERSION="go1.18.1"
> GCCGO="gccgo"
> GOAMD64="v1"
> AR="ar"
> CC="gcc"
> CXX="g++"
> CGO_ENABLED="1"
> GOWORK=""
> CGO_CFLAGS="-g -O2"
> CGO_CPPFLAGS=""
> CGO_CXXFLAGS="-g -O2"
> CGO_FFLAGS="-g -O2"
> CGO_LDFLAGS="-g -O2"
> PKG_CONFIG="pkg-config"
> GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 
> -fdebug-prefix-map=/tmp/go-build3091739208=/tmp/go-build 
> -gno-record-gcc-switches"
> `
>
> attachment is the profile data
> On Wednesday, March 15, 2023 at 10:11:40 AM UTC+8 cheng dong wrote:
>
>> my code run some logic every second in a Tick function, sometimes i could 
>> be very slow(like 5x time), i profile the process and found most of time is 
>> doing runtime.asyncPreempt. however in "source view", these asyncPreempt 
>> disappeared.
>>
>> [image: profile315.jpg]
>>
>

-- 
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/96317d77-d01d-48c8-9a2a-8af472750396n%40googlegroups.com.

Reply via email to