forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add open_perf_event api for reading perf counters (iovisor#643)
* add open_perf_event api for reading perf counters Though the rewriter table method existed, we were not yet opening up the perf counter in the proper way for it to be read. Introduce a c function to enable attaching perf counters to cpu-indexed table slots. The python side opens, assigns, then immediately closes the fd. Only the kernel keeps a reference, so closing the table or deleting the entry will be sufficient to free up the fd when finished. Signed-off-by: Brenden Blanco <[email protected]> * Skip perf array hw counter test if unsupported Hardware counters are not available in all places (some VM environments), so gracefully skip on the particular error condition. Signed-off-by: Brenden Blanco <[email protected]>
- Loading branch information
Showing
7 changed files
with
184 additions
and
14 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
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
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,47 @@ | ||
#!/usr/bin/env python | ||
# Copyright (c) 2016 PLUMgrid | ||
# Licensed under the Apache License, Version 2.0 (the "License") | ||
|
||
import bcc | ||
import ctypes | ||
import multiprocessing | ||
import os | ||
import time | ||
import unittest | ||
|
||
class TestPerfCounter(unittest.TestCase): | ||
def test_cycles(self): | ||
text = """ | ||
BPF_PERF_ARRAY(cnt1, NUM_CPUS); | ||
BPF_TABLE("array", u32, u64, prev, NUM_CPUS); | ||
BPF_HISTOGRAM(dist); | ||
int kprobe__sys_getuid(void *ctx) { | ||
u32 cpu = bpf_get_smp_processor_id(); | ||
u64 val = cnt1.perf_read(cpu); | ||
prev.update(&cpu, &val); | ||
return 0; | ||
} | ||
int kretprobe__sys_getuid(void *ctx) { | ||
u32 cpu = bpf_get_smp_processor_id(); | ||
u64 val = cnt1.perf_read(cpu); | ||
u64 *prevp = prev.lookup(&cpu); | ||
if (prevp) | ||
dist.increment(bpf_log2l(val - *prevp)); | ||
return 0; | ||
} | ||
""" | ||
b = bcc.BPF(text=text, debug=0, | ||
cflags=["-DNUM_CPUS=%d" % multiprocessing.cpu_count()]) | ||
cnt1 = b["cnt1"] | ||
try: | ||
cnt1.open_perf_event(cnt1.HW_CPU_CYCLES) | ||
except: | ||
if ctypes.get_errno() == 2: | ||
raise self.skipTest("hardware events unsupported") | ||
raise | ||
for i in range(0, 100): | ||
os.getuid() | ||
b["dist"].print_log2_hist() | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |