Commit 6255f1f0 authored by Brenden Blanco's avatar Brenden Blanco

Add support for char[N] as a histogram key

Signed-off-by: default avatarBrenden Blanco <bblanco@plumgrid.com>
parent f0b15c49
...@@ -70,6 +70,8 @@ bool BMapDeclVisitor::VisitRecordDecl(RecordDecl *D) { ...@@ -70,6 +70,8 @@ bool BMapDeclVisitor::VisitRecordDecl(RecordDecl *D) {
result_ += "\"" + F->getName().str() + "\", \"unsigned long long\""; result_ += "\"" + F->getName().str() + "\", \"unsigned long long\"";
else else
TraverseDecl(F); TraverseDecl(F);
if (const ConstantArrayType *T = dyn_cast<ConstantArrayType>(F->getType()))
result_ += ", [" + T->getSize().toString(10, false) + "]";
if (F->isBitField()) if (F->isBitField())
result_ += ", " + to_string(F->getBitWidthValue(C)); result_ += ", " + to_string(F->getBitWidthValue(C));
result_ += "], "; result_ += "], ";
......
...@@ -256,7 +256,7 @@ class BPF(object): ...@@ -256,7 +256,7 @@ class BPF(object):
slot = getattr(k, f2) slot = getattr(k, f2)
vals[slot] = v.value vals[slot] = v.value
for bucket, vals in tmp.items(): for bucket, vals in tmp.items():
print("\nBucket %s = %x" % (bucket_type, bucket)) print("\nBucket %s = %r" % (bucket_type, bucket))
self._print_log2_hist(vals, val_type, 0) self._print_log2_hist(vals, val_type, 0)
else: else:
vals = [0] * 65 vals = [0] * 65
...@@ -429,7 +429,6 @@ class BPF(object): ...@@ -429,7 +429,6 @@ class BPF(object):
u"_Bool": ct.c_bool, u"_Bool": ct.c_bool,
u"char": ct.c_char, u"char": ct.c_char,
u"wchar_t": ct.c_wchar, u"wchar_t": ct.c_wchar,
u"char": ct.c_byte,
u"unsigned char": ct.c_ubyte, u"unsigned char": ct.c_ubyte,
u"short": ct.c_short, u"short": ct.c_short,
u"unsigned short": ct.c_ushort, u"unsigned short": ct.c_ushort,
...@@ -452,7 +451,10 @@ class BPF(object): ...@@ -452,7 +451,10 @@ class BPF(object):
if len(t) == 2: if len(t) == 2:
fields.append((t[0], BPF._decode_table_type(t[1]))) fields.append((t[0], BPF._decode_table_type(t[1])))
elif len(t) == 3: elif len(t) == 3:
fields.append((t[0], BPF._decode_table_type(t[1]), t[2])) if isinstance(t[2], list):
fields.append((t[0], BPF._decode_table_type(t[1]) * t[2][0]))
else:
fields.append((t[0], BPF._decode_table_type(t[1]), t[2]))
else: else:
raise Exception("Failed to decode type %s" % str(t)) raise Exception("Failed to decode type %s" % str(t))
cls = type(str(desc[0]), (ct.Structure,), dict(_fields_=fields)) cls = type(str(desc[0]), (ct.Structure,), dict(_fields_=fields))
......
...@@ -5,10 +5,11 @@ ...@@ -5,10 +5,11 @@
from bcc import BPF from bcc import BPF
from ctypes import c_int, c_ulonglong from ctypes import c_int, c_ulonglong
import random import random
import time
from unittest import main, TestCase from unittest import main, TestCase
class TestHistogram(TestCase): class TestHistogram(TestCase):
def _test_simple(self): def test_simple(self):
b = BPF(text=""" b = BPF(text="""
#include <uapi/linux/ptrace.h> #include <uapi/linux/ptrace.h>
#include <linux/bpf.h> #include <linux/bpf.h>
...@@ -52,6 +53,22 @@ int kprobe__htab_map_delete_elem(struct pt_regs *ctx, struct bpf_map *map, u64 * ...@@ -52,6 +53,22 @@ int kprobe__htab_map_delete_elem(struct pt_regs *ctx, struct bpf_map *map, u64 *
except: pass except: pass
b["hist1"].print_log2_hist() b["hist1"].print_log2_hist()
def test_chars(self):
b = BPF(text="""
#include <uapi/linux/ptrace.h>
#include <linux/sched.h>
typedef struct { char name[TASK_COMM_LEN]; u64 slot; } Key;
BPF_HISTOGRAM(hist1, Key, 1024);
int kprobe__finish_task_switch(struct pt_regs *ctx, struct task_struct *prev) {
Key k = {.slot = bpf_log2l(prev->real_start_time)};
if (!bpf_get_current_comm(&k.name, sizeof(k.name)))
hist1.increment(k);
return 0;
}
""")
for i in range(0, 100): time.sleep(0.01)
b["hist1"].print_log2_hist()
if __name__ == "__main__": if __name__ == "__main__":
main() main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment