]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: make runtime.GC() trigger GC even if GOGC=off
authorAustin Clements <austin@google.com>
Mon, 25 Sep 2017 18:58:13 +0000 (14:58 -0400)
committerAustin Clements <austin@google.com>
Tue, 26 Sep 2017 21:55:05 +0000 (21:55 +0000)
Currently, the priority of checks in (gcTrigger).test() puts the
gcpercent<0 test above gcTriggerCycle, which is used for runtime.GC().
This is an unintentional change from 1.8 and before, where
runtime.GC() triggered a GC even if GOGC=off.

Fix this by rearranging the priority so the gcTriggerCycle test
executes even if gcpercent < 0.

Fixes #22023.

Change-Id: I109328d7b643b6824eb9d79061a9e775f0149575
Reviewed-on: https://go-review.googlesource.com/65994
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rick Hudson <rlh@golang.org>
src/runtime/gc_test.go
src/runtime/mgc.go

index 03acc8aaa67c044ecd5748a7c8491c6edb45cc20..ece5641e1f9e64d8f5e39856dc5b4fc9b2fb0ed8 100644 (file)
@@ -499,3 +499,19 @@ func BenchmarkReadMemStats(b *testing.B) {
 
        hugeSink = nil
 }
+
+func TestUserForcedGC(t *testing.T) {
+       // Test that runtime.GC() triggers a GC even if GOGC=off.
+       defer debug.SetGCPercent(debug.SetGCPercent(-1))
+
+       var ms1, ms2 runtime.MemStats
+       runtime.ReadMemStats(&ms1)
+       runtime.GC()
+       runtime.ReadMemStats(&ms2)
+       if ms1.NumGC == ms2.NumGC {
+               t.Fatalf("runtime.GC() did not trigger GC")
+       }
+       if ms1.NumForcedGC == ms2.NumForcedGC {
+               t.Fatalf("runtime.GC() was not accounted in NumForcedGC")
+       }
+}
index 8118be9e212821063aa73cfb91f54b9d26c81171..48ccfe8df2b964e0a8d31e6e9ceb51f20fb7a8cf 100644 (file)
@@ -1158,7 +1158,7 @@ func (t gcTrigger) test() bool {
        if t.kind == gcTriggerAlways {
                return true
        }
-       if gcphase != _GCoff || gcpercent < 0 {
+       if gcphase != _GCoff {
                return false
        }
        switch t.kind {
@@ -1169,6 +1169,9 @@ func (t gcTrigger) test() bool {
                // own write.
                return memstats.heap_live >= memstats.gc_trigger
        case gcTriggerTime:
+               if gcpercent < 0 {
+                       return false
+               }
                lastgc := int64(atomic.Load64(&memstats.last_gc_nanotime))
                return lastgc != 0 && t.now-lastgc > forcegcperiod
        case gcTriggerCycle: