• 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_CONFIG_H_
18 #define SRC_PROFILING_PERF_EVENT_CONFIG_H_
19 
20 #include <string>
21 
22 #include <linux/perf_event.h>
23 #include <stdint.h>
24 #include <sys/types.h>
25 
26 #include "perfetto/base/flat_set.h"
27 #include "perfetto/ext/base/optional.h"
28 #include "perfetto/tracing/core/data_source_config.h"
29 
30 #include "protos/perfetto/config/profiling/perf_event_config.pbzero.h"
31 
32 namespace perfetto {
33 namespace profiling {
34 
35 // Parsed whitelist/blacklist for filtering samples.
36 // An empty whitelist set means that all targets are allowed.
37 struct TargetFilter {
38   base::FlatSet<std::string> cmdlines;
39   base::FlatSet<std::string> exclude_cmdlines;
40   base::FlatSet<pid_t> pids;
41   base::FlatSet<pid_t> exclude_pids;
42 };
43 
44 // Describes a single profiling configuration. Bridges the gap between the data
45 // source config proto, and the raw "perf_event_attr" structs to pass to the
46 // perf_event_open syscall.
47 class EventConfig {
48  public:
49   static base::Optional<EventConfig> Create(const DataSourceConfig& ds_config);
50 
target_all_cpus()51   uint32_t target_all_cpus() const { return target_all_cpus_; }
ring_buffer_pages()52   uint32_t ring_buffer_pages() const { return ring_buffer_pages_; }
read_tick_period_ms()53   uint32_t read_tick_period_ms() const { return read_tick_period_ms_; }
samples_per_tick_limit()54   uint32_t samples_per_tick_limit() const { return samples_per_tick_limit_; }
remote_descriptor_timeout_ms()55   uint32_t remote_descriptor_timeout_ms() const {
56     return remote_descriptor_timeout_ms_;
57   }
unwind_state_clear_period_ms()58   uint32_t unwind_state_clear_period_ms() const {
59     return unwind_state_clear_period_ms_;
60   }
61 
filter()62   const TargetFilter& filter() const { return target_filter_; }
63 
perf_attr()64   perf_event_attr* perf_attr() const {
65     return const_cast<perf_event_attr*>(&perf_event_attr_);
66   }
67 
68  private:
69   EventConfig(const protos::pbzero::PerfEventConfig::Decoder& cfg,
70               uint32_t sampling_frequency,
71               uint32_t ring_buffer_pages,
72               uint32_t read_tick_period_ms,
73               uint32_t samples_per_tick_limit,
74               uint32_t remote_descriptor_timeout_ms,
75               TargetFilter target_filter);
76 
77   // If true, process all system-wide samples.
78   const bool target_all_cpus_;
79 
80   // Size (in 4k pages) of each per-cpu ring buffer shared with the kernel.
81   // Must be a power of two.
82   const uint32_t ring_buffer_pages_;
83 
84   // Parameter struct for |perf_event_open| calls.
85   struct perf_event_attr perf_event_attr_ = {};
86 
87   // How often the ring buffers should be read.
88   const uint32_t read_tick_period_ms_;
89 
90   // Guardrail for the amount of samples a given read attempt will extract from
91   // *each* per-cpu buffer.
92   const uint32_t samples_per_tick_limit_;
93 
94   // Parsed whitelist/blacklist for filtering samples.
95   const TargetFilter target_filter_;
96 
97   // Timeout for proc-fd lookup.
98   const uint32_t remote_descriptor_timeout_ms_;
99 
100   // Optional period for clearing cached unwinder state. Skipped if zero.
101   const uint32_t unwind_state_clear_period_ms_;
102 };
103 
104 }  // namespace profiling
105 }  // namespace perfetto
106 
107 #endif  // SRC_PROFILING_PERF_EVENT_CONFIG_H_
108