-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
191 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
programs: | ||
# See: | ||
# * https://github.com/iovisor/bcc/blob/master/tools/oomkill.py | ||
# * https://github.com/iovisor/bcc/blob/master/tools/oomkill_example.txt | ||
- name: oomkill | ||
metrics: | ||
counters: | ||
- name: oom_kills | ||
help: Count global and cgroup level OOMs | ||
perf_map: events | ||
labels: | ||
- name: cgroup_path | ||
size: 8 | ||
decoders: | ||
- name: uint | ||
- name: cgroup | ||
- name: global_oom | ||
size: 1 | ||
decoders: | ||
- name: uint | ||
kprobes: | ||
oom_kill_process: count_ooms | ||
code: | | ||
#include <uapi/linux/ptrace.h> | ||
#include <linux/oom.h> | ||
#include <linux/memcontrol.h> | ||
// we'll use "BPF_PERF_OUTPUT" map type here to avoid unbound cardinality | ||
BPF_PERF_OUTPUT(events); | ||
struct data_t { | ||
u64 cgroup_id; | ||
u8 global_oom; | ||
}; | ||
void count_ooms(struct pt_regs *ctx, struct oom_control *oc, const char *message) { | ||
struct data_t data = {}; | ||
struct mem_cgroup *mcg = oc->memcg; | ||
if (!mcg) { | ||
data.global_oom = 1; | ||
events.perf_submit(ctx, &data, sizeof(data)); | ||
return; | ||
} | ||
data.cgroup_id = mcg->css.cgroup->kn->id.id; | ||
events.perf_submit(ctx, &data, sizeof(data)); | ||
} |
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,120 @@ | ||
package exporter | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/cloudflare/ebpf_exporter/config" | ||
"github.com/cloudflare/ebpf_exporter/decoder" | ||
"github.com/iovisor/gobpf/bcc" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
type PerfMapSink struct { | ||
counterConfig config.Counter | ||
counterVec *prometheus.CounterVec | ||
dropCounter prometheus.Counter | ||
} | ||
|
||
func NewPerfMapSink(decoders *decoder.Set, module *bcc.Module, counterConfig config.Counter) *PerfMapSink { | ||
var ( | ||
receiveCh = make(chan []byte) | ||
lostCh = make(chan uint64) | ||
) | ||
|
||
sink := &PerfMapSink{ | ||
counterConfig: counterConfig, | ||
dropCounter: createDropCounterForPerfMap(counterConfig), | ||
} | ||
sink.resetCounterVec() | ||
|
||
table := bcc.NewTable(module.TableId(counterConfig.PerfMap), module) | ||
|
||
perfMap, err := bcc.InitPerfMap(table, receiveCh, lostCh) | ||
if err != nil { | ||
log.Fatalf("Can't init PerfMap: %s", err) | ||
} | ||
|
||
go func(sink *PerfMapSink, receiveCh <-chan []byte) { | ||
for rawBytes := range receiveCh { | ||
// https://github.com/cilium/ebpf/pull/94#discussion_r425823371 | ||
// https://lore.kernel.org/patchwork/patch/1244339/ | ||
var validDataSize uint | ||
for _, labelConfig := range sink.counterConfig.Labels { | ||
validDataSize += labelConfig.Size | ||
} | ||
|
||
labelValues, err := decoders.DecodeLabels(rawBytes[:validDataSize], sink.counterConfig.Labels) | ||
if err != nil { | ||
if err == decoder.ErrSkipLabelSet { | ||
continue | ||
} | ||
|
||
log.Printf("Failed to decode labels: %s", err) | ||
} | ||
|
||
sink.counterVec.WithLabelValues(labelValues...).Inc() | ||
|
||
} | ||
}(sink, receiveCh) | ||
|
||
go func(sink *PerfMapSink, lostCh <-chan uint64) { | ||
for droppedEvents := range lostCh { | ||
sink.dropCounter.Add(float64(droppedEvents)) | ||
} | ||
}(sink, lostCh) | ||
|
||
go func(sink *PerfMapSink) { | ||
flushDuration := time.Hour | ||
if sink.counterConfig.PerfMapFlushDuration > 0 { | ||
flushDuration = sink.counterConfig.PerfMapFlushDuration | ||
} | ||
|
||
ticker := time.NewTicker(flushDuration) | ||
|
||
for { | ||
<-ticker.C | ||
sink.resetCounterVec() | ||
} | ||
}(sink) | ||
|
||
perfMap.Start() | ||
|
||
return sink | ||
} | ||
|
||
func (s *PerfMapSink) Collect(ch chan<- prometheus.Metric) { | ||
s.counterVec.Collect(ch) | ||
} | ||
|
||
func (s *PerfMapSink) Describe(ch chan<- *prometheus.Desc) { | ||
s.counterVec.Describe(ch) | ||
} | ||
|
||
func (s *PerfMapSink) resetCounterVec() { | ||
s.counterVec = createCounterVecForPerfMap(s.counterConfig, labelNamesFromCounterConfig(s.counterConfig)) | ||
} | ||
|
||
func createCounterVecForPerfMap(counterConfig config.Counter, labelNames []string) *prometheus.CounterVec { | ||
return prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
Namespace: prometheusNamespace, | ||
Name: counterConfig.Name, | ||
Help: counterConfig.Help, | ||
}, labelNames) | ||
} | ||
|
||
func createDropCounterForPerfMap(counterConfig config.Counter) prometheus.Counter { | ||
return prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
Namespace: "dropped_perf_map_events", | ||
Name: counterConfig.Name, | ||
Help: "Dropped perf map events", | ||
}, []string{}).WithLabelValues() | ||
} | ||
|
||
func labelNamesFromCounterConfig(counterConfig config.Counter) (labelNames []string) { | ||
for _, label := range counterConfig.Labels { | ||
labelNames = append(labelNames, label.Name) | ||
} | ||
|
||
return | ||
} |