Skip to content

Commit 640786d

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

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

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: 23 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" // #nosec
7377
)
7478

7579
func main() {
@@ -134,6 +138,25 @@ 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+
148+
server := &http.Server{
149+
Addr: address,
150+
ReadTimeout: 10 * time.Second,
151+
WriteTimeout: 10 * time.Second,
152+
}
153+
154+
if err = server.ListenAndServe(); err != nil {
155+
log.FromContext(ctx).Errorf("Failed to start profiler: %s", err.Error())
156+
}
157+
}()
158+
}
159+
137160
// ********************************************************************************
138161
// Configure Open Telemetry
139162
// ********************************************************************************

0 commit comments

Comments
 (0)