1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-11 18:20:33 +09:00

GO-3564: Add debug stat to profiler

This commit is contained in:
Sergey 2024-06-20 09:23:58 +02:00
parent 03eae2079e
commit d267d90838
No known key found for this signature in database
GPG key ID: 3B6BEF79160221C6
2 changed files with 23 additions and 2 deletions

View file

@ -10,10 +10,12 @@ import (
"os"
"runtime/pprof"
"runtime/trace"
"strings"
"time"
"github.com/anyproto/any-sync/app"
debug2 "github.com/anyproto/anytype-heart/core/debug"
"github.com/anyproto/anytype-heart/pkg/lib/logging"
"github.com/anyproto/anytype-heart/util/debug"
)
@ -27,7 +29,8 @@ type Service interface {
}
type service struct {
closeCh chan struct{}
closeCh chan struct{}
debugService debug2.Debug
timesHighMemoryUsageDetected int
previousHighMemoryDetected uint64
@ -40,6 +43,7 @@ func New() Service {
}
func (s *service) Init(a *app.App) (err error) {
s.debugService = app.MustComponent[debug2.Debug](a)
return nil
}
@ -54,6 +58,7 @@ func (s *service) Run(ctx context.Context) (err error) {
}
func (s *service) RunProfiler(ctx context.Context, seconds int) (string, error) {
// Start
var tracerBuf bytes.Buffer
err := trace.Start(&tracerBuf)
if err != nil {
@ -71,15 +76,20 @@ func (s *service) RunProfiler(ctx context.Context, seconds int) (string, error)
if err != nil {
return "", fmt.Errorf("write starting heap profile: %w", err)
}
goroutinesStart := debug.Stack(true)
statsStart, err := s.debugService.DebugStat()
if err != nil {
return "", fmt.Errorf("get starting debug stat: %w", err)
}
// Wait
select {
case <-time.After(time.Duration(seconds) * time.Second):
case <-ctx.Done():
case <-s.closeCh:
}
// End
pprof.StopCPUProfile()
trace.Stop()
var heapEndBuf bytes.Buffer
@ -88,7 +98,12 @@ func (s *service) RunProfiler(ctx context.Context, seconds int) (string, error)
return "", fmt.Errorf("write ending heap profile: %w", err)
}
goroutinesEnd := debug.Stack(true)
statsEnd, err := s.debugService.DebugStat()
if err != nil {
return "", fmt.Errorf("get ending debug stat: %w", err)
}
// Write
f, err := os.CreateTemp("", "anytype_profile.*.zip")
if err != nil {
return "", fmt.Errorf("create temp file: %w", err)
@ -100,6 +115,8 @@ func (s *service) RunProfiler(ctx context.Context, seconds int) (string, error)
{name: "heap_end", data: &heapEndBuf},
{name: "goroutines_start.txt", data: bytes.NewReader(goroutinesStart)},
{name: "goroutines_end.txt", data: bytes.NewReader(goroutinesEnd)},
{name: "debug_stats_start.txt", data: strings.NewReader(statsStart)},
{name: "debug_stats_end.txt", data: strings.NewReader(statsEnd)},
})
if err != nil {
return "", errors.Join(fmt.Errorf("create zip archive: %w", err), f.Close())

View file

@ -10,6 +10,7 @@ import (
"os"
"path"
"path/filepath"
"sort"
"strings"
"time"
@ -145,6 +146,9 @@ func (d *debug) SpaceSummary(ctx context.Context, spaceID string) (summary Space
func (d *debug) DebugStat() (string, error) {
stats := d.statService.GetStat()
sort.Slice(stats.Stats, func(i, j int) bool {
return stats.Stats[i].Type < stats.Stats[j].Type
})
marshaled, err := json.Marshal(stats)
if err != nil {
return "", err