1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-08 05:47:07 +09:00

GO-2005: Separate builds for desktop and mobile devices; add doc

This commit is contained in:
Sergey 2023-10-06 12:29:57 +05:00
parent e376522d1b
commit a027c384a0
No known key found for this signature in database
GPG key ID: 3B6BEF79160221C6
4 changed files with 63 additions and 44 deletions

View file

@ -1,59 +1,23 @@
//go:build !gomobile
package profiler
import (
"bytes"
"compress/gzip"
"context"
"encoding/base64"
"fmt"
"runtime"
"runtime/pprof"
"time"
"github.com/anyproto/any-sync/app"
"github.com/anyproto/anytype-heart/pkg/lib/logging"
)
var log = logging.Logger("profiler")
type Service interface {
app.ComponentRunnable
}
const (
highMemoryUsageThreshold = 1024 * 1024 * 1024 // 1 Gb
highMemoryUsageThreshold = 1024 * 1024 // 1 Gb
maxProfiles = 3
growthFactor = 1.5
)
type service struct {
closeCh chan struct{}
timesHighMemoryUsageDetected int
previousHighMemoryDetected uint64
}
func New() Service {
return &service{
closeCh: make(chan struct{}),
}
}
func (s *service) Init(a *app.App) (err error) {
return nil
}
func (s *service) Name() (name string) {
return "profiler"
}
func (s *service) Run(ctx context.Context) (err error) {
go s.run()
return nil
}
func (s *service) run() {
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
@ -101,6 +65,7 @@ func (s *service) detect() (stop bool, err error) {
}
gzipWriter.Close()
// To extract profile from logged string use `base64 -d | gzip -d`
log.With("sysMemory", s.previousHighMemoryDetected, "profile", base64.StdEncoding.EncodeToString(buf.Bytes())).Error("high memory usage detected, logging memory profile")
s.timesHighMemoryUsageDetected++
@ -111,8 +76,3 @@ func (s *service) detect() (stop bool, err error) {
return false, nil
}
func (s *service) Close(ctx context.Context) (err error) {
close(s.closeCh)
return nil
}

View file

@ -0,0 +1,5 @@
//go:build gomobile
package profiler
func (s *service) run() {}

View file

@ -0,0 +1,47 @@
package profiler
import (
"context"
"github.com/anyproto/any-sync/app"
"github.com/anyproto/anytype-heart/pkg/lib/logging"
)
var log = logging.Logger("profiler")
type Service interface {
app.ComponentRunnable
}
type service struct {
closeCh chan struct{}
timesHighMemoryUsageDetected int
previousHighMemoryDetected uint64
}
func New() Service {
return &service{
closeCh: make(chan struct{}),
}
}
func (s *service) Init(a *app.App) (err error) {
return nil
}
func (s *service) Name() (name string) {
return "profiler"
}
func (s *service) Run(ctx context.Context) (err error) {
go s.run()
return nil
}
func (s *service) Close(ctx context.Context) (err error) {
close(s.closeCh)
return nil
}

View file

@ -83,6 +83,13 @@ grpcurl -import-path ../anytype-heart/ -proto pb/protos/service/service.proto -p
echo '{"details": {"name": "hello there", "type": "ot-page"}}' | grpcurl -import-path ../anytype-heart/ -proto pb/protos/service/service.proto -plaintext -d @ localhost:31007 anytype.ClientCommands.ObjectCreate
```
### High memory usage detector
We have service for detecting high memory usage (core/debug/profiler package). It logs profiles, compressed via gzip and represented as base64 string.
To analyze profile:
1) Copy profile value string from log (from Graylog in instance)
2) Decode base64 and decompress gzip. Example for macOS: `pbpaste | base64 -d | gzip -d > mem.profile`
3) Analyze via `go tool pprof mem.profile`
## Running with prometheus and grafana
- `cd metrics/docker` cd into folder with docker-compose file
- `docker-compose up` - run the prometheus/grafana