From e892656f4178a2ba38da6872236a676b8982e58c Mon Sep 17 00:00:00 2001 From: Sean Pollock Date: Fri, 13 Dec 2024 12:38:16 -0500 Subject: [PATCH] Automate pprof insertion (#29) --- .gitignore | 1 + Makefile | 24 +++++++++++++++++++++--- pprof.go | 17 +++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 pprof.go diff --git a/.gitignore b/.gitignore index 444ffa8..75b7431 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ FFmpeg *.DS_Store bin module.tar.gz +.build_tags diff --git a/Makefile b/Makefile index cd89cef..dcf6f4e 100644 --- a/Makefile +++ b/Makefile @@ -5,9 +5,16 @@ TARGET_ARCH ?= $(SOURCE_ARCH) normalize_arch = $(if $(filter aarch64,$(1)),arm64,$(if $(filter x86_64,$(1)),amd64,$(1))) SOURCE_ARCH := $(call normalize_arch,$(SOURCE_ARCH)) TARGET_ARCH := $(call normalize_arch,$(TARGET_ARCH)) +PPROF_ENABLED ?= false +BUILD_TAGS ?= + +ifeq ($(PPROF_ENABLED),true) + BUILD_TAGS := $(BUILD_TAGS) pprof +endif BIN_OUTPUT_PATH = bin/$(TARGET_OS)-$(TARGET_ARCH) TOOL_BIN = bin/gotools/$(shell uname -s)-$(shell uname -m) +BUILD_TAG_FILE := .build_tags FFMPEG_TAG ?= n6.1 FFMPEG_VERSION ?= $(shell pwd)/FFmpeg/$(FFMPEG_TAG) @@ -47,11 +54,17 @@ GOFLAGS := -buildvcs=false export PKG_CONFIG_PATH=$(FFMPEG_BUILD)/lib/pkgconfig export PATH := $(PATH):$(shell go env GOPATH)/bin -.PHONY: lint tool-install test clean module +.PHONY: lint tool-install test clean module build build-pprof + +build: $(BIN_OUTPUT_PATH)/video-store -$(BIN_OUTPUT_PATH)/video-store: *.go cam/*.go $(FFMPEG_BUILD) +build-pprof: + $(MAKE) build PPROF_ENABLED=true + +$(BIN_OUTPUT_PATH)/video-store: *.go cam/*.go $(FFMPEG_BUILD) $(BUILD_TAG_FILE) go mod tidy - CGO_LDFLAGS="$(CGO_LDFLAGS)" CGO_CFLAGS=$(CGO_CFLAGS) go build -o $(BIN_OUTPUT_PATH)/video-store main.go + CGO_LDFLAGS="$(CGO_LDFLAGS)" CGO_CFLAGS=$(CGO_CFLAGS) go build -tags "$(BUILD_TAGS)" -o $(BIN_OUTPUT_PATH)/video-store main.go + echo "$(BUILD_TAGS)" > $(BUILD_TAG_FILE) $(FFMPEG_VERSION_PLATFORM): git clone https://github.com/FFmpeg/FFmpeg.git --depth 1 --branch $(FFMPEG_TAG) $(FFMPEG_VERSION_PLATFORM) @@ -73,6 +86,10 @@ endif endif cd $(FFMPEG_VERSION_PLATFORM) && ./configure $(FFMPEG_OPTS) && $(MAKE) -j$(NPROC) && $(MAKE) install +# Force rebuild if BUILD_TAGS change +$(BUILD_TAG_FILE): + echo "$(BUILD_TAGS)" > $(BUILD_TAG_FILE) + tool-install: GOBIN=`pwd`/$(TOOL_BIN) go install \ github.com/edaniels/golinters/cmd/combined \ @@ -107,5 +124,6 @@ module: $(BIN_OUTPUT_PATH)/video-store clean: rm -rf $(BIN_OUTPUT_PATH) + rm -f $(BUILD_TAG_FILE) rm -rf FFmpeg git clean -fxd diff --git a/pprof.go b/pprof.go new file mode 100644 index 0000000..cb6a242 --- /dev/null +++ b/pprof.go @@ -0,0 +1,17 @@ +//go:build pprof +// +build pprof + +package main + +import ( + "log" + "net/http" + _ "net/http/pprof" +) + +func init() { + go func() { + log.Println("Starting pprof server on :6060") + log.Println(http.ListenAndServe("localhost:6060", nil)) + }() +}