perf: record unwinding errors, pace ticks, make rbuf size configurable

Might need to do randomized per-DS offsets for the ticks (as to not
align them with other such patterns, including the ReadTick in
traced_probes). But will see how big of a problem that is.

Bug: 144281346
Change-Id: I1e457d35211d11f0537d3d9755326844e5f45d2c
diff --git a/src/profiling/perf/event_reader.cc b/src/profiling/perf/event_reader.cc
index d381eb7..e7642bb 100644
--- a/src/profiling/perf/event_reader.cc
+++ b/src/profiling/perf/event_reader.cc
@@ -31,7 +31,7 @@
 
 namespace {
 
-constexpr size_t kDataPagesPerRingBuffer = 256;  // 1 MB (256 x 4k pages)
+constexpr size_t kDefaultDataPagesPerRingBuffer = 256;  // 1 MB (256 x 4k pages)
 
 template <typename T>
 const char* ReadValue(T* value_out, const char* ptr) {
@@ -93,7 +93,7 @@
     int perf_fd,
     size_t data_page_count) {
   // perf_event_open requires the ring buffer to be a power of two in size.
-  PERFETTO_CHECK(IsPowerOfTwo(data_page_count));
+  PERFETTO_DCHECK(IsPowerOfTwo(data_page_count));
 
   PerfRingBuffer ret;
 
@@ -105,7 +105,7 @@
   void* mmap_addr = mmap(nullptr, ret.mmap_sz_, PROT_READ | PROT_WRITE,
                          MAP_SHARED, perf_fd, 0);
   if (mmap_addr == MAP_FAILED) {
-    PERFETTO_PLOG("failed mmap (check perf_event_mlock_kb in procfs)");
+    PERFETTO_PLOG("failed mmap");
     return base::nullopt;
   }
 
@@ -220,8 +220,18 @@
     return base::nullopt;
   }
 
-  auto ring_buffer =
-      PerfRingBuffer::Allocate(perf_fd.get(), kDataPagesPerRingBuffer);
+  // choose a reasonable ring buffer size
+  size_t ring_buffer_pages = kDefaultDataPagesPerRingBuffer;
+  size_t config_pages = event_cfg.ring_buffer_pages();
+  if (config_pages) {
+    if (!IsPowerOfTwo(config_pages)) {
+      PERFETTO_ELOG("kernel buffer size must be a power of two pages");
+      return base::nullopt;
+    }
+    ring_buffer_pages = config_pages;
+  }
+
+  auto ring_buffer = PerfRingBuffer::Allocate(perf_fd.get(), ring_buffer_pages);
   if (!ring_buffer.has_value()) {
     return base::nullopt;
   }