summaryrefslogtreecommitdiff
path: root/cmds/statsd/src/external/SubsystemSleepStatePuller.cpp
blob: 65a1df0eda20dce718c190e297c1254be5f51027 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define DEBUG false  // STOPSHIP if true
#include "Log.h"

#include <android/hardware/power/1.0/IPower.h>
#include <android/hardware/power/1.1/IPower.h>
#include <fcntl.h>
#include <hardware/power.h>
#include <hardware_legacy/power.h>
#include <inttypes.h>
#include <semaphore.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "external/SubsystemSleepStatePuller.h"
#include "external/StatsPuller.h"

#include "SubsystemSleepStatePuller.h"
#include "logd/LogEvent.h"
#include "statslog.h"

using android::hardware::hidl_vec;
using android::hardware::power::V1_0::IPower;
using android::hardware::power::V1_0::PowerStatePlatformSleepState;
using android::hardware::power::V1_0::PowerStateVoter;
using android::hardware::power::V1_0::Status;
using android::hardware::power::V1_1::PowerStateSubsystem;
using android::hardware::power::V1_1::PowerStateSubsystemSleepState;
using android::hardware::Return;
using android::hardware::Void;

using std::make_shared;
using std::shared_ptr;

namespace android {
namespace os {
namespace statsd {

sp<android::hardware::power::V1_0::IPower> gPowerHalV1_0 = nullptr;
sp<android::hardware::power::V1_1::IPower> gPowerHalV1_1 = nullptr;
std::mutex gPowerHalMutex;
bool gPowerHalExists = true;

bool getPowerHal() {
    if (gPowerHalExists && gPowerHalV1_0 == nullptr) {
        gPowerHalV1_0 = android::hardware::power::V1_0::IPower::getService();
        if (gPowerHalV1_0 != nullptr) {
            gPowerHalV1_1 = android::hardware::power::V1_1::IPower::castFrom(gPowerHalV1_0);
            ALOGI("Loaded power HAL service");
        } else {
            ALOGW("Couldn't load power HAL service");
            gPowerHalExists = false;
        }
    }
    return gPowerHalV1_0 != nullptr;
}

SubsystemSleepStatePuller::SubsystemSleepStatePuller() : StatsPuller(android::util::SUBSYSTEM_SLEEP_STATE) {
}

bool SubsystemSleepStatePuller::PullInternal(vector<shared_ptr<LogEvent>>* data) {
    std::lock_guard<std::mutex> lock(gPowerHalMutex);

    if (!getPowerHal()) {
        ALOGE("Power Hal not loaded");
        return false;
    }

    uint64_t timestamp = time(nullptr) * NS_PER_SEC;

    data->clear();

    Return<void> ret;
        ret = gPowerHalV1_0->getPlatformLowPowerStats(
                [&data, timestamp](hidl_vec<PowerStatePlatformSleepState> states, Status status) {
                    if (status != Status::SUCCESS) return;

                    for (size_t i = 0; i < states.size(); i++) {
                        const PowerStatePlatformSleepState& state = states[i];

                        auto statePtr = make_shared<LogEvent>(android::util::SUBSYSTEM_SLEEP_STATE,
                                                              timestamp);
                        statePtr->write(state.name);
                        statePtr->write("");
                        statePtr->write(state.totalTransitions);
                        statePtr->write(state.residencyInMsecSinceBoot);
                        statePtr->init();
                        data->push_back(statePtr);
                        VLOG("powerstate: %s, %lld, %lld, %d", state.name.c_str(),
                             (long long)state.residencyInMsecSinceBoot,
                             (long long)state.totalTransitions,
                             state.supportedOnlyInSuspend ? 1 : 0);
                        for (auto voter : state.voters) {
                            auto voterPtr = make_shared<LogEvent>(android::util::SUBSYSTEM_SLEEP_STATE,
                                                                  timestamp);
                            voterPtr->write(state.name);
                            voterPtr->write(voter.name);
                            voterPtr->write(voter.totalNumberOfTimesVotedSinceBoot);
                            voterPtr->write(voter.totalTimeInMsecVotedForSinceBoot);
                            voterPtr->init();
                            data->push_back(voterPtr);
                            VLOG("powerstatevoter: %s, %s, %lld, %lld", state.name.c_str(),
                                 voter.name.c_str(),
                                 (long long)voter.totalTimeInMsecVotedForSinceBoot,
                                 (long long)voter.totalNumberOfTimesVotedSinceBoot);
                        }
                    }
                });
        if (!ret.isOk()) {
            ALOGE("getLowPowerStats() failed: power HAL service not available");
            gPowerHalV1_0 = nullptr;
            return false;
        }

        // Trying to cast to IPower 1.1, this will succeed only for devices supporting 1.1
        sp<android::hardware::power::V1_1::IPower> gPowerHal_1_1 =
                android::hardware::power::V1_1::IPower::castFrom(gPowerHalV1_0);
        if (gPowerHal_1_1 != nullptr) {
            ret = gPowerHal_1_1->getSubsystemLowPowerStats(
                    [&data, timestamp](hidl_vec<PowerStateSubsystem> subsystems, Status status) {
                        if (status != Status::SUCCESS) return;

                        if (subsystems.size() > 0) {
                            for (size_t i = 0; i < subsystems.size(); i++) {
                                const PowerStateSubsystem& subsystem = subsystems[i];
                                for (size_t j = 0; j < subsystem.states.size(); j++) {
                                    const PowerStateSubsystemSleepState& state =
                                            subsystem.states[j];
                                    auto subsystemStatePtr = make_shared<LogEvent>(
                                        android::util::SUBSYSTEM_SLEEP_STATE, timestamp);
                                    subsystemStatePtr->write(subsystem.name);
                                    subsystemStatePtr->write(state.name);
                                    subsystemStatePtr->write(state.totalTransitions);
                                    subsystemStatePtr->write(state.residencyInMsecSinceBoot);
                                    subsystemStatePtr->init();
                                    data->push_back(subsystemStatePtr);
                                    VLOG("subsystemstate: %s, %s, %lld, %lld, %lld",
                                         subsystem.name.c_str(), state.name.c_str(),
                                         (long long)state.residencyInMsecSinceBoot,
                                         (long long)state.totalTransitions,
                                         (long long)state.lastEntryTimestampMs);
                                }
                            }
                        }
                    });
        }
    return true;
}

}  // namespace statsd
}  // namespace os
}  // namespace android