[Aside: please don't paste images or coloured text. They're very hard to 
read]

It's perfectly normal for your code to be interrupted once every 10ms or so 
for async preemption. You'll see a load of system calls, but it should not 
be making your code run "slowly".  If you find that "most of the time" is 
spent on async preempt, it could mean that your program is idle for the 
rest of the time - e.g. it's waiting for something else, like a response 
from a remote network service.

However if you do find a reproducible case where the CPU is *spinning* and 
spending all its time in async preempt, then that would be of interest.

P.S. It used to be possible to turn off async preemption completely with 
environment variable
GODEBUG=asyncpreemptoff=1

I don't know if this still has any effect.

On Wednesday, 15 March 2023 at 02:48:13 UTC Landon Cooper wrote:

> 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/85acd629-4c30-4330-802d-544076fccc96n%40googlegroups.com.

Reply via email to