github.com/ice-blockchain/go/src@v0.0.0-20240403114104-1564d284e521/internal/trace/v2/testdata/testprog/gc-stress.go (about)

     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  
    58  	procs := runtime.GOMAXPROCS(-1)
    59  	sink = make([][]byte, procs)
    60  
    61  	for i := 0; i < procs; i++ {
    62  		i := i
    63  		go func() {
    64  			for {
    65  				sink[i] = make([]byte, rand.Intn(32<<10))
    66  			}
    67  		}()
    68  	}
    69  	// Increase the chance that we end up starting and stopping
    70  	// mid-GC by only starting to trace after a few milliseconds.
    71  	time.Sleep(5 * time.Millisecond)
    72  
    73  	// Start tracing.
    74  	if err := trace.Start(os.Stdout); err != nil {
    75  		log.Fatalf("failed to start tracing: %v", err)
    76  	}
    77  	defer trace.Stop()
    78  
    79  	// Let the tracing happen for a bit.
    80  	time.Sleep(400 * time.Millisecond)
    81  }