Skip to content

Commit

Permalink
Support heap profiling for TiFlash (#1718)
Browse files Browse the repository at this point in the history
  • Loading branch information
CalvinNeo authored Aug 28, 2024
1 parent 100828c commit 6a0d342
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
31 changes: 31 additions & 0 deletions pkg/apiserver/profiling/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,37 @@ type tiflashFetcher struct {
}

func (f *tiflashFetcher) fetch(op *fetchOptions) ([]byte, error) {
if strings.HasSuffix(op.path, "heap") {
scheme := f.client.GetHTTPScheme()
cmd := exec.Command("perl", "/dev/stdin", "--raw", scheme+"://"+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 tiflash 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 tiflash 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
2 changes: 1 addition & 1 deletion pkg/apiserver/profiling/pprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (f *fetcher) FetchAndWriteToFile(duration uint, fileNameWithoutExt string,
fileExtenstion = "*.proto"
case ProfilingTypeHeap:
url = "/debug/pprof/heap"
if f.target.Kind == model.NodeKindTiKV {
if f.target.Kind == model.NodeKindTiKV || f.target.Kind == model.NodeKindTiFlash {
profilingRawDataType = RawDataTypeJeprof
fileExtenstion = "*.prof"
} else {
Expand Down
4 changes: 2 additions & 2 deletions pkg/apiserver/profiling/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func profileAndWritePprof(_ context.Context, fts *fetchers, target *model.Reques
}
return fetchPprof(&pprofOptions{duration: profileDurationSecs, fileNameWithoutExt: fileNameWithoutExt, target: target, fetcher: &fts.tikv, profilingType: profilingType})
case model.NodeKindTiFlash:
// TiFlash only supports CPU Profiling
if profilingType != ProfilingTypeCPU {
// TiFlash only supports CPU/heap Profiling
if profilingType != ProfilingTypeCPU && profilingType != ProfilingTypeHeap {
return "", "", ErrUnsupportedProfilingType.NewWithNoMessage()
}
return fetchPprof(&pprofOptions{duration: profileDurationSecs, fileNameWithoutExt: fileNameWithoutExt, target: target, fetcher: &fts.tiflash, profilingType: profilingType})
Expand Down
4 changes: 4 additions & 0 deletions pkg/tiflash/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func NewTiFlashClient(lc fx.Lifecycle, httpClient *httpc.Client, config *config.
return client
}

func (c Client) GetHTTPScheme() string {
return c.httpScheme
}

func (c Client) WithTimeout(timeout time.Duration) *Client {
c.timeout = timeout
return &c
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ export default function Page() {
break
default:
}
} else if (component === 'tiflash' && profile_type === 'heap') {
switch (action) {
case Action.VIEW_FLAMEGRAPH:
dataFormat = 'text'
break
case Action.VIEW_GRAPH:
dataFormat = 'svg'
break
case Action.DOWNLOAD:
dataFormat = 'jeprof'
break
default:
}
} else {
switch (action) {
case Action.VIEW_GRAPH:
Expand Down

0 comments on commit 6a0d342

Please sign in to comment.