Skip to content

Commit bd9d47b

Browse files
author
Vitaliy Guschin
committed
Add configuration for turning profiling on/off.
Signed-off-by: Vitaliy Guschin <[email protected]>
1 parent 41f4bc6 commit bd9d47b

File tree

5 files changed

+26
-0
lines changed

5 files changed

+26
-0
lines changed

.golangci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ linters-settings:
5757
simple: true
5858
range-loops: true
5959
for-loops: false
60+
gosec:
61+
excludes:
62+
- G108
63+
- G114
6064
gocritic:
6165
enabled-checks:
6266
- appendAssign

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ docker build .
4646
* `NSM_CGROUP_PATH` - path to the host cgroup directory
4747
* `NSM_VFIO_PATH` - path to the host VFIO directory
4848
* `NSM_MECHANISM_PRIORITY` - sets priorities for mechanisms
49+
* `NSM_ENABLE_PROFILER` - Enable pprof package HTTP server for profiling runtime data.
50+
* `NSM_PROFILER_HTTP_PORT` - Profiling server HTTP port providing data in the format expected by the pprof visualization tool.
4951

5052
# Testing
5153

internal/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ type Config struct {
6464
CgroupPath string `default:"/host/sys/fs/cgroup/devices" desc:"path to the host cgroup directory" split_words:"true"`
6565
VFIOPath string `default:"/host/dev/vfio" desc:"path to the host VFIO directory" split_words:"true"`
6666
MechanismPriority []string `default:"" desc:"sets priorities for mechanisms" split_words:"true"`
67+
EnableProfiler bool `default:"false" desc:"Enable pprof package HTTP server for profiling runtime data. The handled paths all begin with /debug/pprof/." split_words:"true"`
68+
ProfilerHTTPPort uint16 `default:"6060" desc:"Profiling server HTTP port providing data in the format expected by the pprof visualization tool." split_words:"true"`
6769
}
6870

6971
// Process reads config from env

internal/imports/imports_linux.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package main
2222
import (
2323
"context"
2424
"crypto/tls"
25+
"fmt"
2526
"os"
2627
"os/signal"
2728
"path"
@@ -70,6 +71,9 @@ import (
7071
"github.com/networkservicemesh/cmd-forwarder-vpp/internal/devicecfg"
7172
"github.com/networkservicemesh/cmd-forwarder-vpp/internal/vppinit"
7273
"github.com/networkservicemesh/cmd-forwarder-vpp/internal/xconnectns"
74+
75+
"net/http"
76+
_ "net/http/pprof"
7377
)
7478

7579
func main() {
@@ -134,6 +138,18 @@ func main() {
134138
log.EnableTracing(true)
135139
log.FromContext(ctx).WithField("duration", time.Since(now)).Infof("completed phase 1: get config from environment")
136140

141+
// Enable profiling server
142+
if cfg.EnableProfiler {
143+
go func() {
144+
log.FromContext(ctx).Infof("Profiler is enabled. Starting HTTP server on %d", cfg.ProfilerHTTPPort)
145+
146+
address := fmt.Sprintf("localhost:%d", cfg.ProfilerHTTPPort)
147+
if err := http.ListenAndServe(address, nil); err != nil {
148+
log.FromContext(ctx).Errorf("Failed to start profiler: %s", err.Error())
149+
}
150+
}()
151+
}
152+
137153
// ********************************************************************************
138154
// Configure Open Telemetry
139155
// ********************************************************************************

0 commit comments

Comments
 (0)