1 /* 2 * Copyright (C) 2022 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 "perfetto/base/build_config.h" 18 19 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) 20 21 #include "perfetto/base/logging.h" 22 #include "perfetto/ext/base/file_utils.h" 23 #include "perfetto/ext/base/pipe.h" 24 #include "perfetto/ext/base/scoped_file.h" 25 #include "perfetto/ext/base/string_utils.h" 26 #include "perfetto/ext/base/utils.h" 27 #include "perfetto/ext/tracing/core/commit_data_request.h" 28 #include "perfetto/ext/tracing/core/trace_packet.h" 29 #include "perfetto/ext/tracing/core/tracing_service.h" 30 #include "perfetto/protozero/scattered_heap_buffer.h" 31 #include "src/base/test/test_task_runner.h" 32 #include "src/base/test/utils.h" 33 #include "test/gtest_and_gmock.h" 34 #include "test/test_helper.h" 35 36 #include "protos/perfetto/config/power/android_power_config.pbzero.h" 37 #include "protos/perfetto/config/test_config.gen.h" 38 #include "protos/perfetto/config/trace_config.gen.h" 39 #include "protos/perfetto/trace/ftrace/ftrace.gen.h" 40 #include "protos/perfetto/trace/ftrace/ftrace_event.gen.h" 41 #include "protos/perfetto/trace/ftrace/ftrace_event_bundle.gen.h" 42 #include "protos/perfetto/trace/ftrace/ftrace_stats.gen.h" 43 #include "protos/perfetto/trace/perfetto/tracing_service_event.gen.h" 44 #include "protos/perfetto/trace/power/battery_counters.gen.h" 45 #include "protos/perfetto/trace/test_event.gen.h" 46 #include "protos/perfetto/trace/trace.gen.h" 47 #include "protos/perfetto/trace/trace_packet.gen.h" 48 #include "protos/perfetto/trace/trace_packet.pbzero.h" 49 #include "protos/perfetto/trace/trigger.gen.h" 50 51 #include "protos/perfetto/common/sys_stats_counters.gen.h" 52 #include "protos/perfetto/config/sys_stats/sys_stats_config.gen.h" 53 #include "protos/perfetto/trace/sys_stats/sys_stats.gen.h" 54 55 namespace perfetto { 56 57 namespace { 58 59 using ::testing::ContainsRegex; 60 using ::testing::Each; 61 using ::testing::ElementsAreArray; 62 using ::testing::HasSubstr; 63 using ::testing::Property; 64 using ::testing::SizeIs; 65 66 } // namespace 67 TEST(PerfettoAndroidIntegrationTest,TestBatteryTracing)68TEST(PerfettoAndroidIntegrationTest, TestBatteryTracing) { 69 base::TestTaskRunner task_runner; 70 71 TestHelper helper(&task_runner); 72 helper.StartServiceIfRequired(); 73 74 #if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS) 75 ProbesProducerThread probes(GetTestProducerSockName()); 76 probes.Connect(); 77 #endif 78 79 helper.ConnectConsumer(); 80 helper.WaitForConsumerConnect(); 81 82 TraceConfig trace_config; 83 trace_config.add_buffers()->set_size_kb(128); 84 trace_config.set_duration_ms(3000); 85 86 auto* ds_config = trace_config.add_data_sources()->mutable_config(); 87 ds_config->set_name("android.power"); 88 ds_config->set_target_buffer(0); 89 90 using protos::pbzero::AndroidPowerConfig; 91 protozero::HeapBuffered<AndroidPowerConfig> power_config; 92 power_config->set_battery_poll_ms(250); 93 power_config->add_battery_counters( 94 AndroidPowerConfig::BATTERY_COUNTER_CHARGE); 95 power_config->add_battery_counters( 96 AndroidPowerConfig::BATTERY_COUNTER_CAPACITY_PERCENT); 97 ds_config->set_android_power_config_raw(power_config.SerializeAsString()); 98 99 helper.StartTracing(trace_config); 100 helper.WaitForTracingDisabled(); 101 102 helper.ReadData(); 103 helper.WaitForReadData(); 104 105 const auto& packets = helper.trace(); 106 ASSERT_GT(packets.size(), 0u); 107 108 bool has_battery_packet = false; 109 for (const auto& packet : packets) { 110 if (!packet.has_battery()) 111 continue; 112 has_battery_packet = true; 113 // Unfortunately we cannot make any assertions on the charge counter. 114 // On some devices it can reach negative values (b/64685329). 115 EXPECT_GE(packet.battery().capacity_percent(), 0.f); 116 EXPECT_LE(packet.battery().capacity_percent(), 100.f); 117 } 118 119 ASSERT_TRUE(has_battery_packet); 120 } 121 122 } // namespace perfetto 123 124 #endif // PERFETTO_OS_ANDROID 125