Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions server/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import (
"encoding/json"
"errors"
"fmt"
"math"
"math/rand"
"net/http"
"runtime"
"runtime/debug"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -376,6 +378,8 @@ type ServerStats struct {
Gateways []*GatewayStat `json:"gateways,omitempty"`
ActiveServers int `json:"active_servers,omitempty"`
JetStream *JetStreamVarz `json:"jetstream,omitempty"`
MemLimit int64 `json:"gomemlimit,omitempty"`
MaxProcs int `json:"gomaxprocs,omitempty"`
}

// RouteStat holds route statistics.
Expand Down Expand Up @@ -821,6 +825,10 @@ func (s *Server) updateServerUsage(v *ServerStats) {
var vss int64
pse.ProcUsage(&v.CPU, &v.Mem, &vss)
v.Cores = runtime.NumCPU()
v.MaxProcs = runtime.GOMAXPROCS(-1)
if mm := debug.SetMemoryLimit(-1); mm < math.MaxInt64 {
v.MemLimit = mm
}
}

// Generate a route stat for our statz update.
Expand Down
33 changes: 33 additions & 0 deletions server/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"net/http/httptest"
"os"
"reflect"
"runtime"
"runtime/debug"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -3771,3 +3773,34 @@ func TestServerEventsPingStatsSlowConsumersStats(t *testing.T) {
})
}
}

func TestServerEventsStatszMaxProcsMemLimit(t *testing.T) {
// We want to prove that our set values are reflected in STATSZ,
// so we can't use constants that might match the system that
// the test is run on.
omp, omm := runtime.GOMAXPROCS(-1), debug.SetMemoryLimit(-1)
mp, mm := runtime.GOMAXPROCS(omp*2)*2, debug.SetMemoryLimit(omm/2)/2

// When we're done, put everything back.
defer runtime.GOMAXPROCS(omp)
defer debug.SetMemoryLimit(omm)

s, opts := runTrustedServer(t)
defer s.Shutdown()

acc, akp := createAccount(s)
s.setSystemAccount(acc)

url := fmt.Sprintf("nats://%s:%d", opts.Host, opts.Port)
ncs, err := nats.Connect(url, createUserCreds(t, s, akp))
require_NoError(t, err)
defer ncs.Close()

msg, err := ncs.Request("$SYS.REQ.SERVER.PING.STATSZ", nil, time.Second)
require_NoError(t, err)

var stats ServerStatsMsg
require_NoError(t, json.Unmarshal(msg.Data, &stats))
require_Equal(t, stats.Stats.MaxProcs, mp)
require_Equal(t, stats.Stats.MemLimit, mm)
}
6 changes: 6 additions & 0 deletions server/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import (
"encoding/json"
"expvar"
"fmt"
"math"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"runtime/pprof"
"slices"
"sort"
Expand Down Expand Up @@ -1215,6 +1217,7 @@ type Varz struct {
Mem int64 `json:"mem"`
Cores int `json:"cores"`
MaxProcs int `json:"gomaxprocs"`
MemLimit int64 `json:"gomemlimit,omitempty"`
CPU float64 `json:"cpu"`
Connections int `json:"connections"`
TotalConnections uint64 `json:"total_connections"`
Expand Down Expand Up @@ -1605,6 +1608,9 @@ func (s *Server) createVarz(pcpu float64, rss int64) *Varz {
TrustedOperatorsJwt: opts.operatorJWT,
TrustedOperatorsClaim: opts.TrustedOperators,
}
if mm := debug.SetMemoryLimit(-1); mm < math.MaxInt64 {
varz.MemLimit = mm
}
// If this is a leaf without cluster, reset the cluster name (that is otherwise
// set to the server name).
if s.leafNoCluster {
Expand Down
Loading