summaryrefslogtreecommitdiff
path: root/thermal/thermal-helper.cpp
blob: f5f146cf38aec9705ade0362fbcc92b1f20ed9d5 (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
285
286
287
288
289
290
291
292
293
/*
 * 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.
 */

#include <cctype>
#include <cerrno>
#include <cinttypes>
#include <cmath>
#include <cstdlib>
#include <cstring>

#include <android-base/logging.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h>

#include "thermal-helper.h"

namespace android {
namespace hardware {
namespace thermal {
namespace V1_0 {
namespace implementation {

static unsigned int gSkinSensorNum;
static unsigned int gTsensOffset;
static unsigned int gSkinThrottlingThreshold;
static unsigned int gSkinShutdownThreshold;
static unsigned int gVrThrottledBelowMin;

/**
 * Initialization constants based on platform
 *
 * @return true on success or false on error.
 */
bool initThermal() {
    std::string hardware = android::base::GetProperty("ro.hardware", "");
    if (hardware == "walleye") {
        LOG(ERROR) << "Initialization on Walleye";
        gSkinSensorNum = kWalleyeSkinSensorNum;
        gTsensOffset = kWalleyeTsensOffset;
        gSkinThrottlingThreshold = kWalleyeSkinThrottlingThreshold;
        gSkinShutdownThreshold = kWalleyeSkinShutdownThreshold;
        gVrThrottledBelowMin = kWalleyeVrThrottledBelowMin;
    } else if (hardware == "taimen") {
        std::string rev = android::base::GetProperty("ro.revision", "");
        if (rev == "rev_a" || rev == "rev_b") {
            LOG(ERROR) << "Initialization on Taimen pre revision C";
            gSkinSensorNum = kTaimenRabSkinSensorNum;
            gTsensOffset = kTaimenRabTsensOffset;
            gSkinThrottlingThreshold = kTaimenRabSkinThrottlingThreshold;
            gSkinShutdownThreshold = kTaimenRabSkinShutdownThreshold;
            gVrThrottledBelowMin = kTaimenRabVrThrottledBelowMin;
        } else {
            LOG(ERROR) << "Initialization on Taimen revision C and later";
            gSkinSensorNum = kTaimenRcSkinSensorNum;
            gTsensOffset = kTaimenRcTsensOffset;
            gSkinThrottlingThreshold = kTaimenRcSkinThrottlingThreshold;
            gSkinShutdownThreshold = kTaimenRcSkinShutdownThreshold;
            gVrThrottledBelowMin = kTaimenRcVrThrottledBelowMin;
        }
    } else {
        LOG(ERROR) << "Unsupported hardware: " << hardware;
        return false;
    }
    return true;
}

/**
 * Reads device temperature.
 *
 * @param sensor_num Number of sensor file with temperature.
 * @param type Device temperature type.
 * @param name Device temperature name.
 * @param mult Multiplier used to translate temperature to Celsius.
 * @param throttling_threshold Throttling threshold for the temperature.
 * @param shutdown_threshold Shutdown threshold for the temperature.
 * @param out Pointer to temperature_t structure that will be filled with current
 *     values.
 *
 * @return 0 on success or negative value -errno on error.
 */
static ssize_t readTemperature(int sensor_num, TemperatureType type, const char *name, float mult,
                                float throttling_threshold, float shutdown_threshold,
                                float vr_throttling_threshold, Temperature *out) {
    FILE *file;
    char file_name[PATH_MAX];
    float temp;

    sprintf(file_name, kTemperatureFileFormat, sensor_num);
    file = fopen(file_name, "r");
    if (file == NULL) {
        PLOG(ERROR) << "readTemperature: failed to open file (" << file_name << ")";
        return -errno;
    }
    if (1 != fscanf(file, "%f", &temp)) {
        fclose(file);
        PLOG(ERROR) << "readTemperature: failed to read a float";
        return errno ? -errno : -EIO;
    }

    fclose(file);

    (*out).type = type;
    (*out).name = name;
    (*out).currentValue = temp * mult;
    (*out).throttlingThreshold = throttling_threshold;
    (*out).shutdownThreshold = shutdown_threshold;
    (*out).vrThrottlingThreshold = vr_throttling_threshold;

    LOG(DEBUG) << android::base::StringPrintf(
        "readTemperature: %d, %d, %s, %g, %g, %g, %g",
        sensor_num, type, name, temp * mult, throttling_threshold,
        shutdown_threshold, vr_throttling_threshold);

    return 0;
}

static ssize_t getCpuTemperatures(hidl_vec<Temperature> *temperatures) {
    size_t cpu;

    for (cpu = 0; cpu < kCpuNum; cpu++) {
        if (cpu >= temperatures->size()) {
            break;
        }
        // temperature in decidegrees Celsius.
        ssize_t result = readTemperature(kCpuTsensOffset[cpu] + gTsensOffset, TemperatureType::CPU, kCpuLabel[cpu],
                                          0.1, kCpuThrottlingThreshold, kCpuShutdownThreshold, kCpuThrottlingThreshold,
                                          &(*temperatures)[cpu]);
        if (result != 0) {
            return result;
        }
    }
    return cpu;
}

ssize_t fillTemperatures(hidl_vec<Temperature> *temperatures) {
    ssize_t result = 0;
    size_t current_index = 0;

    if (temperatures == NULL || temperatures->size() < kTemperatureNum) {
        LOG(ERROR) << "fillTemperatures: incorrect buffer";
        return -EINVAL;
    }

    result = getCpuTemperatures(temperatures);
    if (result < 0) {
        return result;
    }
    current_index += result;

    // GPU temperature.
    if (current_index < temperatures->size()) {
        // temperature in decidegrees Celsius.
        result = readTemperature(gTsensOffset + kGpuTsensOffset, TemperatureType::GPU, kGpuLabel, 0.1,
                                  NAN, NAN, NAN, &(*temperatures)[current_index]);
        if (result < 0) {
            return result;
        }
        current_index++;
    }

    // Battery temperature.
    if (current_index < temperatures->size()) {
        // battery: temperature in millidegrees Celsius.
        result = readTemperature(kBatterySensorNum, TemperatureType::BATTERY, kBatteryLabel,
                                  0.001, NAN, kBatteryShutdownThreshold, NAN,
                                  &(*temperatures)[current_index]);
        if (result < 0) {
            return result;
        }
        current_index++;
    }

    // Skin temperature.
    if (current_index < temperatures->size()) {
        // temperature in Celsius.
        result = readTemperature(gSkinSensorNum, TemperatureType::SKIN, kSkinLabel, 1.,
                                  gSkinThrottlingThreshold, gSkinShutdownThreshold, gVrThrottledBelowMin,
                                  &(*temperatures)[current_index]);
        if (result < 0) {
            return result;
        }
        current_index++;
    }
    return kTemperatureNum;
}

ssize_t fillCpuUsages(hidl_vec<CpuUsage> *cpuUsages) {
    int vals, cpu_num, online;
    ssize_t read;
    uint64_t user, nice, system, idle, active, total;
    char *line = NULL;
    size_t len = 0;
    size_t size = 0;
    char file_name[PATH_MAX];
    FILE *file;
    FILE *cpu_file;

    if (cpuUsages == NULL || cpuUsages->size() < kCpuNum ) {
        LOG(ERROR) << "fillCpuUsages: incorrect buffer";
        return -EINVAL;
    }

    file = fopen(kCpuUsageFile, "r");
    if (file == NULL) {
        PLOG(ERROR) << "fillCpuUsages: failed to open file (" << kCpuUsageFile << ")";
        return -errno;
    }

    while ((read = getline(&line, &len, file)) != -1) {
        // Skip non "cpu[0-9]" lines.
        if (strnlen(line, read) < 4 || strncmp(line, "cpu", 3) != 0 || !isdigit(line[3])) {
            free(line);
            line = NULL;
            len = 0;
            continue;
        }

        vals = sscanf(line, "cpu%d %" SCNu64 " %" SCNu64 " %" SCNu64 " %" SCNu64, &cpu_num, &user,
                &nice, &system, &idle);

        free(line);
        line = NULL;
        len = 0;

        if (vals != 5 || size == kCpuNum) {
            if (vals != 5) {
                PLOG(ERROR) << "fillCpuUsages: failed to read CPU information from file ("
                            << kCpuUsageFile << ")";
            } else {
                PLOG(ERROR) << "fillCpuUsages: file has incorrect format ("
                            << kCpuUsageFile << ")";
            }
            fclose(file);
            return errno ? -errno : -EIO;
        }

        active = user + nice + system;
        total = active + idle;

        // Read online CPU information.
        snprintf(file_name, PATH_MAX, kCpuOnlineFileFormat, cpu_num);
        cpu_file = fopen(file_name, "r");
        online = 0;
        if (cpu_file == NULL) {
            PLOG(ERROR) << "fillCpuUsages: failed to open file (" << file_name << ")";
            fclose(file);
            return -errno;
        }
        if (1 != fscanf(cpu_file, "%d", &online)) {
            PLOG(ERROR) << "fillCpuUsages: failed to read CPU online information from file ("
                        << file_name << ")";
            fclose(file);
            fclose(cpu_file);
            return errno ? -errno : -EIO;
        }
        fclose(cpu_file);

        (*cpuUsages)[size].name = kCpuLabel[size];
        (*cpuUsages)[size].active = active;
        (*cpuUsages)[size].total = total;
        (*cpuUsages)[size].isOnline = static_cast<bool>(online);

        LOG(DEBUG) << "fillCpuUsages: "<< kCpuLabel[size] << ": "
                   << active << " " << total << " " <<  online;
        size++;
    }
    fclose(file);

    if (size != kCpuNum) {
        PLOG(ERROR) << "fillCpuUsages: file has incorrect format (" << kCpuUsageFile << ")";
        return -EIO;
    }
    return kCpuNum;
}

}  // namespace implementation
}  // namespace V1_0
}  // namespace thermal
}  // namespace hardware
}  // namespace android