aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/dlfilters/dlfilter-show-cycles.c
blob: 9eccc97bff82f64108545ca6a5eedb59a70aa6e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// SPDX-License-Identifier: GPL-2.0
/*
 * dlfilter-show-cycles.c: Print the number of cycles at the start of each line
 * Copyright (c) 2021, Intel Corporation.
 */
#include <perf/perf_dlfilter.h>
#include <string.h>
#include <stdio.h>

#define MAX_CPU 4096

enum {
	INSTR_CYC,
	BRNCH_CYC,
	OTHER_CYC,
	MAX_ENTRY
};

static __u64 cycles[MAX_CPU][MAX_ENTRY];
static __u64 cycles_rpt[MAX_CPU][MAX_ENTRY];

#define BITS		16
#define TABLESZ		(1 << BITS)
#define TABLEMAX	(TABLESZ / 2)
#define MASK		(TABLESZ - 1)

static struct entry {
	__u32 used;
	__s32 tid;
	__u64 cycles[MAX_ENTRY];
	__u64 cycles_rpt[MAX_ENTRY];
} table[TABLESZ];

static int tid_cnt;

static int event_entry(const char *event)
{
	if (!event)
		return OTHER_CYC;
	if (!strncmp(event, "instructions", 12))
		return INSTR_CYC;
	if (!strncmp(event, "branches", 8))
		return BRNCH_CYC;
	return OTHER_CYC;
}

static struct entry *find_entry(__s32 tid)
{
	__u32 pos = tid & MASK;
	struct entry *e;

	e = &table[pos];
	while (e->used) {
		if (e->tid == tid)
			return e;
		if (++pos == TABLESZ)
			pos = 0;
		e = &table[pos];
	}

	if (tid_cnt >= TABLEMAX) {
		fprintf(stderr, "Too many threads\n");
		return NULL;
	}

	tid_cnt += 1;
	e->used = 1;
	e->tid = tid;
	return e;
}

static void add_entry(__s32 tid, int pos, __u64 cnt)
{
	struct entry *e = find_entry(tid);

	if (e)
		e->cycles[pos] += cnt;
}

int filter_event_early(void *data, const struct perf_dlfilter_sample *sample, void *ctx)
{
	__s32 cpu = sample->cpu;
	__s32 tid = sample->tid;
	int pos;

	if (!sample->cyc_cnt)
		return 0;

	pos = event_entry(sample->event);

	if (cpu >= 0 && cpu < MAX_CPU)
		cycles[cpu][pos] += sample->cyc_cnt;
	else if (tid != -1)
		add_entry(tid, pos, sample->cyc_cnt);
	return 0;
}

static void print_vals(__u64 cycles, __u64 delta)
{
	if (delta)
		printf("%10llu %10llu ", cycles, delta);
	else
		printf("%10llu %10s ", cycles, "");
}

int filter_event(void *data, const struct perf_dlfilter_sample *sample, void *ctx)
{
	__s32 cpu = sample->cpu;
	__s32 tid = sample->tid;
	int pos;

	pos = event_entry(sample->event);

	if (cpu >= 0 && cpu < MAX_CPU) {
		print_vals(cycles[cpu][pos], cycles[cpu][pos] - cycles_rpt[cpu][pos]);
		cycles_rpt[cpu][pos] = cycles[cpu][pos];
		return 0;
	}

	if (tid != -1) {
		struct entry *e = find_entry(tid);

		if (e) {
			print_vals(e->cycles[pos], e->cycles[pos] - e->cycles_rpt[pos]);
			e->cycles_rpt[pos] = e->cycles[pos];
			return 0;
		}
	}

	printf("%22s", "");
	return 0;
}

const char *filter_description(const char **long_description)
{
	static char *long_desc = "Cycle counts are accumulated per CPU (or "
		"per thread if CPU is not recorded) from IPC information, and "
		"printed together with the change since the last print, at the "
		"start of each line. Separate counts are kept for branches, "
		"instructions or other events.";

	*long_description = long_desc;
	return "Print the number of cycles at the start of each line";
}