blob: ad56cfc1530eb1c0e363f22241afab748dddef3c [file] [log] [blame]
Yabin Cui67d3abd2015-04-16 15:26:31 -07001/*
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#ifndef SIMPLE_PERF_EVENT_FD_H_
18#define SIMPLE_PERF_EVENT_FD_H_
19
20#include <sys/types.h>
21
22#include <memory>
23#include <string>
Yabin Cuif469c3d2015-10-07 15:00:46 -070024#include <vector>
Yabin Cui67d3abd2015-04-16 15:26:31 -070025
Elliott Hughes66dd09e2015-12-04 14:00:57 -080026#include <android-base/macros.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070027
Yabin Cuidbbda302016-07-28 12:55:41 -070028#include "IOEventLoop.h"
Yabin Cui323e9452015-04-20 18:07:17 -070029#include "perf_event.h"
30
Yabin Cui323e9452015-04-20 18:07:17 -070031struct PerfCounter {
Yabin Cuidbbda302016-07-28 12:55:41 -070032 uint64_t value; // The value of the event specified by the perf_event_file.
Yabin Cui323e9452015-04-20 18:07:17 -070033 uint64_t time_enabled; // The enabled time.
34 uint64_t time_running; // The running time.
35 uint64_t id; // The id of the perf_event_file.
36};
37
Yabin Cui67d3abd2015-04-16 15:26:31 -070038// EventFd represents an opened perf_event_file.
39class EventFd {
40 public:
Yabin Cuidbbda302016-07-28 12:55:41 -070041 static std::unique_ptr<EventFd> OpenEventFile(const perf_event_attr& attr,
42 pid_t tid, int cpu,
43 EventFd* group_event_fd,
44 bool report_error = true);
Yabin Cui67d3abd2015-04-16 15:26:31 -070045
46 ~EventFd();
47
Yabin Cui877751b2016-06-13 18:03:47 -070048 // Give information about this perf_event_file, like (event_name, tid, cpu).
49 std::string Name() const;
50
Yabin Cui9759e1b2015-04-28 15:54:13 -070051 uint64_t Id() const;
52
Yabin Cuidbbda302016-07-28 12:55:41 -070053 pid_t ThreadId() const { return tid_; }
Yabin Cui04d08a32015-08-19 15:01:12 -070054
Yabin Cuidbbda302016-07-28 12:55:41 -070055 int Cpu() const { return cpu_; }
Yabin Cui04d08a32015-08-19 15:01:12 -070056
Yabin Cuidbbda302016-07-28 12:55:41 -070057 int fd() const { return perf_event_fd_; }
58
59 const perf_event_attr& attr() const { return attr_; }
60
61 // It tells the kernel to start counting and recording events specified by
62 // this file.
Yabin Cui56335862016-04-18 13:43:20 -070063 bool EnableEvent();
64
Yabin Cui9759e1b2015-04-28 15:54:13 -070065 bool ReadCounter(PerfCounter* counter) const;
66
Yabin Cui61735922016-07-08 13:56:48 -070067 // Create mapped buffer used to receive records sent by the kernel.
Yabin Cuidbbda302016-07-28 12:55:41 -070068 // mmap_pages should be power of 2.
69 bool CreateMappedBuffer(size_t mmap_pages, bool report_error);
Yabin Cui61735922016-07-08 13:56:48 -070070
71 // Share the mapped buffer used by event_fd. The two EventFds should monitor
72 // the same event on the same cpu, but have different thread ids.
Yabin Cui0a072cd2016-07-13 17:06:50 -070073 bool ShareMappedBuffer(const EventFd& event_fd, bool report_error);
Yabin Cui61735922016-07-08 13:56:48 -070074
Yabin Cuidbbda302016-07-28 12:55:41 -070075 bool HasMappedBuffer() const { return mmap_data_buffer_size_ != 0; }
Yabin Cui9759e1b2015-04-28 15:54:13 -070076
Yabin Cui0a072cd2016-07-13 17:06:50 -070077 void DestroyMappedBuffer();
78
Yabin Cuidbbda302016-07-28 12:55:41 -070079 // When the kernel writes new sampled records to the mapped area, we can get
80 // them by returning the start address and size of the data.
81 size_t GetAvailableMmapData(const char** pdata);
Yabin Cui9759e1b2015-04-28 15:54:13 -070082
Yabin Cui67d3abd2015-04-16 15:26:31 -070083 private:
Yabin Cuidbbda302016-07-28 12:55:41 -070084 EventFd(const perf_event_attr& attr, int perf_event_fd,
85 const std::string& event_name, pid_t tid, int cpu)
86 : attr_(attr),
87 perf_event_fd_(perf_event_fd),
Yabin Cui9759e1b2015-04-28 15:54:13 -070088 id_(0),
89 event_name_(event_name),
Yabin Cuib032de72015-06-17 21:15:09 -070090 tid_(tid),
Yabin Cui9759e1b2015-04-28 15:54:13 -070091 cpu_(cpu),
92 mmap_addr_(nullptr),
Yabin Cui61735922016-07-08 13:56:48 -070093 mmap_len_(0),
94 mmap_metadata_page_(nullptr),
95 mmap_data_buffer_(nullptr),
Yabin Cuidbbda302016-07-28 12:55:41 -070096 mmap_data_buffer_size_(0) {}
Yabin Cui67d3abd2015-04-16 15:26:31 -070097
Yabin Cuidbbda302016-07-28 12:55:41 -070098 // Discard how much data we have read, so the kernel can reuse this part of
99 // mapped area to store new data.
Yabin Cuif469c3d2015-10-07 15:00:46 -0700100 void DiscardMmapData(size_t discard_size);
101
Yabin Cuidbbda302016-07-28 12:55:41 -0700102 const perf_event_attr attr_;
Yabin Cui67d3abd2015-04-16 15:26:31 -0700103 int perf_event_fd_;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700104 mutable uint64_t id_;
Yabin Cui67d3abd2015-04-16 15:26:31 -0700105 const std::string event_name_;
Yabin Cuib032de72015-06-17 21:15:09 -0700106 pid_t tid_;
Yabin Cui67d3abd2015-04-16 15:26:31 -0700107 int cpu_;
108
Yabin Cui9759e1b2015-04-28 15:54:13 -0700109 void* mmap_addr_;
110 size_t mmap_len_;
111 perf_event_mmap_page* mmap_metadata_page_; // The first page of mmap_area.
Yabin Cuidbbda302016-07-28 12:55:41 -0700112 char* mmap_data_buffer_; // Starting from the second page of mmap_area,
113 // containing records written by then kernel.
Yabin Cui9759e1b2015-04-28 15:54:13 -0700114 size_t mmap_data_buffer_size_;
115
Yabin Cuidbbda302016-07-28 12:55:41 -0700116 // As mmap_data_buffer is a ring buffer, it is possible that one record is
117 // wrapped at the end of the buffer. So we need to copy records from
118 // mmap_data_buffer to data_process_buffer before processing them.
Yabin Cuif469c3d2015-10-07 15:00:46 -0700119 static std::vector<char> data_process_buffer_;
120
Yabin Cui67d3abd2015-04-16 15:26:31 -0700121 DISALLOW_COPY_AND_ASSIGN(EventFd);
122};
123
Yabin Cui9fd3cc12015-06-25 17:42:23 -0700124bool IsEventAttrSupportedByKernel(perf_event_attr attr);
125
Yabin Cui67d3abd2015-04-16 15:26:31 -0700126#endif // SIMPLE_PERF_EVENT_FD_H_