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

Expose BPF map kernel memory use by tracing policy #2984

Merged
merged 14 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions api/v1/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion api/v1/tetragon/sensors.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/v1/tetragon/sensors.proto
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ message TracingPolicyStatus {
string error = 8;
// current state of the tracing policy
TracingPolicyState state = 9;
// the amount of kernel memory in bytes used by policy's sensors non-shared BPF maps (memlock)
uint64 kernel_memory_bytes = 10;
}

message ListTracingPoliciesResponse {
Expand Down
23 changes: 23 additions & 0 deletions cmd/tetra/common/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon

package common

import "fmt"

// HumanizeByteCount transforms bytes count into a quickly-readable version, for
// example it transforms 4458824 into "4.46 MB". I copied this code from
// https://yourbasic.org/golang/formatting-byte-size-to-human-readable-format/
func HumanizeByteCount(b int) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := uint64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.2f %cB",
float64(b)/float64(div), "kMGTPE"[exp])
}
34 changes: 34 additions & 0 deletions cmd/tetra/common/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon

package common

import (
"fmt"
"testing"
)

func TestHumanizeByteCount(t *testing.T) {
tests := []struct {
input int
want string
}{
{0, "0 B"},
{2, "2 B"},
{999, "999 B"},
{1000, "1.00 kB"},
{1025, "1.02 kB"}, // rounding is a bit off but that's okay
{1026, "1.03 kB"},
{4458824, "4.46 MB"},
{987654321, "987.65 MB"},
{1010000000, "1.01 GB"},
{12970000000000, "12.97 TB"},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%d", tt.input), func(t *testing.T) {
if got := HumanizeByteCount(tt.input); got != tt.want {
t.Errorf("HumanizeByteCount(%d) = %v, want %v", tt.input, got, tt.want)
}
})
}
}
21 changes: 12 additions & 9 deletions cmd/tetra/debug/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"fmt"
"text/tabwriter"

"github.com/cilium/tetragon/cmd/tetra/common"
"github.com/cilium/tetragon/pkg/bugtool"
"github.com/spf13/cobra"
)

func NewMapCmd() *cobra.Command {
var lines int
var output string
var path string

cmd := cobra.Command{
Use: "maps",
Expand Down Expand Up @@ -51,7 +53,7 @@ adjust the number of item in the table.
return fmt.Errorf("invalid output format %q, please use one of tab or json", output)
}

out, err := bugtool.RunMapsChecks()
out, err := bugtool.RunMapsChecks(path)
if err != nil {
return err
}
Expand All @@ -60,10 +62,10 @@ adjust the number of item in the table.
case "tab":
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 3, ' ', 0)
fmt.Fprintln(w, "AllMaps\tPinnedProgsMaps\tPinnedMaps")
fmt.Fprintf(w, "%d\t%d\t%d\n",
out.TotalByteMemlock.AllMaps,
out.TotalByteMemlock.PinnedProgsMaps,
out.TotalByteMemlock.PinnedMaps,
fmt.Fprintf(w, "%s\t%s\t%s\n",
common.HumanizeByteCount(out.TotalMemlockBytes.AllMaps),
common.HumanizeByteCount(out.TotalMemlockBytes.PinnedProgsMaps),
common.HumanizeByteCount(out.TotalMemlockBytes.PinnedMaps),
)
w.Flush()
cmd.Println()
Expand All @@ -85,14 +87,14 @@ adjust the number of item in the table.
w = tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 3, ' ', 0)
fmt.Fprintln(w, "ID\tName\tType\tKeySize\tValueSize\tMaxEntries\tMemlock")
for _, d := range out.DiffMaps {
fmt.Fprintf(w, "%d\t%s\t%s\t%d\t%d\t%d\t%d\n",
fmt.Fprintf(w, "%d\t%s\t%s\t%d\t%d\t%d\t%s\n",
d.ID,
d.Name,
d.Type,
d.KeySize,
d.ValueSize,
d.MaxEntries,
d.Memlock,
common.HumanizeByteCount(d.MemlockBytes),
)
}
w.Flush()
Expand All @@ -108,14 +110,14 @@ adjust the number of item in the table.
if lines != 0 && i+1 > lines {
break
}
fmt.Fprintf(w, "%s\t%s\t%d\t%d\t%d\t%d\t%d\t%0.1f%%\n",
fmt.Fprintf(w, "%s\t%s\t%d\t%d\t%d\t%d\t%s\t%0.1f%%\n",
d.Name,
d.Type,
d.KeySize,
d.ValueSize,
d.MaxEntries,
d.Count,
d.TotalMemlock,
common.HumanizeByteCount(d.TotalMemlockBytes),
d.PercentOfTotal,
)
}
Expand All @@ -141,6 +143,7 @@ adjust the number of item in the table.
flags := cmd.Flags()
flags.IntVarP(&lines, "lines", "n", 10, "Number of lines for the top BPF map memory consumers.\nUse 0 to print all lines. Only valid with tab output.")
flags.StringVarP(&output, "output", "o", "tab", "Output format. One of tab or json.")
flags.StringVar(&path, "path", bugtool.TetragonBPFFS, "Path of the BPF filesystem.")

return &cmd
}
5 changes: 3 additions & 2 deletions cmd/tetra/tracingpolicy/tracingpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func New() *cobra.Command {
case "text":
// tabwriter config imitates kubectl default output, i.e. 3 spaces padding
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 3, ' ', 0)
fmt.Fprintln(w, "ID\tNAME\tSTATE\tFILTERID\tNAMESPACE\tSENSORS")
fmt.Fprintln(w, "ID\tNAME\tSTATE\tFILTERID\tNAMESPACE\tSENSORS\tKERNELMEMORY")

