blob: 7bee3f833665860910d3913672999ba7a2d0ace9 [file] [log] [blame]
Andy Hung125ff522024-06-28 09:51:30 -07001/*
2 * Copyright (C) 2024 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#include <android-base/logging.h>
17#include <psh_utils/PowerStatsCollector.h>
18#include "PowerStatsProvider.h"
19#include <utils/Timers.h>
20
21namespace android::media::psh_utils {
22
23PowerStatsCollector::PowerStatsCollector() {
24 addProvider(std::make_unique<PowerEntityResidencyDataProvider>());
25 addProvider(std::make_unique<RailEnergyDataProvider>());
Andy Hung44acd222024-07-16 20:32:44 -070026 addProvider(std::make_unique<HealthStatsDataProvider>());
Andy Hung125ff522024-06-28 09:51:30 -070027}
28
29/* static */
30PowerStatsCollector& PowerStatsCollector::getCollector() {
31 [[clang::no_destroy]] static PowerStatsCollector psc;
32 return psc;
33}
34
35std::shared_ptr<PowerStats> PowerStatsCollector::getStats() const {
36 auto result = std::make_shared<PowerStats>();
37 (void)fill(result.get());
38 return result;
39}
40
41void PowerStatsCollector::addProvider(std::unique_ptr<PowerStatsProvider>&& powerStatsProvider) {
42 mPowerStatsProviders.emplace_back(std::move(powerStatsProvider));
43}
44
45int PowerStatsCollector::fill(PowerStats* stats) const {
46 if (!stats) {
47 LOG(ERROR) << __func__ << ": bad args; stat is null";
48 return 1;
49 }
50
51 for (const auto& provider : mPowerStatsProviders) {
52 if (provider->fill(stats) != 0) {
53 LOG(ERROR) << __func__ << ": a data provider failed";
54 continue;
55 }
56 }
57
58 // boot time follows wall clock time, but starts from boot.
59 stats->metadata.start_time_since_boot_ms = systemTime(SYSTEM_TIME_BOOTTIME) / 1'000'000;
60
61 // wall clock time
62 stats->metadata.start_time_epoch_ms = systemTime(SYSTEM_TIME_REALTIME) / 1'000'000;
63
64 // monotonic time follows boot time, but does not include any time suspended.
65 stats->metadata.start_time_monotonic_ms = systemTime() / 1'000'000;
66 return 0;
67}
68
69} // namespace android::media::psh_utils