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) { | |
| 32 static const char* kThermalZonePath = "/sys/class/thermal/"; | |
| 33 SkOSFile::Iter it(kThermalZonePath); | |
| 34 SkString path; | |
| 35 while (it.next(&path, true)) { | |
| 36 if (!path.contains("thermal_zone")) { | |
| 37 continue; | |
| 38 } | |
| 39 | |
| 40 SkString fullPath(kThermalZonePath); | |
| 41 fullPath.append(path); | |
| 42 SkOSFile::Iter thermalZoneIt(fullPath.c_str()); | |
| 43 | |
| 44 SkString filename; | |
| 45 while (thermalZoneIt.next(&filename)) { | |
| 46 if (!(filename.contains("trip_point") && filename.contains("temp"))) { | |
| 47 continue; | |
| 48 } | |
| 49 | |
| 50 fTripPoints.push_back(TripPoint(fullPath, filename, threshold)); | |
| 51 } | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 bool ThermalManager::coolOffIfNecessary(uint32_t sleepIntervalMs, uint32_t timeo utMs) { | |
| 56 uint32_t i = 0, totalTimeSleptMs = 0; | |
| 57 while (i < (uint32_t)fTripPoints.count() && totalTimeSleptMs < timeoutMs) { | |
| 58 if (fTripPoints[i].willTrip()) { | |
| 59 sleep(sleepIntervalMs); | |
| 60 totalTimeSleptMs += sleepIntervalMs; | |
| 61 } else { | |
| 62 i++; | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 return totalTimeSleptMs < timeoutMs; | |
| 67 } | |
| 68 | |
| 69 int32_t ThermalManager::OpenFileAndReadInt32(const char* path) { | |
| 70 FILE* tempFile = fopen(path, "r"); | |
|
djsollen
2016/02/08 20:59:33
I can't think of any, but is there a way to avoid
joshualitt
2016/02/09 13:51:54
There can be many of these. The nexus six has nea
| |
| 71 SkASSERT(tempFile); | |
| 72 int32_t value; | |
| 73 SkDEBUGCODE(int ret = )fscanf(tempFile, "%d", &value); | |
| 74 SkASSERT(ret); | |
| 75 fclose(tempFile); | |
| 76 return value; | |
| 77 } | |
| 78 | |
| 79 ThermalManager::TripPoint::TripPoint(SkString thermalZoneRoot, SkString pointNam e, | |
| 80 int32_t threshold) | |
| 81 : fThermalZoneRoot(thermalZoneRoot) | |
| 82 , fPointName(pointName) { | |
| 83 SkString fullPath(thermalZoneRoot); | |
| 84 fullPath.appendf("/%s", pointName.c_str()); | |
| 85 fPoint = OpenFileAndReadInt32(fullPath.c_str()); | |
| 86 fBase = GetTemp(fThermalZoneRoot); | |
| 87 fDisabled = fBase >= fPoint + fThreshold; // We disable any trip point whic h start off | |
| 88 // triggered | |
| 89 fThreshold = threshold; | |
| 90 if (!fDisabled) { | |
| 91 SkDebugf("Trip point %s base - %d trip point-%d\n", fullPath.c_str(), | |
| 92 fBase, fPoint); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 bool ThermalManager::TripPoint::willTrip() { | |
| 97 int32_t currentTemp = GetTemp(fThermalZoneRoot); | |
| 98 bool wouldTrip = !fDisabled && currentTemp + fThreshold >= fPoint; | |
| 99 | |
| 100 if (wouldTrip) { | |
| 101 SkDebugf("%s/%s would trip {%d,%d,%d,%d}\n", fThermalZoneRoot.c_str(), | |
| 102 fPointName.c_str(), fBase, currentTemp, fPoint, fThreshold); | |
| 103 } | |
| 104 return wouldTrip; | |
| 105 } | |
| 106 | |
| 107 #endif | |
| OLD | NEW |