for _, pol := range res.Policies {
namespace := pol.Namespace
Expand Down Expand Up @@ -197,13 +197,14 @@ func New() *cobra.Command {
}
}

fmt.Fprintf(w, "%d\t%s\t%s\t%d\t%s\t%s\t\n",
fmt.Fprintf(w, "%d\t%s\t%s\t%d\t%s\t%s\t%s\t\n",
pol.Id,
pol.Name,
strings.TrimPrefix(strings.ToLower(pol.State.String()), "tp_state_"),
pol.FilterId,
namespace,
sensors,
common.HumanizeByteCount(int(pol.KernelMemoryBytes)),
)
}
w.Flush()
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/content/en/docs/reference/grpc-api.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions docs/content/en/docs/reference/metrics.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 100 additions & 0 deletions pkg/bpf/maps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon

package bpf

import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"

"github.com/cilium/ebpf"
)

type ExtendedMapInfo struct {
ebpf.MapInfo
Memlock int
}

func ExtendedInfoFromMapID(id int) (ExtendedMapInfo, error) {
m, err := ebpf.NewMapFromID(ebpf.MapID(id))
if err != nil {
return ExtendedMapInfo{}, fmt.Errorf("failed creating a map FD from ID: %w", err)
}
defer m.Close()

xInfo, err := ExtendedInfoFromMap(m)
if err != nil {
return ExtendedMapInfo{}, fmt.Errorf("failed to retrieve extended info from map %v: %w", m, err)
}

return xInfo, nil
}

func ExtendedInfoFromMap(m *ebpf.Map) (ExtendedMapInfo, error) {
info, err := m.Info()
if err != nil {
return ExtendedMapInfo{}, fmt.Errorf("failed to retrieve map info: %w", err)
}

memlock, err := ParseMemlockFromFDInfo(m.FD())
if err != nil {
return ExtendedMapInfo{}, fmt.Errorf("failed to parse memlock from fd (%d) info: %w", m.FD(), err)
}
return ExtendedMapInfo{
MapInfo: *info,
Memlock: memlock,
}, nil
}

func MemlockInfoFromMapID(id int) (ExtendedMapInfo, error) {
m, err := ebpf.NewMapFromID(ebpf.MapID(id))
if err != nil {
return ExtendedMapInfo{}, fmt.Errorf("failed creating a map FD from ID: %w", err)
}
defer m.Close()
memlock, err := ParseMemlockFromFDInfo(m.FD())
if err != nil {
return ExtendedMapInfo{}, fmt.Errorf("failed parsing fdinfo for memlock: %w", err)
}
info, err := m.Info()
if err != nil {
return ExtendedMapInfo{}, fmt.Errorf("failed retrieving info from map: %w", err)
}

return ExtendedMapInfo{
MapInfo: *info,
Memlock: memlock,
}, nil
}

func ParseMemlockFromFDInfo(fd int) (int, error) {
path := fmt.Sprintf("/proc/self/fdinfo/%d", fd)
file, err := os.Open(path)
if err != nil {
return 0, fmt.Errorf("failed to open file %q: %w", path, err)
}
defer file.Close()
return parseMemlockFromFDInfoReader(file)
}

func parseMemlockFromFDInfoReader(r io.Reader) (int, error) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) > 1 && fields[0] == "memlock:" {
memlock, err := strconv.Atoi(fields[1])
if err != nil {
return 0, fmt.Errorf("failed converting memlock to int: %w", err)
}
return memlock, nil
}
}
if err := scanner.Err(); err != nil {
return 0, fmt.Errorf("failed to scan: %w", err)
}
return 0, fmt.Errorf("didn't find memlock field")
}
Loading
Loading