@@ -1913,9 +1913,15 @@ func oneNewExtraM() {
1913
1913
mp .lockedg .set (gp )
1914
1914
mp .isextra = true
1915
1915
gp .lockedm .set (mp )
1916
+ gp .goid = int64 (atomic .Xadd64 (& sched .goidgen , 1 ))
1916
1917
if raceenabled {
1917
1918
gp .racectx = racegostart (abi .FuncPCABIInternal (newextram ) + sys .PCQuantum )
1918
1919
}
1920
+ if trace .enabled {
1921
+ traceGoCreate (gp , 0 ) // no start pc
1922
+ gp .traceseq ++
1923
+ traceEvent (traceEvGoInSyscall , - 1 , uint64 (gp .goid ))
1924
+ }
1919
1925
// put on allg for garbage collector
1920
1926
allgadd (gp )
1921
1927
@@ -2509,15 +2515,12 @@ func execute(gp *g, inheritTime bool) {
2509
2515
}
2510
2516
2511
2517
if trace .enabled {
2512
- // delay emit trace events when entering go from c thread at the first level.
2513
- if ! _g_ .m .isextra || _g_ .m .cgolevel != 0 {
2514
- if gp .syscallsp != 0 && gp .sysblocktraced {
2515
- // GoSysExit has to happen when we have a P, but before GoStart.
2516
- // So we emit it here.
2517
- traceGoSysExit (gp .sysexitticks )
2518
- }
2519
- traceGoStart ()
2518
+ // GoSysExit has to happen when we have a P, but before GoStart.
2519
+ // So we emit it here.
2520
+ if gp .syscallsp != 0 && gp .sysblocktraced {
2521
+ traceGoSysExit (gp .sysexitticks )
2520
2522
}
2523
+ traceGoStart ()
2521
2524
}
2522
2525
2523
2526
gogo (& gp .sched )
@@ -3611,18 +3614,8 @@ func reentersyscall(pc, sp uintptr) {
3611
3614
})
3612
3615
}
3613
3616
3614
- // the goroutine is finished when it's the locked g from extra M,
3615
- // and it's returning back to c thread.
3616
- backToCThread := _g_ .m .isextra && _g_ .m .cgolevel == 0
3617
3617
if trace .enabled {
3618
- if backToCThread {
3619
- systemstack (func () {
3620
- traceGoEnd ()
3621
- traceProcStop (_g_ .m .p .ptr ())
3622
- })
3623
- } else {
3624
- systemstack (traceGoSysCall )
3625
- }
3618
+ systemstack (traceGoSysCall )
3626
3619
// systemstack itself clobbers g.sched.{pc,sp} and we might
3627
3620
// need them later when the G is genuinely blocked in a
3628
3621
// syscall
@@ -3644,28 +3637,12 @@ func reentersyscall(pc, sp uintptr) {
3644
3637
_g_ .sysblocktraced = true
3645
3638
pp := _g_ .m .p .ptr ()
3646
3639
pp .m = 0
3640
+ _g_ .m .oldp .set (pp )
3647
3641
_g_ .m .p = 0
3648
-
3649
- if backToCThread {
3650
- // For a real syscall, we assume it will back to go after a very short time,
3651
- // it's reasonable in most cases. So, keep the P in _Psyscall is a better choice.
3652
- // But in the c to go scene, we can not assume there will be a short time
3653
- // for the next c to go call, to reuse the oldp in exitsyscall.
3654
- // And the Psyscall status P can only be retake by sysmon after 20us ~ 10ms,
3655
- // it means wasting P.
3656
- // So, it's better to handoffp here.
3657
- atomic .Store (& pp .status , _Pidle )
3658
- systemstack (func () {
3659
- handoffp (pp )
3660
- })
3642
+ atomic .Store (& pp .status , _Psyscall )
3643
+ if sched .gcwaiting != 0 {
3644
+ systemstack (entersyscall_gcwait )
3661
3645
save (pc , sp )
3662
- } else {
3663
- _g_ .m .oldp .set (pp )
3664
- atomic .Store (& pp .status , _Psyscall )
3665
- if sched .gcwaiting != 0 {
3666
- systemstack (entersyscall_gcwait )
3667
- save (pc , sp )
3668
- }
3669
3646
}
3670
3647
3671
3648
_g_ .m .locks --
@@ -3784,11 +3761,8 @@ func exitsyscall() {
3784
3761
_g_ .m .oldp = 0
3785
3762
if exitsyscallfast (oldp ) {
3786
3763
if trace .enabled {
3787
- // delay emit trace events when entering go from c thread at the first level.
3788
- if ! _g_ .m .isextra || _g_ .m .cgolevel != 0 {
3789
- if oldp != _g_ .m .p .ptr () || _g_ .m .syscalltick != _g_ .m .p .ptr ().syscalltick {
3790
- systemstack (traceGoStart )
3791
- }
3764
+ if oldp != _g_ .m .p .ptr () || _g_ .m .syscalltick != _g_ .m .p .ptr ().syscalltick {
3765
+ systemstack (traceGoStart )
3792
3766
}
3793
3767
}
3794
3768
// There's a cpu for us, so we can run.
@@ -3877,10 +3851,7 @@ func exitsyscallfast(oldp *p) bool {
3877
3851
osyield ()
3878
3852
}
3879
3853
}
3880
- // delay emit trace events when entering go from c thread at the first level.
3881
- if ! _g_ .m .isextra || _g_ .m .cgolevel != 0 {
3882
- traceGoSysExit (0 )
3883
- }
3854
+ traceGoSysExit (0 )
3884
3855
}
3885
3856
})
3886
3857
if ok {
@@ -3897,10 +3868,6 @@ func exitsyscallfast(oldp *p) bool {
3897
3868
//go:nosplit
3898
3869
func exitsyscallfast_reacquired () {
3899
3870
_g_ := getg ()
3900
- // there is no oldp when entering go from c thread at the first level.
3901
- if _g_ .m .isextra && _g_ .m .cgolevel == 0 {
3902
- panic ("oldp should not existing" )
3903
- }
3904
3871
if _g_ .m .syscalltick != _g_ .m .p .ptr ().syscalltick {
3905
3872
if trace .enabled {
3906
3873
// The p was retaken and then enter into syscall again (since _g_.m.syscalltick has changed).
@@ -4103,20 +4070,6 @@ func newproc(fn *funcval) {
4103
4070
})
4104
4071
}
4105
4072
4106
- func newgoid (p * p ) int64 {
4107
- if p .goidcache == p .goidcacheend {
4108
- // Sched.goidgen is the last allocated id,
4109
- // this batch must be [sched.goidgen+1, sched.goidgen+GoidCacheBatch].
4110
- // At startup sched.goidgen=0, so main goroutine receives goid=1.
4111
- p .goidcache = atomic .Xadd64 (& sched .goidgen , _GoidCacheBatch )
4112
- p .goidcache -= _GoidCacheBatch - 1
4113
- p .goidcacheend = p .goidcache + _GoidCacheBatch
4114
- }
4115
- id := int64 (p .goidcache )
4116
- p .goidcache ++
4117
- return id
4118
- }
4119
-
4120
4073
// Create a new g in state _Grunnable, starting at fn. callerpc is the
4121
4074
// address of the go statement that created this. The caller is responsible
4122
4075
// for adding the new g to the scheduler.
@@ -4180,7 +4133,16 @@ func newproc1(fn *funcval, callergp *g, callerpc uintptr) *g {
4180
4133
casgstatus (newg , _Gdead , _Grunnable )
4181
4134
gcController .addScannableStack (_p_ , int64 (newg .stack .hi - newg .stack .lo ))
4182
4135
4183
- newg .goid = newgoid (_p_ )
4136
+ if _p_ .goidcache == _p_ .goidcacheend {
4137
+ // Sched.goidgen is the last allocated id,
4138
+ // this batch must be [sched.goidgen+1, sched.goidgen+GoidCacheBatch].
4139
+ // At startup sched.goidgen=0, so main goroutine receives goid=1.
4140
+ _p_ .goidcache = atomic .Xadd64 (& sched .goidgen , _GoidCacheBatch )
4141
+ _p_ .goidcache -= _GoidCacheBatch - 1
4142
+ _p_ .goidcacheend = _p_ .goidcache + _GoidCacheBatch
4143
+ }
4144
+ newg .goid = int64 (_p_ .goidcache )
4145
+ _p_ .goidcache ++
4184
4146
if raceenabled {
4185
4147
newg .racectx = racegostart (callerpc )
4186
4148
}
0 commit comments