github.com/x04/go/src@v0.0.0-20200202162449-3d481ceb3525/runtime/gc_test.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package runtime_test 6 7 import ( 8 "github.com/x04/go/src/fmt" 9 "github.com/x04/go/src/os" 10 "github.com/x04/go/src/reflect" 11 "github.com/x04/go/src/runtime" 12 "github.com/x04/go/src/runtime/debug" 13 "github.com/x04/go/src/sync" 14 "github.com/x04/go/src/sync/atomic" 15 "github.com/x04/go/src/testing" 16 "github.com/x04/go/src/time" 17 "github.com/x04/go/src/unsafe" 18 ) 19 20 func TestGcSys(t *testing.T) { 21 if os.Getenv("GOGC") == "off" { 22 t.Skip("skipping test; GOGC=off in environment") 23 } 24 got := runTestProg(t, "testprog", "GCSys") 25 want := "OK\n" 26 if got != want { 27 t.Fatalf("expected %q, but got %q", want, got) 28 } 29 } 30 31 func TestGcDeepNesting(t *testing.T) { 32 type T [2][2][2][2][2][2][2][2][2][2]*int 33 a := new(T) 34 35 // Prevent the compiler from applying escape analysis. 36 // This makes sure new(T) is allocated on heap, not on the stack. 37 t.Logf("%p", a) 38 39 a[0][0][0][0][0][0][0][0][0][0] = new(int) 40 *a[0][0][0][0][0][0][0][0][0][0] = 13 41 runtime.GC() 42 if *a[0][0][0][0][0][0][0][0][0][0] != 13 { 43 t.Fail() 44 } 45 } 46 47 func TestGcMapIndirection(t *testing.T) { 48 defer debug.SetGCPercent(debug.SetGCPercent(1)) 49 runtime.GC() 50 type T struct { 51 a [256]int 52 } 53 m := make(map[T]T) 54 for i := 0; i < 2000; i++ { 55 var a T 56 a.a[0] = i 57 m[a] = T{} 58 } 59 } 60 61 func TestGcArraySlice(t *testing.T) { 62 type X struct { 63 buf [1]byte 64 nextbuf []byte 65 next *X 66 } 67 var head *X 68 for i := 0; i < 10; i++ { 69 p := &X{} 70 p.buf[0] = 42 71 p.next = head 72 if head != nil { 73 p.nextbuf = head.buf[:] 74 } 75 head = p 76 runtime.GC() 77 } 78 for p := head; p != nil; p = p.next { 79 if p.buf[0] != 42 { 80 t.Fatal("corrupted heap") 81 } 82 } 83 } 84 85 func TestGcRescan(t *testing.T) { 86 type X struct { 87 c chan error 88 nextx *X 89 } 90 type Y struct { 91 X 92 nexty *Y 93 p *int 94 } 95 var head *Y 96 for i := 0; i < 10; i++ { 97 p := &Y{} 98 p.c = make(chan error) 99 if head != nil { 100 p.nextx = &head.X 101 } 102 p.nexty = head 103 p.p = new(int) 104 *p.p = 42 105 head = p 106 runtime.GC() 107 } 108 for p := head; p != nil; p = p.nexty { 109 if *p.p != 42 { 110 t.Fatal("corrupted heap") 111 } 112 } 113 } 114 115 func TestGcLastTime(t *testing.T) { 116 ms := new(runtime.MemStats) 117 t0 := time.Now().UnixNano() 118 runtime.GC() 119 t1 := time.Now().UnixNano() 120 runtime.ReadMemStats(ms) 121 last := int64(ms.LastGC) 122 if t0 > last || last > t1 { 123 t.Fatalf("bad last GC time: got %v, want [%v, %v]", last, t0, t1) 124 } 125 pause := ms.PauseNs[(ms.NumGC+255)%256] 126 // Due to timer granularity, pause can actually be 0 on windows 127 // or on virtualized environments. 128 if pause == 0 { 129 t.Logf("last GC pause was 0") 130 } else if pause > 10e9 { 131 t.Logf("bad last GC pause: got %v, want [0, 10e9]", pause) 132 } 133 } 134 135 var hugeSink interface{} 136 137 func TestHugeGCInfo(t *testing.T) { 138 // The test ensures that compiler can chew these huge types even on weakest machines. 139 // The types are not allocated at runtime. 140 if hugeSink != nil { 141 // 400MB on 32 bots, 4TB on 64-bits. 142 const n = (400 << 20) + (unsafe.Sizeof(uintptr(0))-4)<<40 143 hugeSink = new([n]*byte) 144 hugeSink = new([n]uintptr) 145 hugeSink = new(struct { 146 x float64 147 y [n]*byte 148 z []string 149 }) 150 hugeSink = new(struct { 151 x float64 152 y [n]uintptr 153 z []string 154 }) 155 } 156 } 157 158 func TestPeriodicGC(t *testing.T) { 159 if runtime.GOARCH == "wasm" { 160 t.Skip("no sysmon on wasm yet") 161 } 162 163 // Make sure we're not in the middle of a GC. 164 runtime.GC() 165 166 var ms1, ms2 runtime.MemStats 167 runtime.ReadMemStats(&ms1) 168 169 // Make periodic GC run continuously. 170 orig := *runtime.ForceGCPeriod 171 *runtime.ForceGCPeriod = 0 172 173 // Let some periodic GCs happen. In a heavily loaded system, 174 // it's possible these will be delayed, so this is designed to 175 // succeed quickly if things are working, but to give it some 176 // slack if things are slow. 177 var numGCs uint32 178 const want = 2 179 for i := 0; i < 200 && numGCs < want; i++ { 180 time.Sleep(5 * time.Millisecond) 181 182 // Test that periodic GC actually happened. 183 runtime.ReadMemStats(&ms2) 184 numGCs = ms2.NumGC - ms1.NumGC 185 } 186 *runtime.ForceGCPeriod = orig 187 188 if numGCs < want { 189 t.Fatalf("no periodic GC: got %v GCs, want >= 2", numGCs) 190 } 191 } 192 193 func BenchmarkSetTypePtr(b *testing.B) { 194 benchSetType(b, new(*byte)) 195 } 196 197 func BenchmarkSetTypePtr8(b *testing.B) { 198 benchSetType(b, new([8]*byte)) 199 } 200 201 func BenchmarkSetTypePtr16(b *testing.B) { 202 benchSetType(b, new([16]*byte)) 203 } 204 205 func BenchmarkSetTypePtr32(b *testing.B) { 206 benchSetType(b, new([32]*byte)) 207 } 208 209 func BenchmarkSetTypePtr64(b *testing.B) { 210 benchSetType(b, new([64]*byte)) 211 } 212 213 func BenchmarkSetTypePtr126(b *testing.B) { 214 benchSetType(b, new([126]*byte)) 215 } 216 217 func BenchmarkSetTypePtr128(b *testing.B) { 218 benchSetType(b, new([128]*byte)) 219 } 220 221 func BenchmarkSetTypePtrSlice(b *testing.B) { 222 benchSetType(b, make([]*byte, 1<<10)) 223 } 224 225 type Node1 struct { 226 Value [1]uintptr 227 Left, Right *byte 228 } 229 230 func BenchmarkSetTypeNode1(b *testing.B) { 231 benchSetType(b, new(Node1)) 232 } 233 234 func BenchmarkSetTypeNode1Slice(b *testing.B) { 235 benchSetType(b, make([]Node1, 32)) 236 } 237 238 type Node8 struct { 239 Value [8]uintptr 240 Left, Right *byte 241 } 242 243 func BenchmarkSetTypeNode8(b *testing.B) { 244 benchSetType(b, new(Node8)) 245 } 246 247 func BenchmarkSetTypeNode8Slice(b *testing.B) { 248 benchSetType(b, make([]Node8, 32)) 249 } 250 251 type Node64 struct { 252 Value [64]uintptr 253 Left, Right *byte 254 } 255 256 func BenchmarkSetTypeNode64(b *testing.B) { 257 benchSetType(b, new(Node64)) 258 } 259 260 func BenchmarkSetTypeNode64Slice(b *testing.B) { 261 benchSetType(b, make([]Node64, 32)) 262 } 263 264 type Node64Dead struct { 265 Left, Right *byte 266 Value [64]uintptr 267 } 268 269 func BenchmarkSetTypeNode64Dead(b *testing.B) { 270 benchSetType(b, new(Node64Dead)) 271 } 272 273 func BenchmarkSetTypeNode64DeadSlice(b *testing.B) { 274 benchSetType(b, make([]Node64Dead, 32)) 275 } 276 277 type Node124 struct { 278 Value [124]uintptr 279 Left, Right *byte 280 } 281 282 func BenchmarkSetTypeNode124(b *testing.B) { 283 benchSetType(b, new(Node124)) 284 } 285 286 func BenchmarkSetTypeNode124Slice(b *testing.B) { 287 benchSetType(b, make([]Node124, 32)) 288 } 289 290 type Node126 struct { 291 Value [126]uintptr 292 Left, Right *byte 293 } 294 295 func BenchmarkSetTypeNode126(b *testing.B) { 296 benchSetType(b, new(Node126)) 297 } 298 299 func BenchmarkSetTypeNode126Slice(b *testing.B) { 300 benchSetType(b, make([]Node126, 32)) 301 } 302 303 type Node128 struct { 304 Value [128]uintptr 305 Left, Right *byte 306 } 307 308 func BenchmarkSetTypeNode128(b *testing.B) { 309 benchSetType(b, new(Node128)) 310 } 311 312 func BenchmarkSetTypeNode128Slice(b *testing.B) { 313 benchSetType(b, make([]Node128, 32)) 314 } 315 316 type Node130 struct { 317 Value [130]uintptr 318 Left, Right *byte 319 } 320 321 func BenchmarkSetTypeNode130(b *testing.B) { 322 benchSetType(b, new(Node130)) 323 } 324 325 func BenchmarkSetTypeNode130Slice(b *testing.B) { 326 benchSetType(b, make([]Node130, 32)) 327 } 328 329 type Node1024 struct { 330 Value [1024]uintptr 331 Left, Right *byte 332 } 333 334 func BenchmarkSetTypeNode1024(b *testing.B) { 335 benchSetType(b, new(Node1024)) 336 } 337 338 func BenchmarkSetTypeNode1024Slice(b *testing.B) { 339 benchSetType(b, make([]Node1024, 32)) 340 } 341 342 func benchSetType(b *testing.B, x interface{}) { 343 v := reflect.ValueOf(x) 344 t := v.Type() 345 switch t.Kind() { 346 case reflect.Ptr: 347 b.SetBytes(int64(t.Elem().Size())) 348 case reflect.Slice: 349 b.SetBytes(int64(t.Elem().Size()) * int64(v.Len())) 350 } 351 b.ResetTimer() 352 runtime.BenchSetType(b.N, x) 353 } 354 355 func BenchmarkAllocation(b *testing.B) { 356 type T struct { 357 x, y *byte 358 } 359 ngo := runtime.GOMAXPROCS(0) 360 work := make(chan bool, b.N+ngo) 361 result := make(chan *T) 362 for i := 0; i < b.N; i++ { 363 work <- true 364 } 365 for i := 0; i < ngo; i++ { 366 work <- false 367 } 368 for i := 0; i < ngo; i++ { 369 go func() { 370 var x *T 371 for <-work { 372 for i := 0; i < 1000; i++ { 373 x = &T{} 374 } 375 } 376 result <- x 377 }() 378 } 379 for i := 0; i < ngo; i++ { 380 <-result 381 } 382 } 383 384 func TestPrintGC(t *testing.T) { 385 if testing.Short() { 386 t.Skip("Skipping in short mode") 387 } 388 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(2)) 389 done := make(chan bool) 390 go func() { 391 for { 392 select { 393 case <-done: 394 return 395 default: 396 runtime.GC() 397 } 398 } 399 }() 400 for i := 0; i < 1e4; i++ { 401 func() { 402 defer print("") 403 }() 404 } 405 close(done) 406 } 407 408 func testTypeSwitch(x interface{}) error { 409 switch y := x.(type) { 410 case nil: 411 // ok 412 case error: 413 return y 414 } 415 return nil 416 } 417 418 func testAssert(x interface{}) error { 419 if y, ok := x.(error); ok { 420 return y 421 } 422 return nil 423 } 424 425 func testAssertVar(x interface{}) error { 426 var y, ok = x.(error) 427 if ok { 428 return y 429 } 430 return nil 431 } 432 433 var a bool 434 435 //go:noinline 436 func testIfaceEqual(x interface{}) { 437 if x == "abc" { 438 a = true 439 } 440 } 441 442 func TestPageAccounting(t *testing.T) { 443 // Grow the heap in small increments. This used to drop the 444 // pages-in-use count below zero because of a rounding 445 // mismatch (golang.org/issue/15022). 446 const blockSize = 64 << 10 447 blocks := make([]*[blockSize]byte, (64<<20)/blockSize) 448 for i := range blocks { 449 blocks[i] = new([blockSize]byte) 450 } 451 452 // Check that the running page count matches reality. 453 pagesInUse, counted := runtime.CountPagesInUse() 454 if pagesInUse != counted { 455 t.Fatalf("mheap_.pagesInUse is %d, but direct count is %d", pagesInUse, counted) 456 } 457 } 458 459 func TestReadMemStats(t *testing.T) { 460 base, slow := runtime.ReadMemStatsSlow() 461 if base != slow { 462 logDiff(t, "MemStats", reflect.ValueOf(base), reflect.ValueOf(slow)) 463 t.Fatal("memstats mismatch") 464 } 465 } 466 467 func logDiff(t *testing.T, prefix string, got, want reflect.Value) { 468 typ := got.Type() 469 switch typ.Kind() { 470 case reflect.Array, reflect.Slice: 471 if got.Len() != want.Len() { 472 t.Logf("len(%s): got %v, want %v", prefix, got, want) 473 return 474 } 475 for i := 0; i < got.Len(); i++ { 476 logDiff(t, fmt.Sprintf("%s[%d]", prefix, i), got.Index(i), want.Index(i)) 477 } 478 case reflect.Struct: 479 for i := 0; i < typ.NumField(); i++ { 480 gf, wf := got.Field(i), want.Field(i) 481 logDiff(t, prefix+"."+typ.Field(i).Name, gf, wf) 482 } 483 case reflect.Map: 484 t.Fatal("not implemented: logDiff for map") 485 default: 486 if got.Interface() != want.Interface() { 487 t.Logf("%s: got %v, want %v", prefix, got, want) 488 } 489 } 490 } 491 492 func BenchmarkReadMemStats(b *testing.B) { 493 var ms runtime.MemStats 494 const heapSize = 100 << 20 495 x := make([]*[1024]byte, heapSize/1024) 496 for i := range x { 497 x[i] = new([1024]byte) 498 } 499 hugeSink = x 500 501 b.ResetTimer() 502 for i := 0; i < b.N; i++ { 503 runtime.ReadMemStats(&ms) 504 } 505 506 hugeSink = nil 507 } 508 509 func TestUserForcedGC(t *testing.T) { 510 // Test that runtime.GC() triggers a GC even if GOGC=off. 511 defer debug.SetGCPercent(debug.SetGCPercent(-1)) 512 513 var ms1, ms2 runtime.MemStats 514 runtime.ReadMemStats(&ms1) 515 runtime.GC() 516 runtime.ReadMemStats(&ms2) 517 if ms1.NumGC == ms2.NumGC { 518 t.Fatalf("runtime.GC() did not trigger GC") 519 } 520 if ms1.NumForcedGC == ms2.NumForcedGC { 521 t.Fatalf("runtime.GC() was not accounted in NumForcedGC") 522 } 523 } 524 525 func writeBarrierBenchmark(b *testing.B, f func()) { 526 runtime.GC() 527 var ms runtime.MemStats 528 runtime.ReadMemStats(&ms) 529 //b.Logf("heap size: %d MB", ms.HeapAlloc>>20) 530 531 // Keep GC running continuously during the benchmark, which in 532 // turn keeps the write barrier on continuously. 533 var stop uint32 534 done := make(chan bool) 535 go func() { 536 for atomic.LoadUint32(&stop) == 0 { 537 runtime.GC() 538 } 539 close(done) 540 }() 541 defer func() { 542 atomic.StoreUint32(&stop, 1) 543 <-done 544 }() 545 546 b.ResetTimer() 547 f() 548 b.StopTimer() 549 } 550 551 func BenchmarkWriteBarrier(b *testing.B) { 552 if runtime.GOMAXPROCS(-1) < 2 { 553 // We don't want GC to take our time. 554 b.Skip("need GOMAXPROCS >= 2") 555 } 556 557 // Construct a large tree both so the GC runs for a while and 558 // so we have a data structure to manipulate the pointers of. 559 type node struct { 560 l, r *node 561 } 562 var wbRoots []*node 563 var mkTree func(level int) *node 564 mkTree = func(level int) *node { 565 if level == 0 { 566 return nil 567 } 568 n := &node{mkTree(level - 1), mkTree(level - 1)} 569 if level == 10 { 570 // Seed GC with enough early pointers so it 571 // doesn't start termination barriers when it 572 // only has the top of the tree. 573 wbRoots = append(wbRoots, n) 574 } 575 return n 576 } 577 const depth = 22 // 64 MB 578 root := mkTree(22) 579 580 writeBarrierBenchmark(b, func() { 581 var stack [depth]*node 582 tos := -1 583 584 // There are two write barriers per iteration, so i+=2. 585 for i := 0; i < b.N; i += 2 { 586 if tos == -1 { 587 stack[0] = root 588 tos = 0 589 } 590 591 // Perform one step of reversing the tree. 592 n := stack[tos] 593 if n.l == nil { 594 tos-- 595 } else { 596 n.l, n.r = n.r, n.l 597 stack[tos] = n.l 598 stack[tos+1] = n.r 599 tos++ 600 } 601 602 if i%(1<<12) == 0 { 603 // Avoid non-preemptible loops (see issue #10958). 604 runtime.Gosched() 605 } 606 } 607 }) 608 609 runtime.KeepAlive(wbRoots) 610 } 611 612 func BenchmarkBulkWriteBarrier(b *testing.B) { 613 if runtime.GOMAXPROCS(-1) < 2 { 614 // We don't want GC to take our time. 615 b.Skip("need GOMAXPROCS >= 2") 616 } 617 618 // Construct a large set of objects we can copy around. 619 const heapSize = 64 << 20 620 type obj [16]*byte 621 ptrs := make([]*obj, heapSize/unsafe.Sizeof(obj{})) 622 for i := range ptrs { 623 ptrs[i] = new(obj) 624 } 625 626 writeBarrierBenchmark(b, func() { 627 const blockSize = 1024 628 var pos int 629 for i := 0; i < b.N; i += blockSize { 630 // Rotate block. 631 block := ptrs[pos : pos+blockSize] 632 first := block[0] 633 copy(block, block[1:]) 634 block[blockSize-1] = first 635 636 pos += blockSize 637 if pos+blockSize > len(ptrs) { 638 pos = 0 639 } 640 641 runtime.Gosched() 642 } 643 }) 644 645 runtime.KeepAlive(ptrs) 646 } 647 648 func BenchmarkScanStackNoLocals(b *testing.B) { 649 var ready sync.WaitGroup 650 teardown := make(chan bool) 651 for j := 0; j < 10; j++ { 652 ready.Add(1) 653 go func() { 654 x := 100000 655 countpwg(&x, &ready, teardown) 656 }() 657 } 658 ready.Wait() 659 b.ResetTimer() 660 for i := 0; i < b.N; i++ { 661 b.StartTimer() 662 runtime.GC() 663 runtime.GC() 664 b.StopTimer() 665 } 666 close(teardown) 667 } 668 669 func countpwg(n *int, ready *sync.WaitGroup, teardown chan bool) { 670 if *n == 0 { 671 ready.Done() 672 <-teardown 673 return 674 } 675 *n-- 676 countpwg(n, ready, teardown) 677 }