• 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_config.h"
18 
19 #include <linux/perf_event.h>
20 #include <stdint.h>
21 
22 #include "perfetto/base/logging.h"
23 #include "perfetto/ext/base/optional.h"
24 #include "test/gtest_and_gmock.h"
25 
26 #include "protos/perfetto/config/data_source_config.gen.h"
27 #include "protos/perfetto/config/profiling/perf_event_config.gen.h"
28 
29 namespace perfetto {
30 namespace profiling {
31 namespace {
32 
IsPowerOfTwo(size_t v)33 bool IsPowerOfTwo(size_t v) {
34   return (v != 0 && ((v & (v - 1)) == 0));
35 }
36 
AsDataSourceConfig(const protos::gen::PerfEventConfig & perf_cfg)37 static DataSourceConfig AsDataSourceConfig(
38     const protos::gen::PerfEventConfig& perf_cfg) {
39   protos::gen::DataSourceConfig ds_cfg;
40   ds_cfg.set_perf_event_config_raw(perf_cfg.SerializeAsString());
41   return ds_cfg;
42 }
43 
TEST(EventConfigTest,AttrStructConstructed)44 TEST(EventConfigTest, AttrStructConstructed) {
45   protos::gen::PerfEventConfig cfg;
46   base::Optional<EventConfig> event_config =
47       EventConfig::Create(AsDataSourceConfig(cfg));
48 
49   ASSERT_TRUE(event_config.has_value());
50   ASSERT_TRUE(event_config->perf_attr() != nullptr);
51 }
52 
TEST(EventConfigTest,RingBufferPagesValidated)53 TEST(EventConfigTest, RingBufferPagesValidated) {
54   {  // if unset, a default is used
55     protos::gen::PerfEventConfig cfg;
56     base::Optional<EventConfig> event_config =
57         EventConfig::Create(AsDataSourceConfig(cfg));
58 
59     ASSERT_TRUE(event_config.has_value());
60     ASSERT_GT(event_config->ring_buffer_pages(), 0u);
61     ASSERT_TRUE(IsPowerOfTwo(event_config->ring_buffer_pages()));
62   }
63   {  // power of two pages accepted
64     uint32_t num_pages = 128;
65     protos::gen::PerfEventConfig cfg;
66     cfg.set_ring_buffer_pages(num_pages);
67     base::Optional<EventConfig> event_config =
68         EventConfig::Create(AsDataSourceConfig(cfg));
69 
70     ASSERT_TRUE(event_config.has_value());
71     ASSERT_EQ(event_config->ring_buffer_pages(), num_pages);
72   }
73   {  // entire config rejected if not a power of two of pages
74     protos::gen::PerfEventConfig cfg;
75     cfg.set_ring_buffer_pages(7);
76     base::Optional<EventConfig> event_config =
77         EventConfig::Create(AsDataSourceConfig(cfg));
78 
79     ASSERT_FALSE(event_config.has_value());
80   }
81 }
82 
TEST(EventConfigTest,ReadTickPeriodDefaultedIfUnset)83 TEST(EventConfigTest, ReadTickPeriodDefaultedIfUnset) {
84   {  // if unset, a default is used
85     protos::gen::PerfEventConfig cfg;
86     base::Optional<EventConfig> event_config =
87         EventConfig::Create(AsDataSourceConfig(cfg));
88 
89     ASSERT_TRUE(event_config.has_value());
90     ASSERT_GT(event_config->read_tick_period_ms(), 0u);
91   }
92   {  // otherwise, given value used
93     uint32_t period_ms = 250;
94     protos::gen::PerfEventConfig cfg;
95     cfg.set_ring_buffer_read_period_ms(period_ms);
96     base::Optional<EventConfig> event_config =
97         EventConfig::Create(AsDataSourceConfig(cfg));
98 
99     ASSERT_TRUE(event_config.has_value());
100     ASSERT_EQ(event_config->read_tick_period_ms(), period_ms);
101   }
102 }
103 
TEST(EventConfigTest,RemotePeriodTimeoutDefaultedIfUnset)104 TEST(EventConfigTest, RemotePeriodTimeoutDefaultedIfUnset) {
105   {  // if unset, a default is used
106     protos::gen::PerfEventConfig cfg;
107     base::Optional<EventConfig> event_config =
108         EventConfig::Create(AsDataSourceConfig(cfg));
109 
110     ASSERT_TRUE(event_config.has_value());
111     ASSERT_GT(event_config->remote_descriptor_timeout_ms(), 0u);
112   }
113   {  // otherwise, given value used
114     uint32_t timeout_ms = 300;
115     protos::gen::PerfEventConfig cfg;
116     cfg.set_remote_descriptor_timeout_ms(timeout_ms);
117     base::Optional<EventConfig> event_config =
118         EventConfig::Create(AsDataSourceConfig(cfg));
119 
120     ASSERT_TRUE(event_config.has_value());
121     ASSERT_EQ(event_config->remote_descriptor_timeout_ms(), timeout_ms);
122   }
123 }
124 
125 }  // namespace
126 }  // namespace profiling
127 }  // namespace perfetto
128