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

View as plain text