1d590543dSopenharmony_ci/*
2d590543dSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3d590543dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4d590543dSopenharmony_ci * you may not use this file except in compliance with the License.
5d590543dSopenharmony_ci * You may obtain a copy of the License at
6d590543dSopenharmony_ci *
7d590543dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8d590543dSopenharmony_ci *
9d590543dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10d590543dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11d590543dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d590543dSopenharmony_ci * See the License for the specific language governing permissions and
13d590543dSopenharmony_ci * limitations under the License.
14d590543dSopenharmony_ci */
15d590543dSopenharmony_ci
16d590543dSopenharmony_ci#include "entities/uid_entity.h"
17d590543dSopenharmony_ci
18d590543dSopenharmony_ci#ifdef SYS_MGR_CLIENT_ENABLE
19d590543dSopenharmony_ci#include <bundle_constants.h>
20d590543dSopenharmony_ci#include <bundle_mgr_interface.h>
21d590543dSopenharmony_ci#include <ipc_skeleton.h>
22d590543dSopenharmony_ci#include <system_ability_definition.h>
23d590543dSopenharmony_ci#include <sys_mgr_client.h>
24d590543dSopenharmony_ci#endif
25d590543dSopenharmony_ci
26d590543dSopenharmony_ci#include <ohos_account_kits_impl.h>
27d590543dSopenharmony_ci#include "battery_stats_service.h"
28d590543dSopenharmony_ci#include "stats_log.h"
29d590543dSopenharmony_ci
30d590543dSopenharmony_cinamespace OHOS {
31d590543dSopenharmony_cinamespace PowerMgr {
32d590543dSopenharmony_cinamespace {
33d590543dSopenharmony_ci}
34d590543dSopenharmony_ci
35d590543dSopenharmony_ciUidEntity::UidEntity()
36d590543dSopenharmony_ci{
37d590543dSopenharmony_ci    consumptionType_ = BatteryStatsInfo::CONSUMPTION_TYPE_APP;
38d590543dSopenharmony_ci}
39d590543dSopenharmony_ci
40d590543dSopenharmony_civoid UidEntity::UpdateUidMap(int32_t uid)
41d590543dSopenharmony_ci{
42d590543dSopenharmony_ci    std::lock_guard<std::mutex> lock(uidEntityMutex_);
43d590543dSopenharmony_ci    if (uid > StatsUtils::INVALID_VALUE) {
44d590543dSopenharmony_ci        auto iter = uidPowerMap_.find(uid);
45d590543dSopenharmony_ci        if (iter != uidPowerMap_.end()) {
46d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "Uid has already been added, ignore");
47d590543dSopenharmony_ci        } else {
48d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "Update %{public}d to uid power map", uid);
49d590543dSopenharmony_ci            uidPowerMap_.insert(std::pair<int32_t, double>(uid, StatsUtils::DEFAULT_VALUE));
50d590543dSopenharmony_ci        }
51d590543dSopenharmony_ci    }
52d590543dSopenharmony_ci}
53d590543dSopenharmony_ci
54d590543dSopenharmony_cistd::vector<int32_t> UidEntity::GetUids()
55d590543dSopenharmony_ci{
56d590543dSopenharmony_ci    std::lock_guard<std::mutex> lock(uidEntityMutex_);
57d590543dSopenharmony_ci    std::vector<int32_t> uids;
58d590543dSopenharmony_ci    std::transform(uidPowerMap_.begin(), uidPowerMap_.end(), std::back_inserter(uids), [](const auto& item) {
59d590543dSopenharmony_ci        return item.first;
60d590543dSopenharmony_ci    });
61d590543dSopenharmony_ci    return uids;
62d590543dSopenharmony_ci}
63d590543dSopenharmony_ci
64d590543dSopenharmony_cidouble UidEntity::CalculateForConnectivity(int32_t uid)
65d590543dSopenharmony_ci{
66d590543dSopenharmony_ci    double power = StatsUtils::DEFAULT_VALUE;
67d590543dSopenharmony_ci    auto bss = BatteryStatsService::GetInstance();
68d590543dSopenharmony_ci    auto core = bss->GetBatteryStatsCore();
69d590543dSopenharmony_ci    auto bluetoothEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
70d590543dSopenharmony_ci
71d590543dSopenharmony_ci    // Calculate bluetooth power consumption
72d590543dSopenharmony_ci    bluetoothEntity->Calculate(uid);
73d590543dSopenharmony_ci    power += bluetoothEntity->GetEntityPowerMah(uid);
74d590543dSopenharmony_ci    STATS_HILOGD(COMP_SVC, "Connectivity power consumption: %{public}lfmAh for uid: %{public}d", power, uid);
75d590543dSopenharmony_ci    return power;
76d590543dSopenharmony_ci}
77d590543dSopenharmony_ci
78d590543dSopenharmony_cidouble UidEntity::CalculateForCommon(int32_t uid)
79d590543dSopenharmony_ci{
80d590543dSopenharmony_ci    double power = StatsUtils::DEFAULT_VALUE;
81d590543dSopenharmony_ci    auto bss = BatteryStatsService::GetInstance();
82d590543dSopenharmony_ci    auto core = bss->GetBatteryStatsCore();
83d590543dSopenharmony_ci    auto cameraEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_CAMERA);
84d590543dSopenharmony_ci    auto flashlightEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_FLASHLIGHT);
85d590543dSopenharmony_ci    auto audioEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_AUDIO);
86d590543dSopenharmony_ci    auto sensorEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_SENSOR);
87d590543dSopenharmony_ci    auto gnssEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_GNSS);
88d590543dSopenharmony_ci    auto cpuEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_CPU);
89d590543dSopenharmony_ci    auto wakelockEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_WAKELOCK);
90d590543dSopenharmony_ci    auto alarmEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_ALARM);
91d590543dSopenharmony_ci
92d590543dSopenharmony_ci    // Calculate camera power consumption
93d590543dSopenharmony_ci    cameraEntity->Calculate(uid);
94d590543dSopenharmony_ci    power += cameraEntity->GetEntityPowerMah(uid);
95d590543dSopenharmony_ci    // Calculate flashlight power consumption
96d590543dSopenharmony_ci    flashlightEntity->Calculate(uid);
97d590543dSopenharmony_ci    power += flashlightEntity->GetEntityPowerMah(uid);
98d590543dSopenharmony_ci    // Calculate audio power consumption
99d590543dSopenharmony_ci    audioEntity->Calculate(uid);
100d590543dSopenharmony_ci    power += audioEntity->GetEntityPowerMah(uid);
101d590543dSopenharmony_ci    // Calculate sensor power consumption
102d590543dSopenharmony_ci    sensorEntity->Calculate(uid);
103d590543dSopenharmony_ci    power += sensorEntity->GetEntityPowerMah(uid);
104d590543dSopenharmony_ci    // Calculate gnss power consumption
105d590543dSopenharmony_ci    gnssEntity->Calculate(uid);
106d590543dSopenharmony_ci    power += gnssEntity->GetEntityPowerMah(uid);
107d590543dSopenharmony_ci    // Calculate cpu power consumption
108d590543dSopenharmony_ci    cpuEntity->Calculate(uid);
109d590543dSopenharmony_ci    power += cpuEntity->GetEntityPowerMah(uid);
110d590543dSopenharmony_ci    // Calculate cpu power consumption
111d590543dSopenharmony_ci    wakelockEntity->Calculate(uid);
112d590543dSopenharmony_ci    power += wakelockEntity->GetEntityPowerMah(uid);
113d590543dSopenharmony_ci    // Calculate alarm power consumption
114d590543dSopenharmony_ci    alarmEntity->Calculate(uid);
115d590543dSopenharmony_ci    power += alarmEntity->GetEntityPowerMah(uid);
116d590543dSopenharmony_ci
117d590543dSopenharmony_ci    STATS_HILOGD(COMP_SVC, "Common power consumption: %{public}lfmAh for uid: %{public}d", power, uid);
118d590543dSopenharmony_ci    return power;
119d590543dSopenharmony_ci}
120d590543dSopenharmony_ci
121d590543dSopenharmony_civoid UidEntity::Calculate(int32_t uid)
122d590543dSopenharmony_ci{
123d590543dSopenharmony_ci    auto bss = BatteryStatsService::GetInstance();
124d590543dSopenharmony_ci    std::lock_guard<std::mutex> lock(uidEntityMutex_);
125d590543dSopenharmony_ci    auto core = bss->GetBatteryStatsCore();
126d590543dSopenharmony_ci    auto userEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_USER);
127d590543dSopenharmony_ci    for (auto& iter : uidPowerMap_) {
128d590543dSopenharmony_ci        double power = StatsUtils::DEFAULT_VALUE;
129d590543dSopenharmony_ci        power += CalculateForConnectivity(iter.first);
130d590543dSopenharmony_ci        power += CalculateForCommon(iter.first);
131d590543dSopenharmony_ci        iter.second = power;
132d590543dSopenharmony_ci        totalPowerMah_ += power;
133d590543dSopenharmony_ci        AddtoStatsList(iter.first, power);
134d590543dSopenharmony_ci        int32_t uid = iter.first;
135d590543dSopenharmony_ci        int32_t userId = AccountSA::OhosAccountKits::GetInstance().GetDeviceAccountIdByUID(uid);
136d590543dSopenharmony_ci        if (userEntity != nullptr) {
137d590543dSopenharmony_ci            userEntity->AggregateUserPowerMah(userId, power);
138d590543dSopenharmony_ci        }
139d590543dSopenharmony_ci    }
140d590543dSopenharmony_ci}
141d590543dSopenharmony_ci
142d590543dSopenharmony_civoid UidEntity::AddtoStatsList(int32_t uid, double power)
143d590543dSopenharmony_ci{
144d590543dSopenharmony_ci    std::shared_ptr<BatteryStatsInfo> statsInfo = std::make_shared<BatteryStatsInfo>();
145d590543dSopenharmony_ci    statsInfo->SetConsumptioType(BatteryStatsInfo::CONSUMPTION_TYPE_APP);
146d590543dSopenharmony_ci    statsInfo->SetUid(uid);
147d590543dSopenharmony_ci    statsInfo->SetPower(power);
148d590543dSopenharmony_ci    statsInfoList_.push_back(statsInfo);
149d590543dSopenharmony_ci}
150d590543dSopenharmony_ci
151d590543dSopenharmony_cidouble UidEntity::GetEntityPowerMah(int32_t uidOrUserId)
152d590543dSopenharmony_ci{
153d590543dSopenharmony_ci    std::lock_guard<std::mutex> lock(uidEntityMutex_);
154d590543dSopenharmony_ci    double power = StatsUtils::DEFAULT_VALUE;
155d590543dSopenharmony_ci    auto iter = uidPowerMap_.find(uidOrUserId);
156d590543dSopenharmony_ci    if (iter != uidPowerMap_.end()) {
157d590543dSopenharmony_ci        power = iter->second;
158d590543dSopenharmony_ci        STATS_HILOGD(COMP_SVC, "Get app uid power consumption: %{public}lfmAh for uid: %{public}d",
159d590543dSopenharmony_ci            power, uidOrUserId);
160d590543dSopenharmony_ci    } else {
161d590543dSopenharmony_ci        STATS_HILOGD(COMP_SVC,
162d590543dSopenharmony_ci            "No app uid power consumption related to uid: %{public}d was found, return 0", uidOrUserId);
163d590543dSopenharmony_ci    }
164d590543dSopenharmony_ci    return power;
165d590543dSopenharmony_ci}
166d590543dSopenharmony_ci
167d590543dSopenharmony_cidouble UidEntity::GetPowerForConnectivity(StatsUtils::StatsType statsType, int32_t uid)
168d590543dSopenharmony_ci{
169d590543dSopenharmony_ci    double power = StatsUtils::DEFAULT_VALUE;
170d590543dSopenharmony_ci    auto bss = BatteryStatsService::GetInstance();
171d590543dSopenharmony_ci    auto core = bss->GetBatteryStatsCore();
172d590543dSopenharmony_ci    auto bluetoothEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
173d590543dSopenharmony_ci
174d590543dSopenharmony_ci    if (statsType == StatsUtils::STATS_TYPE_BLUETOOTH_BR_SCAN) {
175d590543dSopenharmony_ci        power = bluetoothEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_BLUETOOTH_BR_SCAN, uid);
176d590543dSopenharmony_ci    } else if (statsType == StatsUtils::STATS_TYPE_BLUETOOTH_BLE_SCAN) {
177d590543dSopenharmony_ci        power = bluetoothEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_BLUETOOTH_BLE_SCAN, uid);
178d590543dSopenharmony_ci    }
179d590543dSopenharmony_ci    return power;
180d590543dSopenharmony_ci}
181d590543dSopenharmony_ci
182d590543dSopenharmony_cidouble UidEntity::GetPowerForCommon(StatsUtils::StatsType statsType, int32_t uid)
183d590543dSopenharmony_ci{
184d590543dSopenharmony_ci    double power = StatsUtils::DEFAULT_VALUE;
185d590543dSopenharmony_ci    auto bss = BatteryStatsService::GetInstance();
186d590543dSopenharmony_ci    auto core = bss->GetBatteryStatsCore();
187d590543dSopenharmony_ci    auto cameraEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_CAMERA);
188d590543dSopenharmony_ci    auto flashlightEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_FLASHLIGHT);
189d590543dSopenharmony_ci    auto audioEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_AUDIO);
190d590543dSopenharmony_ci    auto sensorEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_SENSOR);
191d590543dSopenharmony_ci    auto gnssEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_GNSS);
192d590543dSopenharmony_ci    auto cpuEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_CPU);
193d590543dSopenharmony_ci    auto wakelockEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_WAKELOCK);
194d590543dSopenharmony_ci    auto alarmEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_ALARM);
195d590543dSopenharmony_ci
196d590543dSopenharmony_ci    if (statsType == StatsUtils::STATS_TYPE_CAMERA_ON) {
197d590543dSopenharmony_ci        power = cameraEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_CAMERA_ON, uid);
198d590543dSopenharmony_ci    } else if (statsType == StatsUtils::STATS_TYPE_FLASHLIGHT_ON) {
199d590543dSopenharmony_ci        power = flashlightEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_FLASHLIGHT_ON, uid);
200d590543dSopenharmony_ci    } else if (statsType == StatsUtils::STATS_TYPE_GNSS_ON) {
201d590543dSopenharmony_ci        power = gnssEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_GNSS_ON, uid);
202d590543dSopenharmony_ci    } else if (statsType == StatsUtils::STATS_TYPE_SENSOR_GRAVITY_ON) {
203d590543dSopenharmony_ci        power = sensorEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_SENSOR_GRAVITY_ON, uid);
204d590543dSopenharmony_ci    } else if (statsType == StatsUtils::STATS_TYPE_SENSOR_PROXIMITY_ON) {
205d590543dSopenharmony_ci        power = sensorEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_SENSOR_PROXIMITY_ON, uid);
206d590543dSopenharmony_ci    } else if (statsType == StatsUtils::STATS_TYPE_AUDIO_ON) {
207d590543dSopenharmony_ci        power = audioEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_AUDIO_ON, uid);
208d590543dSopenharmony_ci    } else if (statsType == StatsUtils::STATS_TYPE_WAKELOCK_HOLD) {
209d590543dSopenharmony_ci        power = wakelockEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_WAKELOCK_HOLD, uid);
210d590543dSopenharmony_ci    } else if (statsType == StatsUtils::STATS_TYPE_CPU_CLUSTER) {
211d590543dSopenharmony_ci        power = cpuEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_CPU_CLUSTER, uid);
212d590543dSopenharmony_ci    } else if (statsType == StatsUtils::STATS_TYPE_CPU_SPEED) {
213d590543dSopenharmony_ci        power = cpuEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_CPU_SPEED, uid);
214d590543dSopenharmony_ci    } else if (statsType == StatsUtils::STATS_TYPE_CPU_ACTIVE) {
215d590543dSopenharmony_ci        power = cpuEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_CPU_ACTIVE, uid);
216d590543dSopenharmony_ci    } else if (statsType == StatsUtils::STATS_TYPE_ALARM) {
217d590543dSopenharmony_ci        power = alarmEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_ALARM, uid);
218d590543dSopenharmony_ci    }
219d590543dSopenharmony_ci    return power;
220d590543dSopenharmony_ci}
221d590543dSopenharmony_ci
222d590543dSopenharmony_cidouble UidEntity::GetStatsPowerMah(StatsUtils::StatsType statsType, int32_t uid)
223d590543dSopenharmony_ci{
224d590543dSopenharmony_ci    double power = StatsUtils::DEFAULT_VALUE;
225d590543dSopenharmony_ci
226d590543dSopenharmony_ci    switch (statsType) {
227d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_BLUETOOTH_BR_SCAN:
228d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_BLUETOOTH_BLE_SCAN:
229d590543dSopenharmony_ci            power = GetPowerForConnectivity(statsType, uid);
230d590543dSopenharmony_ci            break;
231d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_CAMERA_ON:
232d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_FLASHLIGHT_ON:
233d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_GNSS_ON:
234d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_SENSOR_GRAVITY_ON:
235d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_SENSOR_PROXIMITY_ON:
236d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_AUDIO_ON:
237d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_WAKELOCK_HOLD:
238d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_CPU_CLUSTER:
239d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_CPU_SPEED:
240d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_CPU_ACTIVE:
241d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_ALARM:
242d590543dSopenharmony_ci            power = GetPowerForCommon(statsType, uid);
243d590543dSopenharmony_ci            break;
244d590543dSopenharmony_ci        default:
245d590543dSopenharmony_ci            STATS_HILOGW(COMP_SVC, "Invalid or illegal type got, return 0");
246d590543dSopenharmony_ci            break;
247d590543dSopenharmony_ci    }
248d590543dSopenharmony_ci
249d590543dSopenharmony_ci    STATS_HILOGD(COMP_SVC, "Get %{public}s power: %{public}lfmAh for uid: %{public}d",
250d590543dSopenharmony_ci        StatsUtils::ConvertStatsType(statsType).c_str(), power, uid);
251d590543dSopenharmony_ci    return power;
252d590543dSopenharmony_ci}
253d590543dSopenharmony_ci
254d590543dSopenharmony_civoid UidEntity::Reset()
255d590543dSopenharmony_ci{
256d590543dSopenharmony_ci    std::lock_guard<std::mutex> lock(uidEntityMutex_);
257d590543dSopenharmony_ci    // Reset app Uid total power consumption
258d590543dSopenharmony_ci    for (auto& iter : uidPowerMap_) {
259d590543dSopenharmony_ci        iter.second = StatsUtils::DEFAULT_VALUE;
260d590543dSopenharmony_ci    }
261d590543dSopenharmony_ci}
262d590543dSopenharmony_ci
263d590543dSopenharmony_civoid UidEntity::DumpForBluetooth(int32_t uid, std::string& result)
264d590543dSopenharmony_ci{
265d590543dSopenharmony_ci    auto bss = BatteryStatsService::GetInstance();
266d590543dSopenharmony_ci    // Dump for bluetooth realted info
267d590543dSopenharmony_ci    auto core = bss->GetBatteryStatsCore();
268d590543dSopenharmony_ci    int64_t bluetoothBrScanTime = core->GetTotalTimeMs(uid, StatsUtils::STATS_TYPE_BLUETOOTH_BR_SCAN);
269d590543dSopenharmony_ci    int64_t bluetoothBleScanTime = core->GetTotalTimeMs(uid, StatsUtils::STATS_TYPE_BLUETOOTH_BLE_SCAN);
270d590543dSopenharmony_ci
271d590543dSopenharmony_ci    result.append("Bluetooth Br scan time: ")
272d590543dSopenharmony_ci        .append(ToString(bluetoothBrScanTime))
273d590543dSopenharmony_ci        .append("ms\n")
274d590543dSopenharmony_ci        .append("Bluetooth Ble scan time: ")
275d590543dSopenharmony_ci        .append(ToString(bluetoothBleScanTime))
276d590543dSopenharmony_ci        .append("ms\n");
277d590543dSopenharmony_ci}
278d590543dSopenharmony_ci
279d590543dSopenharmony_civoid UidEntity::DumpForCommon(int32_t uid, std::string& result)
280d590543dSopenharmony_ci{
281d590543dSopenharmony_ci    auto bss = BatteryStatsService::GetInstance();
282d590543dSopenharmony_ci    auto core = bss->GetBatteryStatsCore();
283d590543dSopenharmony_ci    // Dump for camera related info
284d590543dSopenharmony_ci    int64_t cameraTime = core->GetTotalTimeMs(uid, StatsUtils::STATS_TYPE_CAMERA_ON);
285d590543dSopenharmony_ci
286d590543dSopenharmony_ci    // Dump for flashlight related info
287d590543dSopenharmony_ci    int64_t flashlightTime = core->GetTotalTimeMs(uid, StatsUtils::STATS_TYPE_FLASHLIGHT_ON);
288d590543dSopenharmony_ci
289d590543dSopenharmony_ci    // Dump for gnss related info
290d590543dSopenharmony_ci    int64_t gnssTime = core->GetTotalTimeMs(uid, StatsUtils::STATS_TYPE_GNSS_ON);
291d590543dSopenharmony_ci
292d590543dSopenharmony_ci    // Dump for gravity sensor related info
293d590543dSopenharmony_ci    int64_t gravityTime = core->GetTotalTimeMs(uid, StatsUtils::STATS_TYPE_SENSOR_GRAVITY_ON);
294d590543dSopenharmony_ci
295d590543dSopenharmony_ci    // Dump for proximity sensor related info
296d590543dSopenharmony_ci    int64_t proximityTime = core->GetTotalTimeMs(uid, StatsUtils::STATS_TYPE_SENSOR_PROXIMITY_ON);
297d590543dSopenharmony_ci
298d590543dSopenharmony_ci    // Dump for audio related info
299d590543dSopenharmony_ci    int64_t audioTime = core->GetTotalTimeMs(uid, StatsUtils::STATS_TYPE_AUDIO_ON);
300d590543dSopenharmony_ci
301d590543dSopenharmony_ci    // Dump for wakelock related info
302d590543dSopenharmony_ci    int64_t wakelockTime = core->GetTotalTimeMs(uid, StatsUtils::STATS_TYPE_WAKELOCK_HOLD);
303d590543dSopenharmony_ci
304d590543dSopenharmony_ci    // Dump for alarm related info
305d590543dSopenharmony_ci    int64_t alarmCount = core->GetTotalConsumptionCount(StatsUtils::STATS_TYPE_ALARM, uid);
306d590543dSopenharmony_ci
307d590543dSopenharmony_ci    result.append("Camera on time: ")
308d590543dSopenharmony_ci        .append(ToString(cameraTime))
309d590543dSopenharmony_ci        .append("ms\n")
310d590543dSopenharmony_ci        .append("Flashlight scan time: ")
311d590543dSopenharmony_ci        .append(ToString(flashlightTime))
312d590543dSopenharmony_ci        .append("ms\n")
313d590543dSopenharmony_ci        .append("GNSS scan time: ")
314d590543dSopenharmony_ci        .append(ToString(gnssTime))
315d590543dSopenharmony_ci        .append("ms\n")
316d590543dSopenharmony_ci        .append("Gravity sensor on time: ")
317d590543dSopenharmony_ci        .append(ToString(gravityTime))
318d590543dSopenharmony_ci        .append("ms\n")
319d590543dSopenharmony_ci        .append("Proximity sensor on time: ")
320d590543dSopenharmony_ci        .append(ToString(proximityTime))
321d590543dSopenharmony_ci        .append("ms\n")
322d590543dSopenharmony_ci        .append("Audio on time: ")
323d590543dSopenharmony_ci        .append(ToString(audioTime))
324d590543dSopenharmony_ci        .append("ms\n")
325d590543dSopenharmony_ci        .append("Wakelock hold time: ")
326d590543dSopenharmony_ci        .append(ToString(wakelockTime))
327d590543dSopenharmony_ci        .append("ms\n")
328d590543dSopenharmony_ci        .append("Alarm trigger count: ")
329d590543dSopenharmony_ci        .append(ToString(alarmCount))
330d590543dSopenharmony_ci        .append("times\n");
331d590543dSopenharmony_ci}
332d590543dSopenharmony_ci
333d590543dSopenharmony_civoid UidEntity::DumpInfo(std::string& result, int32_t uid)
334d590543dSopenharmony_ci{
335d590543dSopenharmony_ci    auto bss = BatteryStatsService::GetInstance();
336d590543dSopenharmony_ci    std::lock_guard<std::mutex> lock(uidEntityMutex_);
337d590543dSopenharmony_ci    auto core = bss->GetBatteryStatsCore();
338d590543dSopenharmony_ci    for (auto& iter : uidPowerMap_) {
339d590543dSopenharmony_ci        std::string bundleName = "NULL";
340d590543dSopenharmony_ci#ifdef SYS_MGR_CLIENT_ENABLE
341d590543dSopenharmony_ci        auto bundleObj =
342d590543dSopenharmony_ci            DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()
343d590543dSopenharmony_ci                ->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
344d590543dSopenharmony_ci        if (bundleObj == nullptr) {
345d590543dSopenharmony_ci            STATS_HILOGE(COMP_SVC, "Failed to get bundle manager service");
346d590543dSopenharmony_ci        } else {
347d590543dSopenharmony_ci            sptr<AppExecFwk::IBundleMgr> bmgr = iface_cast<AppExecFwk::IBundleMgr>(bundleObj);
348d590543dSopenharmony_ci            if (bmgr == nullptr) {
349d590543dSopenharmony_ci                STATS_HILOGE(COMP_SVC, "Failed to get bundle manager proxy");
350d590543dSopenharmony_ci            } else {
351d590543dSopenharmony_ci                std::string identity = IPCSkeleton::ResetCallingIdentity();
352d590543dSopenharmony_ci                ErrCode res = bmgr->GetNameForUid(iter.first, bundleName);
353d590543dSopenharmony_ci                IPCSkeleton::SetCallingIdentity(identity);
354d590543dSopenharmony_ci                if (res != ERR_OK) {
355d590543dSopenharmony_ci                    STATS_HILOGE(COMP_SVC, "Failed to get bundle name for uid=%{public}d, ErrCode=%{public}d",
356d590543dSopenharmony_ci                        iter.first, static_cast<int32_t>(res));
357d590543dSopenharmony_ci                }
358d590543dSopenharmony_ci            }
359d590543dSopenharmony_ci        }
360d590543dSopenharmony_ci#endif
361d590543dSopenharmony_ci        result.append("\n")
362d590543dSopenharmony_ci            .append(ToString(iter.first))
363d590543dSopenharmony_ci            .append("(Bundle name: ")
364d590543dSopenharmony_ci            .append(bundleName)
365d590543dSopenharmony_ci            .append(")")
366d590543dSopenharmony_ci            .append(":")
367d590543dSopenharmony_ci            .append("\n");
368d590543dSopenharmony_ci        DumpForBluetooth(iter.first, result);
369d590543dSopenharmony_ci        DumpForCommon(iter.first, result);
370d590543dSopenharmony_ci        auto cpuEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_CPU);
371d590543dSopenharmony_ci        if (cpuEntity) {
372d590543dSopenharmony_ci            cpuEntity->DumpInfo(result, iter.first);
373d590543dSopenharmony_ci        }
374d590543dSopenharmony_ci    }
375d590543dSopenharmony_ci}
376d590543dSopenharmony_ci} // namespace PowerMgr
377d590543dSopenharmony_ci} // namespace OHOS