-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from VerizonMedia/add-golang-support
Golang Support via BPF
- Loading branch information
Showing
25 changed files
with
749 additions
and
300 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
ARG KERNEL_VERSION=4.9.125 | ||
|
||
FROM linuxkit/kernel:$KERNEL_VERSION AS ksrc | ||
|
||
FROM golang:1.14-buster as agentbuild | ||
WORKDIR /go/src/github.com/VerizonMedia/kubectl-flame | ||
ADD . /go/src/github.com/VerizonMedia/kubectl-flame | ||
RUN go get -d -v ./... | ||
RUN cd agent && go build -o /go/bin/agent | ||
|
||
FROM alpine as builder | ||
COPY --from=ksrc /kernel-dev.tar / | ||
RUN tar xf /kernel-dev.tar -C / | ||
RUN mv /usr/src/*/ /usr/src/kernel-source/ | ||
RUN apk add git | ||
RUN git clone https://github.com/brendangregg/FlameGraph | ||
RUN git clone https://gist.github.com/edeNFed/83a9438156288661e2283c28fee18b8b bcc-profiler | ||
|
||
FROM alpine | ||
COPY --from=builder /usr/src /usr/src | ||
RUN apk add bcc-tools perl | ||
RUN ln -s $(which python3) /usr/bin/python | ||
RUN mkdir -p /app/FlameGraph | ||
COPY --from=builder /FlameGraph /app/FlameGraph | ||
COPY --from=agentbuild /go/bin/agent /app | ||
COPY --from=builder /bcc-profiler /app/bcc-profiler/ | ||
RUN chmod +x /app/bcc-profiler/profile | ||
|
||
CMD [ "/app/agent" ] |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package profiler | ||
|
||
import ( | ||
"fmt" | ||
"github.com/VerizonMedia/kubectl-flame/agent/details" | ||
"github.com/VerizonMedia/kubectl-flame/agent/utils" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
const ( | ||
kernelSourcesDir = "/usr/src/kernel-source/" | ||
profilerLocation = "/app/bcc-profiler/profile" | ||
rawProfilerOutputFile = "/tmp/raw_profile.txt" | ||
flameGraphScriptLocation = "/app/FlameGraph/flamegraph.pl" | ||
flameGraphOutputLocation = "/tmp/flamegraph.svg" | ||
) | ||
|
||
type BpfProfiler struct{} | ||
|
||
func (b *BpfProfiler) SetUp(job *details.ProfilingJob) error { | ||
exitCode, kernelVersion, err := utils.ExecuteCommand(exec.Command("uname", "-r")) | ||
if err != nil { | ||
return fmt.Errorf("failed to get kernel version, exit code: %d, error: %s", exitCode, err) | ||
} | ||
|
||
expectedSourcesLocation, err := os.Readlink(fmt.Sprintf("/lib/modules/%s/build", | ||
strings.TrimSuffix(kernelVersion, "\n"))) | ||
if err != nil { | ||
return fmt.Errorf("failed to read source link, error: %s", err) | ||
} | ||
|
||
return b.moveSources(expectedSourcesLocation) | ||
} | ||
|
||
func (b *BpfProfiler) Invoke(job *details.ProfilingJob) error { | ||
err := b.runProfiler(job) | ||
if err != nil { | ||
return fmt.Errorf("profiling failed: %s", err) | ||
} | ||
|
||
err = b.generateFlameGraph() | ||
if err != nil { | ||
return fmt.Errorf("flamegraph generation failed: %s", err) | ||
} | ||
|
||
return utils.PublishFlameGraph(flameGraphOutputLocation) | ||
} | ||
|
||
func (b *BpfProfiler) runProfiler(job *details.ProfilingJob) error { | ||
pid, err := utils.FindProcessId(job) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
f, err := os.Create(rawProfilerOutputFile) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
duration := strconv.Itoa(int(job.Duration.Seconds())) | ||
profileCmd := exec.Command(profilerLocation, "-df", "-p", pid, duration) | ||
profileCmd.Stdout = f | ||
|
||
return profileCmd.Run() | ||
} | ||
|
||
func (b *BpfProfiler) generateFlameGraph() error { | ||
inputFile, err := os.Open(rawProfilerOutputFile) | ||
if err != nil { | ||
return err | ||
} | ||
defer inputFile.Close() | ||
|
||
outputFile, err := os.Create(flameGraphOutputLocation) | ||
if err != nil { | ||
return err | ||
} | ||
defer outputFile.Close() | ||
|
||
flameGraphCmd := exec.Command(flameGraphScriptLocation) | ||
flameGraphCmd.Stdin = inputFile | ||
flameGraphCmd.Stdout = outputFile | ||
|
||
return flameGraphCmd.Run() | ||
} | ||
|
||
func (b *BpfProfiler) moveSources(target string) error { | ||
parent, _ := filepath.Split(target) | ||
err := os.MkdirAll(parent, os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, _, err = utils.ExecuteCommand(exec.Command("mv", kernelSourcesDir, target)) | ||
if err != nil { | ||
return fmt.Errorf("failed moving source files, error: %s, tried to move to: %s", err, target) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.