-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BPF_HISTOGRAM type and print support
This adds support for a specialized histogram type, which underneath maps to an array or a hash table, depending on key type. With no arguments, it takes on the type `u64 table[64];`. The other current supported key type is `struct { int32|int64 bucket; int32|int64 slot }`. To print these automatically, print_log2_hist is underneath split into two types of printouts, one which prints the single histogram, and another which prints a histogram for each unique `bucket` value. See test_histogram.py for examples. Fixes: #144 Signed-off-by: Brenden Blanco <bblanco@plumgrid.com>
- Loading branch information
Brenden Blanco
committed
Sep 24, 2015
1 parent
ea5023a
commit f0b15c4
Showing
7 changed files
with
153 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/usr/bin/env python | ||
# Copyright (c) PLUMgrid, Inc. | ||
# Licensed under the Apache License, Version 2.0 (the "License") | ||
|
||
from bcc import BPF | ||
from ctypes import c_int, c_ulonglong | ||
import random | ||
from unittest import main, TestCase | ||
|
||
class TestHistogram(TestCase): | ||
def _test_simple(self): | ||
b = BPF(text=""" | ||
#include <uapi/linux/ptrace.h> | ||
#include <linux/bpf.h> | ||
BPF_HISTOGRAM(hist1); | ||
BPF_HASH(stub); | ||
int kprobe__htab_map_delete_elem(struct pt_regs *ctx, struct bpf_map *map, u64 *k) { | ||
hist1.increment(bpf_log2l(*k)); | ||
return 0; | ||
} | ||
""") | ||
for i in range(0, 32): | ||
for j in range(0, random.randint(1, 10)): | ||
try: del b["stub"][c_ulonglong(1 << i)] | ||
except: pass | ||
b["hist1"].print_log2_hist() | ||
|
||
for i in range(32, 64): | ||
for j in range(0, random.randint(1, 10)): | ||
try: del b["stub"][c_ulonglong(1 << i)] | ||
except: pass | ||
b["hist1"].print_log2_hist() | ||
|
||
def test_struct(self): | ||
b = BPF(text=""" | ||
#include <uapi/linux/ptrace.h> | ||
#include <linux/bpf.h> | ||
typedef struct { void *map; u64 slot; } Key; | ||
BPF_HISTOGRAM(hist1, Key, 1024); | ||
BPF_HASH(stub1); | ||
BPF_HASH(stub2); | ||
int kprobe__htab_map_delete_elem(struct pt_regs *ctx, struct bpf_map *map, u64 *k) { | ||
hist1.increment((Key){map, bpf_log2l(*k)}); | ||
return 0; | ||
} | ||
""") | ||
for i in range(0, 64): | ||
for j in range(0, random.randint(1, 10)): | ||
try: del b["stub1"][c_ulonglong(1 << i)] | ||
except: pass | ||
try: del b["stub2"][c_ulonglong(1 << i)] | ||
except: pass | ||
b["hist1"].print_log2_hist() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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