OSDN Git Service

Atom: TemperatureReported
authorTej Singh <singhtejinder@google.com>
Fri, 16 Feb 2018 08:15:09 +0000 (00:15 -0800)
committerTej Singh <singhtejinder@google.com>
Tue, 27 Feb 2018 04:55:26 +0000 (20:55 -0800)
Makes the temperature reported atom pulled, and adds CPU, GPU, and SKIN
temperatures. Pulls information from the thermal hal.

Test: CTS test on cl in this topic
Change-Id: I0a8e2d1135bdd77e1cc510f24ff5214ce9e14ead

cmds/statsd/Android.mk
cmds/statsd/src/atoms.proto
cmds/statsd/src/external/ResourceHealthManagerPuller.cpp
cmds/statsd/src/external/ResourceThermalManagerPuller.cpp [new file with mode: 0644]
cmds/statsd/src/external/ResourceThermalManagerPuller.h [new file with mode: 0644]
cmds/statsd/src/external/StatsPullerManagerImpl.cpp
core/java/android/os/HardwarePropertiesManager.java
core/java/com/android/internal/os/BatteryStatsImpl.java
core/proto/android/os/enums.proto

index 244fbce..91ab31e 100644 (file)
@@ -36,6 +36,7 @@ statsd_common_src := \
     src/external/StatsCompanionServicePuller.cpp \
     src/external/SubsystemSleepStatePuller.cpp \
     src/external/ResourceHealthManagerPuller.cpp \
+    src/external/ResourceThermalManagerPuller.cpp \
     src/external/CpuTimePerUidPuller.cpp \
     src/external/CpuTimePerUidFreqPuller.cpp \
     src/external/KernelUidCpuActiveTimeReader.cpp \
@@ -99,6 +100,7 @@ statsd_common_shared_libraries := \
     android.hardware.health@2.0 \
     android.hardware.power@1.0 \
     android.hardware.power@1.1 \
+    android.hardware.thermal@1.0 \
     libmemunreachable
 
 # =========
