| 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 |
"math/rand" |
| 10 |
"os" |
| 11 |
"reflect" |
| 12 |
"runtime" |
| 13 |
"runtime/debug" |
| 14 |
"sort" |
| 15 |
"strings" |
| 16 |
"sync" |
| 17 |
"sync/atomic" |
| 18 |
"testing" |
| 19 |
"time" |
| 20 |
"unsafe" |
| 21 |
) |
| 22 |
|
| 23 |
func TestGcSys(t *testing.T) { |
| 24 |
t.Skip("skipping known-flaky test; golang.org/issue/37331") |
| 25 |
if os.Getenv("GOGC") == "off" { |
| 26 |
t.Skip("skipping test; GOGC=off in environment") |
| 27 |
} |
| 28 |
got := runTestProg(t, "testprog", "GCSys") |
| 29 |
want := "OK\n" |
| 30 |
if got != want { |
| 31 |
t.Fatalf("expected %q, but got %q", want, got) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
func TestGcDeepNesting(t *testing.T) { |
| 36 |
type T [2][2][2][2][2][2][2][2][2][2]*int |
| 37 |
a := new(T) |
| 38 |
|
| 39 |
// Prevent the compiler from applying escape analysis. |
| 40 |
// This makes sure new(T) is allocated on heap, not on the stack. |
| 41 |
t.Logf("%p", a) |
| 42 |
|
| 43 |
a[0][0][0][0][0][0][0][0][0][0] = new(int) |
| 44 |
*a[0][0][0][0][0][0][0][0][0][0] = 13 |
| 45 |
runtime.GC() |
| 46 |
if *a[0][0][0][0][0][0][0][0][0][0] != 13 { |
| 47 |
t.Fail() |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
func TestGcMapIndirection(t *testing.T) { |
| 52 |
defer debug.SetGCPercent(debug.SetGCPercent(1)) |
| 53 |
runtime.GC() |
| 54 |
type T struct { |
| 55 |
a [256]int |
| 56 |
} |
| 57 |
m := make(map[T]T) |
| 58 |
for i := 0; i < 2000; i++ { |
| 59 |
var a T |
| 60 |
a.a[0] = i |
| 61 |
m[a] = T{} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func TestGcArraySlice(t *testing.T) { |
| 66 |
type X struct { |
| 67 |
buf [1]byte |
| 68 |
nextbuf []byte |
| 69 |
next *X |
| 70 |
} |
| 71 |
var head *X |
| 72 |
for i := 0; i < 10; i++ { |
| 73 |
p := &X{} |
| 74 |
p.buf[0] = 42 |
| 75 |
p.next = head |
| 76 |
if head != nil { |
| 77 |
p.nextbuf = head.buf[:] |
| 78 |
} |
| 79 |
head = p |
| 80 |
runtime.GC() |
| 81 |
} |
| 82 |
for p := head; p != nil; p = p.next { |
| 83 |
if p.buf[0] != 42 { |
| 84 |
t.Fatal("corrupted heap") |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func TestGcRescan(t *testing.T) { |
| 90 |
type X struct { |
| 91 |
c chan error |
| 92 |
nextx *X |
| 93 |
} |
| 94 |
type Y struct { |
| 95 |
X |
| 96 |
nexty *Y |
| 97 |
p *int |
| 98 |
} |
| 99 |
var head *Y |
| 100 |
for i := 0; i < 10; i++ { |
| 101 |
p := &Y{} |
| 102 |
p.c = make(chan error) |
| 103 |
if head != nil { |
| 104 |
p.nextx = &head.X |
| 105 |
} |
| 106 |
p.nexty = head |
| 107 |
p.p = new(int) |
| 108 |
*p.p = 42 |
| 109 |
head = p |
| 110 |
runtime.GC() |
| 111 |
} |
| 112 |
for p := head; p != nil; p = p.nexty { |
| 113 |
if *p.p != 42 { |
| 114 |
t.Fatal("corrupted heap") |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func TestGcLastTime(t *testing.T) { |
| 120 |
ms := new(runtime.MemStats) |
| 121 |
t0 := time.Now().UnixNano() |
| 122 |
runtime.GC() |
| 123 |
t1 := time.Now().UnixNano() |
| 124 |
runtime.ReadMemStats(ms) |
| 125 |
last := int64(ms.LastGC) |
| 126 |
if t0 > last || last > t1 { |
| 127 |
t.Fatalf("bad last GC time: got %v, want [%v, %v]", last, t0, t1) |
| 128 |
} |
| 129 |
pause := ms.PauseNs[(ms.NumGC+255)%256] |
| 130 |
// Due to timer granularity, pause can actually be 0 on windows |
| 131 |
// or on virtualized environments. |
| 132 |
if pause == 0 { |
| 133 |
t.Logf("last GC pause was 0") |
| 134 |
} else if pause > 10e9 { |
| 135 |
t.Logf("bad last GC pause: got %v, want [0, 10e9]", pause) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
var hugeSink any |
| 140 |
|
| 141 |
func TestHugeGCInfo(t *testing.T) { |
| 142 |
// The test ensures that compiler can chew these huge types even on weakest machines. |
| 143 |
// The types are not allocated at runtime. |
| 144 |
if hugeSink != nil { |
| 145 |
// 400MB on 32 bots, 4TB on 64-bits. |
| 146 |
const n = (400 << 20) + (unsafe.Sizeof(uintptr(0))-4)<<40 |
| 147 |
hugeSink = new([n]*byte) |
| 148 |
hugeSink = new([n]uintptr) |
| 149 |
hugeSink = new(struct { |
| 150 |
x float64 |
| 151 |
y [n]*byte |
| 152 |
z []string |
| 153 |
}) |
| 154 |
hugeSink = new(struct { |
| 155 |
x float64 |
| 156 |
y [n]uintptr |
| 157 |
z []string |
| 158 |
}) |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
/* |
| 163 |
func TestPeriodicGC(t *testing.T) { |
| 164 |
if runtime.GOARCH == "wasm" { |
| 165 |
t.Skip("no sysmon on wasm yet") |
| 166 |
} |
| 167 |
|
| 168 |
// Make sure we're not in the middle of a GC. |
| 169 |
runtime.GC() |
| 170 |
|
| 171 |
var ms1, ms2 runtime.MemStats |
| 172 |
runtime.ReadMemStats(&ms1) |
| 173 |
|
| 174 |
// Make periodic GC run continuously. |
| 175 |
orig := *runtime.ForceGCPeriod |
| 176 |
*runtime.ForceGCPeriod = 0 |
| 177 |
|
| 178 |
// Let some periodic GCs happen. In a heavily loaded system, |
| 179 |
// it's possible these will be delayed, so this is designed to |
| 180 |
// succeed quickly if things are working, but to give it some |
| 181 |
// slack if things are slow. |
| 182 |
var numGCs uint32 |
| 183 |
const want = 2 |
| 184 |
for i := 0; i < 200 && numGCs < want; i++ { |
| 185 |
time.Sleep(5 * time.Millisecond) |
| 186 |
|
| 187 |
// Test that periodic GC actually happened. |
| 188 |
runtime.ReadMemStats(&ms2) |
| 189 |
numGCs = ms2.NumGC - ms1.NumGC |
| 190 |
} |
| 191 |
*runtime.ForceGCPeriod = orig |
| 192 |
|
| 193 |
if numGCs < want { |
| 194 |
t.Fatalf("no periodic GC: got %v GCs, want >= 2", numGCs) |
| 195 |
} |
| 196 |
} |
| 197 |
*/ |
| 198 |
|
| 199 |
func TestGcZombieReporting(t *testing.T) { |
| 200 |
if runtime.Compiler == "gccgo" { |
| 201 |
t.Skip("gccgo uses partially conservative GC") |
| 202 |
} |
| 203 |
// This test is somewhat sensitive to how the allocator works. |
| 204 |
// Pointers in zombies slice may cross-span, thus we |
| 205 |
// add invalidptr=0 for avoiding the badPointer check. |
| 206 |
// See issue https://golang.org/issues/49613/ |
| 207 |
got := runTestProg(t, "testprog", "GCZombie", "GODEBUG=invalidptr=0") |
| 208 |
want := "found pointer to free object" |
| 209 |
if !strings.Contains(got, want) { |
| 210 |
t.Fatalf("expected %q in output, but got %q", want, got) |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
/* |
| 215 |
|
| 216 |
func TestGCTestMoveStackOnNextCall(t *testing.T) { |
| 217 |
t.Parallel() |
| 218 |
var onStack int |
| 219 |
// GCTestMoveStackOnNextCall can fail in rare cases if there's |
| 220 |
// a preemption. This won't happen many times in quick |
| 221 |
// succession, so just retry a few times. |
| 222 |
for retry := 0; retry < 5; retry++ { |
| 223 |
runtime.GCTestMoveStackOnNextCall() |
| 224 |
if moveStackCheck(t, &onStack, uintptr(unsafe.Pointer(&onStack))) { |
| 225 |
// Passed. |
| 226 |
return |
| 227 |
} |
| 228 |
} |
| 229 |
t.Fatal("stack did not move") |
| 230 |
} |
| 231 |
|
| 232 |
// This must not be inlined because the point is to force a stack |
| 233 |
// growth check and move the stack. |
| 234 |
// |
| 235 |
//go:noinline |
| 236 |
func moveStackCheck(t *testing.T, new *int, old uintptr) bool { |
| 237 |
// new should have been updated by the stack move; |
| 238 |
// old should not have. |
| 239 |
|
| 240 |
// Capture new's value before doing anything that could |
| 241 |
// further move the stack. |
| 242 |
new2 := uintptr(unsafe.Pointer(new)) |
| 243 |
|
| 244 |
t.Logf("old stack pointer %x, new stack pointer %x", old, new2) |
| 245 |
if new2 == old { |
| 246 |
// Check that we didn't screw up the test's escape analysis. |
| 247 |
if cls := runtime.GCTestPointerClass(unsafe.Pointer(new)); cls != "stack" { |
| 248 |
t.Fatalf("test bug: new (%#x) should be a stack pointer, not %s", new2, cls) |
| 249 |
} |
| 250 |
// This was a real failure. |
| 251 |
return false |
| 252 |
} |
| 253 |
return true |
| 254 |
} |
| 255 |
|
| 256 |
func TestGCTestMoveStackRepeatedly(t *testing.T) { |
| 257 |
// Move the stack repeatedly to make sure we're not doubling |
| 258 |
// it each time. |
| 259 |
for i := 0; i < 100; i++ { |
| 260 |
runtime.GCTestMoveStackOnNextCall() |
| 261 |
moveStack1(false) |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
//go:noinline |
| 266 |
func moveStack1(x bool) { |
| 267 |
// Make sure this function doesn't get auto-nosplit. |
| 268 |
if x { |
| 269 |
println("x") |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
*/ |
| 274 |
|
| 275 |
func TestGCTestIsReachable(t *testing.T) { |
| 276 |
var all, half []unsafe.Pointer |
| 277 |
var want uint64 |
| 278 |
for i := 0; i < 16; i++ { |
| 279 |
// The tiny allocator muddies things, so we use a |
| 280 |
// scannable type. |
| 281 |
p := unsafe.Pointer(new(*int)) |
| 282 |
all = append(all, p) |
| 283 |
if i%2 == 0 { |
| 284 |
half = append(half, p) |
| 285 |
want |= 1 << i |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
got := runtime.GCTestIsReachable(all...) |
| 290 |
if want != got { |
| 291 |
// gccgo's conservative GC means that we sometimes |
| 292 |
// keep data we shouldn't. |
| 293 |
if runtime.Compiler == "gccgo" { |
| 294 |
if ((got ^ want) & want) != 0 { |
| 295 |
t.Fatalf("some expected bits not set: want %b, got %b", want, got) |
| 296 |
} |
| 297 |
} else { |
| 298 |
t.Fatalf("did not get expected reachable set; want %b, got %b", want, got) |
| 299 |
} |
| 300 |
} |
| 301 |
runtime.KeepAlive(half) |
| 302 |
} |
| 303 |
|
| 304 |
var pointerClassSink *int |
| 305 |
var pointerClassData = 42 |
| 306 |
|
| 307 |
func TestGCTestPointerClass(t *testing.T) { |
| 308 |
if runtime.Compiler == "gccgo" { |
| 309 |
// gofrontend escape analysis doesn't handle passing |
| 310 |
// &onStack through a closure. |
| 311 |
t.Skip("skipping for gofrontend") |
| 312 |
} |
| 313 |
|
| 314 |
t.Parallel() |
| 315 |
check := func(p unsafe.Pointer, want string) { |
| 316 |
t.Helper() |
| 317 |
got := runtime.GCTestPointerClass(p) |
| 318 |
if got != want { |
| 319 |
// Convert the pointer to a uintptr to avoid |
| 320 |
// escaping it. |
| 321 |
t.Errorf("for %#x, want class %s, got %s", uintptr(p), want, got) |
| 322 |
} |
| 323 |
} |
| 324 |
var onStack int |
| 325 |
var notOnStack int |
| 326 |
pointerClassSink = ¬OnStack |
| 327 |
check(unsafe.Pointer(&onStack), "stack") |
| 328 |
check(unsafe.Pointer(¬OnStack), "heap") |
| 329 |
check(unsafe.Pointer(&pointerClassSink), "bss") |
| 330 |
check(unsafe.Pointer(&pointerClassData), "data") |
| 331 |
check(nil, "other") |
| 332 |
} |
| 333 |
|
| 334 |
func BenchmarkSetTypePtr(b *testing.B) { |
| 335 |
benchSetType(b, new(*byte)) |
| 336 |
} |
| 337 |
|
| 338 |
func BenchmarkSetTypePtr8(b *testing.B) { |
| 339 |
benchSetType(b, new([8]*byte)) |
| 340 |
} |
| 341 |
|
| 342 |
func BenchmarkSetTypePtr16(b *testing.B) { |
| 343 |
benchSetType(b, new([16]*byte)) |
| 344 |
} |
| 345 |
|
| 346 |
func BenchmarkSetTypePtr32(b *testing.B) { |
| 347 |
benchSetType(b, new([32]*byte)) |
| 348 |
} |
| 349 |
|
| 350 |
func BenchmarkSetTypePtr64(b *testing.B) { |
| 351 |
benchSetType(b, new([64]*byte)) |
| 352 |
} |
| 353 |
|
| 354 |
func BenchmarkSetTypePtr126(b *testing.B) { |
| 355 |
benchSetType(b, new([126]*byte)) |
| 356 |
} |
| 357 |
|
| 358 |
func BenchmarkSetTypePtr128(b *testing.B) { |
| 359 |
benchSetType(b, new([128]*byte)) |
| 360 |
} |
| 361 |
|
| 362 |
func BenchmarkSetTypePtrSlice(b *testing.B) { |
| 363 |
benchSetType(b, make([]*byte, 1<<10)) |
| 364 |
} |
| 365 |
|
| 366 |
type Node1 struct { |
| 367 |
Value [1]uintptr |
| 368 |
Left, Right *byte |
| 369 |
} |
| 370 |
|
| 371 |
func BenchmarkSetTypeNode1(b *testing.B) { |
| 372 |
benchSetType(b, new(Node1)) |
| 373 |
} |
| 374 |
|
| 375 |
func BenchmarkSetTypeNode1Slice(b *testing.B) { |
| 376 |
benchSetType(b, make([]Node1, 32)) |
| 377 |
} |
| 378 |
|
| 379 |
type Node8 struct { |
| 380 |
Value [8]uintptr |
| 381 |
Left, Right *byte |
| 382 |
} |
| 383 |
|
| 384 |
func BenchmarkSetTypeNode8(b *testing.B) { |
| 385 |
benchSetType(b, new(Node8)) |
| 386 |
} |
| 387 |
|
| 388 |
func BenchmarkSetTypeNode8Slice(b *testing.B) { |
| 389 |
benchSetType(b, make([]Node8, 32)) |
| 390 |
} |
| 391 |
|
| 392 |
type Node64 struct { |
| 393 |
Value [64]uintptr |
| 394 |
Left, Right *byte |
| 395 |
} |
| 396 |
|
| 397 |
func BenchmarkSetTypeNode64(b *testing.B) { |
| 398 |
benchSetType(b, new(Node64)) |
| 399 |
} |
| 400 |
|
| 401 |
func BenchmarkSetTypeNode64Slice(b *testing.B) { |
| 402 |
benchSetType(b, make([]Node64, 32)) |
| 403 |
} |
| 404 |
|
| 405 |
type Node64Dead struct { |
| 406 |
Left, Right *byte |
| 407 |
Value [64]uintptr |
| 408 |
} |
| 409 |
|
| 410 |
func BenchmarkSetTypeNode64Dead(b *testing.B) { |
| 411 |
benchSetType(b, new(Node64Dead)) |
| 412 |
} |
| 413 |
|
| 414 |
func BenchmarkSetTypeNode64DeadSlice(b *testing.B) { |
| 415 |
benchSetType(b, make([]Node64Dead, 32)) |
| 416 |
} |
| 417 |
|
| 418 |
type Node124 struct { |
| 419 |
Value [124]uintptr |
| 420 |
Left, Right *byte |
| 421 |
} |
| 422 |
|
| 423 |
func BenchmarkSetTypeNode124(b *testing.B) { |
| 424 |
benchSetType(b, new(Node124)) |
| 425 |
} |
| 426 |
|
| 427 |
func BenchmarkSetTypeNode124Slice(b *testing.B) { |
| 428 |
benchSetType(b, make([]Node124, 32)) |
| 429 |
} |
| 430 |
|
| 431 |
type Node126 struct { |
| 432 |
Value [126]uintptr |
| 433 |
Left, Right *byte |
| 434 |
} |
| 435 |
|
| 436 |
func BenchmarkSetTypeNode126(b *testing.B) { |
| 437 |
benchSetType(b, new(Node126)) |
| 438 |
} |
| 439 |
|
| 440 |
func BenchmarkSetTypeNode126Slice(b *testing.B) { |
| 441 |
benchSetType(b, make([]Node126, 32)) |
| 442 |
} |
| 443 |
|
| 444 |
type Node128 struct { |
| 445 |
Value [128]uintptr |
| 446 |
Left, Right *byte |
| 447 |
} |
| 448 |
|
| 449 |
func BenchmarkSetTypeNode128(b *testing.B) { |
| 450 |
benchSetType(b, new(Node128)) |
| 451 |
} |
| 452 |
|
| 453 |
func BenchmarkSetTypeNode128Slice(b *testing.B) { |
| 454 |
benchSetType(b, make([]Node128, 32)) |
| 455 |
} |
| 456 |
|
| 457 |
type Node130 struct { |
| 458 |
Value [130]uintptr |
| 459 |
Left, Right *byte |
| 460 |
} |
| 461 |
|
| 462 |
func BenchmarkSetTypeNode130(b *testing.B) { |
| 463 |
benchSetType(b, new(Node130)) |
| 464 |
} |
| 465 |
|
| 466 |
func BenchmarkSetTypeNode130Slice(b *testing.B) { |
| 467 |
benchSetType(b, make([]Node130, 32)) |
| 468 |
} |
| 469 |
|
| 470 |
type Node1024 struct { |
| 471 |
Value [1024]uintptr |
| 472 |
Left, Right *byte |
| 473 |
} |
| 474 |
|
| 475 |
func BenchmarkSetTypeNode1024(b *testing.B) { |
| 476 |
benchSetType(b, new(Node1024)) |
| 477 |
} |
| 478 |
|
| 479 |
func BenchmarkSetTypeNode1024Slice(b *testing.B) { |
| 480 |
benchSetType(b, make([]Node1024, 32)) |
| 481 |
} |
| 482 |
|
| 483 |
func benchSetType(b *testing.B, x any) { |
| 484 |
v := reflect.ValueOf(x) |
| 485 |
t := v.Type() |
| 486 |
switch t.Kind() { |
| 487 |
case reflect.Pointer: |
| 488 |
b.SetBytes(int64(t.Elem().Size())) |
| 489 |
case reflect.Slice: |
| 490 |
b.SetBytes(int64(t.Elem().Size()) * int64(v.Len())) |
| 491 |
} |
| 492 |
b.ResetTimer() |
| 493 |
//runtime.BenchSetType(b.N, x) |
| 494 |
} |
| 495 |
|
| 496 |
func BenchmarkAllocation(b *testing.B) { |
| 497 |
type T struct { |
| 498 |
x, y *byte |
| 499 |
} |
| 500 |
ngo := runtime.GOMAXPROCS(0) |
| 501 |
work := make(chan bool, b.N+ngo) |
| 502 |
result := make(chan *T) |
| 503 |
for i := 0; i < b.N; i++ { |
| 504 |
work <- true |
| 505 |
} |
| 506 |
for i := 0; i < ngo; i++ { |
| 507 |
work <- false |
| 508 |
} |
| 509 |
for i := 0; i < ngo; i++ { |
| 510 |
go func() { |
| 511 |
var x *T |
| 512 |
for <-work { |
| 513 |
for i := 0; i < 1000; i++ { |
| 514 |
x = &T{} |
| 515 |
} |
| 516 |
} |
| 517 |
result <- x |
| 518 |
}() |
| 519 |
} |
| 520 |
for i := 0; i < ngo; i++ { |
| 521 |
<-result |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
func TestPrintGC(t *testing.T) { |
| 526 |
if testing.Short() { |
| 527 |
t.Skip("Skipping in short mode") |
| 528 |
} |
| 529 |
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(2)) |
| 530 |
done := make(chan bool) |
| 531 |
go func() { |
| 532 |
for { |
| 533 |
select { |
| 534 |
case <-done: |
| 535 |
return |
| 536 |
default: |
| 537 |
runtime.GC() |
| 538 |
} |
| 539 |
} |
| 540 |
}() |
| 541 |
for i := 0; i < 1e4; i++ { |
| 542 |
func() { |
| 543 |
defer print("") |
| 544 |
}() |
| 545 |
} |
| 546 |
close(done) |
| 547 |
} |
| 548 |
|
| 549 |
func testTypeSwitch(x any) error { |
| 550 |
switch y := x.(type) { |
| 551 |
case nil: |
| 552 |
// ok |
| 553 |
case error: |
| 554 |
return y |
| 555 |
} |
| 556 |
return nil |
| 557 |
} |
| 558 |
|
| 559 |
func testAssert(x any) error { |
| 560 |
if y, ok := x.(error); ok { |
| 561 |
return y |
| 562 |
} |
| 563 |
return nil |
| 564 |
} |
| 565 |
|
| 566 |
func testAssertVar(x any) error { |
| 567 |
var y, ok = x.(error) |
| 568 |
if ok { |
| 569 |
return y |
| 570 |
} |
| 571 |
return nil |
| 572 |
} |
| 573 |
|
| 574 |
var a bool |
| 575 |
|
| 576 |
//go:noinline |
| 577 |
func testIfaceEqual(x any) { |
| 578 |
if x == "abc" { |
| 579 |
a = true |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
func TestPageAccounting(t *testing.T) { |
| 584 |
// Grow the heap in small increments. This used to drop the |
| 585 |
// pages-in-use count below zero because of a rounding |
| 586 |
// mismatch (golang.org/issue/15022). |
| 587 |
const blockSize = 64 << 10 |
| 588 |
blocks := make([]*[blockSize]byte, (64<<20)/blockSize) |
| 589 |
for i := range blocks { |
| 590 |
blocks[i] = new([blockSize]byte) |
| 591 |
} |
| 592 |
|
| 593 |
// Check that the running page count matches reality. |
| 594 |
pagesInUse, counted := runtime.CountPagesInUse() |
| 595 |
if pagesInUse != counted { |
| 596 |
t.Fatalf("mheap_.pagesInUse is %d, but direct count is %d", pagesInUse, counted) |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
func TestReadMemStats(t *testing.T) { |
| 601 |
base, slow := runtime.ReadMemStatsSlow() |
| 602 |
if base != slow { |
| 603 |
logDiff(t, "MemStats", reflect.ValueOf(base), reflect.ValueOf(slow)) |
| 604 |
t.Fatal("memstats mismatch") |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
func logDiff(t *testing.T, prefix string, got, want reflect.Value) { |
| 609 |
typ := got.Type() |
| 610 |
switch typ.Kind() { |
| 611 |
case reflect.Array, reflect.Slice: |
| 612 |
if got.Len() != want.Len() { |
| 613 |
t.Logf("len(%s): got %v, want %v", prefix, got, want) |
| 614 |
return |
| 615 |
} |
| 616 |
for i := 0; i < got.Len(); i++ { |
| 617 |
logDiff(t, fmt.Sprintf("%s[%d]", prefix, i), got.Index(i), want.Index(i)) |
| 618 |
} |
| 619 |
case reflect.Struct: |
| 620 |
for i := 0; i < typ.NumField(); i++ { |
| 621 |
gf, wf := got.Field(i), want.Field(i) |
| 622 |
logDiff(t, prefix+"."+typ.Field(i).Name, gf, wf) |
| 623 |
} |
| 624 |
case reflect.Map: |
| 625 |
t.Fatal("not implemented: logDiff for map") |
| 626 |
default: |
| 627 |
if got.Interface() != want.Interface() { |
| 628 |
t.Logf("%s: got %v, want %v", prefix, got, want) |
| 629 |
} |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
func BenchmarkReadMemStats(b *testing.B) { |
| 634 |
var ms runtime.MemStats |
| 635 |
const heapSize = 100 << 20 |
| 636 |
x := make([]*[1024]byte, heapSize/1024) |
| 637 |
for i := range x { |
| 638 |
x[i] = new([1024]byte) |
| 639 |
} |
| 640 |
hugeSink = x |
| 641 |
|
| 642 |
b.ResetTimer() |
| 643 |
for i := 0; i < b.N; i++ { |
| 644 |
runtime.ReadMemStats(&ms) |
| 645 |
} |
| 646 |
|
| 647 |
hugeSink = nil |
| 648 |
} |
| 649 |
|
| 650 |
func applyGCLoad(b *testing.B) func() { |
| 651 |
// We’ll apply load to the runtime with maxProcs-1 goroutines |
| 652 |
// and use one more to actually benchmark. It doesn't make sense |
| 653 |
// to try to run this test with only 1 P (that's what |
| 654 |
// BenchmarkReadMemStats is for). |
| 655 |
maxProcs := runtime.GOMAXPROCS(-1) |
| 656 |
if maxProcs == 1 { |
| 657 |
b.Skip("This benchmark can only be run with GOMAXPROCS > 1") |
| 658 |
} |
| 659 |
|
| 660 |
// Code to build a big tree with lots of pointers. |
| 661 |
type node struct { |
| 662 |
children [16]*node |
| 663 |
} |
| 664 |
var buildTree func(depth int) *node |
| 665 |
buildTree = func(depth int) *node { |
| 666 |
tree := new(node) |
| 667 |
if depth != 0 { |
| 668 |
for i := range tree.children { |
| 669 |
tree.children[i] = buildTree(depth - 1) |
| 670 |
} |
| 671 |
} |
| 672 |
return tree |
| 673 |
} |
| 674 |
|
| 675 |
// Keep the GC busy by continuously generating large trees. |
| 676 |
done := make(chan struct{}) |
| 677 |
var wg sync.WaitGroup |
| 678 |
for i := 0; i < maxProcs-1; i++ { |
| 679 |
wg.Add(1) |
| 680 |
go func() { |
| 681 |
defer wg.Done() |
| 682 |
var hold *node |
| 683 |
loop: |
| 684 |
for { |
| 685 |
hold = buildTree(5) |
| 686 |
select { |
| 687 |
case <-done: |
| 688 |
break loop |
| 689 |
default: |
| 690 |
} |
| 691 |
} |
| 692 |
runtime.KeepAlive(hold) |
| 693 |
}() |
| 694 |
} |
| 695 |
return func() { |
| 696 |
close(done) |
| 697 |
wg.Wait() |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
func BenchmarkReadMemStatsLatency(b *testing.B) { |
| 702 |
stop := applyGCLoad(b) |
| 703 |
|
| 704 |
// Spend this much time measuring latencies. |
| 705 |
latencies := make([]time.Duration, 0, 1024) |
| 706 |
|
| 707 |
// Run for timeToBench hitting ReadMemStats continuously |
| 708 |
// and measuring the latency. |
| 709 |
b.ResetTimer() |
| 710 |
var ms runtime.MemStats |
| 711 |
for i := 0; i < b.N; i++ { |
| 712 |
// Sleep for a bit, otherwise we're just going to keep |
| 713 |
// stopping the world and no one will get to do anything. |
| 714 |
time.Sleep(100 * time.Millisecond) |
| 715 |
start := time.Now() |
| 716 |
runtime.ReadMemStats(&ms) |
| 717 |
latencies = append(latencies, time.Now().Sub(start)) |
| 718 |
} |
| 719 |
// Make sure to stop the timer before we wait! The load created above |
| 720 |
// is very heavy-weight and not easy to stop, so we could end up |
| 721 |
// confusing the benchmarking framework for small b.N. |
| 722 |
b.StopTimer() |
| 723 |
stop() |
| 724 |
|
| 725 |
// Disable the default */op metrics. |
| 726 |
// ns/op doesn't mean anything because it's an average, but we |
| 727 |
// have a sleep in our b.N loop above which skews this significantly. |
| 728 |
b.ReportMetric(0, "ns/op") |
| 729 |
b.ReportMetric(0, "B/op") |
| 730 |
b.ReportMetric(0, "allocs/op") |
| 731 |
|
| 732 |
// Sort latencies then report percentiles. |
| 733 |
sort.Slice(latencies, func(i, j int) bool { |
| 734 |
return latencies[i] < latencies[j] |
| 735 |
}) |
| 736 |
b.ReportMetric(float64(latencies[len(latencies)*50/100]), "p50-ns") |
| 737 |
b.ReportMetric(float64(latencies[len(latencies)*90/100]), "p90-ns") |
| 738 |
b.ReportMetric(float64(latencies[len(latencies)*99/100]), "p99-ns") |
| 739 |
} |
| 740 |
|
| 741 |
func TestUserForcedGC(t *testing.T) { |
| 742 |
// Test that runtime.GC() triggers a GC even if GOGC=off. |
| 743 |
defer debug.SetGCPercent(debug.SetGCPercent(-1)) |
| 744 |
|
| 745 |
var ms1, ms2 runtime.MemStats |
| 746 |
runtime.ReadMemStats(&ms1) |
| 747 |
runtime.GC() |
| 748 |
runtime.ReadMemStats(&ms2) |
| 749 |
if ms1.NumGC == ms2.NumGC { |
| 750 |
t.Fatalf("runtime.GC() did not trigger GC") |
| 751 |
} |
| 752 |
if ms1.NumForcedGC == ms2.NumForcedGC { |
| 753 |
t.Fatalf("runtime.GC() was not accounted in NumForcedGC") |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
func writeBarrierBenchmark(b *testing.B, f func()) { |
| 758 |
runtime.GC() |
| 759 |
var ms runtime.MemStats |
| 760 |
runtime.ReadMemStats(&ms) |
| 761 |
//b.Logf("heap size: %d MB", ms.HeapAlloc>>20) |
| 762 |
|
| 763 |
// Keep GC running continuously during the benchmark, which in |
| 764 |
// turn keeps the write barrier on continuously. |
| 765 |
var stop uint32 |
| 766 |
done := make(chan bool) |
| 767 |
go func() { |
| 768 |
for atomic.LoadUint32(&stop) == 0 { |
| 769 |
runtime.GC() |
| 770 |
} |
| 771 |
close(done) |
| 772 |
}() |
| 773 |
defer func() { |
| 774 |
atomic.StoreUint32(&stop, 1) |
| 775 |
<-done |
| 776 |
}() |
| 777 |
|
| 778 |
b.ResetTimer() |
| 779 |
f() |
| 780 |
b.StopTimer() |
| 781 |
} |
| 782 |
|
| 783 |
func BenchmarkWriteBarrier(b *testing.B) { |
| 784 |
if runtime.GOMAXPROCS(-1) < 2 { |
| 785 |
// We don't want GC to take our time. |
| 786 |
b.Skip("need GOMAXPROCS >= 2") |
| 787 |
} |
| 788 |
|
| 789 |
// Construct a large tree both so the GC runs for a while and |
| 790 |
// so we have a data structure to manipulate the pointers of. |
| 791 |
type node struct { |
| 792 |
l, r *node |
| 793 |
} |
| 794 |
var wbRoots []*node |
| 795 |
var mkTree func(level int) *node |
| 796 |
mkTree = func(level int) *node { |
| 797 |
if level == 0 { |
| 798 |
return nil |
| 799 |
} |
| 800 |
n := &node{mkTree(level - 1), mkTree(level - 1)} |
| 801 |
if level == 10 { |
| 802 |
// Seed GC with enough early pointers so it |
| 803 |
// doesn't start termination barriers when it |
| 804 |
// only has the top of the tree. |
| 805 |
wbRoots = append(wbRoots, n) |
| 806 |
} |
| 807 |
return n |
| 808 |
} |
| 809 |
const depth = 22 // 64 MB |
| 810 |
root := mkTree(22) |
| 811 |
|
| 812 |
writeBarrierBenchmark(b, func() { |
| 813 |
var stack [depth]*node |
| 814 |
tos := -1 |
| 815 |
|
| 816 |
// There are two write barriers per iteration, so i+=2. |
| 817 |
for i := 0; i < b.N; i += 2 { |
| 818 |
if tos == -1 { |
| 819 |
stack[0] = root |
| 820 |
tos = 0 |
| 821 |
} |
| 822 |
|
| 823 |
// Perform one step of reversing the tree. |
| 824 |
n := stack[tos] |
| 825 |
if n.l == nil { |
| 826 |
tos-- |
| 827 |
} else { |
| 828 |
n.l, n.r = n.r, n.l |
| 829 |
stack[tos] = n.l |
| 830 |
stack[tos+1] = n.r |
| 831 |
tos++ |
| 832 |
} |
| 833 |
|
| 834 |
if i%(1<<12) == 0 { |
| 835 |
// Avoid non-preemptible loops (see issue #10958). |
| 836 |
runtime.Gosched() |
| 837 |
} |
| 838 |
} |
| 839 |
}) |
| 840 |
|
| 841 |
runtime.KeepAlive(wbRoots) |
| 842 |
} |
| 843 |
|
| 844 |
func BenchmarkBulkWriteBarrier(b *testing.B) { |
| 845 |
if runtime.GOMAXPROCS(-1) < 2 { |
| 846 |
// We don't want GC to take our time. |
| 847 |
b.Skip("need GOMAXPROCS >= 2") |
| 848 |
} |
| 849 |
|
| 850 |
// Construct a large set of objects we can copy around. |
| 851 |
const heapSize = 64 << 20 |
| 852 |
type obj [16]*byte |
| 853 |
ptrs := make([]*obj, heapSize/unsafe.Sizeof(obj{})) |
| 854 |
for i := range ptrs { |
| 855 |
ptrs[i] = new(obj) |
| 856 |
} |
| 857 |
|
| 858 |
writeBarrierBenchmark(b, func() { |
| 859 |
const blockSize = 1024 |
| 860 |
var pos int |
| 861 |
for i := 0; i < b.N; i += blockSize { |
| 862 |
// Rotate block. |
| 863 |
block := ptrs[pos : pos+blockSize] |
| 864 |
first := block[0] |
| 865 |
copy(block, block[1:]) |
| 866 |
block[blockSize-1] = first |
| 867 |
|
| 868 |
pos += blockSize |
| 869 |
if pos+blockSize > len(ptrs) { |
| 870 |
pos = 0 |
| 871 |
} |
| 872 |
|
| 873 |
runtime.Gosched() |
| 874 |
} |
| 875 |
}) |
| 876 |
|
| 877 |
runtime.KeepAlive(ptrs) |
| 878 |
} |
| 879 |
|
| 880 |
func BenchmarkScanStackNoLocals(b *testing.B) { |
| 881 |
var ready sync.WaitGroup |
| 882 |
teardown := make(chan bool) |
| 883 |
for j := 0; j < 10; j++ { |
| 884 |
ready.Add(1) |
| 885 |
go func() { |
| 886 |
x := 100000 |
| 887 |
countpwg(&x, &ready, teardown) |
| 888 |
}() |
| 889 |
} |
| 890 |
ready.Wait() |
| 891 |
b.ResetTimer() |
| 892 |
for i := 0; i < b.N; i++ { |
| 893 |
b.StartTimer() |
| 894 |
runtime.GC() |
| 895 |
runtime.GC() |
| 896 |
b.StopTimer() |
| 897 |
} |
| 898 |
close(teardown) |
| 899 |
} |
| 900 |
|
| 901 |
func BenchmarkMSpanCountAlloc(b *testing.B) { |
| 902 |
// Allocate one dummy mspan for the whole benchmark. |
| 903 |
s := runtime.AllocMSpan() |
| 904 |
defer runtime.FreeMSpan(s) |
| 905 |
|
| 906 |
// n is the number of bytes to benchmark against. |
| 907 |
// n must always be a multiple of 8, since gcBits is |
| 908 |
// always rounded up 8 bytes. |
| 909 |
for _, n := range []int{8, 16, 32, 64, 128} { |
| 910 |
b.Run(fmt.Sprintf("bits=%d", n*8), func(b *testing.B) { |
| 911 |
// Initialize a new byte slice with pseduo-random data. |
| 912 |
bits := make([]byte, n) |
| 913 |
rand.Read(bits) |
| 914 |
|
| 915 |
b.ResetTimer() |
| 916 |
for i := 0; i < b.N; i++ { |
| 917 |
runtime.MSpanCountAlloc(s, bits) |
| 918 |
} |
| 919 |
}) |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
func countpwg(n *int, ready *sync.WaitGroup, teardown chan bool) { |
| 924 |
if *n == 0 { |
| 925 |
ready.Done() |
| 926 |
<-teardown |
| 927 |
return |
| 928 |
} |
| 929 |
*n-- |
| 930 |
countpwg(n, ready, teardown) |
| 931 |
} |