Skip to content

Commit c1f0940

Browse files
author
Vitaliy Guschin
committed
Add an image with profiling enabled.
Signed-off-by: Vitaliy Guschin <[email protected]>
1 parent 41f4bc6 commit c1f0940

File tree

7 files changed

+82
-1
lines changed

7 files changed

+82
-1
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ jobs:
4040
uses: networkservicemesh/.github/.github/workflows/docker-release.yaml@main
4141
with:
4242
tag: ${{ needs.get-tag.outputs.tag }}
43+
build_with_profiler: true
4344
secrets:
4445
token: ${{ secrets.GITHUB_TOKEN }}
4546

Dockerfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ENV GO111MODULE=on
66
ENV CGO_ENABLED=0
77
ENV GOBIN=/bin
88
ARG BUILDARCH=amd64
9+
ARG ENABLE_PROFILER=false
910
RUN rm -r /etc/vpp
1011
RUN go install github.com/go-delve/delve/cmd/[email protected]
1112
RUN go install github.com/grpc-ecosystem/[email protected]
@@ -23,7 +24,11 @@ COPY ./internal/afxdp/afxdp.c ./internal/afxdp/
2324
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
2425
RUN go build ./internal/imports
2526
COPY . .
26-
RUN go build -o /bin/forwarder .
27+
RUN if [ "$ENABLE_PROFILER" = "true" ]; then \
28+
go build -tags profiler -o /bin/forwarder . ; \
29+
else \
30+
go build -o /bin/forwarder . ; \
31+
fi
2732

2833
FROM build as test
2934
CMD go test -test.v ./...

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ 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_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")
4950

5051
# Testing
5152

internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ 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+
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"`
6768
}
6869

6970
// Process reads config from env

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ func main() {
134134
log.EnableTracing(true)
135135
log.FromContext(ctx).WithField("duration", time.Since(now)).Infof("completed phase 1: get config from environment")
136136

137+
// Start profiling server
138+
startProfiler(ctx, cfg.ProfilerHTTPPort)
139+
137140
// ********************************************************************************
138141
// Configure Open Telemetry
139142
// ********************************************************************************

profiler_off.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2024 Pragmagic Inc. and/or its affiliates.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at:
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
//go:build !profiler
18+
19+
package main
20+
21+
import "context"
22+
23+
func startProfiler(_ context.Context, _ uint16) {}

profiler_on.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2024 Pragmagic Inc. and/or its affiliates.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at:
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
//go:build profiler
18+
19+
package main
20+
21+
import (
22+
"context"
23+
"fmt"
24+
"net/http"
25+
_ "net/http/pprof" // #nosec
26+
"time"
27+
28+
"github.com/networkservicemesh/sdk/pkg/tools/log"
29+
)
30+
31+
func startProfiler(ctx context.Context, profilerHTTPPort uint16) {
32+
go func() {
33+
log.FromContext(ctx).Infof("Profiler is enabled. Starting HTTP server on %d", profilerHTTPPort)
34+
35+
address := fmt.Sprintf("localhost:%d", profilerHTTPPort)
36+
37+
server := &http.Server{
38+
Addr: address,
39+
ReadTimeout: 10 * time.Second,
40+
WriteTimeout: 10 * time.Second,
41+
}
42+
43+
if err := server.ListenAndServe(); err != nil {
44+
log.FromContext(ctx).Errorf("Failed to start profiler: %s", err.Error())
45+
}
46+
}()
47+
}

0 commit comments

Comments
 (0)