blob: c166d851eaf219ee7dbbdd0ee56250216b81eda6 [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>());
26}
27
28/* static */
29PowerStatsCollector& PowerStatsCollector::getCollector() {
30 [[clang::no_destroy]] static PowerStatsCollector psc;
31 return psc;
32}
33
34std::shared_ptr<PowerStats> PowerStatsCollector::getStats() const {
35 auto result = std::make_shared<PowerStats>();
36 (void)fill(result.get());
37 return result;
38}
39
40void PowerStatsCollector::addProvider(std::unique_ptr<PowerStatsProvider>&& powerStatsProvider) {
41 mPowerStatsProviders.emplace_back(std::move(powerStatsProvider));
42}
43
44int PowerStatsCollector::fill(PowerStats* stats) const {
45 if (!stats) {
46 LOG(ERROR) << __func__ << ": bad args; stat is null";
47 return 1;
48 }
49
50 for (const auto& provider : mPowerStatsProviders) {
51 if (provider->fill(stats) != 0) {
52 LOG(ERROR) << __func__ << ": a data provider failed";
53 continue;
54 }
55 }
56
57 // boot time follows wall clock time, but starts from boot.
58 stats->metadata.start_time_since_boot_ms = systemTime(SYSTEM_TIME_BOOTTIME) / 1'000'000;
59
60 // wall clock time
61 stats->metadata.start_time_epoch_ms = systemTime(SYSTEM_TIME_REALTIME) / 1'000'000;
62
63 // monotonic time follows boot time, but does not include any time suspended.
64 stats->metadata.start_time_monotonic_ms = systemTime() / 1'000'000;
65 return 0;
66}
67
68} // namespace android::media::psh_utils