Skip to content
22 changes: 22 additions & 0 deletions cmd/serv.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"runtime/pprof"
"strings"
"time"

Expand Down Expand Up @@ -42,6 +45,9 @@ var CmdServ = cli.Command{
Value: "custom/conf/app.ini",
Usage: "Custom configuration file path",
},
cli.BoolFlag{
Name: "enable-pprof",
},
},
}

Expand Down Expand Up @@ -93,6 +99,18 @@ func fail(userMessage, logMessage string, args ...interface{}) {
os.Exit(1)
}

func pprofDumpMemProfileForUsername(username string) {
f, err := ioutil.TempFile("", fmt.Sprintf("gitea_serve_pprof_%s_", username))
if err != nil {
log.GitLogger.Fatal(4, "Could not create memory profile: %v", err)
}
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)
}
f.Close()
}

func runServ(c *cli.Context) error {
if c.IsSet("config") {
setting.CustomConf = c.String("config")
Expand Down Expand Up @@ -143,6 +161,10 @@ func runServ(c *cli.Context) error {
username := strings.ToLower(rr[0])
reponame := strings.ToLower(strings.TrimSuffix(rr[1], ".git"))

if setting.EnablePprof || c.Bool("enable-pprof") {
defer pprofDumpMemProfileForUsername(username)
}

isWiki := false
unitType := models.UnitTypeCode
if strings.HasSuffix(reponame, ".wiki") {
Expand Down
4 changes: 4 additions & 0 deletions custom/conf/app.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ STATIC_ROOT_PATH =
APP_DATA_PATH = data
; Application level GZIP support
ENABLE_GZIP = false
; Application profiling (memory and cpu)
; For "web" command it listens on localhost:6060
; For "serve" command it dumps to disk at $TMPDIR/gitea_serve_pprof_<username>_id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to define this path as a setting? If not that's fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats a nice addition, I will name it PPROF_FILE_PATH. Good? Should we only allow absolute paths or also expand e.g $TMPDIR ? Absolute paths are much easier to implement.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be treated same as other paths in Gitea

ENABLE_PPROF = false
; Landing page, can be "home", "explore", or "organizations"
LANDING_PAGE = home
; Enables git-lfs support. true or false, default is false.
Expand Down