summaryrefslogtreecommitdiff
path: root/thermal/Thermal.cpp
blob: 37edb1370fff1cee00d9c009b9efaed1eb0e5392 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (C) 2018 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.
 */

#include <cerrno>
#include <mutex>
#include <string>

#include <android-base/file.h>
#include <android-base/logging.h>

#include "Thermal.h"
#include "thermal-helper.h"

namespace android {
namespace hardware {
namespace thermal {
namespace V1_1 {
namespace implementation {

namespace {

using ::android::hardware::hidl_death_recipient;
using ::android::hardware::thermal::V1_0::CoolingDevice;
using ::android::hardware::thermal::V1_0::Temperature;
using ::android::hardware::thermal::V1_0::ThermalStatus;
using ::android::hardware::thermal::V1_0::ThermalStatusCode;
using ::android::hardware::Void;
using ::android::hidl::base::V1_0::IBase;
using ::android::wp;

sp<IThermalCallback> gThermalCallback;
std::mutex gThermalCallbackMutex;

template <typename T, typename U>
Return<void> setFailureAndCallback(
    T _hidl_cb, hidl_vec<U> data, const std::string& debug_msg) {
    ThermalStatus status;
    status.code = ThermalStatusCode::FAILURE;
    status.debugMessage = debug_msg;
    _hidl_cb(status, data);
    return Void();
}

template <typename T, typename U>
Return<void> setInitFailureAndCallback(T _hidl_cb, hidl_vec<U> data) {
    return setFailureAndCallback(
        _hidl_cb, data, "Failure initializing thermal HAL");
}

// This function will hold gThermalCallbackMutex when called.
void checkThermalCallbackAndNotify(
        const std::pair<bool, Temperature>& notify_params) {
    std::lock_guard<std::mutex> _lock(gThermalCallbackMutex);
    if (gThermalCallback) {
        Return<void> ret = gThermalCallback->notifyThrottling(
            notify_params.first, notify_params.second);

        if (!ret.isOk()) {
            if (ret.isDeadObject()) {
                gThermalCallback = nullptr;
                LOG(WARNING) << "ThermalCallback died. "
                             << "Throttling event dropped.";
            } else {
                LOG(WARNING)
                    << "Failed to send throttling event to "
                    << "ThermalCallback";
            }
        }
    } else {
        LOG(WARNING)
            << "No ThermalCallback registered. "
            << "Throttling event dropped.";
    }
}

}  // namespace

void Thermal::serviceDied(
        uint64_t cookie __unused, const wp<IBase>& who __unused) {
    std::lock_guard<std::mutex> _lock(gThermalCallbackMutex);
    gThermalCallback = nullptr;
    LOG(ERROR) << "IThermalCallback HIDL service died";
}

// On init we will spawn a thread which will continually watch for
// throttling.  When throttling is seen, if we have a callback registered
// the thread will call notifyThrottling() else it will log the dropped
// throttling event and do nothing.  The thread is only killed when
// Thermal() is killed.
Thermal::Thermal() : thermal_helper_(), cpu_throttling_watcher_() {
    cpu_throttling_watcher_.registerFilesToWatch(
        thermal_helper_.getCoolingDevicePaths());
    cpu_throttling_watcher_.registerCallback(
        std::bind(&Thermal::notifyIfThrottlingSeen, this,
                  std::placeholders::_1));
    cpu_throttling_watcher_.registerQueueOverflowCallback(
        std::bind(&Thermal::resetStateWhenWatcherQueueOverflows, this));
    cpu_throttling_watcher_.startWatchingDeviceFiles();
}

// Methods from ::android::hardware::thermal::V1_0::IThermal.
Return<void> Thermal::getTemperatures(getTemperatures_cb _hidl_cb) {
    ThermalStatus status;
    status.code = ThermalStatusCode::SUCCESS;
    hidl_vec<Temperature> temperatures;

    if (!thermal_helper_.isInitializedOk()) {
        LOG(ERROR) << "ThermalHAL not initialized properly.";
        return setInitFailureAndCallback(_hidl_cb, temperatures);
    }

    if (!thermal_helper_.fillTemperatures(&temperatures)) {
        return setFailureAndCallback(_hidl_cb, temperatures,
                "Failed to read thermal sensors.");
    }

    for (const auto& t : temperatures) {
        LOG(DEBUG) << "getTemperatures "
                   << " Type: " << android::hardware::thermal::V1_0::toString(t.type)
                   << " Name: " << t.name
                   << " CurrentValue: " << t.currentValue
                   << " ThrottlingThreshold: " << t.throttlingThreshold
                   << " ShutdownThreshold: " << t.shutdownThreshold
                   << " VrThrottlingThreshold: " << t.vrThrottlingThreshold;
    }
    _hidl_cb(status, temperatures);
    return Void();
}

Return<void> Thermal::getCpuUsages(getCpuUsages_cb _hidl_cb) {
    ThermalStatus status;
    status.code = ThermalStatusCode::SUCCESS;
    hidl_vec<CpuUsage> cpu_usages;

    if (!thermal_helper_.isInitializedOk()) {
        return setInitFailureAndCallback(_hidl_cb, cpu_usages);
    }

    if (!thermal_helper_.fillCpuUsages(&cpu_usages)) {
        return setFailureAndCallback(_hidl_cb, cpu_usages,
            "Failed to get CPU usages.");
    }

    for (const auto& usage : cpu_usages) {
        LOG(DEBUG) << "getCpuUsages "
                   << " Name: " << usage.name
                   << " Active: " << usage.active
                   << " Total: " << usage.total
                   << " IsOnline: " << usage.isOnline;
    }
    _hidl_cb(status, cpu_usages);
    return Void();
}

Return<void> Thermal::getCoolingDevices(getCoolingDevices_cb _hidl_cb) {
    ThermalStatus status;
    status.code = ThermalStatusCode::SUCCESS;
    hidl_vec<CoolingDevice> cooling_devices;

    if (!thermal_helper_.isInitializedOk()) {
        return setInitFailureAndCallback(_hidl_cb, cooling_devices);
    }
    LOG(DEBUG) << "No cooling device.";
    _hidl_cb(status, cooling_devices);
    return Void();
}

Return<void> Thermal::debug(
        const hidl_handle& handle, const hidl_vec<hidl_string>&) {
    if (handle != nullptr && handle->numFds >= 1) {
        int fd = handle->data[0];
        std::ostringstream dump_buf;

        if (!thermal_helper_.isInitializedOk()) {
            dump_buf << "ThermalHAL not initialized properly." << std::endl;
        } else {
            hidl_vec<Temperature> temperatures;
            hidl_vec<CpuUsage> cpu_usages;

            dump_buf << "getTemperatures:" << std::endl;
            if (!thermal_helper_.fillTemperatures(&temperatures)) {
                dump_buf << "Failed to read thermal sensors." << std::endl;
            }

            for (const auto& t : temperatures) {
                dump_buf << "Name: " << t.name << " Type: "
                         << android::hardware::thermal::V1_0::toString(t.type)
                         << " CurrentValue: " << t.currentValue
                         << " ThrottlingThreshold: " << t.throttlingThreshold
                         << " ShutdownThreshold: " << t.shutdownThreshold
                         << " VrThrottlingThreshold: "
                         << t.vrThrottlingThreshold << std::endl;
            }

            dump_buf << "getCpuUsages:" << std::endl;
            if (!thermal_helper_.fillCpuUsages(&cpu_usages)) {
                dump_buf << "Failed to get CPU usages." << std::endl;
            }

            for (const auto& usage : cpu_usages) {
                dump_buf << "Name: " << usage.name
                         << " Active: " << usage.active
                         << " Total: " << usage.total
                         << " IsOnline: " << usage.isOnline
                         << std::endl;
            }

            if (!thermal_helper_.fillBatteryThresholdDebugInfo(dump_buf)) {
                dump_buf << "error while filling BatteryThresholdDebugInfo." << std::endl;
            }
        }
        std::string buf = dump_buf.str();
        if (!android::base::WriteStringToFd(buf, fd)) {
            PLOG(ERROR) << "Failed to dump state to fd";
        }
        fsync(fd);
    }
    return Void();
}

// Methods from ::android::hardware::thermal::V1_1::IThermal.
Return<void> Thermal::registerThermalCallback(const sp<IThermalCallback>& cb) {
    std::lock_guard<std::mutex> _lock(gThermalCallbackMutex);
    if (gThermalCallback) {
        LOG(WARNING) << "Thermal callback was already assigned!";
    }

    auto cpu_throttling_watcher_status =
        cpu_throttling_watcher_.getWatcherThreadStatus();
    if (cpu_throttling_watcher_status == std::future_status::ready) {
        LOG(ERROR) << "The CPU throttling watcher thread stopped running. "
                   << "Restarting it.";
        cpu_throttling_watcher_.startWatchingDeviceFiles();
    }

    gThermalCallback = cb;
    if (gThermalCallback) {
        gThermalCallback->linkToDeath(this, 0x451F /* cookie, unused */);
        LOG(INFO) << "ThermalCallback registered.";
    } else {
        LOG(INFO) << "ThermalCallback unregistered.";
    }
    return Void();
}

void Thermal::notifyIfThrottlingSeen(
        const std::pair<std::string, std::string>& throttling_data) {
    std::pair<bool, Temperature> notify_params;
    bool shouldNotify = thermal_helper_.checkThrottlingData(
        throttling_data, &notify_params);

    if (shouldNotify) {
        checkThermalCallbackAndNotify(notify_params);
    }
}

void Thermal::resetStateWhenWatcherQueueOverflows() {
    int throttling_value;
    for (const auto& entry : thermal_helper_.getValidCoolingDeviceMap()) {
        if (thermal_helper_.readCoolingDevice(entry.first, &throttling_value)) {
            notifyIfThrottlingSeen(std::make_pair(
                entry.first, std::to_string(throttling_value)));
        }
    }
}

}  // namespace implementation
}  // namespace V1_1
}  // namespace thermal
}  // namespace hardware
}  // namespace android