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

NETOBSERV-754: add pprof, improve memory footprint #449

Merged
merged 5 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions .mk/development.mk
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,19 @@ set-release-kind-downstream:
@echo -e "\n==> Redeploying..."
kubectl rollout status -n $(NAMESPACE) --timeout=60s deployment netobserv-controller-manager
kubectl wait -n $(NAMESPACE) --timeout=60s --for condition=Available=True deployment netobserv-controller-manager

.PHONY: pprof
pprof:
@echo -e "\n==> Enabling pprof... Check https://github.com/netobserv/network-observability-operator/blob/main/DEVELOPMENT.md#profiling for help."
kubectl -n $(NAMESPACE) set env deployment netobserv-controller-manager -c "manager" PROFILING_BIND_ADDRESS=:6060
@echo -e "\n==> Redeploying..."
kubectl rollout status -n $(NAMESPACE) --timeout=60s deployment netobserv-controller-manager
kubectl wait -n $(NAMESPACE) --timeout=60s --for condition=Available=True deployment netobserv-controller-manager
sleep 2
$(MAKE) pprof-pf

.PHONY: pprof-pf
pprof-pf:
@echo -e "\n==> Port-forwarding..."
oc get pods
kubectl port-forward -n $(NAMESPACE) $(shell kubectl get pod -l app=netobserv-operator -o jsonpath="{.items[0].metadata.name}") 6060
15 changes: 15 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,18 @@ Remove the tag after you tested:
git tag -d "0.0.0-rc0"
git push --delete upstream 0.0.0-rc0
```

## Profiling

You can use `pprof` for profiling. Run `pprof` make target to start listening and port-forward on 6060:

```bash
make pprof
```

In another terminal, run for instance:

```bash
curl "http://localhost:6060/debug/pprof/heap?gc" -o /tmp/heap
go tool pprof -http localhost:3435 /tmp/heap
```
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ spec:
- --flowlogs-pipeline-image=$(RELATED_IMAGE_FLOWLOGS_PIPELINE)
- --console-plugin-image=$(RELATED_IMAGE_CONSOLE_PLUGIN)
- --downstream-deployment=$(DOWNSTREAM_DEPLOYMENT)
- --profiling-bind-address=$(PROFILING_BIND_ADDRESS)
command:
- /manager
env:
Expand All @@ -698,6 +699,7 @@ spec:
value: quay.io/netobserv/network-observability-console-plugin:v0.1.11
- name: DOWNSTREAM_DEPLOYMENT
value: "false"
- name: PROFILING_BIND_ADDRESS
image: quay.io/netobserv/network-observability-operator:1.0.4
imagePullPolicy: Always
livenessProbe:
Expand Down
3 changes: 3 additions & 0 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ spec:
- --flowlogs-pipeline-image=$(RELATED_IMAGE_FLOWLOGS_PIPELINE)
- --console-plugin-image=$(RELATED_IMAGE_CONSOLE_PLUGIN)
- --downstream-deployment=$(DOWNSTREAM_DEPLOYMENT)
- --profiling-bind-address=$(PROFILING_BIND_ADDRESS)
env:
- name: RELATED_IMAGE_EBPF_AGENT
value: quay.io/netobserv/netobserv-ebpf-agent:v0.3.2
Expand All @@ -37,6 +38,8 @@ spec:
value: quay.io/netobserv/network-observability-console-plugin:v0.1.11
- name: DOWNSTREAM_DEPLOYMENT
value: "false"
- name: PROFILING_BIND_ADDRESS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not call it PROFILE_PORT since what we pass here is is just the port like we do in the agent and pls add profile md file to doc with examples of the different collections with HOWTOs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the operator already uses this kind of addresses (e.g. for its metric server) so I prefer to be consistent within this repo
I've added some doc in DEVELOPMENT.md

value: ""
image: controller:latest
name: manager
imagePullPolicy: Always
Expand Down
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
"os"

"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -81,12 +83,14 @@ func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
var pprofAddr string
var versionFlag bool

config := operator.Config{}

flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.StringVar(&pprofAddr, "profiling-bind-address", "", "The address the profiling endpoint binds to, such as ':6060'. Leave unset to disable profiling.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
Expand Down Expand Up @@ -116,6 +120,14 @@ func main() {
os.Exit(1)
}

if pprofAddr != "" {
setupLog.WithValues("address", pprofAddr).Info("starting profiling server")
go func() {
err := http.ListenAndServe(pprofAddr, nil)
setupLog.Error(err, "stopped profiling server")
}()
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: server.Options{
Expand Down