Skip to content

Commit

Permalink
use bpf_log2l helper
Browse files Browse the repository at this point in the history
  • Loading branch information
brendangregg committed Sep 21, 2015
1 parent 8f63810 commit 30abd81
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 48 deletions.
25 changes: 1 addition & 24 deletions examples/bitehist.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* bitehist.c Block I/O size histogram.
* For Linux, uses BCC, eBPF. See .py file.
*
* Based on eBPF sample tracex2 by Alexi Starovoitov.
* Copyright (c) 2013-2015 PLUMgrid, http:https://plumgrid.com
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
Expand All @@ -16,31 +15,9 @@

BPF_TABLE("array", int, u64, dist, 64);

static unsigned int log2(unsigned int v)
{
unsigned int r;
unsigned int shift;

r = (v > 0xFFFF) << 4; v >>= r;
shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
shift = (v > 0xF) << 2; v >>= shift; r |= shift;
shift = (v > 0x3) << 1; v >>= shift; r |= shift;
r |= (v >> 1);
return r;
}

static unsigned int log2l(unsigned long v)
{
unsigned int hi = v >> 32;
if (hi)
return log2(hi) + 32 + 1;
else
return log2(v) + 1;
}

int kprobe__blk_account_io_completion(struct pt_regs *ctx, struct request *req)
{
int index = log2l(req->__data_len / 1024);
int index = bpf_log2l(req->__data_len / 1024);
u64 *leaf = dist.lookup(&index);
if (leaf) (*leaf)++;

Expand Down
25 changes: 1 addition & 24 deletions examples/vfsreadlat.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* vfsreadlat.c VFS read latency distribution.
* For Linux, uses BCC, eBPF. See .py file.
*
* Based on eBPF sample tracex2 by Alexi Starovoitov.
* Copyright (c) 2013-2015 PLUMgrid, http:https://plumgrid.com
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
Expand All @@ -16,28 +15,6 @@
BPF_HASH(start, u32);
BPF_TABLE("array", int, u64, dist, 64);

static unsigned int log2(unsigned int v)
{
unsigned int r;
unsigned int shift;

r = (v > 0xFFFF) << 4; v >>= r;
shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
shift = (v > 0xF) << 2; v >>= shift; r |= shift;
shift = (v > 0x3) << 1; v >>= shift; r |= shift;
r |= (v >> 1);
return r;
}

static unsigned int log2l(unsigned long v)
{
unsigned int hi = v >> 32;
if (hi)
return log2(hi) + 32 + 1;
else
return log2(v) + 1;
}

int do_entry(struct pt_regs *ctx)
{
u32 pid;
Expand All @@ -59,7 +36,7 @@ int do_return(struct pt_regs *ctx)

if (tsp != 0) {
delta = bpf_ktime_get_ns() - *tsp;
int index = log2l(delta / 1000);
int index = bpf_log2l(delta / 1000);
u64 *leaf = dist.lookup(&index);
if (leaf) (*leaf)++;
start.delete(&pid);
Expand Down

0 comments on commit 30abd81

Please sign in to comment.