Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
profiler: add extension hook for C allocation profiling (#1264)
This is a first pass at adding an internal bridge for integrating C allocation profiling with the regular heap profile. C allocation profiling will require cgo and possibly other external dependencies and non-standard linker flags. As such, the main profiler package should not directly import the C allocation profiler implementation. But there still needs to be some way to actually _use_ the allocation profiler if it's available. This commit adds a bridge for doing so, and makes a few changes to enable actually running the profile and merging it with the heap profile.
- Loading branch information
Showing
4 changed files
with
147 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2022 Datadog, Inc. | ||
|
||
// Package extensions provides an interface for using optional features. | ||
// | ||
// Features such as C allocation profiling might require cgo, unsafe code, and | ||
// external non-Go dependencies which might not be desirable for typical users. | ||
// The main profiler package should not import any package implementing such | ||
// features directly as doing so may have undesired side effects. This package | ||
// provides a bridge between the implementation of such optional features and | ||
// the main profiler package. | ||
package extensions | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/google/pprof/profile" | ||
) | ||
|
||
// CAllocationProfiler is the interface for profiling allocations done through | ||
// the standard malloc/calloc/realloc APIs. | ||
// | ||
// A CAllocationProfiler implementation is not necessarily safe to use from | ||
// multiple goroutines concurrently. | ||
type CAllocationProfiler interface { | ||
// Start begins sampling C allocations at the given rate, in bytes. | ||
// There will be an average of one sample for every rate bytes | ||
// allocated. | ||
Start(rate int) | ||
// Stop cancels ongoing C allocation profiling and returns the resulting | ||
// profile. The profile will have the correct sample types such that it | ||
// can be merged with the Go heap profile. Returns a non-nil error if | ||
// any part of the profiling failed. | ||
Stop() (*profile.Profile, error) | ||
} | ||
|
||
var ( | ||
mu sync.Mutex | ||
cAllocationProfiler CAllocationProfiler | ||
) | ||
|
||
// GetCAllocationProfiler returns the currently registered C allocation | ||
// profiler, if one is registered. | ||
func GetCAllocationProfiler() (impl CAllocationProfiler, registered bool) { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
if cAllocationProfiler == nil { | ||
return nil, false | ||
} | ||
return cAllocationProfiler, true | ||
} | ||
|
||
// SetCAllocationProfiler registers a C allocation profiler implementation. | ||
func SetCAllocationProfiler(c CAllocationProfiler) { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
cAllocationProfiler = c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters