xref: /OK3568_Linux_fs/kernel/tools/testing/selftests/powerpc/pmu/event.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright 2013, Michael Ellerman, IBM Corp.
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #define _GNU_SOURCE
7*4882a593Smuzhiyun #include <unistd.h>
8*4882a593Smuzhiyun #include <sys/syscall.h>
9*4882a593Smuzhiyun #include <string.h>
10*4882a593Smuzhiyun #include <stdio.h>
11*4882a593Smuzhiyun #include <sys/ioctl.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include "event.h"
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun 
perf_event_open(struct perf_event_attr * attr,pid_t pid,int cpu,int group_fd,unsigned long flags)16*4882a593Smuzhiyun int perf_event_open(struct perf_event_attr *attr, pid_t pid, int cpu,
17*4882a593Smuzhiyun 		int group_fd, unsigned long flags)
18*4882a593Smuzhiyun {
19*4882a593Smuzhiyun 	return syscall(__NR_perf_event_open, attr, pid, cpu,
20*4882a593Smuzhiyun 			   group_fd, flags);
21*4882a593Smuzhiyun }
22*4882a593Smuzhiyun 
event_init_opts(struct event * e,u64 config,int type,char * name)23*4882a593Smuzhiyun void event_init_opts(struct event *e, u64 config, int type, char *name)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun 	memset(e, 0, sizeof(*e));
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun 	e->name = name;
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun 	e->attr.type = type;
30*4882a593Smuzhiyun 	e->attr.config = config;
31*4882a593Smuzhiyun 	e->attr.size = sizeof(e->attr);
32*4882a593Smuzhiyun 	/* This has to match the structure layout in the header */
33*4882a593Smuzhiyun 	e->attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | \
34*4882a593Smuzhiyun 				  PERF_FORMAT_TOTAL_TIME_RUNNING;
35*4882a593Smuzhiyun }
36*4882a593Smuzhiyun 
event_init_named(struct event * e,u64 config,char * name)37*4882a593Smuzhiyun void event_init_named(struct event *e, u64 config, char *name)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun 	event_init_opts(e, config, PERF_TYPE_RAW, name);
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun 
event_init(struct event * e,u64 config)42*4882a593Smuzhiyun void event_init(struct event *e, u64 config)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	event_init_opts(e, config, PERF_TYPE_RAW, "event");
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #define PERF_CURRENT_PID	0
48*4882a593Smuzhiyun #define PERF_NO_PID		-1
49*4882a593Smuzhiyun #define PERF_NO_CPU		-1
50*4882a593Smuzhiyun #define PERF_NO_GROUP		-1
51*4882a593Smuzhiyun 
event_open_with_options(struct event * e,pid_t pid,int cpu,int group_fd)52*4882a593Smuzhiyun int event_open_with_options(struct event *e, pid_t pid, int cpu, int group_fd)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	e->fd = perf_event_open(&e->attr, pid, cpu, group_fd, 0);
55*4882a593Smuzhiyun 	if (e->fd == -1) {
56*4882a593Smuzhiyun 		perror("perf_event_open");
57*4882a593Smuzhiyun 		return -1;
58*4882a593Smuzhiyun 	}
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	return 0;
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun 
event_open_with_group(struct event * e,int group_fd)63*4882a593Smuzhiyun int event_open_with_group(struct event *e, int group_fd)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	return event_open_with_options(e, PERF_CURRENT_PID, PERF_NO_CPU, group_fd);
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun 
event_open_with_pid(struct event * e,pid_t pid)68*4882a593Smuzhiyun int event_open_with_pid(struct event *e, pid_t pid)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	return event_open_with_options(e, pid, PERF_NO_CPU, PERF_NO_GROUP);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun 
event_open_with_cpu(struct event * e,int cpu)73*4882a593Smuzhiyun int event_open_with_cpu(struct event *e, int cpu)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	return event_open_with_options(e, PERF_NO_PID, cpu, PERF_NO_GROUP);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
event_open(struct event * e)78*4882a593Smuzhiyun int event_open(struct event *e)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	return event_open_with_options(e, PERF_CURRENT_PID, PERF_NO_CPU, PERF_NO_GROUP);
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
event_close(struct event * e)83*4882a593Smuzhiyun void event_close(struct event *e)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	close(e->fd);
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
event_enable(struct event * e)88*4882a593Smuzhiyun int event_enable(struct event *e)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	return ioctl(e->fd, PERF_EVENT_IOC_ENABLE);
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun 
event_disable(struct event * e)93*4882a593Smuzhiyun int event_disable(struct event *e)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun 	return ioctl(e->fd, PERF_EVENT_IOC_DISABLE);
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
event_reset(struct event * e)98*4882a593Smuzhiyun int event_reset(struct event *e)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun 	return ioctl(e->fd, PERF_EVENT_IOC_RESET);
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun 
event_read(struct event * e)103*4882a593Smuzhiyun int event_read(struct event *e)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun 	int rc;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	rc = read(e->fd, &e->result, sizeof(e->result));
108*4882a593Smuzhiyun 	if (rc != sizeof(e->result)) {
109*4882a593Smuzhiyun 		fprintf(stderr, "read error on event %p!\n", e);
110*4882a593Smuzhiyun 		return -1;
111*4882a593Smuzhiyun 	}
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	return 0;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun 
event_report_justified(struct event * e,int name_width,int result_width)116*4882a593Smuzhiyun void event_report_justified(struct event *e, int name_width, int result_width)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	printf("%*s: result %*llu ", name_width, e->name, result_width,
119*4882a593Smuzhiyun 	       e->result.value);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	if (e->result.running == e->result.enabled)
122*4882a593Smuzhiyun 		printf("running/enabled %llu\n", e->result.running);
123*4882a593Smuzhiyun 	else
124*4882a593Smuzhiyun 		printf("running %llu enabled %llu\n", e->result.running,
125*4882a593Smuzhiyun 			e->result.enabled);
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun 
event_report(struct event * e)128*4882a593Smuzhiyun void event_report(struct event *e)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	event_report_justified(e, 0, 0);
131*4882a593Smuzhiyun }
132