OSDN Git Service

b7c1b4ce91bc353e4750c29d05f3e921c2bb92e3
[android-x86/system-extras.git] / simpleperf / event_fd.cpp
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "event_fd.h"
18
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <sys/ioctl.h>
22 #include <sys/syscall.h>
23 #include <sys/types.h>
24 #include <memory>
25
26 #include <base/logging.h>
27 #include <base/stringprintf.h>
28
29 #include "event_type.h"
30 #include "event_attr.h"
31 #include "perf_event.h"
32 #include "utils.h"
33
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);
37 }
38
39 std::unique_ptr<EventFd> EventFd::OpenEventFileForProcess(const EventAttr& attr, pid_t pid) {
40   return OpenEventFile(attr, pid, -1);
41 }
42
43 std::unique_ptr<EventFd> EventFd::OpenEventFileForCpu(const EventAttr& attr, int cpu) {
44   return OpenEventFile(attr, -1, cpu);
45 }
46
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;
54   }
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 "
60                 << cpu << ") failed";
61     return nullptr;
62   }
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";
66     return nullptr;
67   }
68   return std::unique_ptr<EventFd>(new EventFd(perf_event_fd, event_name, pid, cpu));
69 }
70
71 EventFd::~EventFd() {
72   close(perf_event_fd_);
73 }
74
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_);
78 }
79
80 bool EventFd::EnableEvent() {
81   int result = ioctl(perf_event_fd_, PERF_EVENT_IOC_ENABLE, 0);
82   if (result < 0) {
83     PLOG(ERROR) << "ioctl(enable) " << Name() << " failed";
84     return false;
85   }
86   return true;
87 }
88
89 bool EventFd::DisableEvent() {
90   int result = ioctl(perf_event_fd_, PERF_EVENT_IOC_DISABLE, 0);
91   if (result < 0) {
92     PLOG(ERROR) << "ioctl(disable) " << Name() << " failed";
93     return false;
94   }
95   return true;
96 }
97
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";
102     return false;
103   }
104   return true;
105 }