index 04ebfcd..7159b9b 100644 (file)
@@ -72,7 +72,7 @@ message Atom {
         BatteryLevelChanged battery_level_changed = 30;
         ChargingStateChanged charging_state_changed = 31;
         PluggedStateChanged plugged_state_changed = 32;
-        DeviceTemperatureReported device_temperature_reported = 33;
+        // TODO: 33 is blank, but is available for use.
         DeviceOnStatusChanged device_on_status_changed = 34;
         WakeupAlarmOccurred wakeup_alarm_occurred = 35;
         KernelWakeupReported kernel_wakeup_reported = 36;
@@ -105,7 +105,7 @@ message Atom {
     }
 
     // Pulled events will start at field 10000.
-    // Next: 10021
+    // Next: 10022
     oneof pulled {
         WifiBytesTransfer wifi_bytes_transfer = 10000;
         WifiBytesTransferByFgBg wifi_bytes_transfer_by_fg_bg = 10001;
@@ -128,6 +128,7 @@ message Atom {
         DiskSpace disk_space = 10018;
         RemainingBatteryCapacity remaining_battery_capacity = 10019;
         FullBatteryCapacity full_battery_capacity = 10020;
+        Temperature temperature = 10021;
     }
 }
 
@@ -536,17 +537,6 @@ message PluggedStateChanged {
     optional android.os.BatteryPluggedStateEnum state = 1;
 }
 
-/**
- * Logs the temperature of the device, in tenths of a degree Celsius.
- *
- * Logged from:
- *   frameworks/base/core/java/com/android/internal/os/BatteryStatsImpl.java
- */
-message DeviceTemperatureReported {
-    // Temperature in tenths of a degree C.
-    optional int32 temperature = 1;
-}
-
 // TODO: Define this more precisely.
 // TODO: Log the ON state somewhere. It isn't currently logged anywhere.
 /**
@@ -1508,7 +1498,8 @@ message DiskSpace {
 
 /**
  * Pulls battery coulomb counter, which is the remaining battery charge in uAh.
- * Logged from: frameworks/base/cmds/statsd/src/external/ResourceHealthManagerPuller.cpp
+ * Pulled from:
+ *   frameworks/base/cmds/statsd/src/external/ResourceHealthManagerPuller.cpp
  */
 message RemainingBatteryCapacity {
     optional int32 charge_uAh = 1;
@@ -1516,8 +1507,26 @@ message RemainingBatteryCapacity {
 
 /**
  * Pulls battery capacity, which is the battery capacity when full in uAh.
- * Logged from: frameworks/base/cmds/statsd/src/external/ResourceHealthManagerPuller.cpp
+ * Pulled from:
+ *   frameworks/base/cmds/statsd/src/external/ResourceHealthManagerPuller.cpp
  */
 message FullBatteryCapacity {
     optional int32 capacity_uAh = 1;
+}
+
+/**
+ * Pulls the temperature of various parts of the device, in Celsius.
+ *
+ * Pulled from:
+ *   frameworks/base/cmds/statsd/src/external/ResourceThermalManagerPuller.cpp
+ */
+message Temperature {
+    // The type of temperature being reported. Eg. CPU, GPU, SKIN, BATTERY.
+    optional android.os.TemperatureTypeEnum sensor_location = 1;
+
+    // The name of the temperature source. Eg. CPU0
+    optional string sensor_name = 2;
+
+    // Temperature in degrees C.
+    optional float temperature_C = 3;
 }
\ No newline at end of file
index 261cb43..3741202 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 The Android Open Source Project
+ * Copyright (C) 2018 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#define DEBUG true  // STOPSHIP if true
+#define DEBUG false  // STOPSHIP if true
 #include "Log.h"
 
 #include <android/hardware/health/2.0/IHealth.h>
@@ -47,7 +47,6 @@ sp<android::hardware::health::V2_0::IHealth> gHealthHal = nullptr;
 bool getHealthHal() {
     if (gHealthHal == nullptr) {
         gHealthHal = get_health_service();
-
     }
     return gHealthHal != nullptr;
 }
diff --git a/cmds/statsd/src/external/ResourceThermalManagerPuller.cpp b/cmds/statsd/src/external/ResourceThermalManagerPuller.cpp
new file mode 100644 (file)
index 0000000..b3acdfc
--- /dev/null
@@ -0,0 +1,144 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define DEBUG false  // STOPSHIP if true
+#include "Log.h"
+
+#include <android/hardware/thermal/1.0/IThermal.h>
+#include "external/ResourceThermalManagerPuller.h"
+#include "external/StatsPuller.h"
+
+#include "ResourceThermalManagerPuller.h"
+#include "logd/LogEvent.h"
+#include "statslog.h"
+#include "stats_log_util.h"
+
+#include <chrono>
+
+using android::hardware::hidl_death_recipient;
+using android::hardware::hidl_vec;
+using android::hidl::base::V1_0::IBase;
+using android::hardware::thermal::V1_0::IThermal;
+using android::hardware::thermal::V1_0::Temperature;
+using android::hardware::thermal::V1_0::ThermalStatus;
+using android::hardware::thermal::V1_0::ThermalStatusCode;
+using android::hardware::Return;
+using android::hardware::Void;
+
+using std::chrono::duration_cast;
+using std::chrono::nanoseconds;
+using std::chrono::system_clock;
+using std::make_shared;
+using std::shared_ptr;
+
+namespace android {
+namespace os {
+namespace statsd {
+
+bool getThermalHalLocked();
+sp<android::hardware::thermal::V1_0::IThermal> gThermalHal = nullptr;
+std::mutex gThermalHalMutex;
+
+struct ThermalHalDeathRecipient : virtual public hidl_death_recipient {
+      virtual void serviceDied(uint64_t cookie, const wp<IBase>& who) override {
+          std::lock_guard<std::mutex> lock(gThermalHalMutex);
+          ALOGE("ThermalHAL just died");
+          gThermalHal = nullptr;
+          getThermalHalLocked();
+      }
+};
+
+sp<ThermalHalDeathRecipient> gThermalHalDeathRecipient = nullptr;
+
+// The caller must be holding gThermalHalMutex.
+bool getThermalHalLocked() {
+    if (gThermalHal == nullptr) {
+            gThermalHal = IThermal::getService();
+            if (gThermalHal == nullptr) {
+                ALOGE("Unable to get Thermal service.");
+            } else {
+                if (gThermalHalDeathRecipient == nullptr) {
+                    gThermalHalDeathRecipient = new ThermalHalDeathRecipient();
+                }
+                hardware::Return<bool> linked = gThermalHal->linkToDeath(
+                    gThermalHalDeathRecipient, 0x451F /* cookie */);
+                if (!linked.isOk()) {
+                    ALOGE("Transaction error in linking to ThermalHAL death: %s",
+                            linked.description().c_str());
+                    gThermalHal = nullptr;
+                } else if (!linked) {
+                    ALOGW("Unable to link to ThermalHal death notifications");
+                    gThermalHal = nullptr;
+                } else {
+                    ALOGD("Link to death notification successful");
+                }
+            }
+    }
+    return gThermalHal != nullptr;
+}
+
+ResourceThermalManagerPuller::ResourceThermalManagerPuller() :
+        StatsPuller(android::util::TEMPERATURE) {
+}
+
+bool ResourceThermalManagerPuller::PullInternal(vector<shared_ptr<LogEvent>>* data) {
+    std::lock_guard<std::mutex> lock(gThermalHalMutex);
+    if (!getThermalHalLocked()) {
+        ALOGE("Thermal Hal not loaded");
+        return false;
+    }
+
+    int64_t wallClockTimestampNs = getWallClockNs();
+    int64_t elapsedTimestampNs = getElapsedRealtimeNs();
+
+    data->clear();
+    bool resultSuccess = true;
+
+    Return<void> ret = gThermalHal->getTemperatures(
+            [&](ThermalStatus status, const hidl_vec<Temperature>& temps) {
+        if (status.code != ThermalStatusCode::SUCCESS) {
+            ALOGE("Failed to get temperatures from ThermalHAL. Status: %d", status.code);
+            resultSuccess = false;
+            return;
+        }
+        if (mTagId == android::util::TEMPERATURE) {
+            for (size_t i = 0; i < temps.size(); ++i) {
+                auto ptr = make_shared<LogEvent>(android::util::TEMPERATURE,
+                        wallClockTimestampNs, elapsedTimestampNs);
+                ptr->write((static_cast<int>(temps[i].type)));
+                ptr->write(temps[i].name);
+                ptr->write(temps[i].currentValue);
+                ptr->init();
+                data->push_back(ptr);
+            }
+        } else {
+            ALOGE("Unsupported tag in ResourceThermalManagerPuller: %d", mTagId);
+        }
+    });
+    if (!ret.isOk()) {
+        ALOGE("getThermalHalLocked() failed: thermal HAL service not available. Description: %s",
+                ret.description().c_str());
+        if (ret.isDeadObject()) {
+            gThermalHal = nullptr;
+        }
+        return false;
+    }
+    return resultSuccess;
+}
+
+}  // namespace statsd
+}  // namespace os
+}  // namespace android
diff --git a/cmds/statsd/src/external/ResourceThermalManagerPuller.h b/cmds/statsd/src/external/ResourceThermalManagerPuller.h
new file mode 100644 (file)
index 0000000..13c675a
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <utils/String16.h>
+#include "StatsPuller.h"
+
+namespace android {
+namespace os {
+namespace statsd {
+
+/**
+ * Reads IThermal.hal
+ */
+class ResourceThermalManagerPuller : public StatsPuller {
+public:
+    ResourceThermalManagerPuller();
+    bool PullInternal(vector<std::shared_ptr<LogEvent>>* data) override;
+};
+
+}  // namespace statsd
+}  // namespace os
+}  // namespace android
\ No newline at end of file
index bee9939..880dfd1 100644 (file)
 #include <climits>
 #include "CpuTimePerUidFreqPuller.h"
 #include "CpuTimePerUidPuller.h"
-#include "ResourceHealthManagerPuller.h"
 #include "KernelUidCpuActiveTimeReader.h"
 #include "KernelUidCpuClusterTimeReader.h"
-#include "SubsystemSleepStatePuller.h"
+#include "ResourceHealthManagerPuller.h"
+#include "ResourceThermalManagerPuller.h"
 #include "StatsCompanionServicePuller.h"
 #include "StatsPullerManagerImpl.h"
 #include "StatsService.h"
@@ -118,7 +118,9 @@ const std::map<int, PullAtomInfo> StatsPullerManagerImpl::kAllPullAtomInfo = {
          {{}, {}, 1, new ResourceHealthManagerPuller(android::util::FULL_BATTERY_CAPACITY)}},
         // process_memory_state
         {android::util::PROCESS_MEMORY_STATE,
-         {{4,5,6,7,8}, {2,3}, 0, new StatsCompanionServicePuller(android::util::PROCESS_MEMORY_STATE)}}};
+         {{4,5,6,7,8}, {2,3}, 0, new StatsCompanionServicePuller(android::util::PROCESS_MEMORY_STATE)}},
+        // temperature
+        {android::util::TEMPERATURE, {{}, {}, 1, new ResourceThermalManagerPuller()}}};
 
 StatsPullerManagerImpl::StatsPullerManagerImpl()
     : mCurrentPullingInterval(LONG_MAX) {
index eae7d70..3d072c5 100644 (file)
@@ -63,6 +63,8 @@ public class HardwarePropertiesManager {
     /**
      * Device temperature types.
      */
+    // These constants are also defined in android/os/enums.proto.
+    // Any change to the types here or in the thermal hal should be made in the proto as well.
     /** Temperature of CPUs in Celsius. */
     public static final int DEVICE_TEMPERATURE_CPU = Constants.TemperatureType.CPU;
 
index 9b4ea33..9bfc76a 100644 (file)
@@ -12587,7 +12587,7 @@ public class BatteryStatsImpl extends BatteryStats {
         temp = Math.max(0, temp);
 
         reportChangesToStatsLog(mHaveBatteryLevel ? mHistoryCur : null,
-                status, plugType, level, temp);
+                status, plugType, level);
 
         final boolean onBattery = isOnBattery(plugType, status);
         final long uptime = mClocks.uptimeMillis();
@@ -12786,7 +12786,7 @@ public class BatteryStatsImpl extends BatteryStats {
     // Inform StatsLog of setBatteryState changes.
     // If this is the first reporting, pass in recentPast == null.
     private void reportChangesToStatsLog(HistoryItem recentPast,
-            final int status, final int plugType, final int level, final int temp) {
+            final int status, final int plugType, final int level) {
 
         if (recentPast == null || recentPast.batteryStatus != status) {
             StatsLog.write(StatsLog.CHARGING_STATE_CHANGED, status);
@@ -12797,8 +12797,6 @@ public class BatteryStatsImpl extends BatteryStats {
         if (recentPast == null || recentPast.batteryLevel != level) {
             StatsLog.write(StatsLog.BATTERY_LEVEL_CHANGED, level);
         }
-        // Let's just always print the temperature, regardless of whether it changed.
-        StatsLog.write(StatsLog.DEVICE_TEMPERATURE_REPORTED, temp);
     }
 
     public long getAwakeTimeBattery() {
index fe9b7ac..aa99ac7 100644 (file)
@@ -56,6 +56,17 @@ enum BatteryStatusEnum {
     BATTERY_STATUS_FULL = 5;
 }
 
+// These constants are defined in hardware/interfaces/thermal/1.0/types.hal
+// They are primarily used by android/os/HardwarePropertiesManager.java.
+// Any change to the types in the thermal hal should be made here as well.
+enum TemperatureTypeEnum {
+    TEMPERATURE_TYPE_UKNOWN = -1;
+    TEMPERATURE_TYPE_CPU = 0;
+    TEMPERATURE_TYPE_GPU = 1;
+    TEMPERATURE_TYPE_BATTERY = 2;
+    TEMPERATURE_TYPE_SKIN = 3;
+}
+
 // Wakelock types, primarily used by android/os/PowerManager.java.
 enum WakeLockLevelEnum {
     // NOTE: Wake lock levels were previously defined as a bit field, except