summaryrefslogtreecommitdiffstats
path: root/modules/pprof/pprof.go
diff options
context:
space:
mode:
authorJerry Jacobs <xor-gate@users.noreply.github.com>2018-08-07 20:49:18 +0200
committertechknowlogick <techknowlogick@users.noreply.github.com>2018-08-07 14:49:18 -0400
commitb1bc08e2686e2aa73aaf2108fdc50d9e700def38 (patch)
tree5f3f155e6290176c4e0b0aaa1cc557d87df56210 /modules/pprof/pprof.go
parented3589f4296a17ee201f44dcf6e87f8bd5741d09 (diff)
downloadgitea-b1bc08e2686e2aa73aaf2108fdc50d9e700def38.tar.gz
gitea-b1bc08e2686e2aa73aaf2108fdc50d9e700def38.zip
cmd/serve: pprof cpu and memory profile dumps to disk (#4560)
Diffstat (limited to 'modules/pprof/pprof.go')
-rw-r--r--modules/pprof/pprof.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/modules/pprof/pprof.go b/modules/pprof/pprof.go
new file mode 100644
index 0000000000..7f4fc653f3
--- /dev/null
+++ b/modules/pprof/pprof.go
@@ -0,0 +1,42 @@
+// Copyright 2018 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package pprof
+
+import (
+ "fmt"
+ "io/ioutil"
+ "runtime"
+ "runtime/pprof"
+
+ "code.gitea.io/gitea/modules/log"
+)
+
+// DumpMemProfileForUsername dumps a memory profile at pprofDataPath as memprofile_<username>_<temporary id>
+func DumpMemProfileForUsername(pprofDataPath, username string) {
+ f, err := ioutil.TempFile(pprofDataPath, fmt.Sprintf("memprofile_%s_", username))
+ if err != nil {
+ log.GitLogger.Fatal(4, "Could not create memory profile: %v", err)
+ }
+ defer f.Close()
+ runtime.GC() // get up-to-date statistics
+ if err := pprof.WriteHeapProfile(f); err != nil {
+ log.GitLogger.Fatal(4, "Could not write memory profile: %v", err)
+ }
+}
+
+// DumpCPUProfileForUsername dumps a CPU profile at pprofDataPath as cpuprofile_<username>_<temporary id>
+// it returns the stop function which stops, writes and closes the CPU profile file
+func DumpCPUProfileForUsername(pprofDataPath, username string) func() {
+ f, err := ioutil.TempFile(pprofDataPath, fmt.Sprintf("cpuprofile_%s_", username))
+ if err != nil {
+ log.GitLogger.Fatal(4, "Could not create cpu profile: %v", err)
+ }
+
+ pprof.StartCPUProfile(f)
+ return func() {
+ pprof.StopCPUProfile()
+ f.Close()
+ }
+}