chore: improved perf stats
This commit is contained in:
@@ -16,7 +16,9 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/syncthing/syncthing/lib/locations"
|
||||||
"github.com/syncthing/syncthing/lib/protocol"
|
"github.com/syncthing/syncthing/lib/protocol"
|
||||||
|
"golang.org/x/exp/constraints"
|
||||||
)
|
)
|
||||||
|
|
||||||
func startPerfStats() {
|
func startPerfStats() {
|
||||||
@@ -29,37 +31,68 @@ func savePerfStats(file string) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var prevUsage int64
|
var prevTime time.Time
|
||||||
var prevTime int64
|
var curRus, prevRus syscall.Rusage
|
||||||
var rusage syscall.Rusage
|
var curMem, prevMem runtime.MemStats
|
||||||
var memstats runtime.MemStats
|
|
||||||
var prevIn, prevOut int64
|
var prevIn, prevOut int64
|
||||||
|
|
||||||
t0 := time.Now()
|
t0 := time.Now()
|
||||||
|
syscall.Getrusage(syscall.RUSAGE_SELF, &prevRus)
|
||||||
|
runtime.ReadMemStats(&prevMem)
|
||||||
|
|
||||||
|
fmt.Fprintf(fd, "TIME_S\tCPU_S\tHEAP_KIB\tRSS_KIB\tNETIN_KBPS\tNETOUT_KBPS\tDBSIZE_KIB\n")
|
||||||
|
|
||||||
for t := range time.NewTicker(250 * time.Millisecond).C {
|
for t := range time.NewTicker(250 * time.Millisecond).C {
|
||||||
if err := syscall.Getrusage(syscall.RUSAGE_SELF, &rusage); err != nil {
|
syscall.Getrusage(syscall.RUSAGE_SELF, &curRus)
|
||||||
continue
|
runtime.ReadMemStats(&curMem)
|
||||||
}
|
|
||||||
|
|
||||||
curTime := time.Now().UnixNano()
|
|
||||||
timeDiff := curTime - prevTime
|
|
||||||
curUsage := rusage.Utime.Nano() + rusage.Stime.Nano()
|
|
||||||
usageDiff := curUsage - prevUsage
|
|
||||||
cpuUsagePercent := 100 * float64(usageDiff) / float64(timeDiff)
|
|
||||||
prevTime = curTime
|
|
||||||
prevUsage = curUsage
|
|
||||||
in, out := protocol.TotalInOut()
|
in, out := protocol.TotalInOut()
|
||||||
var inRate, outRate float64
|
timeDiff := t.Sub(prevTime)
|
||||||
if timeDiff > 0 {
|
|
||||||
inRate = float64(in-prevIn) / (float64(timeDiff) / 1e9) // bytes per second
|
fmt.Fprintf(fd, "%.03f\t%f\t%d\t%d\t%.0f\t%.0f\t%d\n",
|
||||||
outRate = float64(out-prevOut) / (float64(timeDiff) / 1e9) // bytes per second
|
t.Sub(t0).Seconds(),
|
||||||
}
|
rate(cpusec(&prevRus), cpusec(&curRus), timeDiff, 1),
|
||||||
|
(curMem.Sys-curMem.HeapReleased)/1024,
|
||||||
|
curRus.Maxrss/1024,
|
||||||
|
rate(prevIn, in, timeDiff, 1e3),
|
||||||
|
rate(prevOut, out, timeDiff, 1e3),
|
||||||
|
dirsize(locations.Get(locations.Database))/1024,
|
||||||
|
)
|
||||||
|
|
||||||
|
prevTime = t
|
||||||
|
prevRus = curRus
|
||||||
|
prevMem = curMem
|
||||||
prevIn, prevOut = in, out
|
prevIn, prevOut = in, out
|
||||||
|
|
||||||
runtime.ReadMemStats(&memstats)
|
|
||||||
|
|
||||||
startms := int(t.Sub(t0).Seconds() * 1000)
|
|
||||||
|
|
||||||
fmt.Fprintf(fd, "%d\t%f\t%d\t%d\t%.0f\t%.0f\n", startms, cpuUsagePercent, memstats.Alloc, memstats.Sys-memstats.HeapReleased, inRate, outRate)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func cpusec(r *syscall.Rusage) float64 {
|
||||||
|
return float64(r.Utime.Nano()+r.Stime.Nano()) / float64(time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
type number interface {
|
||||||
|
constraints.Float | constraints.Integer
|
||||||
|
}
|
||||||
|
|
||||||
|
func rate[T number](prev, cur T, d time.Duration, div float64) float64 {
|
||||||
|
diff := cur - prev
|
||||||
|
rate := float64(diff) / d.Seconds() / div
|
||||||
|
return rate
|
||||||
|
}
|
||||||
|
|
||||||
|
func dirsize(location string) int64 {
|
||||||
|
entries, err := os.ReadDir(location)
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
var size int64
|
||||||
|
for _, entry := range entries {
|
||||||
|
fi, err := entry.Info()
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
size += fi.Size()
|
||||||
|
}
|
||||||
|
|
||||||
|
return size
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user