1 // SPDX-License-Identifier: GPL-2.0
3 * dlfilter-show-cycles.c: Print the number of cycles at the start of each line
4 * Copyright (c) 2021, Intel Corporation.
6 #include <perf/perf_dlfilter.h>
19 static __u64 cycles
[MAX_CPU
][MAX_ENTRY
];
20 static __u64 cycles_rpt
[MAX_CPU
][MAX_ENTRY
];
23 #define TABLESZ (1 << BITS)
24 #define TABLEMAX (TABLESZ / 2)
25 #define MASK (TABLESZ - 1)
30 __u64 cycles
[MAX_ENTRY
];
31 __u64 cycles_rpt
[MAX_ENTRY
];
36 static int event_entry(const char *event
)
40 if (!strncmp(event
, "instructions", 12))
42 if (!strncmp(event
, "branches", 8))
47 static struct entry
*find_entry(__s32 tid
)
49 __u32 pos
= tid
& MASK
;
61 if (tid_cnt
>= TABLEMAX
) {
62 fprintf(stderr
, "Too many threads\n");
72 static void add_entry(__s32 tid
, int pos
, __u64 cnt
)
74 struct entry
*e
= find_entry(tid
);
77 e
->cycles
[pos
] += cnt
;
80 int filter_event_early(void *data
, const struct perf_dlfilter_sample
*sample
, void *ctx
)
82 __s32 cpu
= sample
->cpu
;
83 __s32 tid
= sample
->tid
;
89 pos
= event_entry(sample
->event
);
91 if (cpu
>= 0 && cpu
< MAX_CPU
)
92 cycles
[cpu
][pos
] += sample
->cyc_cnt
;
94 add_entry(tid
, pos
, sample
->cyc_cnt
);
98 static void print_vals(__u64 cycles
, __u64 delta
)
101 printf("%10llu %10llu ", (unsigned long long)cycles
, (unsigned long long)delta
);
103 printf("%10llu %10s ", (unsigned long long)cycles
, "");
106 int filter_event(void *data
, const struct perf_dlfilter_sample
*sample
, void *ctx
)
108 __s32 cpu
= sample
->cpu
;
109 __s32 tid
= sample
->tid
;
112 pos
= event_entry(sample
->event
);
114 if (cpu
>= 0 && cpu
< MAX_CPU
) {
115 print_vals(cycles
[cpu
][pos
], cycles
[cpu
][pos
] - cycles_rpt
[cpu
][pos
]);
116 cycles_rpt
[cpu
][pos
] = cycles
[cpu
][pos
];
121 struct entry
*e
= find_entry(tid
);
124 print_vals(e
->cycles
[pos
], e
->cycles
[pos
] - e
->cycles_rpt
[pos
]);
125 e
->cycles_rpt
[pos
] = e
->cycles
[pos
];
134 const char *filter_description(const char **long_description
)
136 static char *long_desc
= "Cycle counts are accumulated per CPU (or "
137 "per thread if CPU is not recorded) from IPC information, and "
138 "printed together with the change since the last print, at the "
139 "start of each line. Separate counts are kept for branches, "
140 "instructions or other events.";
142 *long_description
= long_desc
;
143 return "Print the number of cycles at the start of each line";