perf_event_open: generalize to multiple events

This commit is contained in:
Ciro Santilli 六四事件 法轮功
2020-11-18 00:00:01 +00:00
parent d4a27987d6
commit 367df352d3
2 changed files with 77 additions and 34 deletions

View File

@@ -787,7 +787,14 @@ path_properties_tuples = (
'gem5_unimplemented_syscall': True 'gem5_unimplemented_syscall': True
}, },
'pagemap_dump.c': {'requires_argument': True}, 'pagemap_dump.c': {'requires_argument': True},
'perf_event_open.c': {'extra_objs_lkmc_common': True}, 'perf_event_open.c': {
# QEMU the syscall just fails on QEMU, presumably because QEMU
# does not have a microarchitecture model, and so it must just set
# CPU bits that inform the kernel that the feature is not available.
# https://cirosantilli.com/linux-kernel-module-cheat#gem5-vs-qemu
'allowed_emulators': {'gem5'},
'extra_objs_lkmc_common': True,
},
'poweroff.c': {'requires_sudo': True}, 'poweroff.c': {'requires_sudo': True},
'proc_events.c': {'requires_sudo': True}, 'proc_events.c': {'requires_sudo': True},
'proc_events.c': {'requires_sudo': True}, 'proc_events.c': {'requires_sudo': True},

View File

@@ -1,7 +1,5 @@
/* https://cirosantilli.com/linux-kernel-module-cheat#perf-event-open /* https://cirosantilli.com/linux-kernel-module-cheat#perf-event-open
* * Adapted from `man perf_event_open` in manpages 5.05-1. */
* Malloc n bytes as given from the command line.
*/
#include <asm/unistd.h> #include <asm/unistd.h>
#include <linux/perf_event.h> #include <linux/perf_event.h>
@@ -11,57 +9,95 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <unistd.h> #include <unistd.h>
#define LKMC_M5OPS_ENABLE 1
#include <lkmc.h> #include <lkmc.h>
static long static long perf_event_open(struct perf_event_attr *hw_event,
perf_event_open(struct perf_event_attr *hw_event, pid_t pid, uint32_t type, uint64_t config, pid_t pid,
int cpu, int group_fd, unsigned long flags) int cpu, int group_fd, unsigned long flags
{ ) {
int ret; int ret;
memset(hw_event, 0, sizeof(struct perf_event_attr));
hw_event->type = type;
hw_event->size = sizeof(struct perf_event_attr);
hw_event->config = config;
hw_event->disabled = 1;
hw_event->exclude_kernel = 1;
/* Don't count hypervisor events. */
hw_event->exclude_hv = 1;
ret = syscall(__NR_perf_event_open, hw_event, pid, cpu, ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
group_fd, flags); group_fd, flags);
if (ret == -1) {
fprintf(stderr, "Error opening leader %llx\n", hw_event->config);
exit(EXIT_FAILURE);
}
return ret; return ret;
} }
int typedef struct {
main(int argc, char **argv) char *name;
{ uint32_t type;
struct perf_event_attr pe; uint64_t config;
long long count; } Desc;
int fd;
#define DESC(type,config) {#config, type, config}
int
main(int argc, char **argv) {
size_t i;
int gem5;
long long count;
uint64_t n; uint64_t n;
Desc descs[] = {
DESC(PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS),
DESC(PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES),
DESC(PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES),
DESC(PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS),
DESC(PERF_TYPE_HW_CACHE, PERF_COUNT_HW_CACHE_L1D | PERF_COUNT_HW_CACHE_OP_READ << 8 | PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
};
struct perf_event_attr pes[LKMC_ARRAY_SIZE(descs)];
int fds[LKMC_ARRAY_SIZE(descs)];
if (argc > 1) { if (argc > 1) {
n = strtoll(argv[1], NULL, 0); n = strtoll(argv[1], NULL, 0);
} else { } else {
n = 100; n = 10000;
}
if (argc > 2) {
gem5 = argv[2][0] == '1';
} else {
gem5 = 0;
} }
memset(&pe, 0, sizeof(struct perf_event_attr)); for (i = 0; i < LKMC_ARRAY_SIZE(descs); i++)
pe.type = PERF_TYPE_HARDWARE; fds[i] = perf_event_open(&pes[i],
pe.size = sizeof(struct perf_event_attr); descs[i].type, descs[i].config, 0, -1, -1, 0);
pe.config = PERF_COUNT_HW_INSTRUCTIONS;
pe.disabled = 1;
pe.exclude_kernel = 1;
// Don't count hypervisor events.
pe.exclude_hv = 1;
fd = perf_event_open(&pe, 0, -1, -1, 0); /* Start the counts. */
if (fd == -1) { for (i = 0; i < LKMC_ARRAY_SIZE(descs); i++)
fprintf(stderr, "Error opening leader %llx\n", pe.config); ioctl(fds[i], PERF_EVENT_IOC_RESET, 0);
exit(EXIT_FAILURE); for (i = 0; i < LKMC_ARRAY_SIZE(descs); i++)
ioctl(fds[i], PERF_EVENT_IOC_ENABLE, 0);
if (gem5) {
/* Cross check with gem5 stats to see if things make sense.. */
LKMC_M5OPS_RESETSTATS;
} }
ioctl(fd, PERF_EVENT_IOC_RESET, 0); /* Mesure this function. */
ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
lkmc_busy_loop(n, 1); lkmc_busy_loop(n, 1);
ioctl(fd, PERF_EVENT_IOC_DISABLE, 0); /* Stop the count. */
read(fd, &count, sizeof(long long)); if (gem5) { LKMC_M5OPS_DUMPSTATS; }
for (i = 0; i < LKMC_ARRAY_SIZE(descs); i++)
ioctl(fds[i], PERF_EVENT_IOC_DISABLE, 0);
printf("Used %lld instructions\n", count); /* Print results. */
for (i = 0; i < LKMC_ARRAY_SIZE(descs); i++) {
close(fd); read(fds[i], &count, sizeof(long long));
printf("%s %lld\n", descs[i].name, count);
}
for (i = 0; i < LKMC_ARRAY_SIZE(descs); i++)
close(fds[i]);
} }