Skip to content

Commit e368751

Browse files
Jakub Kicinskiborkmann
authored andcommitted
tools: bpftool: use PERF_SAMPLE_TIME instead of reading the clock
Ask the kernel to include sample time in each even instead of reading the clock. This is also more accurate because our clock reading was done when user space would dump the buffer, not when sample was produced. Suggested-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com> Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent cb9c28e commit e368751

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

tools/bpf/bpftool/map_perf_ring.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct event_ring_info {
3939

4040
struct perf_event_sample {
4141
struct perf_event_header header;
42+
u64 time;
4243
__u32 size;
4344
unsigned char data[];
4445
};
@@ -57,24 +58,18 @@ print_bpf_output(struct event_ring_info *ring, struct perf_event_sample *e)
5758
__u64 id;
5859
__u64 lost;
5960
} *lost = (void *)e;
60-
struct timespec ts;
61-
62-
if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
63-
perror("Can't read clock for timestamp");
64-
return;
65-
}
6661

6762
if (json_output) {
6863
jsonw_start_object(json_wtr);
69-
jsonw_name(json_wtr, "timestamp");
70-
jsonw_uint(json_wtr, ts.tv_sec * 1000000000ull + ts.tv_nsec);
7164
jsonw_name(json_wtr, "type");
7265
jsonw_uint(json_wtr, e->header.type);
7366
jsonw_name(json_wtr, "cpu");
7467
jsonw_uint(json_wtr, ring->cpu);
7568
jsonw_name(json_wtr, "index");
7669
jsonw_uint(json_wtr, ring->key);
7770
if (e->header.type == PERF_RECORD_SAMPLE) {
71+
jsonw_name(json_wtr, "timestamp");
72+
jsonw_uint(json_wtr, e->time);
7873
jsonw_name(json_wtr, "data");
7974
print_data_json(e->data, e->size);
8075
} else if (e->header.type == PERF_RECORD_LOST) {
@@ -89,8 +84,8 @@ print_bpf_output(struct event_ring_info *ring, struct perf_event_sample *e)
8984
jsonw_end_object(json_wtr);
9085
} else {
9186
if (e->header.type == PERF_RECORD_SAMPLE) {
92-
printf("== @%ld.%ld CPU: %d index: %d =====\n",
93-
(long)ts.tv_sec, ts.tv_nsec,
87+
printf("== @%lld.%09lld CPU: %d index: %d =====\n",
88+
e->time / 1000000000ULL, e->time % 1000000000ULL,
9489
ring->cpu, ring->key);
9590
fprint_hex(stdout, e->data, e->size, " ");
9691
printf("\n");
@@ -185,7 +180,7 @@ static void perf_event_unmap(void *mem)
185180
static int bpf_perf_event_open(int map_fd, int key, int cpu)
186181
{
187182
struct perf_event_attr attr = {
188-
.sample_type = PERF_SAMPLE_RAW,
183+
.sample_type = PERF_SAMPLE_RAW | PERF_SAMPLE_TIME,
189184
.type = PERF_TYPE_SOFTWARE,
190185
.config = PERF_COUNT_SW_BPF_OUTPUT,
191186
};

0 commit comments

Comments
 (0)