Skip to content

Commit

Permalink
profiling: Add the support for TiKV heap profiling (#1600)
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor1996 authored Oct 30, 2023
1 parent 58f855e commit 9e2ba5e
Show file tree
Hide file tree
Showing 8 changed files with 5,855 additions and 19 deletions.
38 changes: 38 additions & 0 deletions pkg/apiserver/profiling/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
package profiling

import (
_ "embed"
"fmt"
"io"
"os/exec"
"strconv"
"strings"
"time"

"go.uber.org/fx"
Expand Down Expand Up @@ -64,7 +69,40 @@ type tikvFetcher struct {
client *tikv.Client
}

//go:embed jeprof.in
var jeprof string

func (f *tikvFetcher) fetch(op *fetchOptions) ([]byte, error) {
if strings.HasSuffix(op.path, "heap") {
cmd := exec.Command("perl", "/dev/stdin", "--raw", "http://"+op.ip+":"+strconv.Itoa(op.port)+op.path) //nolint:gosec
cmd.Stdin = strings.NewReader(jeprof)
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
// use jeprof to fetch tikv heap profile
err = cmd.Start()
if err != nil {
return nil, err
}
data, err := io.ReadAll(stdout)
if err != nil {
return nil, err
}
errMsg, err := io.ReadAll(stderr)
if err != nil {
return nil, err
}
err = cmd.Wait()
if err != nil {
return nil, fmt.Errorf("failed to fetch tikv heap profile: %s", errMsg)
}
return data, nil
}
return f.client.WithTimeout(maxProfilingTimeout).AddRequestHeader("Content-Type", "application/protobuf").SendGetRequest(op.ip, op.port, op.path)
}

Expand Down
Loading

0 comments on commit 9e2ba5e

Please sign in to comment.