diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8fcf39e9..dc5e766c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -40,6 +40,7 @@ jobs: uses: networkservicemesh/.github/.github/workflows/docker-release.yaml@main with: tag: ${{ needs.get-tag.outputs.tag }} + build_with_profiler: true secrets: token: ${{ secrets.GITHUB_TOKEN }} diff --git a/Dockerfile b/Dockerfile index e0006515..32bb8195 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,6 +6,7 @@ ENV GO111MODULE=on ENV CGO_ENABLED=0 ENV GOBIN=/bin ARG BUILDARCH=amd64 +ARG ENABLE_PROFILER=false RUN rm -r /etc/vpp RUN go install github.com/go-delve/delve/cmd/dlv@v1.21.0 RUN go install github.com/grpc-ecosystem/grpc-health-probe@v0.4.25 @@ -23,7 +24,11 @@ COPY ./internal/afxdp/afxdp.c ./internal/afxdp/ RUN clang -O3 -g -Wextra -Wall -target bpf -I/usr/include/$(uname -m)-linux-gnu -I/usr/include -c -o /bin/afxdp.o ./internal/afxdp/afxdp.c RUN go build ./internal/imports COPY . . -RUN go build -o /bin/forwarder . +RUN if [ "$ENABLE_PROFILER" = "true" ]; then \ + go build -tags profiler -o /bin/forwarder . ; \ + else \ + go build -o /bin/forwarder . ; \ + fi FROM build as test CMD go test -test.v ./... diff --git a/README.md b/README.md index edecd8e6..44d6913f 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ docker build . * `NSM_CGROUP_PATH` - path to the host cgroup directory * `NSM_VFIO_PATH` - path to the host VFIO directory * `NSM_MECHANISM_PRIORITY` - sets priorities for mechanisms +* `NSM_PROFILER_HTTP_PORT` - Profiling server HTTP port providing data in the format expected by the pprof visualization tool. Profiler is running only on images with the "-pprof" tag suffix (default: "6060") # Testing diff --git a/internal/config/config.go b/internal/config/config.go index 9e9c5498..df93a246 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -64,6 +64,7 @@ type Config struct { CgroupPath string `default:"/host/sys/fs/cgroup/devices" desc:"path to the host cgroup directory" split_words:"true"` VFIOPath string `default:"/host/dev/vfio" desc:"path to the host VFIO directory" split_words:"true"` MechanismPriority []string `default:"" desc:"sets priorities for mechanisms" split_words:"true"` + ProfilerHTTPPort uint16 `default:"6060" desc:"Profiling server HTTP port providing data in the format expected by the pprof visualization tool. Profiler is running only on images with the '-pprof' tag suffix" split_words:"true"` } // Process reads config from env diff --git a/main.go b/main.go index dcea1705..87e1aabb 100644 --- a/main.go +++ b/main.go @@ -134,6 +134,9 @@ func main() { log.EnableTracing(true) log.FromContext(ctx).WithField("duration", time.Since(now)).Infof("completed phase 1: get config from environment") + // Start profiling server + startProfiler(ctx, cfg.ProfilerHTTPPort) + // ******************************************************************************** // Configure Open Telemetry // ******************************************************************************** diff --git a/profiler_off.go b/profiler_off.go new file mode 100644 index 00000000..fce981af --- /dev/null +++ b/profiler_off.go @@ -0,0 +1,23 @@ +// Copyright (c) 2024 Pragmagic Inc. and/or its affiliates. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build !profiler + +package main + +import "context" + +func startProfiler(_ context.Context, _ uint16) {} diff --git a/profiler_on.go b/profiler_on.go new file mode 100644 index 00000000..ba0e05ba --- /dev/null +++ b/profiler_on.go @@ -0,0 +1,47 @@ +// Copyright (c) 2024 Pragmagic Inc. and/or its affiliates. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build profiler + +package main + +import ( + "context" + "fmt" + "net/http" + _ "net/http/pprof" // #nosec + "time" + + "github.com/networkservicemesh/sdk/pkg/tools/log" +) + +func startProfiler(ctx context.Context, profilerHTTPPort uint16) { + go func() { + log.FromContext(ctx).Infof("Profiler is enabled. Starting HTTP server on %d", profilerHTTPPort) + + address := fmt.Sprintf("localhost:%d", profilerHTTPPort) + + server := &http.Server{ + Addr: address, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + } + + if err := server.ListenAndServe(); err != nil { + log.FromContext(ctx).Errorf("Failed to start profiler: %s", err.Error()) + } + }() +}