github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/src/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 "fmt" 9 "os" 10 "reflect" 11 "runtime" 12 "runtime/debug" 13 "testing" 14 "time" 15 "unsafe" 16 ) 17 18 func TestGcSys(t *testing.T) { 19 if os.Getenv("GOGC") == "off" { 20 t.Skip("skipping test; GOGC=off in environment") 21 } 22 got := runTestProg(t, "testprog", "GCSys") 23 want := "OK\n" 24 if got != want { 25 t.Fatalf("expected %q, but got %q", want, got) 26 } 27 } 28 29 func TestGcDeepNesting(t *testing.T) { 30 type T [2][2][2][2][2][2][2][2][2][2]*int 31 a := new(T) 32 33 // Prevent the compiler from applying escape analysis. 34 // This makes sure new(T) is allocated on heap, not on the stack. 35 t.Logf("%p", a) 36 37 a[0][0][0][0][0][0][0][0][0][0] = new(int) 38 *a[0][0][0][0][0][0][0][0][0][0] = 13 39 runtime.GC() 40 if *a[0][0][0][0][0][0][0][0][0][0] != 13 { 41 t.Fail() 42 } 43 } 44 45 func TestGcHashmapIndirection(t *testing.T) { 46 defer debug.SetGCPercent(debug.SetGCPercent(1)) 47 runtime.GC() 48 type T struct { 49 a [256]int 50 } 51 m := make(map[T]T) 52 for i := 0; i < 2000; i++ { 53 var a T 54 a.a[0] = i 55 m[a] = T{} 56 } 57 } 58 59 func TestGcArraySlice(t *testing.T) { 60 type X struct { 61 buf [1]byte 62 nextbuf []byte 63 next *X 64 } 65 var head *X 66 for i := 0; i < 10; i++ { 67 p := &X{} 68 p.buf[0] = 42 69 p.next = head 70 if head != nil { 71 p.nextbuf = head.buf[:] 72 } 73 head = p 74 runtime.GC() 75 } 76 for p := head; p != nil; p = p.next { 77 if p.buf[0] != 42 { 78 t.Fatal("corrupted heap") 79 } 80 } 81 } 82 83 func TestGcRescan(t *testing.T) { 84 type X struct { 85 c chan error 86 nextx *X 87 } 88 type Y struct { 89 X 90 nexty *Y 91 p *int 92 } 93 var head *Y 94 for i := 0; i < 10; i++ { 95 p := &Y{} 96 p.c = make(chan error) 97 if head != nil { 98 p.nextx = &head.X 99 } 100 p.nexty = head 101 p.p = new(int) 102 *p.p = 42 103 head = p 104 runtime.GC() 105 } 106 for p := head; p != nil; p = p.nexty { 107 if *p.p != 42 { 108 t.Fatal("corrupted heap") 109 } 110 } 111 } 112 113 func TestGcLastTime(t *testing.T) { 114 ms := new(runtime.MemStats) 115 t0 := time.Now().UnixNano() 116 runtime.GC() 117 t1 := time.Now().UnixNano() 118 runtime.ReadMemStats(ms) 119 last := int64(ms.LastGC) 120 if t0 > last || last > t1 { 121 t.Fatalf("bad last GC time: got %v, want [%v, %v]", last, t0, t1) 122 } 123 pause := ms.PauseNs[(ms.NumGC+255)%256] 124 // Due to timer granularity, pause can actually be 0 on windows 125 // or on virtualized environments. 126 if pause == 0 { 127 t.Logf("last GC pause was 0") 128 } else if pause > 10e9 { 129 t.Logf("bad last GC pause: got %v, want [0, 10e9]", pause) 130 } 131 } 132 133 var hugeSink interface{} 134 135 func TestHugeGCInfo(t *testing.T) { 136 // The test ensures that compiler can chew these huge types even on weakest machines. 137 // The types are not allocated at runtime. 138 if hugeSink != nil { 139 // 400MB on 32 bots, 4TB on 64-bits. 140 const n = (400 << 20) + (unsafe.Sizeof(uintptr(0))-4)<<40 141 hugeSink = new([n]*byte) 142 hugeSink = new([n]uintptr) 143 hugeSink = new(struct { 144 x float64 145 y [n]*byte 146 z []string 147 }) 148 hugeSink = new(struct { 149 x float64 150 y [n]uintptr 151 z []string 152 }) 153 } 154 } 155 156 func TestPeriodicGC(t *testing.T) { 157 // Make sure we're not in the middle of a GC. 158 runtime.GC() 159 160 var ms1, ms2 runtime.MemStats 161 runtime.ReadMemStats(&ms1) 162 163 // Make periodic GC run continuously. 164 orig := *runtime.ForceGCPeriod 165 *runtime.ForceGCPeriod = 0 166 167 // Let some periodic GCs happen. In a heavily loaded system, 168 // it's possible these will be delayed, so this is designed to 169 // succeed quickly if things are working, but to give it some 170 // slack if things are slow. 171 var numGCs uint32 172 const want = 2 173 for i := 0; i < 20 && numGCs < want; i++ { 174 time.Sleep(5 * time.Millisecond) 175 176 // Test that periodic GC actually happened. 177 runtime.ReadMemStats(&ms2) 178 numGCs = ms2.NumGC - ms1.NumGC 179 } 180 *runtime.ForceGCPeriod = orig 181 182 if numGCs < want { 183 t.Fatalf("no periodic GC: got %v GCs, want >= 2", numGCs) 184 } 185 } 186 187 func BenchmarkSetTypePtr(b *testing.B) { 188 benchSetType(b, new(*byte)) 189 } 190 191 func BenchmarkSetTypePtr8(b *testing.B) { 192 benchSetType(b, new([8]*byte)) 193 } 194 195 func BenchmarkSetTypePtr16(b *testing.B) { 196 benchSetType(b, new([16]*byte)) 197 } 198 199 func BenchmarkSetTypePtr32(b *testing.B) { 200 benchSetType(b, new([32]*byte)) 201 } 202 203 func BenchmarkSetTypePtr64(b *testing.B) { 204 benchSetType(b, new([64]*byte)) 205 } 206 207 func BenchmarkSetTypePtr126(b *testing.B) { 208 benchSetType(b, new([126]*byte)) 209 } 210 211 func BenchmarkSetTypePtr128(b *testing.B) { 212 benchSetType(b, new([128]*byte)) 213 } 214 215 func BenchmarkSetTypePtrSlice(b *testing.B) { 216 benchSetType(b, make([]*byte, 1<<10)) 217 } 218 219 type Node1 struct { 220 Value [1]uintptr 221 Left, Right *byte 222 } 223 224 func BenchmarkSetTypeNode1(b *testing.B) { 225 benchSetType(b, new(Node1)) 226 } 227 228 func BenchmarkSetTypeNode1Slice(b *testing.B) { 229 benchSetType(b, make([]Node1, 32)) 230 } 231 232 type Node8 struct { 233 Value [8]uintptr 234 Left, Right *byte 235 } 236 237 func BenchmarkSetTypeNode8(b *testing.B) { 238 benchSetType(b, new(Node8)) 239 } 240 241 func BenchmarkSetTypeNode8Slice(b *testing.B) { 242 benchSetType(b, make([]Node8, 32)) 243 } 244 245 type Node64 struct { 246 Value [64]uintptr 247 Left, Right *byte 248 } 249 250 func BenchmarkSetTypeNode64(b *testing.B) { 251 benchSetType(b, new(Node64)) 252 } 253 254 func BenchmarkSetTypeNode64Slice(b *testing.B) { 255 benchSetType(b, make([]Node64, 32)) 256 } 257 258 type Node64Dead struct { 259 Left, Right *byte 260 Value [64]uintptr 261 } 262 263 func BenchmarkSetTypeNode64Dead(b *testing.B) { 264 benchSetType(b, new(Node64Dead)) 265 } 266 267 func BenchmarkSetTypeNode64DeadSlice(b *testing.B) { 268 benchSetType(b, make([]Node64Dead, 32)) 269 } 270 271 type Node124 struct { 272 Value [124]uintptr 273 Left, Right *byte 274 } 275 276 func BenchmarkSetTypeNode124(b *testing.B) { 277 benchSetType(b, new(Node124)) 278 } 279 280 func BenchmarkSetTypeNode124Slice(b *testing.B) { 281 benchSetType(b, make([]Node124, 32)) 282 } 283 284 type Node126 struct { 285 Value [126]uintptr 286 Left, Right *byte 287 } 288 289 func BenchmarkSetTypeNode126(b *testing.B) { 290 benchSetType(b, new(Node126)) 291 } 292 293 func BenchmarkSetTypeNode126Slice(b *testing.B) { 294 benchSetType(b, make([]Node126, 32)) 295 } 296 297 type Node128 struct { 298 Value [128]uintptr 299 Left, Right *byte 300 } 301 302 func BenchmarkSetTypeNode128(b *testing.B) { 303 benchSetType(b, new(Node128)) 304 } 305 306 func BenchmarkSetTypeNode128Slice(b *testing.B) { 307 benchSetType(b, make([]Node128, 32)) 308 } 309 310 type Node130 struct { 311 Value [130]uintptr 312 Left, Right *byte 313 } 314 315 func BenchmarkSetTypeNode130(b *testing.B) { 316 benchSetType(b, new(Node130)) 317 } 318 319 func BenchmarkSetTypeNode130Slice(b *testing.B) { 320 benchSetType(b, make([]Node130, 32)) 321 } 322 323 type Node1024 struct { 324 Value [1024]uintptr 325 Left, Right *byte 326 } 327 328 func BenchmarkSetTypeNode1024(b *testing.B) { 329 benchSetType(b, new(Node1024)) 330 } 331 332 func BenchmarkSetTypeNode1024Slice(b *testing.B) { 333 benchSetType(b, make([]Node1024, 32)) 334 } 335 336 func benchSetType(b *testing.B, x interface{}) { 337 v := reflect.ValueOf(x) 338 t := v.Type() 339 switch t.Kind() { 340 case reflect.Ptr: 341 b.SetBytes(int64(t.Elem().Size())) 342 case reflect.Slice: 343 b.SetBytes(int64(t.Elem().Size()) * int64(v.Len())) 344 } 345 b.ResetTimer() 346 runtime.BenchSetType(b.N, x) 347 } 348 349 func BenchmarkAllocation(b *testing.B) { 350 type T struct { 351 x, y *byte 352 } 353 ngo := runtime.GOMAXPROCS(0) 354 work := make(chan bool, b.N+ngo) 355 result := make(chan *T) 356 for i := 0; i < b.N; i++ { 357 work <- true 358 } 359 for i := 0; i < ngo; i++ { 360 work <- false 361 } 362 for i := 0; i < ngo; i++ { 363 go func() { 364 var x *T 365 for <-work { 366 for i := 0; i < 1000; i++ { 367 x = &T{} 368 } 369 } 370 result <- x 371 }() 372 } 373 for i := 0; i < ngo; i++ { 374 <-result 375 } 376 } 377 378 func TestPrintGC(t *testing.T) { 379 if testing.Short() { 380 t.Skip("Skipping in short mode") 381 } 382 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(2)) 383 done := make(chan bool) 384 go func() { 385 for { 386 select { 387 case <-done: 388 return 389 default: 390 runtime.GC() 391 } 392 } 393 }() 394 for i := 0; i < 1e4; i++ { 395 func() { 396 defer print("") 397 }() 398 } 399 close(done) 400 } 401 402 func testTypeSwitch(x interface{}) error { 403 switch y := x.(type) { 404 case nil: 405 // ok 406 case error: 407 return y 408 } 409 return nil 410 } 411 412 func testAssert(x interface{}) error { 413 if y, ok := x.(error); ok { 414 return y 415 } 416 return nil 417 } 418 419 func testAssertVar(x interface{}) error { 420 var y, ok = x.(error) 421 if ok { 422 return y 423 } 424 return nil 425 } 426 427 var a bool 428 429 //go:noinline 430 func testIfaceEqual(x interface{}) { 431 if x == "abc" { 432 a = true 433 } 434 } 435 436 func TestPageAccounting(t *testing.T) { 437 // Grow the heap in small increments. This used to drop the 438 // pages-in-use count below zero because of a rounding 439 // mismatch (golang.org/issue/15022). 440 const blockSize = 64 << 10 441 blocks := make([]*[blockSize]byte, (64<<20)/blockSize) 442 for i := range blocks { 443 blocks[i] = new([blockSize]byte) 444 } 445 446 // Check that the running page count matches reality. 447 pagesInUse, counted := runtime.CountPagesInUse() 448 if pagesInUse != counted { 449 t.Fatalf("mheap_.pagesInUse is %d, but direct count is %d", pagesInUse, counted) 450 } 451 } 452 453 func TestReadMemStats(t *testing.T) { 454 base, slow := runtime.ReadMemStatsSlow() 455 if base != slow { 456 logDiff(t, "MemStats", reflect.ValueOf(base), reflect.ValueOf(slow)) 457 t.Fatal("memstats mismatch") 458 } 459 } 460 461 func logDiff(t *testing.T, prefix string, got, want reflect.Value) { 462 typ := got.Type() 463 switch typ.Kind() { 464 case reflect.Array, reflect.Slice: 465 if got.Len() != want.Len() { 466 t.Logf("len(%s): got %v, want %v", prefix, got, want) 467 return 468 } 469 for i := 0; i < got.Len(); i++ { 470 logDiff(t, fmt.Sprintf("%s[%d]", prefix, i), got.Index(i), want.Index(i)) 471 } 472 case reflect.Struct: 473 for i := 0; i < typ.NumField(); i++ { 474 gf, wf := got.Field(i), want.Field(i) 475 logDiff(t, prefix+"."+typ.Field(i).Name, gf, wf) 476 } 477 case reflect.Map: 478 t.Fatal("not implemented: logDiff for map") 479 default: 480 if got.Interface() != want.Interface() { 481 t.Logf("%s: got %v, want %v", prefix, got, want) 482 } 483 } 484 } 485 486 func BenchmarkReadMemStats(b *testing.B) { 487 var ms runtime.MemStats 488 const heapSize = 100 << 20 489 x := make([]*[1024]byte, heapSize/1024) 490 for i := range x { 491 x[i] = new([1024]byte) 492 } 493 hugeSink = x 494 495 b.ResetTimer() 496 for i := 0; i < b.N; i++ { 497 runtime.ReadMemStats(&ms) 498 } 499 500 hugeSink = nil 501 }