Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prof_block_rate option for enabling/configuring the block profile #4587

Merged
merged 1 commit into from
Sep 25, 2023
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
3 changes: 3 additions & 0 deletions server/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ type Options struct {
Websocket WebsocketOpts `json:"-"`
MQTT MQTTOpts `json:"-"`
ProfPort int `json:"-"`
ProfBlockRate int `json:"-"`
PidFile string `json:"-"`
PortsFileDir string `json:"-"`
LogFile string `json:"-"`
Expand Down Expand Up @@ -1013,6 +1014,8 @@ func (o *Options) processConfigFileLine(k string, v interface{}, errors *[]error
o.PortsFileDir = v.(string)
case "prof_port":
o.ProfPort = int(v.(int64))
case "prof_block_rate":
o.ProfBlockRate = int(v.(int64))
case "max_control_line":
if v.(int64) > 1<<31-1 {
err := &configErr{tk, fmt.Sprintf("%s value is too big", k)}
Expand Down
16 changes: 16 additions & 0 deletions server/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,16 @@ func (o *mqttInactiveThresholdReload) Apply(s *Server) {
s.Noticef("Reloaded: MQTT consumer_inactive_threshold = %v", o.newValue)
}

type profBlockRateReload struct {
noopOption
newValue int
}

func (o *profBlockRateReload) Apply(s *Server) {
s.setBlockProfileRate(o.newValue)
s.Noticef("Reloaded: block_prof_rate = %v", o.newValue)
}

type leafNodeOption struct {
noopOption
tlsFirstChanged bool
Expand Down Expand Up @@ -1589,6 +1599,12 @@ func (s *Server) diffOptions(newOpts *Options) ([]option, error) {
diffOpts = append(diffOpts, &ocspOption{newValue: newValue.(*OCSPConfig)})
case "ocspcacheconfig":
diffOpts = append(diffOpts, &ocspResponseCacheOption{newValue: newValue.(*OCSPResponseCacheConfig)})
case "profblockrate":
new := newValue.(int)
old := oldValue.(int)
if new != old {
diffOpts = append(diffOpts, &profBlockRateReload{newValue: new})
}
default:
// TODO(ik): Implement String() on those options to have a nice print.
// %v is difficult to figure what's what, %+v print private fields and
Expand Down
15 changes: 15 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,10 @@ func (s *Server) Start() {
// Pprof http endpoint for the profiler.
if opts.ProfPort != 0 {
s.StartProfiler()
} else {
// It's still possible to access this profile via a SYS endpoint, so set
// this anyway. (Otherwise StartProfiler would have called it.)
s.setBlockProfileRate(opts.ProfBlockRate)
derekcollison marked this conversation as resolved.
Show resolved Hide resolved
}

if opts.ConfigFile != _EMPTY_ {
Expand Down Expand Up @@ -2701,6 +2705,8 @@ func (s *Server) StartProfiler() {
s.profiler = l
s.profilingServer = srv

s.setBlockProfileRate(opts.ProfBlockRate)

go func() {
// if this errors out, it's probably because the server is being shutdown
err := srv.Serve(l)
Expand All @@ -2718,6 +2724,15 @@ func (s *Server) StartProfiler() {
s.mu.Unlock()
}

func (s *Server) setBlockProfileRate(rate int) {
// Passing i ProfBlockRate <= 0 here will disable or > 0 will enable.
runtime.SetBlockProfileRate(rate)

if rate > 0 {
s.Warnf("Block profiling is enabled (rate %d), this may have a performance impact", rate)
}
}

// StartHTTPMonitoring will enable the HTTP monitoring port.
// DEPRECATED: Should use StartMonitoring.
func (s *Server) StartHTTPMonitoring() {
Expand Down