• 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/memory/heapprofd_producer.h"
18 
19 #include "perfetto/ext/tracing/core/basic_types.h"
20 #include "perfetto/ext/tracing/core/commit_data_request.h"
21 #include "perfetto/tracing/core/data_source_descriptor.h"
22 #include "src/base/test/test_task_runner.h"
23 #include "test/gtest_and_gmock.h"
24 
25 namespace perfetto {
26 namespace profiling {
27 
28 using ::testing::Contains;
29 using ::testing::Pair;
30 using ::testing::Eq;
31 using ::testing::Property;
32 
33 class MockProducerEndpoint : public TracingService::ProducerEndpoint {
34  public:
35   MOCK_METHOD1(UnregisterDataSource, void(const std::string&));
36   MOCK_METHOD1(NotifyFlushComplete, void(FlushRequestID));
37   MOCK_METHOD1(NotifyDataSourceStarted, void(DataSourceInstanceID));
38   MOCK_METHOD1(NotifyDataSourceStopped, void(DataSourceInstanceID));
39 
40   MOCK_CONST_METHOD0(shared_memory, SharedMemory*());
41   MOCK_CONST_METHOD0(shared_buffer_page_size_kb, size_t());
42   MOCK_METHOD2(CreateTraceWriter,
43                std::unique_ptr<TraceWriter>(BufferID, BufferExhaustedPolicy));
44   MOCK_METHOD0(MaybeSharedMemoryArbiter, SharedMemoryArbiter*());
45   MOCK_CONST_METHOD0(IsShmemProvidedByProducer, bool());
46   MOCK_METHOD1(ActivateTriggers, void(const std::vector<std::string>&));
47 
48   MOCK_METHOD1(RegisterDataSource, void(const DataSourceDescriptor&));
49   MOCK_METHOD2(CommitData, void(const CommitDataRequest&, CommitDataCallback));
50   MOCK_METHOD2(RegisterTraceWriter, void(uint32_t, uint32_t));
51   MOCK_METHOD1(UnregisterTraceWriter, void(uint32_t));
52   MOCK_METHOD1(Sync, void(std::function<void()>));
53 };
54 
TEST(LogHistogramTest,Simple)55 TEST(LogHistogramTest, Simple) {
56   LogHistogram h;
57   h.Add(1);
58   h.Add(0);
59   EXPECT_THAT(h.GetData(), Contains(Pair(2, 1)));
60   EXPECT_THAT(h.GetData(), Contains(Pair(1, 1)));
61 }
62 
TEST(LogHistogramTest,Overflow)63 TEST(LogHistogramTest, Overflow) {
64   LogHistogram h;
65   h.Add(std::numeric_limits<uint64_t>::max());
66   EXPECT_THAT(h.GetData(), Contains(Pair(LogHistogram::kMaxBucket, 1)));
67 }
68 
TEST(HeapprofdProducerTest,ExposesDataSource)69 TEST(HeapprofdProducerTest, ExposesDataSource) {
70   base::TestTaskRunner task_runner;
71   HeapprofdProducer producer(HeapprofdMode::kCentral, &task_runner);
72 
73   std::unique_ptr<MockProducerEndpoint> endpoint(new MockProducerEndpoint());
74   EXPECT_CALL(*endpoint,
75               RegisterDataSource(Property(&DataSourceDescriptor::name,
76                                           Eq("android.heapprofd"))))
77       .Times(1);
78   producer.SetProducerEndpoint(std::move(endpoint));
79   producer.OnConnect();
80 }
81 
82 }  // namespace profiling
83 }  // namespace perfetto
84