Chromium Code Reviews| Index: tools/ThermalManager.cpp |
| diff --git a/tools/ThermalManager.cpp b/tools/ThermalManager.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9853d9de3075ed781f287898e513274bfb1914ab |
| --- /dev/null |
| +++ b/tools/ThermalManager.cpp |
| @@ -0,0 +1,91 @@ |
| +/* |
| + * 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" |
| + |
| +#if USE_THERMAL_MANAGER |
| + |
| +#include "SkOSFile.h" |
| + |
| +#include <stdio.h> |
| + |
| +ThermalManager::ThermalManager(int32_t threshold) { |
| + 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 sleepInterval, uint32_t maxSleeps) { |
| + uint32_t i = 0, sleeps = 0; |
| + while (i < (uint32_t)fTripPoints.count() && sleeps++ < maxSleeps) { |
| + if (fTripPoints[i].willTrip()) { |
| + sleep(sleepInterval); |
| + } else { |
| + i++; |
| + } |
| + } |
| + |
| + return sleeps < maxSleeps; |
| +} |
| + |
| +int32_t ThermalManager::OpenFileAndReadInt32(const char* path) { |
| + FILE* tempFile = fopen(path, "r"); |
| + SkASSERT(tempFile); |
| + int32_t value; |
| + SkDEBUGCODE(int ret = )fscanf(tempFile, "%d", &value); |
| + SkASSERT(ret); |
| + 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); |
| + fDisabled = fBase >= fPoint + fThreshold; // We disable any trip point which start off |
|
djsollen
2016/02/05 04:54:15
why do we need to disable these?
joshualitt
2016/02/05 13:50:20
I added a comment in the header. Some trip points
|
| + // triggered |
| + fThreshold = threshold; |
| + if (!fDisabled) { |
| + SkDebugf("Trip point %s base - %d trip point-%d\n", fullPath.c_str(), |
| + fBase, fPoint); |
| + } |
| +} |
| + |
| +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 |