aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/ThermalManager.cpp
blob: e0c7c44c6992ae2cef57295218f67665a84aaf34 (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
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "ThermalManager.h"

#include "SkOSFile.h"

#include <stdio.h>

#ifndef SK_BUILD_FOR_WIN32
    #include <unistd.h>
#endif

#ifdef THERMAL_MANAGER_SUPPORTED

/*
 * ThermalManager is completely dependent on sysfs to monitor thermal temperatures.  In sysfs
 * thermal management is controlled by a number of thermal zones.  They are laid out as follows:
 * /sys/class/thermal/thermal_zoneN where N is the number of the thermal zone starting at 0.
 *
 * Inside each thermal_zone folder is a file called 'temp,' which has the current temperature
 * reading from the sensor in that zone, as well as 0 or more files called 'trip_point_N_temp.'
 *
 * When the reading in temp is greater than one of the numbers in the trip_point files, then the
 * kernel will take some kind of action.  This is all documented online.
 *
 * In any case, the goal of this class is to sleep right before a trip point is about to be
 * triggered, thus naturally cooling the system and preventing thermal throttling.
 */

ThermalManager::ThermalManager(int32_t threshold, uint32_t sleepIntervalMs, uint32_t timeoutMs)
    : fSleepIntervalMs(sleepIntervalMs)
    , fTimeoutMs(timeoutMs) {
    static const char* kThermalZonePath = "/sys/class/thermal/";
    SkOSFile::Iter it(kThermalZonePath);
    SkString path;
    while (it.next(&path, true)) {
        if (!path.contains("thermal_zone")) {
            continue;
        }

        SkString fullPath(kThermalZonePath);
        fullPath.append(path);
        SkOSFile::Iter thermalZoneIt(fullPath.c_str());

        SkString filename;
        while (thermalZoneIt.next(&filename)) {
            if (!(filename.contains("trip_point") && filename.contains("temp"))) {
                continue;
            }

            fTripPoints.push_back(TripPoint(fullPath, filename, threshold));
        }
    }
}

bool ThermalManager::coolOffIfNecessary() {
    uint32_t i = 0, totalTimeSleptMs = 0;
    while (i < (uint32_t)fTripPoints.count() && totalTimeSleptMs < fTimeoutMs) {
        if (fTripPoints[i].willTrip()) {
            sleep(fSleepIntervalMs);
            totalTimeSleptMs += fSleepIntervalMs;
        } else {
            i++;
        }
    }

    return totalTimeSleptMs < fTimeoutMs;
}

int32_t ThermalManager::OpenFileAndReadInt32(const char* path) {
    FILE* tempFile = fopen(path, "r");
    SkASSERT(tempFile);
    int32_t value;
    int ret = fscanf(tempFile, "%d", &value);
    if (!ret) {
        SkDebugf("Could not read temperature\n");
        SkASSERT(false);
    }

    fclose(tempFile);
    return value;
}

ThermalManager::TripPoint::TripPoint(SkString thermalZoneRoot, SkString pointName,
                                     int32_t threshold)
    : fThermalZoneRoot(thermalZoneRoot)
    , fPointName(pointName) {
    SkString fullPath(thermalZoneRoot);
    fullPath.appendf("/%s", pointName.c_str());
    fPoint = OpenFileAndReadInt32(fullPath.c_str());
    fBase = GetTemp(fThermalZoneRoot);
    fThreshold = threshold;
    fDisabled = fBase + fThreshold >= fPoint;  // We disable any trip point which start off
                                               // triggered
}

bool ThermalManager::TripPoint::willTrip() {
    int32_t currentTemp = GetTemp(fThermalZoneRoot);
    bool wouldTrip = !fDisabled && currentTemp + fThreshold >= fPoint;

    if (wouldTrip) {
        SkDebugf("%s/%s would trip {%d,%d,%d,%d}\n", fThermalZoneRoot.c_str(),
                 fPointName.c_str(), fBase, currentTemp, fPoint, fThreshold);
    }
    return wouldTrip;
}

#endif