Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2016 Google Inc. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license that can be | |
| 5 * found in the LICENSE file. | |
| 6 */ | |
| 7 | |
| 8 #include "ThermalManager.h" | |
| 9 | |
| 10 #include "SkOSFile.h" | |
| 11 | |
| 12 #include <stdio.h> | |
| 13 | |
| 14 #ifdef THERMAL_MANAGER_SUPPORTED | |
| 15 | |
| 16 /* | |
| 17 * ThermalManager is completely dependent on sysfs to monitor thermal temperatur es. In sysfs | |
| 18 * thermal management is controlled by a number of thermal zones. They are laid out as follows: | |
| 19 * /sys/class/thermal/thermal_zoneN where N is the number of the thermal zone st arting at 0. | |
| 20 * | |
| 21 * Inside each thermal_zone folder is a file called 'temp,' which has the curren t temperature | |
| 22 * reading from the sensor in that zone, as well as 0 or more files called 'trip _point_N_temp.' | |
| 23 * | |
| 24 * When the reading in temp is greater than one of the numbers in the trip_point files, then the | |
| 25 * kernel will take some kind of action. This is all documented online. | |
| 26 * | |
| 27 * In any case, the goal of this class is to sleep right before a trip point is about to be | |
| 28 * triggered, thus naturally cooling the system and preventing thermal throttlin g. | |
| 29 */ | |
| 30 | |
| 31 ThermalManager::ThermalManager(int32_t threshold, uint32_t sleepIntervalMs, uint 32_t timeoutMs) | |
| 32 : fSleepIntervalMs(sleepIntervalMs) | |
| 33 , fTimeoutMs(timeoutMs) { | |
| 34 static const char* kThermalZonePath = "/sys/class/thermal/"; | |
| 35 SkOSFile::Iter it(kThermalZonePath); | |
| 36 SkString path; | |
| 37 while (it.next(&path, true)) { | |
| 38 if (!path.contains("thermal_zone")) { | |
| 39 continue; | |
| 40 } | |
| 41 | |
| 42 SkString fullPath(kThermalZonePath); | |
| 43 fullPath.append(path); | |
| 44 SkOSFile::Iter thermalZoneIt(fullPath.c_str()); | |
| 45 | |
| 46 SkString filename; | |
| 47 while (thermalZoneIt.next(&filename)) { | |
| 48 if (!(filename.contains("trip_point") && filename.contains("temp"))) { | |
| 49 continue; | |
| 50 } | |
| 51 | |
| 52 fTripPoints.push_back(TripPoint(fullPath, filename, threshold)); | |
| 53 } | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 bool ThermalManager::coolOffIfNecessary() { | |
| 58 uint32_t i = 0, totalTimeSleptMs = 0; | |
| 59 while (i < (uint32_t)fTripPoints.count() && totalTimeSleptMs < fTimeoutMs) { | |
| 60 if (fTripPoints[i].willTrip()) { | |
| 61 sleep(fSleepIntervalMs); | |
|
scroggo
2016/02/10 17:01:11
This breaks the AOSP mips build. From [1]:
extern
| |
| 62 totalTimeSleptMs += fSleepIntervalMs; | |
| 63 } else { | |
| 64 i++; | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 return totalTimeSleptMs < fTimeoutMs; | |
| 69 } | |
| 70 | |
| 71 int32_t ThermalManager::OpenFileAndReadInt32(const char* path) { | |
| 72 FILE* tempFile = fopen(path, "r"); | |
| 73 SkASSERT(tempFile); | |
| 74 int32_t value; | |
| 75 int ret = fscanf(tempFile, "%d", &value); | |
| 76 if (!ret) { | |
| 77 SkDebugf("Could not read temperature\n"); | |
| 78 SkASSERT(false); | |
| 79 } | |
| 80 | |
| 81 fclose(tempFile); | |
| 82 return value; | |
| 83 } | |
| 84 | |
| 85 ThermalManager::TripPoint::TripPoint(SkString thermalZoneRoot, SkString pointNam e, | |
| 86 int32_t threshold) | |
| 87 : fThermalZoneRoot(thermalZoneRoot) | |
| 88 , fPointName(pointName) { | |
| 89 SkString fullPath(thermalZoneRoot); | |
| 90 fullPath.appendf("/%s", pointName.c_str()); | |
| 91 fPoint = OpenFileAndReadInt32(fullPath.c_str()); | |
| 92 fBase = GetTemp(fThermalZoneRoot); | |
| 93 fDisabled = fBase >= fPoint + fThreshold; // We disable any trip point whic h start off | |
| 94 // triggered | |
| 95 fThreshold = threshold; | |
| 96 if (!fDisabled) { | |
| 97 SkDebugf("Trip point %s base - %d trip point-%d\n", fullPath.c_str(), | |
| 98 fBase, fPoint); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 bool ThermalManager::TripPoint::willTrip() { | |
| 103 int32_t currentTemp = GetTemp(fThermalZoneRoot); | |
| 104 bool wouldTrip = !fDisabled && currentTemp + fThreshold >= fPoint; | |
| 105 | |
| 106 if (wouldTrip) { | |
| 107 SkDebugf("%s/%s would trip {%d,%d,%d,%d}\n", fThermalZoneRoot.c_str(), | |
| 108 fPointName.c_str(), fBase, currentTemp, fPoint, fThreshold); | |
| 109 } | |
| 110 return wouldTrip; | |
| 111 } | |
| 112 | |
| 113 #endif | |
| OLD | NEW |