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 
25 #include "perfetto/ext/base/optional.h"
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 base::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   // Allow base::Optional<EventReader> without making the constructor public.
75   template <typename EventReader, bool>
76   friend struct base::internal::OptionalStorageBase;
77 
78   static base::Optional<EventReader> ConfigureEvents(
79       uint32_t cpu,
80       const EventConfig& event_cfg);
81 
82   // Consumes records from the ring buffer until either encountering a sample,
83   // or catching up to the writer. The other record of interest
84   // (PERF_RECORD_LOST) is handled via the given callback.
85   base::Optional<ParsedSample> ReadUntilSample(
86       std::function<void(uint64_t)> lost_events_callback);
87 
88   void EnableEvents();
89   // Pauses the event counting, without invalidating existing samples.
90   void DisableEvents();
91 
cpu()92   uint32_t cpu() const { return cpu_; }
93 
94   ~EventReader() = default;
95 
96   // move-only
97   EventReader(const EventReader&) = delete;
98   EventReader& operator=(const EventReader&) = delete;
99   EventReader(EventReader&&) noexcept = default;
100   EventReader& operator=(EventReader&&) noexcept;
101 
102  private:
103   EventReader(uint32_t cpu,
104               perf_event_attr event_attr,
105               base::ScopedFile perf_fd,
106               PerfRingBuffer ring_buffer);
107 
108   ParsedSample ParseSampleRecord(uint32_t cpu, const char* record_start);
109 
110   // All events are cpu-bound (thread-scoped events not supported).
111   const uint32_t cpu_;
112   const perf_event_attr event_attr_;
113   base::ScopedFile perf_fd_;
114   PerfRingBuffer ring_buffer_;
115 };
116 
117 }  // namespace profiling
118 }  // namespace perfetto
119 
120 #endif  // SRC_PROFILING_PERF_EVENT_READER_H_
121