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#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>
24#include <vector>
25
26#include <android-base/macros.h>
27
28#include "IOEventLoop.h"
29#include "perf_event.h"
30
31struct PerfCounter {
32  uint64_t value;  // The value of the event specified by the perf_event_file.
33  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
38// EventFd represents an opened perf_event_file.
39class EventFd {
40 public:
41  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);
45
46  ~EventFd();
47
48  // Give information about this perf_event_file, like (event_name, tid, cpu).
49  std::string Name() const;
50
51  uint64_t Id() const;
52
53  pid_t ThreadId() const { return tid_; }
54
55  int Cpu() const { return cpu_; }
56
57  const perf_event_attr& attr() const { return attr_; }
58
59  // It tells the kernel to start counting and recording events specified by
60  // this file.
61  bool EnableEvent();
62
63  bool ReadCounter(PerfCounter* counter);
64
65  // Create mapped buffer used to receive records sent by the kernel.
66  // mmap_pages should be power of 2.
67  bool CreateMappedBuffer(size_t mmap_pages, bool report_error);
68
69  // Share the mapped buffer used by event_fd. The two EventFds should monitor
70  // the same event on the same cpu, but have different thread ids.
71  bool ShareMappedBuffer(const EventFd& event_fd, bool report_error);
72
73  bool HasMappedBuffer() const { return mmap_data_buffer_size_ != 0; }
74
75  void DestroyMappedBuffer();
76
77  // When the kernel writes new sampled records to the mapped area, we can get
78  // them by returning the start address and size of the data.
79  size_t GetAvailableMmapData(std::vector<char>& buffer, size_t& buffer_pos);
80
81  // [callback] is called when there is data available in the mapped buffer.
82  bool StartPolling(IOEventLoop& loop, const std::function<bool()>& callback);
83  bool StopPolling();
84
85 private:
86  EventFd(const perf_event_attr& attr, int perf_event_fd,
87          const std::string& event_name, pid_t tid, int cpu)
88      : attr_(attr),
89        perf_event_fd_(perf_event_fd),
90        id_(0),
91        event_name_(event_name),
92        tid_(tid),
93        cpu_(cpu),
94        mmap_addr_(nullptr),
95        mmap_len_(0),
96        mmap_metadata_page_(nullptr),
97        mmap_data_buffer_(nullptr),
98        mmap_data_buffer_size_(0),
99        ioevent_ref_(nullptr),
100        last_counter_value_(0) {}
101
102  bool InnerReadCounter(PerfCounter* counter) const;
103  // Discard how much data we have read, so the kernel can reuse this part of
104  // mapped area to store new data.
105  void DiscardMmapData(size_t discard_size);
106
107  const perf_event_attr attr_;
108  int perf_event_fd_;
109  mutable uint64_t id_;
110  const std::string event_name_;
111  pid_t tid_;
112  int cpu_;
113
114  void* mmap_addr_;
115  size_t mmap_len_;
116  perf_event_mmap_page* mmap_metadata_page_;  // The first page of mmap_area.
117  char* mmap_data_buffer_;  // Starting from the second page of mmap_area,
118                            // containing records written by then kernel.
119  size_t mmap_data_buffer_size_;
120
121  // As mmap_data_buffer is a ring buffer, it is possible that one record is
122  // wrapped at the end of the buffer. So we need to copy records from
123  // mmap_data_buffer to data_process_buffer before processing them.
124  static std::vector<char> data_process_buffer_;
125
126  IOEventRef ioevent_ref_;
127
128  // Used by atrace to generate value difference between two ReadCounter() calls.
129  uint64_t last_counter_value_;
130
131  DISALLOW_COPY_AND_ASSIGN(EventFd);
132};
133
134bool IsEventAttrSupported(const perf_event_attr& attr);
135
136#endif  // SIMPLE_PERF_EVENT_FD_H_
137