• 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 #include "src/profiling/perf/event_reader.h"
18 
19 #include <linux/perf_event.h>
20 #include <sys/ioctl.h>
21 #include <sys/mman.h>
22 #include <sys/syscall.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 
26 #include "perfetto/ext/base/utils.h"
27 #include "src/profiling/perf/regs_parsing.h"
28 
29 namespace perfetto {
30 namespace profiling {
31 
32 namespace {
33 
34 template <typename T>
ReadValue(T * value_out,const char * ptr)35 const char* ReadValue(T* value_out, const char* ptr) {
36   memcpy(value_out, reinterpret_cast<const void*>(ptr), sizeof(T));
37   return ptr + sizeof(T);
38 }
39 
40 template <typename T>
ReadValues(T * out,const char * ptr,size_t num_values)41 const char* ReadValues(T* out, const char* ptr, size_t num_values) {
42   size_t sz = sizeof(T) * num_values;
43   memcpy(out, reinterpret_cast<const void*>(ptr), sz);
44   return ptr + sz;
45 }
46 
IsPowerOfTwo(size_t v)47 bool IsPowerOfTwo(size_t v) {
48   return (v != 0 && ((v & (v - 1)) == 0));
49 }
50 
perf_event_open(perf_event_attr * attr,pid_t pid,int cpu,int group_fd,unsigned long flags)51 static int perf_event_open(perf_event_attr* attr,
52                            pid_t pid,
53                            int cpu,
54                            int group_fd,
55                            unsigned long flags) {
56   return static_cast<int>(
57       syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags));
58 }
59 
PerfEventOpen(uint32_t cpu,perf_event_attr * perf_attr,int group_fd=-1)60 base::ScopedFile PerfEventOpen(uint32_t cpu,
61                                perf_event_attr* perf_attr,
62                                int group_fd = -1) {
63   base::ScopedFile perf_fd{perf_event_open(perf_attr, /*pid=*/-1,
64                                            static_cast<int>(cpu), group_fd,
65                                            PERF_FLAG_FD_CLOEXEC)};
66   return perf_fd;
67 }
68 
69 // If counting tracepoints, set an event filter if requested.
MaybeApplyTracepointFilter(int fd,const PerfCounter & event)70 bool MaybeApplyTracepointFilter(int fd, const PerfCounter& event) {
71   if (event.type != PerfCounter::Type::kTracepoint ||
72       event.tracepoint_filter.empty()) {
73     return true;
74   }
75   PERFETTO_DCHECK(event.attr_type == PERF_TYPE_TRACEPOINT);
76 
77   if (ioctl(fd, PERF_EVENT_IOC_SET_FILTER, event.tracepoint_filter.c_str())) {
78     PERFETTO_PLOG("Failed ioctl to set event filter");
79     return false;
80   }
81   return true;
82 }
83 
84 }  // namespace
85 
PerfRingBuffer(PerfRingBuffer && other)86 PerfRingBuffer::PerfRingBuffer(PerfRingBuffer&& other) noexcept
87     : metadata_page_(other.metadata_page_),
88       mmap_sz_(other.mmap_sz_),
89       data_buf_(other.data_buf_),
90       data_buf_sz_(other.data_buf_sz_) {
91   other.metadata_page_ = nullptr;
92   other.mmap_sz_ = 0;
93   other.data_buf_ = nullptr;
94   other.data_buf_sz_ = 0;
95 }
96 
operator =(PerfRingBuffer && other)97 PerfRingBuffer& PerfRingBuffer::operator=(PerfRingBuffer&& other) noexcept {
98   if (this == &other)
99     return *this;
100 
101   this->~PerfRingBuffer();
102   new (this) PerfRingBuffer(std::move(other));
103   return *this;
104 }
105 
~PerfRingBuffer()106 PerfRingBuffer::~PerfRingBuffer() {
107   if (!valid())
108     return;
109 
110   if (munmap(reinterpret_cast<void*>(metadata_page_), mmap_sz_) != 0)
111     PERFETTO_PLOG("failed munmap");
112 }
113 
Allocate(int perf_fd,size_t data_page_count)114 std::optional<PerfRingBuffer> PerfRingBuffer::Allocate(int perf_fd,
115                                                        size_t data_page_count) {
116   // perf_event_open requires the ring buffer to be a power of two in size.
117   PERFETTO_DCHECK(IsPowerOfTwo(data_page_count));
118 
119   PerfRingBuffer ret;
120 
121   // mmap request is one page larger than the buffer size (for the metadata).
122   ret.data_buf_sz_ = data_page_count * base::kPageSize;
123   ret.mmap_sz_ = ret.data_buf_sz_ + base::kPageSize;
124 
125   // If PROT_WRITE, kernel won't overwrite unread samples.
126   void* mmap_addr = mmap(nullptr, ret.mmap_sz_, PROT_READ | PROT_WRITE,
127                          MAP_SHARED, perf_fd, 0);
128   if (mmap_addr == MAP_FAILED) {
129     PERFETTO_PLOG("failed mmap");
130     return std::nullopt;
131   }
132 
133   // Expected layout is [ metadata page ] [ data pages ... ]
134   ret.metadata_page_ = reinterpret_cast<perf_event_mmap_page*>(mmap_addr);
135   ret.data_buf_ = reinterpret_cast<char*>(mmap_addr) + base::kPageSize;
136   PERFETTO_CHECK(ret.metadata_page_->data_offset == base::kPageSize);
137   PERFETTO_CHECK(ret.metadata_page_->data_size == ret.data_buf_sz_);
138 
139   PERFETTO_DCHECK(IsPowerOfTwo(ret.data_buf_sz_));
140 
141   return std::make_optional(std::move(ret));
142 }
143 
144 // See |perf_output_put_handle| for the necessary synchronization between the
145 // kernel and this userspace thread (which are using the same shared memory, but
146 // might be on different cores).
147 // TODO(rsavitski): is there false sharing between |data_tail| and |data_head|?
148 // Is there an argument for maintaining our own copy of |data_tail| instead of
149 // reloading it?
ReadRecordNonconsuming()150 char* PerfRingBuffer::ReadRecordNonconsuming() {
151   static_assert(sizeof(std::atomic<uint64_t>) == sizeof(uint64_t), "");
152 
153   PERFETTO_DCHECK(valid());
154 
155   // |data_tail| is written only by this userspace thread, so we can safely read
156   // it without any synchronization.
157   uint64_t read_offset = metadata_page_->data_tail;
158 
159   // |data_head| is written by the kernel, perform an acquiring load such that
160   // the payload reads below are ordered after this load.
161   uint64_t write_offset =
162       reinterpret_cast<std::atomic<uint64_t>*>(&metadata_page_->data_head)
163           ->load(std::memory_order_acquire);
164 
165   PERFETTO_DCHECK(read_offset <= write_offset);
166   if (write_offset == read_offset)
167     return nullptr;  // no new data
168 
169   size_t read_pos = static_cast<size_t>(read_offset & (data_buf_sz_ - 1));
170 
171   // event header (64 bits) guaranteed to be contiguous
172   PERFETTO_DCHECK(read_pos <= data_buf_sz_ - sizeof(perf_event_header));
173   PERFETTO_DCHECK(0 == reinterpret_cast<size_t>(data_buf_ + read_pos) %
174                            alignof(perf_event_header));
175 
176   perf_event_header* evt_header =
177       reinterpret_cast<perf_event_header*>(data_buf_ + read_pos);
178   uint16_t evt_size = evt_header->size;
179 
180   // event wrapped - reconstruct it, and return a pointer to the buffer
181   if (read_pos + evt_size > data_buf_sz_) {
182     PERFETTO_DLOG("PerfRingBuffer: returning reconstructed event");
183 
184     size_t prefix_sz = data_buf_sz_ - read_pos;
185     memcpy(&reconstructed_record_[0], data_buf_ + read_pos, prefix_sz);
186     memcpy(&reconstructed_record_[0] + prefix_sz, data_buf_,
187            evt_size - prefix_sz);
188     return &reconstructed_record_[0];
189   } else {
190     // usual case - contiguous sample
191     return data_buf_ + read_pos;
192   }
193 }
194 
Consume(size_t bytes)195 void PerfRingBuffer::Consume(size_t bytes) {
196   PERFETTO_DCHECK(valid());
197 
198   // Advance |data_tail|, which is written only by this thread. The store of the
199   // updated value needs to have release semantics such that the preceding
200   // payload reads are ordered before it. The reader in this case is the kernel,
201   // which reads |data_tail| to calculate the available ring buffer capacity
202   // before trying to store a new record.
203   uint64_t updated_tail = metadata_page_->data_tail + bytes;
204   reinterpret_cast<std::atomic<uint64_t>*>(&metadata_page_->data_tail)
205       ->store(updated_tail, std::memory_order_release);
206 }
207 
EventReader(uint32_t cpu,perf_event_attr event_attr,base::ScopedFile perf_fd,PerfRingBuffer ring_buffer)208 EventReader::EventReader(uint32_t cpu,
209                          perf_event_attr event_attr,
210                          base::ScopedFile perf_fd,
211                          PerfRingBuffer ring_buffer)
212     : cpu_(cpu),
213       event_attr_(event_attr),
214       perf_fd_(std::move(perf_fd)),
215       ring_buffer_(std::move(ring_buffer)) {}
216 
operator =(EventReader && other)217 EventReader& EventReader::operator=(EventReader&& other) noexcept {
218   if (this == &other)
219     return *this;
220 
221   this->~EventReader();
222   new (this) EventReader(std::move(other));
223   return *this;
224 }
225 
ConfigureEvents(uint32_t cpu,const EventConfig & event_cfg)226 std::optional<EventReader> EventReader::ConfigureEvents(
227     uint32_t cpu,
228     const EventConfig& event_cfg) {
229   auto leader_fd = PerfEventOpen(cpu, event_cfg.perf_attr());
230   if (!leader_fd) {
231     PERFETTO_PLOG("Failed perf_event_open");
232     return std::nullopt;
233   }
234   if (!MaybeApplyTracepointFilter(leader_fd.get(), event_cfg.timebase_event()))
235     return std::nullopt;
236 
237   auto ring_buffer =
238       PerfRingBuffer::Allocate(leader_fd.get(), event_cfg.ring_buffer_pages());
239   if (!ring_buffer.has_value()) {
240     return std::nullopt;
241   }
242   return EventReader(cpu, *event_cfg.perf_attr(), std::move(leader_fd),
243                      std::move(ring_buffer.value()));
244 }
245 
ReadUntilSample(std::function<void (uint64_t)> records_lost_callback)246 std::optional<ParsedSample> EventReader::ReadUntilSample(
247     std::function<void(uint64_t)> records_lost_callback) {
248   for (;;) {
249     char* event = ring_buffer_.ReadRecordNonconsuming();
250     if (!event)
251       return std::nullopt;  // caught up with the writer
252 
253     auto* event_hdr = reinterpret_cast<const perf_event_header*>(event);
254 
255     if (event_hdr->type == PERF_RECORD_SAMPLE) {
256       ParsedSample sample = ParseSampleRecord(cpu_, event);
257       ring_buffer_.Consume(event_hdr->size);
258       return std::make_optional(std::move(sample));
259     }
260 
261     if (event_hdr->type == PERF_RECORD_LOST) {
262       /*
263        * struct {
264        *   struct perf_event_header header;
265        *   u64 id;
266        *   u64 lost;
267        *   struct sample_id sample_id;
268        * };
269        */
270       uint64_t records_lost = *reinterpret_cast<const uint64_t*>(
271           event + sizeof(perf_event_header) + sizeof(uint64_t));
272 
273       records_lost_callback(records_lost);
274       ring_buffer_.Consume(event_hdr->size);
275       continue;  // keep looking for a sample
276     }
277 
278     // Kernel had to throttle irqs.
279     if (event_hdr->type == PERF_RECORD_THROTTLE ||
280         event_hdr->type == PERF_RECORD_UNTHROTTLE) {
281       ring_buffer_.Consume(event_hdr->size);
282       continue;  // keep looking for a sample
283     }
284 
285     PERFETTO_DFATAL_OR_ELOG("Unsupported event type [%zu]",
286                             static_cast<size_t>(event_hdr->type));
287     ring_buffer_.Consume(event_hdr->size);
288   }
289 }
290 
291 // Generally, samples can belong to any cpu (which can be recorded with
292 // PERF_SAMPLE_CPU). However, this producer uses only cpu-scoped events,
293 // therefore it is already known.
ParseSampleRecord(uint32_t cpu,const char * record_start)294 ParsedSample EventReader::ParseSampleRecord(uint32_t cpu,
295                                             const char* record_start) {
296   if (event_attr_.sample_type &
297       (~uint64_t(PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_STACK_USER |
298                  PERF_SAMPLE_REGS_USER | PERF_SAMPLE_CALLCHAIN |
299                  PERF_SAMPLE_READ))) {
300     PERFETTO_FATAL("Unsupported sampling option");
301   }
302 
303   auto* event_hdr = reinterpret_cast<const perf_event_header*>(record_start);
304   size_t sample_size = event_hdr->size;
305 
306   ParsedSample sample = {};
307   sample.common.cpu = cpu;
308   sample.common.cpu_mode = event_hdr->misc & PERF_RECORD_MISC_CPUMODE_MASK;
309 
310   // Parse the payload, which consists of concatenated data for each
311   // |attr.sample_type| flag.
312   const char* parse_pos = record_start + sizeof(perf_event_header);
313 
314   if (event_attr_.sample_type & PERF_SAMPLE_TID) {
315     uint32_t pid = 0;
316     uint32_t tid = 0;
317     parse_pos = ReadValue(&pid, parse_pos);
318     parse_pos = ReadValue(&tid, parse_pos);
319     sample.common.pid = static_cast<pid_t>(pid);
320     sample.common.tid = static_cast<pid_t>(tid);
321   }
322 
323   if (event_attr_.sample_type & PERF_SAMPLE_TIME) {
324     parse_pos = ReadValue(&sample.common.timestamp, parse_pos);
325   }
326 
327   if (event_attr_.sample_type & PERF_SAMPLE_READ) {
328     parse_pos = ReadValue(&sample.common.timebase_count, parse_pos);
329   }
330 
331   if (event_attr_.sample_type & PERF_SAMPLE_CALLCHAIN) {
332     uint64_t chain_len = 0;
333     parse_pos = ReadValue(&chain_len, parse_pos);
334     sample.kernel_ips.resize(static_cast<size_t>(chain_len));
335     parse_pos = ReadValues<uint64_t>(sample.kernel_ips.data(), parse_pos,
336                                      static_cast<size_t>(chain_len));
337   }
338 
339   if (event_attr_.sample_type & PERF_SAMPLE_REGS_USER) {
340     // Can be empty, e.g. if we sampled a kernel thread.
341     sample.regs = ReadPerfUserRegsData(&parse_pos);
342   }
343 
344   if (event_attr_.sample_type & PERF_SAMPLE_STACK_USER) {
345     // Maximum possible sampled stack size for this sample. Can be lower than
346     // the requested size if there wasn't enough room in the sample (which is
347     // limited to 64k).
348     uint64_t max_stack_size;
349     parse_pos = ReadValue(&max_stack_size, parse_pos);
350 
351     const char* stack_start = parse_pos;
352     parse_pos += max_stack_size;  // skip to dyn_size
353 
354     // Payload written conditionally, e.g. kernel threads don't have a
355     // user stack.
356     if (max_stack_size > 0) {
357       uint64_t filled_stack_size;
358       parse_pos = ReadValue(&filled_stack_size, parse_pos);
359 
360       // copy stack bytes into a vector
361       size_t payload_sz = static_cast<size_t>(filled_stack_size);
362       sample.stack.resize(payload_sz);
363       memcpy(sample.stack.data(), stack_start, payload_sz);
364 
365       // remember whether the stack sample is (most likely) truncated
366       sample.stack_maxed = (filled_stack_size == max_stack_size);
367     }
368   }
369 
370   PERFETTO_CHECK(parse_pos == record_start + sample_size);
371   return sample;
372 }
373 
EnableEvents()374 void EventReader::EnableEvents() {
375   int ret = ioctl(perf_fd_.get(), PERF_EVENT_IOC_ENABLE);
376   PERFETTO_CHECK(ret == 0);
377 }
378 
DisableEvents()379 void EventReader::DisableEvents() {
380   int ret = ioctl(perf_fd_.get(), PERF_EVENT_IOC_DISABLE);
381   PERFETTO_CHECK(ret == 0);
382 }
383 
384 }  // namespace profiling
385 }  // namespace perfetto
386