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 "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 
22 #include "perfetto/tracing/core/basic_types.h"
23 #include "perfetto/tracing/core/commit_data_request.h"
24 #include "perfetto/tracing/core/data_source_descriptor.h"
25 
26 #include "src/base/test/test_task_runner.h"
27 
28 namespace perfetto {
29 namespace profiling {
30 
31 using ::testing::Contains;
32 using ::testing::Pair;
33 using ::testing::Eq;
34 using ::testing::Property;
35 
36 class MockProducerEndpoint : public TracingService::ProducerEndpoint {
37  public:
38   MOCK_METHOD1(UnregisterDataSource, void(const std::string&));
39   MOCK_METHOD1(NotifyFlushComplete, void(FlushRequestID));
40   MOCK_METHOD1(NotifyDataSourceStarted, void(DataSourceInstanceID));
41   MOCK_METHOD1(NotifyDataSourceStopped, void(DataSourceInstanceID));
42 
43   MOCK_CONST_METHOD0(shared_memory, SharedMemory*());
44   MOCK_CONST_METHOD0(shared_buffer_page_size_kb, size_t());
45   MOCK_METHOD1(CreateTraceWriter, std::unique_ptr<TraceWriter>(BufferID));
46   MOCK_METHOD0(GetInProcessShmemArbiter, SharedMemoryArbiter*());
47   MOCK_METHOD1(ActivateTriggers, void(const std::vector<std::string>&));
48 
49   MOCK_METHOD1(RegisterDataSource, void(const DataSourceDescriptor&));
50   MOCK_METHOD2(CommitData, void(const CommitDataRequest&, CommitDataCallback));
51   MOCK_METHOD2(RegisterTraceWriter, void(uint32_t, uint32_t));
52   MOCK_METHOD1(UnregisterTraceWriter, void(uint32_t));
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