Skip to content

Commit 3f90bc3

Browse files
sisttrihao022
authored andcommitted
feat(oom): report memory limit and usage on OOM kill
1 parent 3d5fd43 commit 3f90bc3

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

bpf/dropwatch.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,12 @@ int bpf_kfree_skb_prog(struct trace_event_raw_kfree_skb *ctx)
205205

206206
/* meta */
207207
data->meta.ktime_ns = bpf_ktime_get_ns();
208+
/* tgid_pid and comm in SoftIRQ Context do not accurately reflect
209+
* the process where the drop occurred, only for reference.
210+
*/
208211
data->meta.tgid_pid = bpf_get_current_pid_tgid();
209212
bpf_get_current_comm(&data->meta.comm, sizeof(data->meta.comm));
213+
210214
data->meta.kfree_skb_addr = (u64)(unsigned long)ctx->location;
211215
data->meta.queue_mapping = BPF_CORE_READ(skb, queue_mapping);
212216
data->meta.drop_reason = SKB_DROP_REASON_UNSUPPORT;

bpf/oom.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ struct oom_info {
2424
u32 victim_pid;
2525
u64 trigger_memcg_css;
2626
u64 victim_memcg_css;
27+
u64 mem_limit_pages;
28+
u64 mem_usage_pages;
2729
};
2830

2931
SEC("kprobe/oom_kill_process")
@@ -50,6 +52,13 @@ int BPF_KPROBE(oom_kill_process, struct oom_control *oc, const char *message)
5052
info.trigger_memcg_css =
5153
(u64)BPF_CORE_READ(trigger_task, cgroups, subsys[memory_cgrp_id]);
5254

55+
info.mem_limit_pages = BPF_CORE_READ(oc, totalpages);
56+
struct mem_cgroup *memcg = BPF_CORE_READ(oc, memcg);
57+
if (memcg) {
58+
info.mem_usage_pages =
59+
(u64)BPF_CORE_READ(memcg, memory.usage.counter);
60+
}
61+
5362
bpf_perf_event_output(ctx, &oom_perf_events, COMPAT_BPF_F_CURRENT_CPU,
5463
&info, sizeof(info));
5564
return 0;

core/events/oom.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package events
1717
import (
1818
"context"
1919
"fmt"
20+
"os"
2021
"sync"
2122
"time"
2223

@@ -38,6 +39,8 @@ type perfEventData struct {
3839
VictimPid int32
3940
TriggerMemcgCSS uint64
4041
VictimMemcgCSS uint64
42+
MemLimitPages uint64
43+
MemUsagePages uint64
4144
}
4245

4346
type OOMTracingData struct {
@@ -51,6 +54,8 @@ type OOMTracingData struct {
5154
VictimContainerHostname string `json:"victim_container_hostname"`
5255
VictimPid int32 `json:"victim_pid"`
5356
VictimProcessName string `json:"victim_process_name"`
57+
CgroupMemoryLimit uint64 `json:"cgroup_memory_limit"`
58+
CgroupMemoryUsage uint64 `json:"cgroup_memory_usage"`
5459
}
5560

5661
type oomMetric struct {
@@ -121,6 +126,8 @@ func (c *oomCollector) Start(ctx context.Context) error {
121126

122127
b.WaitDetachByBreaker(childCtx, cancel)
123128

129+
pageSize := uint64(os.Getpagesize())
130+
124131
for {
125132
select {
126133
case <-childCtx.Done():
@@ -146,6 +153,8 @@ func (c *oomCollector) Start(ctx context.Context) error {
146153
VictimPid: data.VictimPid,
147154
VictimProcessName: bytesutil.ToStr(data.VictimProcessName[:]),
148155
VictimContainerID: cssContainers[data.VictimMemcgCSS],
156+
CgroupMemoryLimit: data.MemLimitPages * pageSize,
157+
CgroupMemoryUsage: data.MemUsagePages * pageSize,
149158
}
150159

151160
// leave the hostname empty if this is not container.

0 commit comments

Comments
 (0)