Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

profiling: Add the support for TiKV heap profiling #1600

Merged
merged 13 commits into from
Oct 30, 2023
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