Source file src/runtime/pprof/runtime.go

     1  // Copyright 2017 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 pprof
     6  
     7  import (
     8  	"context"
     9  	"runtime"
    10  	"unsafe"
    11  )
    12  
    13  // runtime_FrameStartLine is defined in runtime/symtab.go.
    14  //
    15  //go:noescape
    16  func runtime_FrameStartLine(f *runtime.Frame) int
    17  
    18  // runtime_FrameSymbolName is defined in runtime/symtab.go.
    19  //
    20  //go:noescape
    21  func runtime_FrameSymbolName(f *runtime.Frame) string
    22  
    23  // runtime_expandFinalInlineFrame is defined in runtime/symtab.go.
    24  func runtime_expandFinalInlineFrame(stk []uintptr) []uintptr
    25  
    26  // runtime_setProfLabel is defined in runtime/proflabel.go.
    27  func runtime_setProfLabel(labels unsafe.Pointer)
    28  
    29  // runtime_getProfLabel is defined in runtime/proflabel.go.
    30  func runtime_getProfLabel() unsafe.Pointer
    31  
    32  // runtime_goroutineleakcount is defined in runtime/proc.go.
    33  func runtime_goroutineleakcount() int
    34  
    35  // runtime_goroutineLeakGC is defined in runtime/mgc.go.
    36  func runtime_goroutineLeakGC()
    37  
    38  // SetGoroutineLabels sets the current goroutine's labels to match ctx.
    39  // A new goroutine inherits the labels of the goroutine that created it.
    40  // This is a lower-level API than [Do], which should be used instead when possible.
    41  func SetGoroutineLabels(ctx context.Context) {
    42  	ctxLabels, _ := ctx.Value(labelContextKey{}).(*labelMap)
    43  	runtime_setProfLabel(unsafe.Pointer(ctxLabels))
    44  }
    45  
    46  // Do calls f with a copy of the parent context with the
    47  // given labels added to the parent's label map.
    48  // Goroutines spawned while executing f will inherit the augmented label-set.
    49  // Each key/value pair in labels is inserted into the label map in the
    50  // order provided, overriding any previous value for the same key.
    51  // The augmented label map will be set for the duration of the call to f
    52  // and restored once f returns.
    53  func Do(ctx context.Context, labels LabelSet, f func(context.Context)) {
    54  	defer SetGoroutineLabels(ctx)
    55  	ctx = WithLabels(ctx, labels)
    56  	SetGoroutineLabels(ctx)
    57  	f(ctx)
    58  }
    59  

View as plain text