Skip to content

Commit 4094583

Browse files
authored
profiler: Reduce mem usage in maps with no filters (#1588)
Right now we always allocate 5k entries in `debug_pids` in kernel memory even if the filter mode `--debug-process-names` is not enabled. Let's only allocate the entries if we are filtering processes. The saved memory isn't too much, around a few KB, but kernel memory is precious and in particular this memory, as it's locked. Test Plan ========= **no filters** ``` [javierhonduco@fedora parca-agent]$ sudo bpftool map | grep debug_pids -A3 739: hash name debug_pids flags 0x0 key 4B value 1B max_entries 1 memlock 4096B btf_id 670 pids parca-agent-deb(154991) ``` **with filters** ``` [javierhonduco@fedora parca-agent]$ sudo bpftool map | grep debug_pids -A3 765: hash name debug_pids flags 0x0 key 4B value 1B max_entries 5000 memlock 40960B btf_id 697 pids parca-agent-deb(156286) ```
2 parents 987f691 + 1dc4360 commit 4094583

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

bpf/cpu/cpu.bpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ typedef struct {
205205

206206
/*================================ MAPS =====================================*/
207207

208-
BPF_HASH(debug_pids, int, u8, MAX_PROCESSES);
208+
BPF_HASH(debug_pids, int, u8, 1); // Table size will be updated in userspace.
209209
BPF_HASH(process_info, int, process_info_t, MAX_PROCESSES);
210210

211211
BPF_STACK_TRACE(stack_traces, MAX_STACK_TRACES_ENTRIES);

pkg/profiler/cpu/cpu.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func loadBpfProgram(logger log.Logger, reg prometheus.Registerer, debugEnabled,
258258
}
259259

260260
level.Info(logger).Log("msg", "Attempting to create unwind shards", "count", unwindShards)
261-
if err := bpfMaps.adjustMapSizes(unwindShards); err != nil {
261+
if err := bpfMaps.adjustMapSizes(debugEnabled, unwindShards); err != nil {
262262
return nil, nil, fmt.Errorf("failed to adjust map sizes: %w", err)
263263
}
264264

pkg/profiler/cpu/maps.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const (
6363
maxUnwindTableSize = 250 * 1000 // Always needs to be sync with MAX_UNWIND_TABLE_SIZE in the BPF program.
6464
maxMappingsPerProcess = 250 // Always need to be in sync with MAX_MAPPINGS_PER_PROCESS.
6565
maxUnwindTableChunks = 30 // Always need to be in sync with MAX_UNWIND_TABLE_CHUNKS.
66+
maxProcesses = 5000 // Always need to be in sync with MAX_PROCESSES.
6667

6768
/*
6869
TODO: once we generate the bindings automatically, remove this.
@@ -290,20 +291,30 @@ func (m *bpfMaps) close() error {
290291
// adjustMapSizes updates the amount of unwind shards.
291292
//
292293
// Note: It must be called before `BPFLoadObject()`.
293-
func (m *bpfMaps) adjustMapSizes(unwindTableShards uint32) error {
294+
func (m *bpfMaps) adjustMapSizes(debugEnabled bool, unwindTableShards uint32) error {
294295
unwindTables, err := m.module.GetMap(unwindTablesMapName)
295296
if err != nil {
296297
return fmt.Errorf("get unwind tables map: %w", err)
297298
}
298299

299-
// Adjust unwind tables size.
300+
// Adjust unwind_tables size.
300301
sizeBefore := unwindTables.GetMaxEntries()
301302
if err := unwindTables.Resize(unwindTableShards); err != nil {
302303
return fmt.Errorf("resize unwind tables map from %d to %d elements: %w", sizeBefore, unwindTableShards, err)
303304
}
304305

305306
m.maxUnwindShards = uint64(unwindTableShards)
306307

308+
// Adjust debug_pids size.
309+
if debugEnabled {
310+
debugPIDs, err := m.module.GetMap(debugPIDsMapName)
311+
if err != nil {
312+
return fmt.Errorf("get debug pids map: %w", err)
313+
}
314+
if err := debugPIDs.Resize(maxProcesses); err != nil {
315+
return fmt.Errorf("resize debug pids map from default to %d elements: %w", maxProcesses, err)
316+
}
317+
}
307318
return nil
308319
}
309320

0 commit comments

Comments
 (0)