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

View as plain text