gitech
1// Copyright 2018 The Gitea Authors. All rights reserved.
2// SPDX-License-Identifier: MIT
3
4package pprof5
6import (7"fmt"8"os"9"runtime"10"runtime/pprof"11
12"code.gitea.io/gitea/modules/log"13)
14
15// DumpMemProfileForUsername dumps a memory profile at pprofDataPath as memprofile_<username>_<temporary id>
16func DumpMemProfileForUsername(pprofDataPath, username string) error {17f, err := os.CreateTemp(pprofDataPath, fmt.Sprintf("memprofile_%s_", username))18if err != nil {19return err20}21defer f.Close()22runtime.GC() // get up-to-date statistics23return pprof.WriteHeapProfile(f)24}
25
26// DumpCPUProfileForUsername dumps a CPU profile at pprofDataPath as cpuprofile_<username>_<temporary id>
27// the stop function it returns stops, writes and closes the CPU profile file
28func DumpCPUProfileForUsername(pprofDataPath, username string) (func(), error) {29f, err := os.CreateTemp(pprofDataPath, fmt.Sprintf("cpuprofile_%s_", username))30if err != nil {31return nil, err32}33
34err = pprof.StartCPUProfile(f)35if err != nil {36log.Fatal("StartCPUProfile: %v", err)37}38return func() {39pprof.StopCPUProfile()40err = f.Close()41if err != nil {42log.Fatal("StopCPUProfile Close: %v", err)43}44}, nil45}
46