-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract lookupcgroupv2 out of the otlp reporter (#227)
- Loading branch information
Showing
2 changed files
with
60 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package libpf // import "go.opentelemetry.io/ebpf-profiler/libpf" | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"regexp" | ||
|
||
lru "github.com/elastic/go-freelru" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
cgroupv2PathPattern = regexp.MustCompile(`0:.*?:(.*)`) | ||
) | ||
|
||
// LookupCgroupv2 returns the cgroupv2 ID for pid. | ||
func LookupCgroupv2(cgrouplru *lru.SyncedLRU[PID, string], pid PID) (string, error) { | ||
id, ok := cgrouplru.Get(pid) | ||
if ok { | ||
return id, nil | ||
} | ||
|
||
// Slow path | ||
f, err := os.Open(fmt.Sprintf("/proc/%d/cgroup", pid)) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer f.Close() | ||
|
||
var genericCgroupv2 string | ||
scanner := bufio.NewScanner(f) | ||
buf := make([]byte, 512) | ||
// Providing a predefined buffer overrides the internal buffer that Scanner uses (4096 bytes). | ||
// We can do that and also set a maximum allocation size on the following call. | ||
// With a maximum of 4096 characters path in the kernel, 8192 should be fine here. We don't | ||
// expect lines in /proc/<PID>/cgroup to be longer than that. | ||
scanner.Buffer(buf, 8192) | ||
var pathParts []string | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
pathParts = cgroupv2PathPattern.FindStringSubmatch(line) | ||
if pathParts == nil { | ||
log.Debugf("Could not extract cgroupv2 path from line: %s", line) | ||
continue | ||
} | ||
genericCgroupv2 = pathParts[1] | ||
break | ||
} | ||
|
||
// Cache the cgroupv2 information. | ||
// To avoid busy lookups, also empty cgroupv2 information is cached. | ||
cgrouplru.Add(pid, genericCgroupv2) | ||
|
||
return genericCgroupv2, nil | ||
} |
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