diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-01-30 23:07:13 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-01-30 23:07:13 -0700 |
commit | 9eef4dd91c36b60a2d5a68141f1d0c07e25be129 (patch) | |
tree | 4754b06e54d7e888e636472007fc1bc87aa72d72 /lib/profile/profile.go | |
parent | 0134f07a4b97a455557277b2c89e0ee5ad6b2e62 (diff) | |
parent | 50a8b3eac39caccedb3ec34c150ba37e40cc2da5 (diff) |
Merge branch 'lukeshu/fast-json'
Diffstat (limited to 'lib/profile/profile.go')
-rw-r--r-- | lib/profile/profile.go | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/lib/profile/profile.go b/lib/profile/profile.go new file mode 100644 index 0000000..27c7e61 --- /dev/null +++ b/lib/profile/profile.go @@ -0,0 +1,89 @@ +// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com> +// +// SPDX-License-Identifier: GPL-2.0-or-later + +package profile + +import ( + "io" + "runtime/pprof" + "runtime/trace" +) + +type StopFunc = func() error + +type startFunc = func(io.Writer) (StopFunc, error) + +// CPU arranges to write a CPU profile to the given Writer, and +// returns a function to be called on shutdown. +func CPU(w io.Writer) (StopFunc, error) { + if err := pprof.StartCPUProfile(w); err != nil { + return nil, err + } + return func() error { + pprof.StopCPUProfile() + return nil + }, nil +} + +var _ startFunc = CPU + +// Profile arranges to write the given named-profile to the given +// Writer, and returns a function to be called on shutdown. +// +// CPU profiles are not named profiles; there is a separate .CPU() +// function for writing CPU profiles. +// +// The Go runtime has several built-in named profiles, and it is +// possible for programs to create their own named profiles with +// runtime/pprof.NewProfile(). +// +// This package provides ProfileXXX constants for the built-in named +// profiles, and a .Profiles() function that return the list of all +// profile names. +func Profile(w io.Writer, name string) (StopFunc, error) { + return func() error { + if prof := pprof.Lookup(name); prof != nil { + return prof.WriteTo(w, 0) + } + return nil + }, nil +} + +// Profiles returns a list of all profile names that may be passed to +// .Profile(); both profiles built-in to the Go runtime, and +// program-added profiles. +func Profiles() []string { + full := pprof.Profiles() + names := make([]string, len(full)) + for i, prof := range full { + names[i] = prof.Name() + } + return names +} + +// The Go runtime's built-in named profiles; to be passed to +// .Profile(). +const ( + ProfileGoroutine = "goroutine" + ProfileThreadCreate = "threadcreate" + ProfileHeap = "heap" + ProfileAllocs = "allocs" + ProfileBlock = "block" + ProfileMutex = "mutex" +) + +// Trace arranges to write a trace (https://pkg.go.dev/runtime/trace) +// to the given Writer, and returns a function to be called on +// shutdown. +func Trace(w io.Writer) (StopFunc, error) { + if err := trace.Start(w); err != nil { + return nil, err + } + return func() error { + trace.Stop() + return nil + }, nil +} + +var _ startFunc = Trace |