Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright 2013, Michael Ellerman, IBM Corp.
0004  */
0005 
0006 #define _GNU_SOURCE
0007 #include <unistd.h>
0008 #include <sys/syscall.h>
0009 #include <string.h>
0010 #include <stdio.h>
0011 #include <stdbool.h>
0012 #include <sys/ioctl.h>
0013 
0014 #include "event.h"
0015 
0016 
0017 int perf_event_open(struct perf_event_attr *attr, pid_t pid, int cpu,
0018         int group_fd, unsigned long flags)
0019 {
0020     return syscall(__NR_perf_event_open, attr, pid, cpu,
0021                group_fd, flags);
0022 }
0023 
0024 static void  __event_init_opts(struct event *e, u64 config,
0025                    int type, char *name, bool sampling)
0026 {
0027     memset(e, 0, sizeof(*e));
0028 
0029     e->name = name;
0030 
0031     e->attr.type = type;
0032     e->attr.config = config;
0033     e->attr.size = sizeof(e->attr);
0034     /* This has to match the structure layout in the header */
0035     e->attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | \
0036                   PERF_FORMAT_TOTAL_TIME_RUNNING;
0037     if (sampling) {
0038         e->attr.sample_period = 1000;
0039         e->attr.sample_type = PERF_SAMPLE_REGS_INTR;
0040         e->attr.disabled = 1;
0041     }
0042 }
0043 
0044 void event_init_opts(struct event *e, u64 config, int type, char *name)
0045 {
0046     __event_init_opts(e, config, type, name, false);
0047 }
0048 
0049 void event_init_named(struct event *e, u64 config, char *name)
0050 {
0051     event_init_opts(e, config, PERF_TYPE_RAW, name);
0052 }
0053 
0054 void event_init(struct event *e, u64 config)
0055 {
0056     event_init_opts(e, config, PERF_TYPE_RAW, "event");
0057 }
0058 
0059 void event_init_sampling(struct event *e, u64 config)
0060 {
0061     __event_init_opts(e, config, PERF_TYPE_RAW, "event", true);
0062 }
0063 
0064 #define PERF_CURRENT_PID    0
0065 #define PERF_NO_PID     -1
0066 #define PERF_NO_CPU     -1
0067 #define PERF_NO_GROUP       -1
0068 
0069 int event_open_with_options(struct event *e, pid_t pid, int cpu, int group_fd)
0070 {
0071     e->fd = perf_event_open(&e->attr, pid, cpu, group_fd, 0);
0072     if (e->fd == -1) {
0073         perror("perf_event_open");
0074         return -1;
0075     }
0076 
0077     return 0;
0078 }
0079 
0080 int event_open_with_group(struct event *e, int group_fd)
0081 {
0082     return event_open_with_options(e, PERF_CURRENT_PID, PERF_NO_CPU, group_fd);
0083 }
0084 
0085 int event_open_with_pid(struct event *e, pid_t pid)
0086 {
0087     return event_open_with_options(e, pid, PERF_NO_CPU, PERF_NO_GROUP);
0088 }
0089 
0090 int event_open_with_cpu(struct event *e, int cpu)
0091 {
0092     return event_open_with_options(e, PERF_NO_PID, cpu, PERF_NO_GROUP);
0093 }
0094 
0095 int event_open(struct event *e)
0096 {
0097     return event_open_with_options(e, PERF_CURRENT_PID, PERF_NO_CPU, PERF_NO_GROUP);
0098 }
0099 
0100 void event_close(struct event *e)
0101 {
0102     close(e->fd);
0103 }
0104 
0105 int event_enable(struct event *e)
0106 {
0107     return ioctl(e->fd, PERF_EVENT_IOC_ENABLE);
0108 }
0109 
0110 int event_disable(struct event *e)
0111 {
0112     return ioctl(e->fd, PERF_EVENT_IOC_DISABLE);
0113 }
0114 
0115 int event_reset(struct event *e)
0116 {
0117     return ioctl(e->fd, PERF_EVENT_IOC_RESET);
0118 }
0119 
0120 int event_read(struct event *e)
0121 {
0122     int rc;
0123 
0124     rc = read(e->fd, &e->result, sizeof(e->result));
0125     if (rc != sizeof(e->result)) {
0126         fprintf(stderr, "read error on event %p!\n", e);
0127         return -1;
0128     }
0129 
0130     return 0;
0131 }
0132 
0133 void event_report_justified(struct event *e, int name_width, int result_width)
0134 {
0135     printf("%*s: result %*llu ", name_width, e->name, result_width,
0136            e->result.value);
0137 
0138     if (e->result.running == e->result.enabled)
0139         printf("running/enabled %llu\n", e->result.running);
0140     else
0141         printf("running %llu enabled %llu\n", e->result.running,
0142             e->result.enabled);
0143 }
0144 
0145 void event_report(struct event *e)
0146 {
0147     event_report_justified(e, 0, 0);
0148 }