github.com/go-darwin/sys@v0.0.0-20220510002607-68fd01f054ca/testdata/testprog/vdso.go (about)

     1  // Copyright 2019 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  // Invoke signal hander in the VDSO context (see issue 32912).
     6  
     7  package main
     8  
     9  import (
    10  	"fmt"
    11  	"os"
    12  	"runtime/pprof"
    13  	"time"
    14  )
    15  
    16  func init() {
    17  	register("SignalInVDSO", signalInVDSO)
    18  }
    19  
    20  func signalInVDSO() {
    21  	f, err := os.CreateTemp("", "timeprofnow")
    22  	if err != nil {
    23  		fmt.Fprintln(os.Stderr, err)
    24  		os.Exit(2)
    25  	}
    26  
    27  	if err := pprof.StartCPUProfile(f); err != nil {
    28  		fmt.Fprintln(os.Stderr, err)
    29  		os.Exit(2)
    30  	}
    31  
    32  	t0 := time.Now()
    33  	t1 := t0
    34  	// We should get a profiling signal 100 times a second,
    35  	// so running for 1 second should be sufficient.
    36  	for t1.Sub(t0) < time.Second {
    37  		t1 = time.Now()
    38  	}
    39  
    40  	pprof.StopCPUProfile()
    41  
    42  	name := f.Name()
    43  	if err := f.Close(); err != nil {
    44  		fmt.Fprintln(os.Stderr, err)
    45  		os.Exit(2)
    46  	}
    47  
    48  	if err := os.Remove(name); err != nil {
    49  		fmt.Fprintln(os.Stderr, err)
    50  		os.Exit(2)
    51  	}
    52  
    53  	fmt.Println("success")
    54  }