...

Source file src/internal/trace/v2/testdata/testprog/gc-stress.go

Documentation: internal/trace/v2/testdata/testprog

     1  // Copyright 2023 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  // Tests a GC-heavy program. This is useful for shaking out
     6  // all sorts of corner cases about GC-related ranges.
     7  
     8  //go:build ignore
     9  
    10  package main
    11  
    12  import (
    13  	"log"
    14  	"math/rand"
    15  	"os"
    16  	"runtime"
    17  	"runtime/trace"
    18  	"time"
    19  )
    20  
    21  type node struct {
    22  	children [4]*node
    23  	data     [128]byte
    24  }
    25  
    26  func makeTree(depth int) *node {
    27  	if depth == 0 {
    28  		return new(node)
    29  	}
    30  	return &node{
    31  		children: [4]*node{
    32  			makeTree(depth - 1),
    33  			makeTree(depth - 1),
    34  			makeTree(depth - 1),
    35  			makeTree(depth - 1),
    36  		},
    37  	}
    38  }
    39  
    40  var trees [16]*node
    41  var ballast *[16]*[8192]*node
    42  var sink []byte
    43  
    44  func main() {
    45  	for i := range trees {
    46  		trees[i] = makeTree(6)
    47  	}
    48  	ballast = new([16]*[8192]*node)
    49  	for i := range ballast {
    50  		ballast[i] = new([8192]*node)
    51  		for j := range ballast[i] {
    52  			ballast[i][j] = &node{
    53  				data: [128]byte{1, 2, 3, 4},
    54  			}
    55  		}
    56  	}
    57  	for i := 0; i < runtime.GOMAXPROCS(-1); i++ {
    58  		go func() {
    59  			for {
    60  				sink = make([]byte, rand.Intn(32<<10))
    61  			}
    62  		}()
    63  	}
    64  	// Increase the chance that we end up starting and stopping
    65  	// mid-GC by only starting to trace after a few milliseconds.
    66  	time.Sleep(5 * time.Millisecond)
    67  
    68  	// Start tracing.
    69  	if err := trace.Start(os.Stdout); err != nil {
    70  		log.Fatalf("failed to start tracing: %v", err)
    71  	}
    72  	defer trace.Stop()
    73  
    74  	// Let the tracing happen for a bit.
    75  	time.Sleep(400 * time.Millisecond)
    76  }
    77  

View as plain text