Skip to content

Commit

Permalink
Unify perf_event type and config check
Browse files Browse the repository at this point in the history
  • Loading branch information
palmtenor committed May 20, 2017
1 parent cb3d161 commit 9875221
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/cc/libbpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,32 @@ void * bpf_open_perf_buffer(perf_reader_raw_cb raw_cb,
return NULL;
}

int invalid_perf_config(uint32_t type, uint64_t config) {
switch (type) {
case PERF_TYPE_HARDWARE:
return config >= PERF_COUNT_HW_MAX;
case PERF_TYPE_SOFTWARE:
return config >= PERF_COUNT_SW_MAX;
case PERF_TYPE_RAW:
return 0;
default:
return 1;
}
}

int bpf_open_perf_event(uint32_t type, uint64_t config, int pid, int cpu) {
int fd;
struct perf_event_attr attr = {};

if (type != PERF_TYPE_HARDWARE && type != PERF_TYPE_RAW) {
fprintf(stderr, "Unsupported perf event type\n");
return -1;
}
if (invalid_perf_config(type, config)) {
fprintf(stderr, "Invalid perf event config\n");
return -1;
}

attr.sample_period = LONG_MAX;
attr.type = type;
attr.config = config;
Expand Down Expand Up @@ -733,8 +755,7 @@ int bpf_attach_perf_event(int progfd, uint32_t ev_type, uint32_t ev_config,
fprintf(stderr, "Unsupported perf event type\n");
return -1;
}
if ((ev_type == PERF_TYPE_HARDWARE && ev_config >= PERF_COUNT_HW_MAX) ||
(ev_type == PERF_TYPE_SOFTWARE && ev_config >= PERF_COUNT_SW_MAX)) {
if (invalid_perf_config(ev_type, ev_config)) {
fprintf(stderr, "Invalid perf event config\n");
return -1;
}
Expand Down

0 comments on commit 9875221

Please sign in to comment.