| # Copyright (c) 2016 PLUMgrid |
| # Licensed under the Apache License, Version 2.0 (the "License") |
| class TestPerfCounter(unittest.TestCase): |
| BPF_PERF_ARRAY(cnt1, NUM_CPUS); |
| BPF_TABLE("array", u32, u64, prev, NUM_CPUS); |
| int kprobe__sys_getuid(void *ctx) { |
| u32 cpu = bpf_get_smp_processor_id(); |
| u64 val = cnt1.perf_read(cpu); |
| int kretprobe__sys_getuid(void *ctx) { |
| u32 cpu = bpf_get_smp_processor_id(); |
| u64 val = cnt1.perf_read(cpu); |
| u64 *prevp = prev.lookup(&cpu); |
| dist.increment(bpf_log2l(val - *prevp)); |
| b = bcc.BPF(text=text, debug=0, |
| cflags=["-DNUM_CPUS=%d" % multiprocessing.cpu_count()]) |
| cnt1.open_perf_event(cnt1.HW_CPU_CYCLES) |
| if ctypes.get_errno() == 2: |
| raise self.skipTest("hardware events unsupported") |
| b["dist"].print_log2_hist() |
| if __name__ == "__main__": |