-
Notifications
You must be signed in to change notification settings - Fork 720
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
api: add pprof zip api #4017
Merged
Merged
api: add pprof zip api #4017
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
eeec548
add debug zip
bufferflies b0d7344
change zip name and time format
bufferflies 478af7e
import、annotation、log
bufferflies 4645aad
pass ut
bufferflies d512bf8
add unit test
bufferflies 6ab8b30
pass ut
bufferflies 48672d2
Merge branch 'master' into feature/pprofAll
bufferflies 7d73d85
refine api route
bufferflies c2f2ec7
Merge branch 'feature/pprofAll' of github.com:bufferflies/pd into fea…
bufferflies 7d92428
pass ut
bufferflies 7286366
remove new prof handler
bufferflies 6ab26f3
code lint
bufferflies bba56e0
Merge branch 'master' into feature/pprofAll
nolouch b04e1f0
Merge branch 'master' into feature/pprofAll
ti-chi-bot f869b98
Merge branch 'master' into feature/pprofAll
ti-chi-bot addbf3e
Merge branch 'master' into feature/pprofAll
ti-chi-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// Copyright 2021 TiKV Project Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package api | ||
|
||
import ( | ||
"archive/zip" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"runtime" | ||
"runtime/pprof" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/pingcap/log" | ||
"github.com/tikv/pd/server" | ||
"github.com/tikv/pd/server/versioninfo" | ||
"github.com/unrolled/render" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// ProfHandler pprof handler | ||
type ProfHandler struct { | ||
svr *server.Server | ||
rd *render.Render | ||
} | ||
|
||
// newProfHandler constructor for ProfHandler | ||
func newProfHandler(svr *server.Server, rd *render.Render) *ProfHandler { | ||
return &ProfHandler{ | ||
svr: svr, | ||
rd: rd, | ||
} | ||
|
||
} | ||
|
||
// @Summary debug zip of PD servers. | ||
// @Produce application/octet-stream | ||
// @Router /debug/zip [get] | ||
func (h *ProfHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="pd_debug"`+time.Now().Format("20060102_150405")+".zip")) | ||
|
||
// dump goroutine/heap/mutex | ||
items := []struct { | ||
name string | ||
gc int | ||
debug int | ||
second int | ||
}{ | ||
{name: "goroutine", debug: 2}, | ||
{name: "heap", gc: 1}, | ||
{name: "mutex"}, | ||
{name: "allocs"}, | ||
} | ||
zw := zip.NewWriter(w) | ||
for _, item := range items { | ||
p := pprof.Lookup(item.name) | ||
if p == nil { | ||
h.rd.JSON(w, http.StatusNotFound, fmt.Sprintf("pprof can not find name:%s", item.name)) | ||
bufferflies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return | ||
} | ||
if item.gc > 0 { | ||
runtime.GC() | ||
} | ||
fw, err := zw.Create(item.name) | ||
if err != nil { | ||
h.rd.JSON(w, http.StatusInternalServerError, fmt.Sprintf("Create zipped %s fail: %v", item.name, err)) | ||
return | ||
} | ||
err = p.WriteTo(fw, item.debug) | ||
if err != nil { | ||
log.Error("write failed", zap.Error(err)) | ||
} | ||
} | ||
|
||
// dump profile | ||
fw, err := zw.Create("profile") | ||
if err != nil { | ||
h.rd.JSON(w, http.StatusInternalServerError, fmt.Sprintf("create zip %s failed :%v", "profile", err)) | ||
return | ||
} | ||
if err := pprof.StartCPUProfile(fw); err != nil { | ||
h.rd.JSON(w, http.StatusInternalServerError, fmt.Sprintf("Could not enable CPU profiling: %v", err)) | ||
return | ||
} | ||
sec, err := strconv.ParseInt(r.FormValue("seconds"), 10, 64) | ||
if sec <= 0 || err != nil { | ||
sec = 30 | ||
} | ||
sleepWithCtx(r.Context(), time.Duration(sec)*time.Second) | ||
pprof.StopCPUProfile() | ||
|
||
// dump config | ||
fw, err = zw.Create("config") | ||
if err != nil { | ||
h.rd.JSON(w, http.StatusInternalServerError, fmt.Sprintf("Create zipped %s fail: %v", "config", err)) | ||
return | ||
} | ||
// dump config all | ||
config := h.svr.GetConfig() | ||
js, err := json.Marshal(config) | ||
if err != nil { | ||
h.rd.JSON(w, http.StatusInternalServerError, fmt.Sprintf("json marshal config info fail: %v", err)) | ||
return | ||
} | ||
if _, err = fw.Write(js); err != nil { | ||
log.Error("write config failed", zap.Error(err)) | ||
} | ||
|
||
// dump version | ||
fw, err = zw.Create("version") | ||
if err != nil { | ||
h.rd.JSON(w, http.StatusInternalServerError, fmt.Sprintf("Create zipped %s fail: %v", "version", err)) | ||
return | ||
} | ||
versions, err := json.Marshal(&version{ | ||
Version: versioninfo.PDReleaseVersion, | ||
Branch: versioninfo.PDGitBranch, | ||
BuildTime: versioninfo.PDBuildTS, | ||
Hash: versioninfo.PDGitHash, | ||
}) | ||
if err != nil { | ||
log.Error("json marshal version failed", zap.Error(err)) | ||
} | ||
if _, err = fw.Write(versions); err != nil { | ||
log.Error("write version failed", zap.Error(err)) | ||
} | ||
|
||
if err = zw.Close(); err != nil { | ||
log.Error("zip close error", zap.Error(err)) | ||
} | ||
} | ||
|
||
func sleepWithCtx(ctx context.Context, d time.Duration) { | ||
select { | ||
case <-time.After(d): | ||
case <-ctx.Done(): | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The format is a little bit different with TiDB.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, it will be better to splite date and time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can let tidb with the new format.