mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
runtime/pprof: move common code to writeProfileInternal function
This patch provides changes according to TODO. Since writeMutex and writeBlock functions have a lot of code in common, it is better to move this code to one function. Change-Id: I81aaad067b0cb1647824909f3b5f6861add3a7ba Reviewed-on: https://go-review.googlesource.com/c/go/+/280152 Reviewed-by: Cherry Zhang <cherryyz@google.com> Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
This commit is contained in:
parent
e4253cd023
commit
79d03ad739
1 changed files with 13 additions and 45 deletions
|
|
@ -843,45 +843,7 @@ func countMutex() int {
|
||||||
|
|
||||||
// writeBlock writes the current blocking profile to w.
|
// writeBlock writes the current blocking profile to w.
|
||||||
func writeBlock(w io.Writer, debug int) error {
|
func writeBlock(w io.Writer, debug int) error {
|
||||||
var p []runtime.BlockProfileRecord
|
return writeProfileInternal(w, debug, "contention", runtime.BlockProfile, scaleBlockProfile)
|
||||||
n, ok := runtime.BlockProfile(nil)
|
|
||||||
for {
|
|
||||||
p = make([]runtime.BlockProfileRecord, n+50)
|
|
||||||
n, ok = runtime.BlockProfile(p)
|
|
||||||
if ok {
|
|
||||||
p = p[:n]
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Slice(p, func(i, j int) bool { return p[i].Cycles > p[j].Cycles })
|
|
||||||
|
|
||||||
if debug <= 0 {
|
|
||||||
return printCountCycleProfile(w, "contentions", "delay", scaleBlockProfile, p)
|
|
||||||
}
|
|
||||||
|
|
||||||
b := bufio.NewWriter(w)
|
|
||||||
tw := tabwriter.NewWriter(w, 1, 8, 1, '\t', 0)
|
|
||||||
w = tw
|
|
||||||
|
|
||||||
fmt.Fprintf(w, "--- contention:\n")
|
|
||||||
fmt.Fprintf(w, "cycles/second=%v\n", runtime_cyclesPerSecond())
|
|
||||||
for i := range p {
|
|
||||||
r := &p[i]
|
|
||||||
fmt.Fprintf(w, "%v %v @", r.Cycles, r.Count)
|
|
||||||
for _, pc := range r.Stack() {
|
|
||||||
fmt.Fprintf(w, " %#x", pc)
|
|
||||||
}
|
|
||||||
fmt.Fprint(w, "\n")
|
|
||||||
if debug > 0 {
|
|
||||||
printStackRecord(w, r.Stack(), true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if tw != nil {
|
|
||||||
tw.Flush()
|
|
||||||
}
|
|
||||||
return b.Flush()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func scaleBlockProfile(cnt int64, ns float64) (int64, float64) {
|
func scaleBlockProfile(cnt int64, ns float64) (int64, float64) {
|
||||||
|
|
@ -894,12 +856,16 @@ func scaleBlockProfile(cnt int64, ns float64) (int64, float64) {
|
||||||
|
|
||||||
// writeMutex writes the current mutex profile to w.
|
// writeMutex writes the current mutex profile to w.
|
||||||
func writeMutex(w io.Writer, debug int) error {
|
func writeMutex(w io.Writer, debug int) error {
|
||||||
// TODO(pjw): too much common code with writeBlock. FIX!
|
return writeProfileInternal(w, debug, "mutex", runtime.MutexProfile, scaleMutexProfile)
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeProfileInternal writes the current blocking or mutex profile depending on the passed parameters
|
||||||
|
func writeProfileInternal(w io.Writer, debug int, name string, runtimeProfile func([]runtime.BlockProfileRecord) (int, bool), scaleProfile func(int64, float64) (int64, float64)) error {
|
||||||
var p []runtime.BlockProfileRecord
|
var p []runtime.BlockProfileRecord
|
||||||
n, ok := runtime.MutexProfile(nil)
|
n, ok := runtimeProfile(nil)
|
||||||
for {
|
for {
|
||||||
p = make([]runtime.BlockProfileRecord, n+50)
|
p = make([]runtime.BlockProfileRecord, n+50)
|
||||||
n, ok = runtime.MutexProfile(p)
|
n, ok = runtimeProfile(p)
|
||||||
if ok {
|
if ok {
|
||||||
p = p[:n]
|
p = p[:n]
|
||||||
break
|
break
|
||||||
|
|
@ -909,16 +875,18 @@ func writeMutex(w io.Writer, debug int) error {
|
||||||
sort.Slice(p, func(i, j int) bool { return p[i].Cycles > p[j].Cycles })
|
sort.Slice(p, func(i, j int) bool { return p[i].Cycles > p[j].Cycles })
|
||||||
|
|
||||||
if debug <= 0 {
|
if debug <= 0 {
|
||||||
return printCountCycleProfile(w, "contentions", "delay", scaleMutexProfile, p)
|
return printCountCycleProfile(w, "contentions", "delay", scaleProfile, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
b := bufio.NewWriter(w)
|
b := bufio.NewWriter(w)
|
||||||
tw := tabwriter.NewWriter(w, 1, 8, 1, '\t', 0)
|
tw := tabwriter.NewWriter(w, 1, 8, 1, '\t', 0)
|
||||||
w = tw
|
w = tw
|
||||||
|
|
||||||
fmt.Fprintf(w, "--- mutex:\n")
|
fmt.Fprintf(w, "--- %v:\n", name)
|
||||||
fmt.Fprintf(w, "cycles/second=%v\n", runtime_cyclesPerSecond())
|
fmt.Fprintf(w, "cycles/second=%v\n", runtime_cyclesPerSecond())
|
||||||
|
if name == "mutex" {
|
||||||
fmt.Fprintf(w, "sampling period=%d\n", runtime.SetMutexProfileFraction(-1))
|
fmt.Fprintf(w, "sampling period=%d\n", runtime.SetMutexProfileFraction(-1))
|
||||||
|
}
|
||||||
for i := range p {
|
for i := range p {
|
||||||
r := &p[i]
|
r := &p[i]
|
||||||
fmt.Fprintf(w, "%v %v @", r.Cycles, r.Count)
|
fmt.Fprintf(w, "%v %v @", r.Cycles, r.Count)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue