• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 SRC_PROFILING_PERF_EVENT_READER_H_
18 #define SRC_PROFILING_PERF_EVENT_READER_H_
19 
20 #include <linux/perf_event.h>
21 #include <stdint.h>
22 #include <sys/mman.h>
23 #include <sys/types.h>
24 #include <optional>
25 
26 #include "perfetto/ext/base/scoped_file.h"
27 #include "perfetto/ext/tracing/core/basic_types.h"
28 #include "src/profiling/perf/common_types.h"
29 #include "src/profiling/perf/event_config.h"
30 
31 namespace perfetto {
32 namespace profiling {
33 
34 class PerfRingBuffer {
35  public:
36   static std::optional<PerfRingBuffer> Allocate(int perf_fd,
37                                                 size_t data_page_count);
38 
39   ~PerfRingBuffer();
40 
41   // move-only
42   PerfRingBuffer(const PerfRingBuffer&) = delete;
43   PerfRingBuffer& operator=(const PerfRingBuffer&) = delete;
44   PerfRingBuffer(PerfRingBuffer&& other) noexcept;
45   PerfRingBuffer& operator=(PerfRingBuffer&& other) noexcept;
46 
47   char* ReadRecordNonconsuming();
48   void Consume(size_t bytes);
49 
50  private:
51   PerfRingBuffer() = default;
52 
valid()53   bool valid() const { return metadata_page_ != nullptr; }
54 
55   // Points at the start of the mmap'd region.
56   perf_event_mmap_page* metadata_page_ = nullptr;
57 
58   // Size of the mmap'd region (1 metadata page + data_buf_sz_).
59   size_t mmap_sz_ = 0;
60 
61   // mmap'd ring buffer
62   char* data_buf_ = nullptr;
63   size_t data_buf_sz_ = 0;
64 
65   // When a record wraps around the ring buffer boundary, it is reconstructed in
66   // a contiguous form in this buffer. This allows us to always return a pointer
67   // to a contiguous record.
68   constexpr static size_t kMaxPerfRecordSize = 1 << 16;  // max size 64k
69   alignas(uint64_t) char reconstructed_record_[kMaxPerfRecordSize];
70 };
71 
72 class EventReader {
73  public:
74   static std::optional<EventReader> ConfigureEvents(
75       uint32_t cpu,
76       const EventConfig& event_cfg);
77 
78   // Consumes records from the ring buffer until either encountering a sample,
79   // or catching up to the writer. The other record of interest
80   // (PERF_RECORD_LOST) is handled via the given callback.
81   std::optional<ParsedSample> ReadUntilSample(
82       std::function<void(uint64_t)> lost_events_callback);
83 
84   void EnableEvents();
85   // Pauses the event counting, without invalidating existing samples.
86   void DisableEvents();
87 
cpu()88   uint32_t cpu() const { return cpu_; }
89 
90   ~EventReader() = default;
91 
92   // move-only
93   EventReader(const EventReader&) = delete;
94   EventReader& operator=(const EventReader&) = delete;
95   EventReader(EventReader&&) noexcept = default;
96   EventReader& operator=(EventReader&&) noexcept;
97 
98  private:
99   EventReader(uint32_t cpu,
100               perf_event_attr event_attr,
101               base::ScopedFile perf_fd,
102               PerfRingBuffer ring_buffer);
103 
104   ParsedSample ParseSampleRecord(uint32_t cpu, const char* record_start);
105 
106   // All events are cpu-bound (thread-scoped events not supported).
107   const uint32_t cpu_;
108   const perf_event_attr event_attr_;
109   base::ScopedFile perf_fd_;
110   PerfRingBuffer ring_buffer_;
111 };
112 
113 }  // namespace profiling
114 }  // namespace perfetto
115 
116 #endif  // SRC_PROFILING_PERF_EVENT_READER_H_
117