2 * Copyright (C) 2015 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <sys/ioctl.h>
22 #include <sys/syscall.h>
23 #include <sys/types.h>
26 #include <base/logging.h>
27 #include <base/stringprintf.h>
29 #include "event_type.h"
30 #include "event_attr.h"
31 #include "perf_event.h"
34 static int perf_event_open(perf_event_attr* attr, pid_t pid, int cpu, int group_fd,
35 unsigned long flags) {
36 return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags);
39 std::unique_ptr<EventFd> EventFd::OpenEventFileForProcess(const EventAttr& attr, pid_t pid) {
40 return OpenEventFile(attr, pid, -1);
43 std::unique_ptr<EventFd> EventFd::OpenEventFileForCpu(const EventAttr& attr, int cpu) {
44 return OpenEventFile(attr, -1, cpu);
47 std::unique_ptr<EventFd> EventFd::OpenEventFile(const EventAttr& attr, pid_t pid, int cpu) {
48 perf_event_attr perf_attr = attr.Attr();
49 std::string event_name = "unknown event";
50 const EventType* event_type =
51 EventTypeFactory::FindEventTypeByConfig(perf_attr.type, perf_attr.config);
52 if (event_type != nullptr) {
53 event_name = event_type->name;
55 int perf_event_fd = perf_event_open(&perf_attr, pid, cpu, -1, 0);
56 if (perf_event_fd == -1) {
57 // It depends whether the perf_event_file configuration is supported by the kernel and the
58 // machine. So fail to open the file is not an error.
59 PLOG(DEBUG) << "open perf_event_file (event " << event_name << ", pid " << pid << ", cpu "
63 if (fcntl(perf_event_fd, F_SETFD, FD_CLOEXEC) == -1) {
64 PLOG(ERROR) << "fcntl(FD_CLOEXEC) for perf_event_file (event " << event_name << ", pid " << pid
65 << ", cpu " << cpu << ") failed";
68 return std::unique_ptr<EventFd>(new EventFd(perf_event_fd, event_name, pid, cpu));
72 close(perf_event_fd_);
75 std::string EventFd::Name() const {
76 return android::base::StringPrintf("perf_event_file(event %s, pid %d, cpu %d)",
77 event_name_.c_str(), pid_, cpu_);
80 bool EventFd::EnableEvent() {
81 int result = ioctl(perf_event_fd_, PERF_EVENT_IOC_ENABLE, 0);
83 PLOG(ERROR) << "ioctl(enable) " << Name() << " failed";
89 bool EventFd::DisableEvent() {
90 int result = ioctl(perf_event_fd_, PERF_EVENT_IOC_DISABLE, 0);
92 PLOG(ERROR) << "ioctl(disable) " << Name() << " failed";
98 bool EventFd::ReadCounter(PerfCounter* counter) {
99 CHECK(counter != nullptr);
100 if (!ReadNBytesFromFile(perf_event_fd_, counter, sizeof(*counter))) {
101 PLOG(ERROR) << "ReadCounter from " << Name() << " failed";