Skip to content

Commit

Permalink
libbpf-tools/softirqs: Add -c/--cpu option to filter cpu
Browse files Browse the repository at this point in the history
tools/softirqs.py supports `-c/--cpu` otption to filter cpu,
It's reasonable to support a same otption for libbpf-tools/softirqs.

Reported-by: Tang Yizhou <[email protected]>
Signed-off-by: Dantezy <[email protected]>
  • Loading branch information
ZhangYet committed Sep 30, 2024
1 parent 9f3d0df commit d6eb263
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
18 changes: 16 additions & 2 deletions libbpf-tools/softirqs.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

const volatile bool targ_dist = false;
const volatile bool targ_ns = false;
const volatile int targ_cpu = -1;

struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
Expand All @@ -21,8 +22,18 @@ __u64 counts[NR_SOFTIRQS] = {};
__u64 time[NR_SOFTIRQS] = {};
struct hist hists[NR_SOFTIRQS] = {};

static bool is_target_cpu() {
if (targ_cpu < 0)
return true;

return bpf_get_smp_processor_id() == (u32)targ_cpu;
}

static int handle_entry(unsigned int vec_nr)
{
if (!is_target_cpu())
return 0;

u64 ts = bpf_ktime_get_ns();
u32 key = 0;

Expand All @@ -32,11 +43,14 @@ static int handle_entry(unsigned int vec_nr)

static int handle_exit(unsigned int vec_nr)
{
if (!is_target_cpu())
return 0;

u64 delta, *tsp;
u32 key = 0;

if (vec_nr >= NR_SOFTIRQS)
return 0;
vec_nr &= 0xf; // to avoid compiler opti make verifier error

tsp = bpf_map_lookup_elem(&start, &key);
if (!tsp)
return 0;
Expand Down
13 changes: 13 additions & 0 deletions libbpf-tools/softirqs.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ struct env {
int times;
bool timestamp;
bool verbose;
int targ_cpu;
} env = {
.interval = 99999999,
.times = 99999999,
.count = false,
.targ_cpu = -1,
};

static volatile bool exiting;
Expand All @@ -43,6 +45,7 @@ const char argp_program_doc[] =
"EXAMPLES:\n"
" softirqs # sum soft irq event time\n"
" softirqs -d # show soft irq event time as histograms\n"
" softirqs -c 1 # show soft irq event time on cpu 1\n"
" softirqs 1 10 # print 1 second summaries, 10 times\n"
" softirqs -NT 1 # 1s summaries, nanoseconds, and timestamps\n";

Expand All @@ -51,6 +54,7 @@ static const struct argp_option opts[] = {
{ "timestamp", 'T', NULL, 0, "Include timestamp on output", 0 },
{ "nanoseconds", 'N', NULL, 0, "Output in nanoseconds", 0 },
{ "count", 'C', NULL, 0, "Show event counts with timing", 0 },
{"cpu", 'c', "CPU", 0, "Trace this cpu only", 0},
{ "verbose", 'v', NULL, 0, "Verbose debug output", 0 },
{ NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help", 0 },
{},
Expand Down Expand Up @@ -79,6 +83,14 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
case 'C':
env.count = true;
break;
case 'c':
errno = 0;
env.targ_cpu = atoi(arg);
if (errno || env.targ_cpu < 0) {
fprintf(stderr, "invalid cpu: %s\n", arg);
argp_usage(state);
}
break;
case ARGP_KEY_ARG:
errno = 0;
if (pos_args == 0) {
Expand Down Expand Up @@ -228,6 +240,7 @@ int main(int argc, char **argv)
/* initialize global data (filtering options) */
obj->rodata->targ_dist = env.distributed;
obj->rodata->targ_ns = env.nanoseconds;
obj->rodata->targ_cpu = env.targ_cpu;

err = softirqs_bpf__load(obj);
if (err) {
Expand Down

0 comments on commit d6eb263

Please sign in to comment.