Skip to content

Commit

Permalink
added thread count to runtime stats. go never closes OS threads once …
Browse files Browse the repository at this point in the history
…opened, see: golang/go#14592
  • Loading branch information
simonmittag committed Jun 8, 2022
1 parent 848ed34 commit 0d70874
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package j8a
import (
"fmt"
"github.com/hako/durafmt"
"github.com/rs/zerolog/log"
"github.com/shirou/gopsutil/process"
"os"
"runtime/pprof"
"sync"
"time"

"github.com/rs/zerolog/log"
"github.com/shirou/gopsutil/process"
)

type sample struct {
Expand All @@ -21,6 +21,7 @@ type sample struct {
time time.Time
dwnOpenTcpCons uint64
dwnMaxOpenTcpCons uint64
threads int
}

type growthRate struct {
Expand All @@ -45,8 +46,9 @@ const pidMemPct = "pidMemPct"
const pidRssBytes = "pidRssBytes"
const pidVmsBytes = "pidVmsBytes"
const pidSwapBytes = "pidSwapBytes"
const pidDwnOpenTcpCons = "pidDwnOpenTcpCons"
const pidDwnMaxOpenTcpCons = "pidDwnMaxOpenTcpCons"
const pidDwnOpenTcpConns = "pidDwnOpenTcpConns"
const pidDwnMaxOpenTcpConns = "pidDwnMaxOpenTcpConns"
const pidOSThreads = "pidOSThreads"
const serverPerformance = "server performance"
const pcd2f = "%.2f"

Expand All @@ -57,11 +59,12 @@ func (s sample) log() {
Int32(pid, s.pid).
Str(pidCPUCorePct, fmt.Sprintf(pcd2f, s.cpuPc)).
Str(pidMemPct, fmt.Sprintf(pcd2f, s.mPc)).
Uint64(pidDwnOpenTcpCons, s.dwnOpenTcpCons).
Uint64(pidDwnMaxOpenTcpCons, s.dwnMaxOpenTcpCons).
Uint64(pidDwnOpenTcpConns, s.dwnOpenTcpCons).
Uint64(pidDwnMaxOpenTcpConns, s.dwnMaxOpenTcpCons).
Uint64(pidRssBytes, s.rssBytes).
Uint64(pidVmsBytes, s.vmsBytes).
Uint64(pidSwapBytes, s.swapBytes).
Int(pidOSThreads, s.threads).
Msg(serverPerformance)
}

Expand Down Expand Up @@ -108,6 +111,7 @@ High:
func (rt *Runtime) getSample(proc *process.Process) sample {
procStatsLock.Lock()

var threadProfile = pprof.Lookup("threadcreate")
cpuPc, _ := proc.Percent(time.Millisecond * cpuSampleMilliSeconds)
mPc, _ := proc.MemoryPercent()
mInfo, _ := proc.MemoryInfo()
Expand All @@ -123,6 +127,7 @@ func (rt *Runtime) getSample(proc *process.Process) sample {
time: time.Now(),
dwnOpenTcpCons: rt.ConnectionWatcher.Count(),
dwnMaxOpenTcpCons: rt.ConnectionWatcher.MaxCount(),
threads: threadProfile.Count(),
}
}

Expand Down

0 comments on commit 0d70874

Please sign in to comment.