Create a thermal manager class and wire it in to nanobench behind a flag

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1671573002

Review URL: https://codereview.chromium.org/1671573002
diff --git a/bench/nanobench.cpp b/bench/nanobench.cpp
index aeae4f4..4d8ba44 100644
--- a/bench/nanobench.cpp
+++ b/bench/nanobench.cpp
@@ -38,6 +38,7 @@
 #include "SkSurface.h"
 #include "SkTaskGroup.h"
 #include "SkThreadUtils.h"
+#include "ThermalManager.h"
 
 #include <stdlib.h>
 
@@ -111,6 +112,8 @@
 DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
 DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
 DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
+DEFINE_string(useThermalManager, "0,1,10,1000", "enabled,threshold,sleepTimeMs,TimeoutMs for "
+                                                "thermalManager\n");
 
 DEFINE_string(sourceType, "",
         "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
@@ -1056,6 +1059,16 @@
     SkTArray<Config> configs;
     create_configs(&configs);
 
+#ifdef THERMAL_MANAGER_SUPPORTED
+    int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
+    if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
+                    &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
+        SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
+        exit(1);
+    }
+    ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
+#endif
+
     if (FLAGS_keepAlive) {
         start_keepalive();
     }
@@ -1073,6 +1086,11 @@
             bench->delayedSetup();
         }
         for (int i = 0; i < configs.count(); ++i) {
+#ifdef THERMAL_MANAGER_SUPPORTED
+            if (tmEnabled && !tm.coolOffIfNecessary()) {
+                SkDebugf("Could not cool off, timings will be throttled\n");
+            }
+#endif
             Target* target = is_enabled(b, configs[i]);
             if (!target) {
                 continue;
diff --git a/gyp/bench.gyp b/gyp/bench.gyp
index 7b84ab9..7c5d6df 100644
--- a/gyp/bench.gyp
+++ b/gyp/bench.gyp
@@ -26,6 +26,7 @@
         'tools.gyp:crash_handler',
         'tools.gyp:proc_stats',
         'tools.gyp:timer',
+        'tools.gyp:thermal_manager',
       ],
       'conditions': [
         ['skia_android_framework', {
diff --git a/gyp/tools.gyp b/gyp/tools.gyp
index e303669..9bcda35 100644
--- a/gyp/tools.gyp
+++ b/gyp/tools.gyp
@@ -435,6 +435,19 @@
       ],
     },
     {
+      'target_name': 'thermal_manager',
+      'type': 'static_library',
+      'sources': [
+        '../tools/ThermalManager.cpp',
+      ],
+      'dependencies': [
+        'skia_lib.gyp:skia_lib',
+      ],
+      'direct_dependent_settings': {
+        'include_dirs': [ '../tools', ],
+      },
+    },
+    {
       'target_name': 'test_public_includes',
       'type': 'static_library',
       # Ensure that our public headers don't have unused params so that clients
diff --git a/tools/ThermalManager.cpp b/tools/ThermalManager.cpp
new file mode 100644
index 0000000..24770ab
--- /dev/null
+++ b/tools/ThermalManager.cpp
@@ -0,0 +1,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>
+
+#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);
+    fDisabled = fBase >= fPoint + fThreshold;  // We disable any trip point which start off
+                                               // 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
diff --git a/tools/ThermalManager.h b/tools/ThermalManager.h
new file mode 100644
index 0000000..74e9dd1
--- /dev/null
+++ b/tools/ThermalManager.h
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef ThermalManager_DEFINED
+#define ThermalManager_DEFINED
+
+#include "SkString.h"
+#include "SkTArray.h"
+
+#if defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_UNIX)
+#    define THERMAL_MANAGER_SUPPORTED
+#endif
+
+#ifdef THERMAL_MANAGER_SUPPORTED
+
+/*
+ * This simple class monitors the thermal part of sysfs to ensure we don't trigger thermal events
+ */
+
+class ThermalManager {
+public:
+    ThermalManager(int32_t threshold, uint32_t sleepIntervalMs, uint32_t timeoutMs);
+
+    bool coolOffIfNecessary();
+
+private:
+    static int32_t OpenFileAndReadInt32(const char* path);
+
+    // current temperature can be read from /thermalZonePath/temp
+    static int32_t GetTemp(SkString thermalZonePath) {
+        SkString temperatureFilePath(thermalZonePath);
+        temperatureFilePath.appendf("/temp");
+        return OpenFileAndReadInt32(temperatureFilePath.c_str());
+    }
+
+    struct TripPoint {
+        TripPoint(SkString thermalZoneRoot, SkString pointName, int32_t threshold);
+
+        bool willTrip();
+
+        SkString fThermalZoneRoot;
+        SkString fPointName;
+        int32_t fBase;
+        int32_t fPoint;
+        int32_t fThreshold;
+
+        // Certain trip points seem to start tripped.  For example, I have seen trip points of 0 or
+        // negative numbers.
+        bool fDisabled;
+    };
+
+    SkTArray<TripPoint> fTripPoints;
+    uint32_t fSleepIntervalMs;
+    uint32_t fTimeoutMs;
+};
+#endif
+#endif